def sync_vm(self, vm): compute = TmpObj(self.context) compute.state = unicode(vm['state']) # Ensure IDeployed marker is set, unless not in another state if not IDeployed.providedBy(compute): noLongerProvides(self.context, IUndeployed) noLongerProvides(self.context, IDeploying) alsoProvides(self.context, IDeployed) if 'ctid' in vm: compute.ctid = vm['ctid'] if vm['ctid'] != '-' else -1 for idx, console in enumerate(vm['consoles']): if console['type'] == 'pty' and not self.context.consoles['tty%s' % idx]: self.context.consoles.add(TtyConsole('tty%s' % idx, console['pty'])) if console['type'] == 'openvz' and not self.context.consoles['tty%s' % idx]: self.context.consoles.add(OpenVzConsole('tty%s' % idx, console['cid'])) if console['type'] == 'vnc' and not self.context.consoles['vnc']: self.context.consoles.add(VncConsole( self.context.__parent__.__parent__.hostname, int(console['port']))) # XXX TODO: handle removal of consoles when they are no longer reported from upstream # networks for interface in vm['interfaces']: if not self.context.interfaces[interface['name']]: iface = NetworkInterface(interface['name'], None, interface['mac'], 'active') if 'ipv4_address' in interface: iface.ipv4_address = interface['ipv4_address'] self.context.interfaces.add(iface) # XXX TODO: handle removal of interfaces when they are no longer reported from upstream # XXX hack, openvz specific compute.cpu_info = self.context.__parent__.__parent__.cpu_info compute.memory = vm['memory'] diskspace = dict((unicode(k), v) for k, v in vm['diskspace'].items()) diskspace[u'total'] = sum([0.0] + vm['diskspace'].values()) # round diskspace values for i in diskspace: diskspace[i] = round(diskspace[i], 2) compute.diskspace = diskspace if compute.state == 'active': compute.uptime = get_f(vm, 'uptime') else: compute.uptime = None compute.num_cores = vm['vcpu'] compute.swap_size = vm.get('swap') or compute.swap_size compute.kernel = vm.get('kernel') or compute.kernel with SuppressEvents(self.context): compute.apply()
def finalize_vm(): ippools = db.get_root()['oms_root']['ippools'] ip = netaddr.IPAddress(self.context.ipv4_address.split('/')[0]) if ippools.free(ip): ulog = UserLogger(principal=cmd.protocol.interaction.participations[0].principal, subject=self.context, owner=self.context.__owner__) ulog.log('Deallocated IP: %s', ip) vm = traverse1(canonical_path(self.context)) if vm is not None: noLongerProvides(vm, IDeployed) alsoProvides(vm, IUndeployed)
def finalize_vm(): ippools = db.get_root()['oms_root']['ippools'] ip = netaddr.IPAddress(self.context.ipv4_address.split('/')[0]) log.msg('Attempting to deallocate IP %s from the pools' % ip, system='undeploy-action') if ippools.free(ip): ulog = UserLogger(principal=cmd.protocol.interaction. participations[0].principal, subject=self.context, owner=self.context.__owner__) ulog.log('Deallocated IP: %s', ip) log.msg('Deallocated IP %s' % ip, system='ippool') vm = traverse1(canonical_path(self.context)) if vm is not None: noLongerProvides(vm, IDeployed) alsoProvides(vm, IUndeployed)
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 }))
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 __init__(self, hostname, state=None, memory=None, template=None, ipv4_address=None, mgt_stack=None): super(Compute, self).__init__() self.hostname = hostname self.memory = memory self.state = state self.template = template if ipv4_address: self._ipv4_address = ipv4_address self._mgt_stack = mgt_stack if self.template: alsoProvides(self, IVirtualCompute) alsoProvides(self, IUndeployed) elif self._mgt_stack: alsoProvides(self, self._mgt_stack) assert self.hostname
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 sync_vm(self, vm): compute = TmpObj(self.context) compute.state = unicode(vm['state']) # Ensure IDeployed marker is set, unless not in another state if not IDeployed.providedBy(compute): noLongerProvides(self.context, IUndeployed) noLongerProvides(self.context, IDeploying) alsoProvides(self.context, IDeployed) if 'ctid' in vm: compute.ctid = vm['ctid'] if vm['ctid'] != '-' else -1 for idx, console in enumerate(vm['consoles']): if console['type'] == 'pty' and not self.context.consoles['tty%s' % idx]: self.context.consoles.add( TtyConsole('tty%s' % idx, console['pty'])) if console['type'] == 'openvz' and not self.context.consoles[ 'tty%s' % idx]: self.context.consoles.add( OpenVzConsole('tty%s' % idx, console['cid'])) if console['type'] == 'vnc' and not self.context.consoles['vnc']: self.context.consoles.add( VncConsole(self.context.__parent__.__parent__.hostname, int(console['port']))) # XXX TODO: handle removal of consoles when they are no longer reported from upstream # networks for interface in vm['interfaces']: if not self.context.interfaces[interface['name']]: iface = NetworkInterface(interface['name'], None, interface['mac'], 'active') if 'ipv4_address' in interface: iface.ipv4_address = interface['ipv4_address'] self.context.interfaces.add(iface) # XXX TODO: handle removal of interfaces when they are no longer reported from upstream # XXX hack, openvz specific compute.cpu_info = self.context.__parent__.__parent__.cpu_info compute.memory = vm['memory'] diskspace = dict((unicode(k), v) for k, v in vm['diskspace'].items()) diskspace[u'total'] = sum([0.0] + vm['diskspace'].values()) # round diskspace values for i in diskspace: diskspace[i] = round(diskspace[i], 2) compute.diskspace = diskspace if compute.state == 'active': compute.uptime = get_f(vm, 'uptime') else: compute.uptime = None compute.num_cores = vm['vcpu'] compute.swap_size = vm.get('swap') or compute.swap_size compute.kernel = vm.get('kernel') or compute.kernel with SuppressEvents(self.context): compute.apply()
def set_inprogress(self): if self.inprogress_marker is not None: alsoProvides(self.context, self.inprogress_marker) if self.state is not None: self.context.state = self.state