def update_vm_process(self, libvirt_id, vm_config): if self.vm_procs.get(libvirt_id) != vm_config.oid: vm_process = VmProcess(node=self, libvirt_id=libvirt_id, vm_config=vm_config) Cluster.instance().store(vm_process) self.vm_procs[libvirt_id] = vm_process.oid
def update_host_nic(self, name, **kw): nic = self.get_host_nic(name) if nic: nic.set(**kw) else: nic = HostNic(host=self, name=name, **kw) self.host_nics[name] = nic.oid Cluster.instance().store(nic)
def setUp(self): AsyncTestCase.setUp(self) for key in options.keys(): del options[key] define_common_options() define_node_options() parse_config_file(os.path.join(DIR, 'config_data.py')) self.init_storages() self.node_oid = str(uuid.uuid1()) Cluster.instance().init()
def storage(self): cluster = Cluster.instance() storage = cluster.get(self.storage_oid) if storage is None: raise RuntimeError( "Storage %s for mountpoint %s not exists" % ( self.storage_oid, self)) return storage
def get_host_nic(self, nic_name): cluster = Cluster.instance() try: nic_oid = self.host_nics[nic_name] nic = cluster.get(nic_oid) if nic is None: raise KeyError(nic_name) return nic except KeyError: return None
def on_ifconfig(report): cluster = Cluster.instance() node = cluster.get(report.node_oid) if not node: # Node is not ready return for name, info in report.parsed_data.items(): mac = info.get('mac') if not mac: continue # lo node.update_host_nic(name, **info)
def on_mngr_msg(client, body, routing_key): default_mngr_callback(client, body, routing_key) event = Entity.from_json(body) if isinstance(event, NodeOnlineReport): node = Cluster.instance().get(self.node_oid) self.assertEqual(len(node.storages), 2) for mount_point in node.storages: if mount_point.storage_oid == self.storage1_oid: self.assertEqual(mount_point.path, self.storage1_path) self.stop()
def test_vmxml_report_updates_cluster(self): on_report(NodeOnlineReport.create(self.node_oid, hostname='testhost')) report = VmXMLReport.create(self.node_oid, raw_data = LIBVIRT_XML) on_report(report) cluster = Cluster.instance() vms = cluster.entities_by_class('VmProcess') self.assertEqual(len(vms), 1) node = cluster.get(self.node_oid) self.assertEqual(len(node.get_vm_procs()), 1) vm_proc = vms[0] self.assertEqual(vm_proc.vm_config.nics[0].target, 'vnet0')
def test_crud(self): cluster = Cluster.instance() node = Node(hostname='testhost') cluster.store(node) on_report(IFConfigReport.create(node.oid, raw_data=IFCONFIG_DATA)) network = Network(title='TestNetwork') virbr2 = node.get_host_nic('virbr2') network.add_host_nic(virbr2) self.assertTrue(virbr2.oid in network.host_nics) eth2 = node.get_host_nic('eth2') self.assertEqual(len(network.host_nics), 1) self.assertFalse(eth2.oid in network.host_nics) # only bridge should be added
def test_update(self): cluster = Cluster.instance() node = Node(oid=self.node_oid, hostname='hostname') cluster.store(node) node.vm_procs = dict(a='dummy') self.assertEqual(cluster.get(self.node_oid).vm_procs['a'], 'dummy') cluster.store(Node(oid=self.node_oid, hostname='hostname')) self.assertEqual(cluster.get(self.node_oid).vm_procs['a'], 'dummy')
def get(self, path): """Return view for entity """ entity = Cluster.instance().get(path) if not entity: raise HTTPError(404) self.render('entity.html', class_name = entity.__class__.__name__, oid = entity.oid, cmd = entity.oid, table = get_entity_view(entity).get_html())
def on_mngr_msg(client, body, routing_key): report = Entity.from_json(body) on_report(report) self.assertEqual(report.reporter_oid, self.node_oid) if report.__class__.__name__ == 'NodeOnlineReport': on_report(IFConfigReport.create(self.node_oid, raw_data=IFCONFIG_DATA)) client.send_task(VMInventoryTask(node_oid=self.node_oid)) if report.__class__.__name__ == 'VmXMLReport': cluster = Cluster.instance() vm_config = cluster.entities_by_class('VmConfig')[0] self.assertEqual(vm_config.oid, 'c2127a40-eb4c-4e3c-af5b-ab455fd8bb40') self.stop()
def test_update_cluster(self): import uuid storage_oid = str(uuid.uuid4()) on_report(NodeOnlineReport.create(self.node_oid, hostname='testhost', storages=[dict( node_oid=self.node_oid, storage_oid=storage_oid, path='/home/storage1')])) on_report(DFReport.create(self.node_oid, raw_data=DF_RAW)) cluster = Cluster.instance() storage = cluster.get(storage_oid) self.assertTrue(storage) self.assertEqual(storage.avail, '702G')
def post(self): import IPython; IPython.embed() cmd = self.get_argument('cmd', '') cluster = Cluster.instance() view = None if cmd == 'vms': view = vm_list_view( cluster.entities_by_class('VmProcess')) entity = cluster.search(cmd) if entity: self.redirect('/%s' % entity.oid) self.render('page.html', cmd=cmd, view=view)
def test_report_arrived(self): node_oid = str(uuid.uuid1()) on_report(NodeOnlineReport.create(node_oid, hostname='testhost')) on_report(IFConfigReport.create(node_oid, raw_data=IFCONFIG_DATA)) on_report(BrctlShowReport.create(node_oid, raw_data=BRCTL_SHOW_DATA)) cluster = Cluster.instance() node = cluster.get(node_oid) self.assertTrue(node) eth2 = node.get_host_nic('eth2') self.assertTrue(eth2) self.assertEqual(eth2.in_bridge, 'virbr2') virbr2 = node.get_host_nic('virbr2') self.assertEqual(virbr2.bridge_for, ['eth2', 'vnet0', 'vnet1'])
def test_entites_storing(self): cluster = Cluster.instance() item1 = Item('foo') item2 = Item('bar') item3 = Baz('baz') cluster.store(item1) cluster.store(item2) cluster.store(item3) self.assertEqual(cluster.get(item1.oid), item1) self.assertEqual(len(cluster.entities_by_class('Item')), 2) self.assertEqual(len(cluster.entities_by_class(Baz)), 1) self.assertEqual(cluster.entities_by_class(Baz)[0], item3) cluster.delete(item1) self.assertEqual(len(cluster.oids_by_class(Item)), 1)
def on_brctl_show(report): cluster = Cluster.instance() node = cluster.get(report.node_oid) if not node: return for bridge_name, nic_names in report.parsed_data.items(): for nic_name in nic_names: nic = node.get_host_nic(nic_name) if nic: # if not - didn't get ifconfig report yet, nothing we can do nic.in_bridge = bridge_name cluster.store(nic) bridge = node.get_host_nic(bridge_name) if bridge: bridge.bridge_for = nic_names cluster.store(bridge)
def on_mngr_msg(client, body, routing_key): inst = self.entity_from_json(body) default_callback(client, body, routing_key) if isinstance(inst, NodeOnlineReport): self.node.publish_report( IFConfigReport.create(self.node_oid, raw_data=RAW_DATA)) if isinstance(inst, IFConfigReport): self.assertEqual(inst.raw_data, RAW_DATA) cluster = Cluster.instance() node = cluster.get(self.node_oid) self.assertTrue(node) self.assertEqual(node.get_host_nic('eth1').inet_addr, '10.0.1.1') self.assertEqual(node.get_host_nic('vnet0').rx_bytes, 11546476) self.stop()
def test_node_online_update_cluster(self): def on_mngr_msg(client, body, routing_key): default_mngr_callback(client, body, routing_key) event = Entity.from_json(body) if isinstance(event, NodeOnlineReport): node = Cluster.instance().get(self.node_oid) self.assertEqual(len(node.storages), 2) for mount_point in node.storages: if mount_point.storage_oid == self.storage1_oid: self.assertEqual(mount_point.path, self.storage1_path) self.stop() self.set_manager(on_mngr_msg) self.set_node() self.wait() cluster = Cluster.instance() node = cluster.get(self.node_oid) self.assertTrue(isinstance(node, Node))
def on_node_online(report): from swarm.stuff import Storage from swarm.tasks import VMInventoryTask cluster = Cluster.instance() if not cluster.is_stored(report.node_oid): node = Node(oid=report.node_oid, hostname=report.hostname, state='online') cluster.store(node) if CLIENT: task = VMInventoryTask(node_oid=node.oid) cluster.store(task) CLIENT.send_task(task) else: log.warn("No client available in on_node_online") else: node = cluster.get(report.node_oid) node.state = 'online' Storage.update_points(node, report.storages)
def on_vmxml(report): "Procedures on libvirt xml report" cluster = Cluster.instance() node = cluster.get(report.node_oid) if node is None: return on_report_from_offline_node(report) data = report.parsed_data nics = [] for nic in data['nics']: nics.append(VmNic(**nic)) vm_config = VmConfig(oid=data['uuid'], vcpu=data['vcpu'], memory=data['memory'], name=data['name'], features=data['features'], libvirt_xml=report.raw_data, nics=nics) cluster.store(vm_config) if data.get('libvirt_id'): node.update_vm_process(data['libvirt_id'], vm_config)
def on_dfreport(report): def choose_mount_point(report, storage_point): report_pathes = [(len(x), x) for x in report.parsed_data.keys()] report_pathes.sort(key=lambda x:x[0], reverse=True) report_pathes = [x[1] for x in report_pathes] for mount_point in report_pathes: if storage_point.path.startswith(mount_point): return mount_point raise RuntimeError("No mount point for given storage points") cluster = Cluster.instance() if not cluster.is_stored(report.node_oid): return for storage_point in report.node.storages: mount_point = choose_mount_point(report, storage_point) storage = cluster.get(storage_point.storage_oid) or Storage( storage_point.storage_oid) storage.avail = report.parsed_data[mount_point]['avail'] cluster.store(storage)
def update_points(cls, node, points): cluster = Cluster.instance() def is_exists(info): for spoint in cluster.entities_by_class(StoragePoint): if spoint.node_oid == node.oid and \ spoint.path == info['path'] and \ spoint.storage_oid == info['storage_oid']: return True return False for point in points: storage_oid = point['storage_oid'] if not cluster.is_stored(storage_oid): cluster.store(Storage(oid=storage_oid)) if not is_exists(point): cluster.store(StoragePoint(node_oid=node.oid, storage_oid=point['storage_oid'], path=point['path']))
def get(self): cluster = Cluster.instance() import IPython; IPython.embed() self.redirect('/')
def host(self): cluster = Cluster.instance() for proc in cluster.entities_by_class("VmProcess"): if proc.vm_config.oid == self.oid: return proc.node return None
def storages(self): return [x for x in Cluster.instance().entities_by_class( StoragePoint) if x.node_oid == self.oid]
def get_vm_procs(self): cluster = Cluster.instance() return [cluster.get(x) for x in self.vm_procs.values()]
def from_store(self, value): from swarm.cluster import Cluster return Cluster.instance().get(value)
def node(self): from swarm.cluster import Cluster return Cluster.instance().get_or_error(self.node_oid)
def test_instance(self): self.assertEqual( Cluster.instance(), Cluster.instance())