def iterate_recursively(container): seen = set() for item in container.listcontent(): if ICompute.providedBy(item): setattr(item, status_name, bool(status)) if (IVirtualizationContainer.providedBy(item) or ICompute.providedBy(item)): if item.__name__ not in seen: seen.add(item.__name__) iterate_recursively(item)
def collect(container): from opennode.knot.model.machines import Machines seen = set() for item in container.listcontent(): if ICompute.providedBy(item) and item.ctid is not None: computes[str(item.ctid)] = Symlink(str(item.ctid), item) if (isinstance(item, Machines) or isinstance(item, Computes) or ICompute.providedBy(item) or IVirtualizationContainer.providedBy(item)): if item.__name__ not in seen: seen.add(item.__name__) collect(item)
def condition_generator(m): yield ICompute.providedBy(m) yield find_compute_v12n_container(m, container) yield not getattr(m, 'exclude_from_allocation', None) yield not getattr(m, 'failure', None) if not get_config().getboolean('overcommit', 'memory', False): yield self.context.memory_usage < m.memory else: log.msg('Memory filtering is disabled.', system='action-allocate') if not get_config().getboolean('overcommit', 'disk', False): yield sum( map( lambda (pk, pv): pv, filter(lambda (pk, pv): pk != 'total', self.context.diskspace.iteritems()))) < ( m.diskspace.get(param, 0) - m.diskspace_usage.get(param, 0)) else: log.msg('Diskspace filtering is disabled.', system='action-allocate') if not get_config().getboolean('overcommit', 'cores', False): yield self.context.num_cores <= m.num_cores else: log.msg('\'Total # of cores\' filtering is disabled.', system='action-allocate') templates = m['vms-%s' % container]['templates'] yield self.context.template in map( lambda t: t.name, filter(lambda t: ITemplate.providedBy(t), templates.listcontent() if templates else []))
def delete_virtual_compute(model, event): if not ICompute.providedBy(model.__parent__.__parent__): return if IDeployed.providedBy(model): log.msg( 'Deleting compute %s which is in IDeployed state, shutting down and ' 'undeploying first' % model.hostname, system='compute-backend') yield DestroyComputeAction(model).execute(DetachedProtocol(), object()) yield UndeployAction(model).execute(DetachedProtocol(), object()) else: log.msg('Deleting compute %s which is already in IUndeployed state' % model.hostname, system='compute-backend') owner = (yield db.get(model, '__owner__')) ulog = UserLogger(subject=model, owner=owner) ulog.log('Deleted %s' % model) @db.transact def deallocate_ip(): ippools = db.get_root()['oms_root']['ippools'] ip = netaddr.IPAddress(model.ipv4_address.split('/')[0]) if ippools.free(ip): ulog.log('Deallocated IP: %s', ip) yield deallocate_ip()
def get_computes(): oms_root = db.get_root()['oms_root'] res = [(i, i.hostname) for i in map(follow_symlinks, oms_root['computes'].listcontent()) if ICompute.providedBy(i)] return res
def execute(self, args): for machine in db.get_root()['oms_root']['machines']: if not ICompute.providedBy(machine) or IVirtualCompute.providedBy( machine): continue InstallSaltAction(machine).execute(DetachedProtocol(), object())
def get_computes(): oms_root = db.get_root()['oms_root'] res = [(i, i.hostname) for i in map( follow_symlinks, oms_root['computes'].listcontent()) if ICompute.providedBy(i)] return res
def allocate_virtual_compute_from_hangar(model, event): if not IVirtualizationContainer.providedBy(model.__parent__): return if IDeployed.providedBy(model): return auto_allocate = get_config().getboolean('vms', 'auto_allocate', True) if not auto_allocate: return if IHangar.providedBy(model.__parent__.__parent__): action = AllocateAction msg = 'Allocated compute %s' elif ICompute.providedBy(model.__parent__.__parent__): action = DeployAction msg = 'Deployed compute %s' else: return try: path = canonical_path(model) owner = model.__owner__ ul = UserLogger(subject=model, owner=owner) log.msg('Attempting %s for %s (%s, %s)' % (action.__name__, model, path, owner), system='create-event') d = task.deferLater(reactor, 2.0, virtual_compute_action, action, path, event) d.addCallback(lambda r: ul.log(msg % path)) if not get_config().getboolean('stats', 'only_report_on_sync', True): d.addCallback(lambda r: defer.maybeDeferred(getUtility(IUserStatisticsProvider).update, owner)) d.addErrback(log.err) except Exception: log.err(system='create-event')
def delete_virtual_compute(model, event): if not ICompute.providedBy(model.__parent__.__parent__): return if IDeployed.providedBy(model): log.msg('Deleting compute %s which is in IDeployed state, shutting down and ' 'undeploying first' % model.hostname, system='compute-backend') yield DestroyComputeAction(model).execute(DetachedProtocol(), object()) yield UndeployAction(model).execute(DetachedProtocol(), object()) else: log.msg('Deleting compute %s which is already in IUndeployed state' % model.hostname, system='compute-backend') owner = (yield db.get(model, '__owner__')) ulog = UserLogger(subject=model, owner=owner) ulog.log('Deleted %s' % model) @db.transact def deallocate_ip(): ippools = db.get_root()['oms_root']['ippools'] ip = netaddr.IPAddress(model.ipv4_address.split('/')[0]) if ippools.free(ip): ulog.log('Deallocated IP: %s', ip) yield deallocate_ip()
def allowed_classes_gen(item): from opennode.knot.model.machines import Machines from opennode.knot.model.virtualizationcontainer import IVirtualizationContainer yield isinstance(item, Machines) yield isinstance(item, Computes) yield ICompute.providedBy(item) yield IVirtualizationContainer.providedBy(item) yield IHangar.providedBy(item)
def get_gatherers(): oms_root = db.get_root()["oms_root"] computes = filter( lambda c: c and ICompute.providedBy(c) and not c.failure, map(follow_symlinks, oms_root["computes"].listcontent()), ) gatherers = filter(None, (queryAdapter(c, IMetricsGatherer) for c in computes)) return gatherers
def execute(self, args): for arg in args.host: obj = self.traverse(arg) if ICompute.providedBy(obj): address = obj.hostname.encode('utf-8') else: address = arg res = ping(address) self.write("%s is %s\n" % (address, ["unreachable", "alive"][res]))
def allowed_classes_gen(item): from opennode.knot.model.compute import ICompute, IVirtualCompute from opennode.knot.model.machines import Machines from opennode.knot.model.virtualizationcontainer import IVirtualizationContainer yield isinstance(item, Machines) yield isinstance(item, Templates) yield IVirtualizationContainer.providedBy(item) yield ICompute.providedBy(item) yield IVirtualCompute.providedBy(item)
def collect(container): seen = set() for item in container.listcontent(): if ICompute.providedBy(item): computes[item.__name__] = Symlink(item.__name__, item) if any(allowed_classes_gen(item)): if item.__name__ not in seen: seen.add(item.__name__) collect(item)
def get_compute_ips(): try: computes = map(follow_symlinks, filter(lambda c: ICompute.providedBy(follow_symlinks(c)), db.get_root()['oms_root']['computes'].listcontent())) pools = db.get_root()['oms_root']['ippools'] for c in computes: ip = IPAddress(c.ipv4_address.split('/')[0]) pool = pools.find_pool(ip) if pool is not None and not pool.get(ip): log.msg('Marking %s as used...' % ip, system='sync-ippool') pool.use(ip) except Exception: log.err(system='sync-ippool') raise
def get_compute_ips(): try: computes = map( follow_symlinks, filter( lambda c: ICompute.providedBy(follow_symlinks(c)), db.get_root()['oms_root']['computes'].listcontent())) pools = db.get_root()['oms_root']['ippools'] for c in computes: ip = IPAddress(c.ipv4_address.split('/')[0]) pool = pools.find_pool(ip) if pool is not None and not pool.get(ip): log.msg('Marking %s as used...' % ip, system='sync-ippool') pool.use(ip) except Exception: log.err(system='sync-ippool') raise
def allocate_virtual_compute_from_hangar(model, event): if not IVirtualizationContainer.providedBy(model.__parent__): return if IDeployed.providedBy(model): return auto_allocate = get_config().getboolean('vms', 'auto_allocate', True) if not auto_allocate: return if IHangar.providedBy(model.__parent__.__parent__): action = AllocateAction msg = 'Allocated compute %s' elif ICompute.providedBy(model.__parent__.__parent__): action = DeployAction msg = 'Deployed compute %s' else: return try: path = canonical_path(model) owner = model.__owner__ ul = UserLogger(subject=model, owner=owner) log.msg('Attempting %s for %s (%s, %s)' % (action.__name__, model, path, owner), system='create-event') d = task.deferLater(reactor, 2.0, virtual_compute_action, action, path, event) d.addCallback(lambda r: ul.log(msg % path)) if not get_config().getboolean('stats', 'only_report_on_sync', True): d.addCallback(lambda r: defer.maybeDeferred( getUtility(IUserStatisticsProvider).update, owner)) d.addErrback(log.err) except Exception: log.err(system='create-event')
def condition_generator(m): yield ICompute.providedBy(m) yield find_compute_v12n_container(m, container) yield not getattr(m, 'exclude_from_allocation', None) if not get_config().getboolean('overcommit', 'memory', False): yield self.context.memory_usage < m.memory else: log.msg('Memory filtering is disabled.', system='action-allocate') if not get_config().getboolean('overcommit', 'disk', False): yield sum(map(lambda (pk, pv): pv, filter(lambda (pk, pv): pk != 'total', self.context.diskspace.iteritems()))) < (m.diskspace.get(param, 0) - m.diskspace_usage.get(param, 0)) else: log.msg('Diskspace filtering is disabled.', system='action-allocate') if not get_config().getboolean('overcommit', 'cores', False): yield self.context.num_cores <= m.num_cores else: log.msg('\'Total # of cores\' filtering is disabled.', system='action-allocate') templates = m['vms-%s' % container]['templates'] yield self.context.template in map(lambda t: t.name, filter(lambda t: ITemplate.providedBy(t), templates.listcontent() if templates else []))
def execute(self, args): for machine in db.get_root()['oms_root']['machines']: if not ICompute.providedBy(machine) or IVirtualCompute.providedBy(machine): continue InstallSaltAction(machine).execute(DetachedProtocol(), object())
def subject(self, args): return tuple(machine for machine in db.get_root()['oms_root']['machines'] if ICompute.providedBy(machine) and not IVirtualCompute.providedBy(machine))
def get_gatherers(): oms_root = db.get_root()['oms_root'] computes = filter(lambda c: c and ICompute.providedBy(c) and not c.failure, map(follow_symlinks, oms_root['computes'].listcontent())) gatherers = filter(None, (queryAdapter(c, IMetricsGatherer) for c in computes)) return gatherers
def _execute(self, cmd, args): @db.ro_transact def get_destination(): return (args.__parent__ if IVirtualizationContainer.providedBy(args) else cmd.traverse(args.dest_path)) @db.ro_transact def get_hostname(target): return target.hostname name = yield db.get(self.context, '__name__') source_vms = yield db.get(self.context, '__parent__') destination = yield get_destination() assert ICompute.providedBy(destination), 'Destination must be a Compute' assert not IVirtualCompute.providedBy(destination), 'Cannot migrate to a VM' destination_hostname = yield get_hostname(destination) destination_vms = follow_symlinks(destination['vms']) assert (yield db.get(destination_vms, 'backend')) == (yield db.get(source_vms, 'backend')),\ 'Destination backend is different from source' @db.transact def set_additional_keys(): self._additional_keys = [canonical_path(destination_vms), canonical_path(destination)] yield set_additional_keys() yield self.reacquire_until_clear() log.msg('Initiating migration for %s to %s' % (name, destination_hostname), system='migrate') try: if not (yield self._check_vm_pre(name, destination_hostname, destination_vms)): return source_submitter = IVirtualizationContainerSubmitter(source_vms) yield source_submitter.submit(IMigrateVM, name, destination_hostname, (not args.offline), False) log.msg('Migration done. Checking... %s' % destination_vms, system='migrate') if (yield self._check_vm_post(cmd, name, destination_hostname, destination_vms)): log.msg('Migration finished successfully!', system='migrate') @db.transact def mv_and_inherit(): machines = db.get_root()['oms_root']['machines'] computes = db.get_root()['oms_root']['computes'] try: destination_compute = machines[destination.__name__] vm_compute = follow_symlinks(computes[self.context.__name__]) vm_compute.failure = destination_compute.failure vm_compute.suspicious = destination_compute.suspicious dvms = follow_symlinks(destination_compute['vms']) dvms.add(vm_compute) log.msg('Model moved.', system='migrate') except IndexError: log.msg('Model NOT moved: destination compute or vms do not exist', system='migrate') except KeyError: log.msg('Model NOT moved: already moved by sync?', system='migrate') yield mv_and_inherit() except OperationRemoteError as e: self._action_log(cmd, 'Failed migration of %s to %s: remote error %s' % ( name, destination_hostname, '\n%s' % e.remote_tb if e.remote_tb else ''), logLevel=ERROR, system='migrate')
def _execute(self, cmd, args): @db.ro_transact def get_destination(): return (args.__parent__ if IVirtualizationContainer.providedBy(args) else cmd.traverse(args.dest_path)) @db.ro_transact def get_hostname(target): return target.hostname name = yield db.get(self.context, '__name__') source_vms = yield db.get(self.context, '__parent__') destination = yield get_destination() assert ICompute.providedBy( destination), 'Destination must be a Compute' assert not IVirtualCompute.providedBy( destination), 'Cannot migrate to a VM' destination_hostname = yield get_hostname(destination) destination_vms = follow_symlinks(destination['vms']) assert (yield db.get(destination_vms, 'backend')) == (yield db.get(source_vms, 'backend')),\ 'Destination backend is different from source' @db.transact def set_additional_keys(): self._additional_keys = [ canonical_path(destination_vms), canonical_path(destination) ] yield set_additional_keys() yield self.reacquire_until_clear() log.msg('Initiating migration for %s to %s' % (name, destination_hostname), system='migrate') try: if not (yield self._check_vm_pre(cmd, name, destination_hostname, destination_vms)): return source_submitter = IVirtualizationContainerSubmitter(source_vms) yield source_submitter.submit(IMigrateVM, name, destination_hostname, (not args.offline), False) log.msg('Migration done. Checking... %s' % destination_vms, system='migrate') if (yield self._check_vm_post(cmd, name, destination_hostname, destination_vms)): log.msg('Migration finished successfully!', system='migrate') @db.transact def mv_and_inherit(): machines = db.get_root()['oms_root']['machines'] computes = db.get_root()['oms_root']['computes'] try: destination_compute = machines[destination.__name__] vm_compute = follow_symlinks( computes[self.context.__name__]) vm_compute.failure = destination_compute.failure vm_compute.suspicious = destination_compute.suspicious dvms = follow_symlinks(destination_compute['vms']) dvms.add(vm_compute) log.msg('Model moved.', system='migrate') except IndexError: log.msg( 'Model NOT moved: destination compute or vms do not exist', system='migrate') except KeyError: log.msg('Model NOT moved: already moved by sync?', system='migrate') yield mv_and_inherit() except OperationRemoteError as e: self._action_log(cmd, 'Failed migration of %s to %s: remote error %s' % (name, destination_hostname, '\n%s' % e.remote_tb if e.remote_tb else ''), logLevel=ERROR, system='migrate')
def get_manageable_machines(): oms_root = db.get_root()['oms_root'] machines = map(follow_symlinks, oms_root['machines'].listcontent()) res = [(m, m.hostname) for m in machines if ICompute.providedBy(m) and IManageable.providedBy(m)] return res