def canonical_path(item): path = [] from opennode.oms.security.authentication import sudo while item: p = sudo(follow_symlinks(sudo(item))) assert p.__name__ is not None, '%s.__name__ is None' % p path.insert(0, p.__name__) item = p.__parent__ return '/'.join(path)
def auto_tags(self): res = [u'state:' + self.context.state] if self.context.state else [] if self.context.architecture: for i in self.context.architecture: res.append(u'arch:' + i) from opennode.knot.model.virtualizationcontainer import IVirtualizationContainer p = sudo(self.context) if (IVirtualCompute.providedBy(p) and IVirtualizationContainer.providedBy(p.__parent__)): res.append(u'virt_type:' + p.__parent__.backend) res.append(u'virt:yes') else: res.append(u'virt:no') config = get_config() if config.has_section('netenv-tags'): for tag, nets in config.items('netenv-tags'): try: if (self.context.ipv4_address is not None and len(netaddr.all_matching_cidrs(self.context.ipv4_address.split('/')[0], nets.split(','))) > 0): res.append(u'env:' + tag) except ValueError: # graceful ignoring of incorrect ips pass return res
def handle_virtual_compute_config_change_request(compute, event): c = sudo(compute) compute_p = yield db.get(c, '__parent__') compute_type = yield db.get(compute_p, 'backend') # At the moment we only handle openvz backend updates (OMS-568) if compute_type != 'openvz': return update_param_whitelist = ['diskspace', 'memory', 'num_cores', 'swap_size'] param_modifier = {'diskspace': lambda d: d['total']} unit_corrections_coeff = {'memory': 1 / 1024.0, 'swap_size': 1 / 1024.0, 'diskspace': 1 / 1024.0} params_to_update = filter(lambda (k, v): k in update_param_whitelist, event.modified.iteritems()) if len(params_to_update) == 0: return # correct unit coefficients (usually MB -> GB) params_to_update = map(lambda (k, v): (k, param_modifier.get(k, lambda x: x)(v)), params_to_update) params_to_update = map(lambda (k, v): (k, unit_corrections_coeff.get(k) * v if k in unit_corrections_coeff else v), params_to_update) @db.transact def update_vm_limits(cpu_limit): logger.debug("Setting cpu_limit to %s, previous value %s" % (cpu_limit / 100.0, c.cpu_limit)) c.cpu_limit = cpu_limit / 100.0 cores_setting = filter(lambda(k, v): k == 'num_cores', params_to_update) if len(cores_setting) == 1: # adjust cpu_limit to follow the number of cores as well cpu_limit = int(cores_setting[0][1] * get_config().getfloat('vms', 'cpu_limit', 80)) log.msg("Updating cpulimit to %s" % cpu_limit, system='vm-configuration-update') params_to_update.append(('cpu_limit', cpu_limit)) yield update_vm_limits(cpu_limit) submitter = IVirtualizationContainerSubmitter((yield db.get(compute, '__parent__'))) try: yield submitter.submit(IUpdateVM, (yield db.get(compute, '__name__')), dict(params_to_update)) except Exception as e: @db.transact def reset_to_original_values(): for mk, mv in event.modified.iteritems(): setattr(compute, mk, event.original[mk]) yield reset_to_original_values() raise e # must re-throw, because sys.exc_info seems to get erased with the yield else: owner = (yield db.get(compute, '__owner__')) UserLogger(subject=compute, owner=owner).log('Compute "%s" configuration changed' % compute)
def handle_virtual_compute_config_change_request(compute, event): c = sudo(compute) compute_p = yield db.get(c, '__parent__') compute_type = yield db.get(compute_p, 'backend') # At the moment we only handle openvz backend updates (OMS-568) if compute_type != 'openvz': return update_param_whitelist = ['diskspace', 'memory', 'num_cores', 'swap_size'] param_modifier = {'diskspace': lambda d: d['total']} unit_corrections_coeff = { 'memory': 1 / 1024.0, 'swap_size': 1 / 1024.0, 'diskspace': 1 / 1024.0 } params_to_update = filter(lambda (k, v): k in update_param_whitelist, event.modified.iteritems()) if len(params_to_update) == 0: return # correct unit coefficients (usually MB -> GB) params_to_update = map( lambda (k, v): (k, param_modifier.get(k, lambda x: x)(v)), params_to_update) params_to_update = map( lambda (k, v): (k, unit_corrections_coeff.get(k) * v if k in unit_corrections_coeff else v), params_to_update) @db.transact def update_vm_limits(cpu_limit): logger.debug("Setting cpu_limit to %s, previous value %s" % (cpu_limit / 100.0, c.cpu_limit)) c.cpu_limit = cpu_limit / 100.0 cores_setting = filter(lambda (k, v): k == 'num_cores', params_to_update) if len(cores_setting) == 1: # adjust cpu_limit to follow the number of cores as well cpu_limit = int(cores_setting[0][1] * get_config().getfloat('vms', 'cpu_limit', 80)) log.msg("Updating cpulimit to %s" % cpu_limit, system='vm-configuration-update') params_to_update.append(('cpu_limit', cpu_limit)) yield update_vm_limits(cpu_limit) submitter = IVirtualizationContainerSubmitter( (yield db.get(compute, '__parent__'))) try: yield submitter.submit(IUpdateVM, (yield db.get(compute, '__name__')), dict(params_to_update)) except Exception as e: @db.transact def reset_to_original_values(): for mk, mv in event.modified.iteritems(): setattr(compute, mk, event.original[mk]) yield reset_to_original_values() raise e # must re-throw, because sys.exc_info seems to get erased with the yield else: owner = (yield db.get(compute, '__owner__')) UserLogger(subject=compute, owner=owner).log( 'Compute "%s" configuration changed' % compute)
def get_job(): p = sudo(proxy.remove_persistent_proxy(self.context)) job = job_interface(p.__parent__) backend = p.backend backend_uri = backends.get(backend, backend) return (job, backend_uri)
def hostname(self): return sudo(self.context).hostname