def req_create(self, request): try: document = minidom.parseString(request["data"]) rootNode = document.documentElement if rootNode.nodeName != "vm": raise Exception("invalid root node") except: Logger.warn("Invalid xml input !!") doc = Document() rootNode = doc.createElement('error') rootNode.setAttribute("id", "name") doc.appendChild(rootNode) return self.req_answer(doc) ram = rootNode.getAttribute("ram") vcpu = rootNode.getAttribute("vcpu") master = rootNode.getAttribute("master") if not self.role_instance.pool.masters.has_key(master): return False obj_master = self.role_instance.pool.masters[master] new_id = self.role_instance.pool.get_last_instance(master) id_tmp = b36.b362int(new_id) + 1 name = b36.int2b36(id_tmp, 4) instance = Instance(obj_master, name, self.role_instance.pool, self.role_instance.virt_co) ret = self.role_instance.create_vm(instance, ram, vcpu) doc = Document() rootNode = doc.createElement("vm") rootNode.setAttribute("name", instance.get_name()) rootNode.setAttribute("status", ret) doc.appendChild(rootNode) return self.req_answer(doc)
def req_create(self,request): try: document = minidom.parseString(request["data"]) rootNode = document.documentElement if rootNode.nodeName != "vm": raise Exception("invalid root node") except: Logger.warn("Invalid xml input !!") doc = Document() rootNode = doc.createElement('error') rootNode.setAttribute("id", "name") doc.appendChild(rootNode) return self.req_answer(doc) ram = rootNode.getAttribute("ram") vcpu = rootNode.getAttribute("vcpu") master = rootNode.getAttribute("master") if not self.role_instance.pool.masters.has_key(master): return False obj_master = self.role_instance.pool.masters[master] new_id = self.role_instance.pool.get_last_instance(master) id_tmp = b36.b362int(new_id) + 1 name = b36.int2b36(id_tmp, 4) instance = Instance(obj_master, name, self.role_instance.pool, self.role_instance.virt_co) ret = self.role_instance.create_vm(instance, ram, vcpu) doc = Document() rootNode = doc.createElement("vm") rootNode.setAttribute("name",instance.get_name()) rootNode.setAttribute("status",ret) doc.appendChild(rootNode) return self.req_answer(doc)
def add_instance(self, instance_id): if not self._check_instance_exist(instance_id): return Response(code="failed", message="instance %s doesn't exist" % instance_id, data=None) elif not self._check_instance_boot_from_volume(instance_id): return Response(code="failed", message="instance %s doesn't booted from volume" % instance_id, data=None) elif not self._check_instance_power_on(instance_id): return Response(code="failed", message="instance %s is not power on" % instance_id, data=None) else: try: # Live migration VM to cluster node final_host = self._check_instance_host(instance_id) if final_host == None: message = "Cluster-- instance host ({host_name}) not in the cluster, failed to add protection to instance ({vm_id})".format( host_name=final_host, vm_id=instance_id) logging.error(message) result = Response(code="failed", message=message, data=None) return result # check the parameters of the Instance object initialization instance_name = self.nova_client.get_instance_name(instance_id) instance_status = self.nova_client.get_instance_state( instance_id) provider_network = self.nova_client.get_instance_network( instance_id) parameter_list = [ instance_name, final_host, instance_status, provider_network ] if None in parameter_list: none_index = parameter_list.index(None) parameter_name_list = [ 'instance name', 'located host', 'instance status', 'provider network ip of instance' ] message = "Cluster-- Failed to get the information ({parameter_name)}) for adding instance ({instance_id}) from Openstack, please check the settings.".format( parameter_name=parameter_name_list[none_index], instance_id=instance_id) result = Response(code="failed", message=message, data=None) return result instance = Instance(id=instance_id, name=instance_name, host=final_host, status=instance_status, network=provider_network) self.instance_list.append(instance) # send information of new instance to node detection thread update_action = InstanceQueueConfig.ADD_INSTANCE instance_name = instance.get_name() instance_network_provider = instance.get_network_provider() instance_id = instance.get_id() host = self.get_node_by_name(final_host) host.send_update_instance(update_action, instance_name, instance_network_provider, instance_id) message = "Cluster--Cluster add instance success ! The instance id is %s." % ( instance_id) logging.info(message) # result = {"code":"0","cluster id":self.name,"node":final_host,"instance id":instance_id,"message":message} result = Response(code="succeed", message=message, data={ "cluster_name": self.name, "node": final_host, "instance id": instance_id }) return result except Exception as e: print str(e) message = "Cluster--Cluster add instance fail ,please check again! The instance id is %s." % ( instance_id) logging.error(message) # result = {"code":"1","cluster id":self.name,"instance id":instance_id,"message":message} result = Response(code="failed", message=message, data={ "cluster_name": self.name, "instance id": instance_id }) return result