def _build_instance_node(self, instance): """Build an instance node Create an instance node for the graph using nova and the `server` nova object. :param instance: Nova VM object. :return: An instance node for the graph. """ flavor = instance.flavor instance_attributes = { "uuid": instance.id, "human_id": instance.human_id, "memory": flavor["ram"], "disk": flavor["disk"], "disk_capacity": flavor["disk"], "vcpus": flavor["vcpus"], "state": getattr(instance, "OS-EXT-STS:vm_state"), "metadata": instance.metadata} # node_attributes = dict() # node_attributes["layer"] = "virtual" # node_attributes["category"] = "compute" # node_attributes["type"] = "compute" # node_attributes["attributes"] = instance_attributes return element.Instance(**instance_attributes)
def build_scenario_1(self): instances = [] model = modelroot.ModelRoot() # number of nodes node_count = 5 # number max of instance per node node_instance_count = 7 # total number of virtual machine instance_count = (node_count * node_instance_count) for id_ in range(0, node_count): node_uuid = "Node_{0}".format(id_) hostname = "hostname_{0}".format(id_) node_attributes = { "id": id_, "uuid": node_uuid, "hostname": hostname, "memory": 132, "disk": 250, "disk_capacity": 250, "vcpus": 40, } node = element.ComputeNode(**node_attributes) model.add_node(node) for i in range(0, instance_count): instance_uuid = "INSTANCE_{0}".format(i) instance_attributes = { "uuid": instance_uuid, "memory": 2, "disk": 20, "disk_capacity": 20, "vcpus": 10, } instance = element.Instance(**instance_attributes) instances.append(instance) model.add_instance(instance) mappings = [ ("INSTANCE_0", "Node_0"), ("INSTANCE_1", "Node_0"), ("INSTANCE_2", "Node_1"), ("INSTANCE_3", "Node_2"), ("INSTANCE_4", "Node_2"), ("INSTANCE_5", "Node_2"), ("INSTANCE_6", "Node_3"), ("INSTANCE_7", "Node_4"), ] for instance_uuid, node_uuid in mappings: model.map_instance( model.get_instance_by_uuid(instance_uuid), model.get_node_by_uuid(node_uuid), ) return model
def get_or_create_instance(self, uuid): try: instance = self.cluster_data_model.get_instance_by_uuid(uuid) except exception.InstanceNotFound: # The instance didn't exist yet so we create a new instance object LOG.debug("New instance created: %s", uuid) instance = element.Instance() instance.uuid = uuid self.cluster_data_model.add_instance(instance) return instance
def execute(self): """Build the compute cluster data model""" LOG.debug("Building latest Nova cluster data model") model = model_root.ModelRoot() mem = element.Resource(element.ResourceType.memory) num_cores = element.Resource(element.ResourceType.cpu_cores) disk = element.Resource(element.ResourceType.disk) disk_capacity = element.Resource(element.ResourceType.disk_capacity) model.create_resource(mem) model.create_resource(num_cores) model.create_resource(disk) model.create_resource(disk_capacity) flavor_cache = {} nodes = self.wrapper.get_compute_node_list() for n in nodes: service = self.wrapper.nova.services.find(id=n.service['id']) # create node in cluster_model_collector node = element.ComputeNode(n.id) node.uuid = service.host node.hostname = n.hypervisor_hostname # set capacity mem.set_capacity(node, n.memory_mb) disk.set_capacity(node, n.free_disk_gb) disk_capacity.set_capacity(node, n.local_gb) num_cores.set_capacity(node, n.vcpus) node.state = n.state node.status = n.status model.add_node(node) instances = self.wrapper.get_instances_by_node(str(service.host)) for v in instances: # create VM in cluster_model_collector instance = element.Instance() instance.uuid = v.id # nova/nova/compute/instance_states.py instance.state = getattr(v, 'OS-EXT-STS:vm_state') # set capacity self.wrapper.get_flavor_instance(v, flavor_cache) mem.set_capacity(instance, v.flavor['ram']) # FIXME: update all strategies to use disk_capacity # for instances instead of disk disk.set_capacity(instance, v.flavor['disk']) disk_capacity.set_capacity(instance, v.flavor['disk']) num_cores.set_capacity(instance, v.flavor['vcpus']) model.map_instance(instance, node) return model
def test_get_node_by_instance_uuid(self): model = model_root.ModelRoot() uuid_ = "{0}".format(uuidutils.generate_uuid()) node = element.ComputeNode(id=1) node.uuid = uuid_ model.add_node(node) self.assertEqual(node, model.get_node_by_uuid(uuid_)) uuid_ = "{0}".format(uuidutils.generate_uuid()) instance = element.Instance(id=1) instance.uuid = uuid_ model.add_instance(instance) self.assertEqual(instance, model.get_instance_by_uuid(uuid_)) model.map_instance(instance, node) self.assertEqual(node, model.get_node_by_instance_uuid(instance.uuid))
def from_xml(cls, data): model = cls() root = etree.fromstring(data) mem = element.Resource(element.ResourceType.memory) num_cores = element.Resource(element.ResourceType.cpu_cores) disk = element.Resource(element.ResourceType.disk) disk_capacity = element.Resource(element.ResourceType.disk_capacity) model.create_resource(mem) model.create_resource(num_cores) model.create_resource(disk) model.create_resource(disk_capacity) for cn in root.findall('.//ComputeNode'): node = element.ComputeNode(cn.get('id')) node.uuid = cn.get('uuid') node.hostname = cn.get('hostname') # set capacity mem.set_capacity(node, int(cn.get(str(mem.name)))) disk.set_capacity(node, int(cn.get(str(disk.name)))) disk_capacity.set_capacity(node, int(cn.get(str(disk_capacity.name)))) num_cores.set_capacity(node, int(cn.get(str(num_cores.name)))) node.state = cn.get('state') node.status = cn.get('status') model.add_node(node) for inst in root.findall('.//Instance'): instance = element.Instance() instance.uuid = inst.get('uuid') instance.state = inst.get('state') mem.set_capacity(instance, int(inst.get(str(mem.name)))) disk.set_capacity(instance, int(inst.get(str(disk.name)))) disk_capacity.set_capacity(instance, int(inst.get(str(disk_capacity.name)))) num_cores.set_capacity(instance, int(inst.get(str(num_cores.name)))) parent = inst.getparent() if parent.tag == 'ComputeNode': node = model.get_node_by_uuid(parent.get('uuid')) model.map_instance(instance, node) else: model.add_instance(instance) return model
def get_or_create_instance(self, instance_uuid, node_uuid=None): try: if node_uuid: self.get_or_create_node(node_uuid) except exception.ComputeNodeNotFound: LOG.warning("Could not find compute node %(node)s for " "instance %(instance)s", dict(node=node_uuid, instance=instance_uuid)) try: instance = self.cluster_data_model.get_instance_by_uuid( instance_uuid) except exception.InstanceNotFound: # The instance didn't exist yet so we create a new instance object LOG.debug("New instance created: %s", instance_uuid) instance = element.Instance(uuid=instance_uuid) self.cluster_data_model.add_instance(instance) return instance
def from_xml(cls, data): model = cls() root = etree.fromstring(data) for cn in root.findall('.//ComputeNode'): node = element.ComputeNode(**cn.attrib) model.add_node(node) for inst in root.findall('.//Instance'): instance = element.Instance(**inst.attrib) model.add_instance(instance) parent = inst.getparent() if parent.tag == 'ComputeNode': node = model.get_node_by_uuid(parent.get('uuid')) model.map_instance(instance, node) else: model.add_instance(instance) return model
def test_namedelement(self): instance = element.Instance() instance.state = element.InstanceState.ACTIVE self.assertEqual(element.InstanceState.ACTIVE, instance.state) instance.human_id = "human_05" self.assertEqual("human_05", instance.human_id)
def build_scenario_1(self): instances = [] model = modelroot.ModelRoot() # number of nodes node_count = 5 # number max of instance per node node_instance_count = 7 # total number of virtual machine instance_count = (node_count * node_instance_count) for id_ in range(0, node_count): node_uuid = "Node_{0}".format(id_) hostname = "hostname_{0}".format(id_) node_attributes = { "id": id_, "uuid": node_uuid, "hostname": hostname, "memory": 132, "disk": 250, "disk_capacity": 250, "vcpus": 40, } node = element.ComputeNode(**node_attributes) model.add_node(node) for i in range(0, instance_count): instance_uuid = "INSTANCE_{0}".format(i) if instance_uuid == "INSTANCE_1": project_id = "26F03131-32CB-4697-9D61-9123F87A8147" elif instance_uuid == "INSTANCE_2": project_id = "109F7909-0607-4712-B32C-5CC6D49D2F15" else: project_id = "91FFFE30-78A0-4152-ACD2-8310FF274DC9" instance_attributes = { "uuid": instance_uuid, "memory": 2, "disk": 20, "disk_capacity": 20, "vcpus": 10, "metadata": '{"optimize": true,"top": "floor","nested": {"x": "y"}}', "project_id": project_id } instance = element.Instance(**instance_attributes) instances.append(instance) model.add_instance(instance) mappings = [ ("INSTANCE_0", "Node_0"), ("INSTANCE_1", "Node_0"), ("INSTANCE_2", "Node_1"), ("INSTANCE_3", "Node_2"), ("INSTANCE_4", "Node_2"), ("INSTANCE_5", "Node_2"), ("INSTANCE_6", "Node_3"), ("INSTANCE_7", "Node_4"), ] for instance_uuid, node_uuid in mappings: model.map_instance( model.get_instance_by_uuid(instance_uuid), model.get_node_by_uuid(node_uuid), ) return model
def build_scenario_1(self): instances = [] current_state_cluster = modelroot.ModelRoot() # number of nodes node_count = 5 # number max of instance per node node_instance_count = 7 # total number of virtual machine instance_count = (node_count * node_instance_count) # define ressouce ( CPU, MEM disk, ... ) mem = element.Resource(element.ResourceType.memory) # 2199.954 Mhz num_cores = element.Resource(element.ResourceType.cpu_cores) disk = element.Resource(element.ResourceType.disk) disk_capacity = element.Resource(element.ResourceType.disk_capacity) current_state_cluster.create_resource(mem) current_state_cluster.create_resource(num_cores) current_state_cluster.create_resource(disk) current_state_cluster.create_resource(disk_capacity) for id_ in range(0, node_count): node_uuid = "Node_{0}".format(id_) node = element.ComputeNode(id_) node.uuid = node_uuid node.hostname = "hostname_{0}".format(id_) mem.set_capacity(node, 132) disk.set_capacity(node, 250) disk_capacity.set_capacity(node, 250) num_cores.set_capacity(node, 40) current_state_cluster.add_node(node) for i in range(0, instance_count): instance_uuid = "INSTANCE_{0}".format(i) instance = element.Instance() instance.uuid = instance_uuid mem.set_capacity(instance, 2) disk.set_capacity(instance, 20) disk_capacity.set_capacity(instance, 20) num_cores.set_capacity(instance, 10) instances.append(instance) current_state_cluster.add_instance(instance) current_state_cluster.mapping.map( current_state_cluster.get_node_by_uuid("Node_0"), current_state_cluster.get_instance_by_uuid("INSTANCE_0")) current_state_cluster.mapping.map( current_state_cluster.get_node_by_uuid("Node_0"), current_state_cluster.get_instance_by_uuid("INSTANCE_1")) current_state_cluster.mapping.map( current_state_cluster.get_node_by_uuid("Node_1"), current_state_cluster.get_instance_by_uuid("INSTANCE_2")) current_state_cluster.mapping.map( current_state_cluster.get_node_by_uuid("Node_2"), current_state_cluster.get_instance_by_uuid("INSTANCE_3")) current_state_cluster.mapping.map( current_state_cluster.get_node_by_uuid("Node_2"), current_state_cluster.get_instance_by_uuid("INSTANCE_4")) current_state_cluster.mapping.map( current_state_cluster.get_node_by_uuid("Node_2"), current_state_cluster.get_instance_by_uuid("INSTANCE_5")) current_state_cluster.mapping.map( current_state_cluster.get_node_by_uuid("Node_3"), current_state_cluster.get_instance_by_uuid("INSTANCE_6")) current_state_cluster.mapping.map( current_state_cluster.get_node_by_uuid("Node_4"), current_state_cluster.get_instance_by_uuid("INSTANCE_7")) return current_state_cluster