def add_deployed_model_remove_from_hangar(c, target): path = canonical_path(target) target = traverse1(path) cpath = canonical_path(c) c = traverse1(cpath) if c is None: raise Exception('Compute not found: "%s"' % cpath) new_compute = Compute(unicode(hostname), u'inactive') new_compute.__name__ = name new_compute.__owner__ = owner_obj new_compute.template = unicode(template) new_compute._ipv4_address = unicode(ipaddr) new_compute.mac_address = getattr(c, 'mac_address', None) new_compute.memory = getattr(c, 'memory', 0) new_compute.diskspace = getattr(c, 'diskspace', {u'total': 0}) new_compute.num_cores = getattr(c, 'num_cores', 0) alsoProvides(new_compute, IVirtualCompute) alsoProvides(new_compute, IDeployed) noLongerProvides(new_compute, IManageable) target.add(new_compute) container = c.__parent__ del container[name] timestamp = int(time.time() * 1000) IStream(new_compute).add((timestamp, {'event': 'change', 'name': 'features', 'value': new_compute.features, 'old_value': self.context.features})) IStream(new_compute).add((timestamp, {'event': 'change', 'name': 'ipv4_address', 'value': new_compute._ipv4_address, 'old_value': self.context._ipv4_address}))
def make_compute(hostname=u'tux-for-test', state=u'active', memory=2000): return Compute(hostname, state, memory)
def _sync_vms_transact(self, remote_vms): local_vms = [i for i in self.context.listcontent() if IVirtualCompute.providedBy(i)] remote_uuids = set(i['uuid'] for i in remote_vms) local_uuids = set(i.__name__ for i in local_vms) root = db.get_root()['oms_root'] machines = root['machines'] for vm_uuid in remote_uuids.difference(local_uuids): remote_vm = [rvm for rvm in remote_vms if rvm['uuid'] == vm_uuid][0] existing_machine = follow_symlinks(machines['by-name'][remote_vm['name']]) if existing_machine: # XXX: this VM is a nested VM, for now let's hack it this way new_compute = Symlink(existing_machine.__name__, existing_machine) self.context._add(new_compute) else: log.msg('Adding virtual compute %s...' % vm_uuid, system='v12n-sync', logLevel=logging.WARNING) new_compute = Compute(unicode(remote_vm['name']), unicode(remote_vm['state'])) new_compute.__name__ = vm_uuid new_compute.template = unicode(remote_vm['template']) alsoProvides(new_compute, IVirtualCompute) alsoProvides(new_compute, IDeployed) # for now let's force new synced computes to not have salt installed # XXX: not sure if removing a parent interface will remove the child also noLongerProvides(new_compute, IManageable) self.context.add(new_compute) for vm_uuid in remote_uuids.intersection(local_uuids): noLongerProvides(self.context[vm_uuid], IUndeployed) alsoProvides(self.context[vm_uuid], IDeployed) for vm_uuid in local_uuids.difference(remote_uuids): if IDeploying.providedBy(self.context[vm_uuid]): log.msg("Don't delete undeployed VM while in IDeploying state", system='v12n') continue noLongerProvides(self.context[vm_uuid], IDeployed) alsoProvides(self.context[vm_uuid], IUndeployed) self.context[vm_uuid].state = u'inactive' if get_config().getboolean('sync', 'delete_on_sync'): log.msg("Deleting compute %s" % vm_uuid, system='v12n-sync', logLevel=logging.WARNING) compute = self.context[vm_uuid] del self.context[vm_uuid] handle(compute, ModelDeletedEvent(self.context)) # TODO: eliminate cross-import between compute and v12ncontainer from opennode.knot.backend.compute import ICompute from opennode.knot.backend.syncaction import SyncAction # sync each vm for compute in self.context.listcontent(): if not IVirtualCompute.providedBy(compute): continue log.msg('Attempting to sync %s' % compute, system='sync-vms') if not ICompute.providedBy(compute.__parent__.__parent__): log.msg('Inconsistent data: %s, Compute is expected. Attempting to fix %s' % (compute.__parent__.__parent__, compute), system='sync-vms', logLevel=logging.WARNING) compute.__parent__ = self.context log.msg('Fixing %s %s' % (compute, 'successful!' if ICompute.providedBy(compute.__parent__.__parent__) else 'failed!'), system='sync-vms', logLevel=logging.WARNING) if not ICompute.providedBy(compute.__parent__.__parent__): return action = SyncAction(compute) matching = [rvm for rvm in remote_vms if rvm['uuid'] == compute.__name__] if not matching: continue remote_vm = matching[0] # todo delegate all this into the action itself default_console = action._default_console() action._sync_consoles() action.sync_owner_transact(remote_vm) action.sync_vm(remote_vm) action.create_default_console(default_console)
def update(): machines = db.get_root()['oms_root']['machines'] machine = Compute(unicode(host), u'active', mgt_stack=mgt_stack) machine.__name__ = str(uuid5(NAMESPACE_DNS, host)) machines.add(machine) return machine.__name__
def add_deployed_model_remove_from_hangar(c, target): path = canonical_path(target) target = traverse1(path) cpath = canonical_path(c) c = traverse1(cpath) if c is None: raise Exception('Compute not found: "%s"' % cpath) new_compute = Compute(unicode(hostname), u'inactive') new_compute.__name__ = name new_compute.__owner__ = owner_obj new_compute.template = unicode(template) new_compute._ipv4_address = unicode(ipaddr) new_compute.mac_address = getattr(c, 'mac_address', None) new_compute.memory = getattr(c, 'memory', 0) new_compute.diskspace = getattr(c, 'diskspace', {u'total': 0}) new_compute.num_cores = getattr(c, 'num_cores', 0) new_compute.license_activated = getattr( c, 'license_activated', True) alsoProvides(new_compute, IVirtualCompute) alsoProvides(new_compute, IDeployed) noLongerProvides(new_compute, IManageable) target.add(new_compute) container = c.__parent__ del container[name] timestamp = int(time.time() * 1000) IStream(new_compute).add((timestamp, { 'event': 'change', 'name': 'features', 'value': new_compute.features, 'old_value': self.context.features })) IStream(new_compute).add((timestamp, { 'event': 'change', 'name': 'ipv4_address', 'value': new_compute._ipv4_address, 'old_value': self.context._ipv4_address }))