def sync_hw(self, full): if not any_stack_installed(self.context): return try: info = yield IGetComputeInfo(self.context).run() uptime = yield IGetHWUptime(self.context).run() disk_usage = yield IGetDiskUsage(self.context).run() except OperationRemoteError as e: log.msg(e.message, system='sync-hw') if e.remote_tb: log.msg(e.remote_tb, system='sync-hw') return # TODO: Improve error handling def disk_info(aspect): res = dict((unicode(k), round(float(v[aspect]) / 1024, 2)) for k, v in disk_usage.items() if v['device'].startswith('/dev/')) res[u'total'] = sum([0.0] + res.values()) return res try: routes = yield IGetRoutes(self.context).run() if full else [] except Exception: routes = [] try: yield self._sync_hw(info, disk_info('total'), disk_info('used'), routes, uptime) except Exception: log.err(system='sync-action')
def ensure_vms(self, full): """ Ensures that current HN model has all virtualization container objects """ if (not any_stack_installed(self.context) or # if not full sync and there are virtualization containers available (not full and len(filter(lambda n: 'vms' in n, self.context.listnames())) > 0)): return vms_types = yield IGetVirtualizationContainers(self.context).run() if not vms_types: return url_to_backend_type = dict((v, k) for k, v in backends.items()) @db.transact def add_container(backend_type): log.msg('Adding backend %s' % backend_type, system='sync') vms = VirtualizationContainer(unicode(backend_type)) if vms.__name__ in self.context.listnames(): return self.context.add(vms) if not self.context['vms']: self.context.add(Symlink('vms', self.context[vms.__name__])) for vms_type in vms_types: backend_type = url_to_backend_type.get(vms_type) if not backend_type: log.msg('Unrecognized backend: %s. Skipping' % vms_type, system='sync') continue yield add_container(backend_type)
def sync_hw(self, full): if not any_stack_installed(self.context): return try: info = yield IGetComputeInfo(self.context).run() uptime = yield IGetHWUptime(self.context).run() disk_usage = yield IGetDiskUsage(self.context).run() except OperationRemoteError as e: log.msg(e.message, system='sync-hw') if e.remote_tb: log.msg(e.remote_tb, system='sync-hw') return # TODO: Improve error handling def disk_info(aspect): res = dict((unicode(k), round(float(v[aspect]) / 1024, 2)) for k, v in disk_usage.items() if v['device'].startswith('/dev/')) res[u'total'] = sum([0.0] + res.values()) return res try: routes = yield IGetRoutes(self.context).run() if full else [] except Exception: routes = [] try: yield self._sync_hw(info, disk_info('total'), disk_info('used'), routes, uptime) except Exception: log.err(system='sync-action')
def _execute(self, cmd, args): self._full = getattr(args, 'full', False) log.msg('Executing SyncAction on %s (%s) (Full: %s)' % (self.context, canonical_path(self.context), self._full), system='sync-action') if any_stack_installed(self.context): try: yield self.sync_agent_version(self._full) except OperationRemoteError: log.err(system='sync-action') yield self.sync_hw(self._full) try: yield self.ensure_vms(self._full) except Exception: log.err(system='sync-action') if self._full: try: yield SyncTemplatesAction(self.context)._execute( DetachedProtocol(), object()) except Exception: log.err(system='sync-action') else: log.msg('No stacks installed on %s: %s' % (self.context, self.context.features)) try: default = yield self.default_console() yield self.sync_consoles() if IVirtualCompute.providedBy(self.context): yield self._sync_virtual() yield self._create_default_console(default) except Exception: log.err(system='sync-action') @db.transact def set_additional_keys(): vms = follow_symlinks(self.context['vms']) if vms: self._additional_keys = (canonical_path(vms), ) yield set_additional_keys() dl = yield self.reacquire() if dl is not None: return yield self.sync_vms()
def _execute(self, cmd, args): self._full = getattr(args, 'full', False) log.msg('Executing SyncAction on %s (%s) (Full: %s)' % (self.context, canonical_path(self.context), self._full), system='sync-action') if any_stack_installed(self.context): try: yield self.sync_agent_version(self._full) except OperationRemoteError: log.err(system='sync-action') yield self.sync_hw(self._full) try: yield self.ensure_vms(self._full) except Exception: log.err(system='sync-action') if self._full: try: yield SyncTemplatesAction(self.context)._execute(DetachedProtocol(), object()) except Exception: log.err(system='sync-action') else: log.msg('No stacks installed on %s: %s' % (self.context, self.context.features)) try: default = yield self.default_console() yield self.sync_consoles() if IVirtualCompute.providedBy(self.context): yield self._sync_virtual() yield self._create_default_console(default) except Exception: log.err(system='sync-action') @db.transact def set_additional_keys(): vms = follow_symlinks(self.context['vms']) if vms: self._additional_keys = (canonical_path(vms),) yield set_additional_keys() dl = yield self.reacquire() if dl is not None: return yield self.sync_vms()
def ensure_vms(self, full): """ Ensures that current HN model has all virtualization container objects """ if (not any_stack_installed(self.context) or # if not full sync and there are virtualization containers available (not full and len(filter(lambda n: 'vms' in n, self.context.listnames())) > 0)): return vms_types = yield IGetVirtualizationContainers(self.context).run() if not vms_types: return url_to_backend_type = dict((v, k) for k, v in backends.items()) @db.transact def add_container(backend_type): log.msg('Adding backend %s' % backend_type, system='sync') vms = VirtualizationContainer(unicode(backend_type)) if vms.__name__ in self.context.listnames(): return self.context.add(vms) if not self.context['vms']: self.context.add(Symlink('vms', self.context[vms.__name__])) for vms_type in vms_types: backend_type = url_to_backend_type.get(vms_type) if not backend_type: log.msg('Unrecognized backend: %s. Skipping' % vms_type, system='sync') continue yield add_container(backend_type)
def _execute(self, cmd, args): # XXX for some strange reason args is object() if type(args) == argparse.Namespace: self._full = args.full log.msg('Executing SyncAction on %s (%s)' % (self.context, canonical_path(self.context)), system='sync-action') if any_stack_installed(self.context): yield self.sync_agent_version(self._full) yield self.sync_hw(self._full) yield self.ensure_vms(self._full) if self._full: yield SyncTemplatesAction(self.context)._execute(DetachedProtocol(), object()) else: log.msg('No stacks installed on %s: %s' % (self.context, self.context.features)) default = yield self.default_console() yield self.sync_consoles() if IVirtualCompute.providedBy(self.context): yield self._sync_virtual() yield self._create_default_console(default) @db.transact def set_additional_keys(): vms = follow_symlinks(self.context['vms']) if vms: self._additional_keys = (canonical_path(vms),) yield set_additional_keys() dl = yield self.reacquire() if dl is not None: return yield self.sync_vms()
def _execute(self, cmd, args): if not any_stack_installed(self.context): return @db.transact def update_templates(container, templates): if not container['templates']: container.add(Templates()) template_container = container['templates'] for template in templates: name = template['template_name'] if not template_container['by-name'][name]: template_container.add(Template(unicode(name), get_u(template, 'domain_type'))) t = template_container['by-name'][name].target t.cores = (get_i(template, 'vcpu_min'), get_i(template, 'vcpu'), max(-1, get_i(template, 'vcpu_max'))) t.memory = (get_f(template, 'memory_min'), get_f(template, 'memory'), max(-1.0, get_f(template, 'memory_max'))) t.swap = (get_f(template, 'swap_min'), get_f(template, 'swap'), max(-1.0, get_f(template, 'swap_max'))) t.disk = (get_f(template, 'disk_min'), get_f(template, 'disk'), max(-1.0, get_f(template, 'disk_max'))) t.nameserver = get_u(template, 'nameserver') t.username = get_u(template, 'username') t.password = get_u(template, 'passwd') t.cpu_limit = (get_i(template, 'vcpulimit_min'), get_i(template, 'vcpulimit')) t.ip = get_u(template, 'ip_address') # delete templates no more offered upstream template_names = template_container['by-name'].listnames() vanished_template_names = set(template_names).difference( template['template_name'] for template in templates) for template in vanished_template_names: template_container.remove(follow_symlinks(template_container['by-name'][template])) for container in self.context.listcontent(): if not IVirtualizationContainer.providedBy(container): continue submitter = IVirtualizationContainerSubmitter(container) templates = yield submitter.submit(IGetLocalTemplates) if not templates: log.msg('Did not find any templates on %s/%s' % (self.context, container), system='sync-templates') continue log.msg('Synced templates on %s (%s). Updating %s templates' % (self.context, container, len(templates)), system='sync-templates') yield update_templates(container, templates)
def _execute(self, cmd, args): if not any_stack_installed(self.context): return @db.transact def update_templates(container, templates): if not container['templates']: container.add(Templates()) template_container = container['templates'] for template in templates: name = template['template_name'] if not template_container['by-name'][name]: template_container.add( Template(unicode(name), get_u(template, 'domain_type'))) t = template_container['by-name'][name].target t.cores = (get_i(template, 'vcpu_min'), get_i(template, 'vcpu'), max(-1, get_i(template, 'vcpu_max'))) t.memory = (get_f(template, 'memory_min'), get_f(template, 'memory'), max(-1.0, get_f(template, 'memory_max'))) t.swap = (get_f(template, 'swap_min'), get_f(template, 'swap'), max(-1.0, get_f(template, 'swap_max'))) t.disk = (get_f(template, 'disk_min'), get_f(template, 'disk'), max(-1.0, get_f(template, 'disk_max'))) t.nameserver = get_u(template, 'nameserver') t.username = get_u(template, 'username') t.password = get_u(template, 'passwd') t.cpu_limit = (get_i(template, 'vcpulimit_min'), get_i(template, 'vcpulimit')) t.ip = get_u(template, 'ip_address') # delete templates no more offered upstream template_names = template_container['by-name'].listnames() vanished_template_names = set(template_names).difference( template['template_name'] for template in templates) for template in vanished_template_names: template_container.remove( follow_symlinks(template_container['by-name'][template])) for container in self.context.listcontent(): if not IVirtualizationContainer.providedBy(container): continue submitter = IVirtualizationContainerSubmitter(container) templates = yield submitter.submit(IGetLocalTemplates) if not templates: log.msg('Did not find any templates on %s/%s' % (self.context, container), system='sync-templates') continue log.msg('Synced templates on %s (%s). Updating %s templates' % (self.context, container, len(templates)), system='sync-templates') yield update_templates(container, templates)