def flavor_create(instance): assert instance def _generate_name(instance): name = u"%s.cpu-%s-ram-%s-disk-%s" % (settings.OS_NAME_PREFIX, instance.cpu, instance.memory, instance.sys_disk) return name def _get_flavor_by_name(instance, name): rc = create_rc_by_instance(instance) flavor = None novaAdmin = get_nova_admin(instance) try: flavors = novaAdmin.flavors.list(rc) except Exception: flavors = [] raise if flavors is not None: for f in flavors: if f.name == name: flavor = f break return flavor LOG.info(u"Flavor create start, [%s].", instance) begin = datetime.datetime.now() rc = create_rc_by_instance(instance) name = _generate_name(instance) flavor = _get_flavor_by_name(instance, name) if flavor is None: try: LOG.info(u"Flavor not exist, create new, [%s][%s].", instance, name) novaadmin = get_nova_admin(instance) flavor = novaadmin.flavors.create(ram=instance.memory, name=name, vcpus=instance.cpu, disk=instance.sys_disk, is_public=True) except nova.nova_exceptions.Conflict: LOG.info(u"Flavor name conflict, [%s][%s].", instance, name) flavor = _get_flavor_by_name(instance, name) except: raise end = datetime.datetime.now() LOG.info(u"Flavor create end, [%s][%s], apply [%s] seconds.", instance, name, (end - begin).seconds) return flavor
def instance_get_spice_console(instance): assert instance if instance.uuid is None: return None LOG.info("****** i am vdi view with method get ********") spice = {} if instance: LOG.info("******") novaAdmin = get_nova_admin(instance) LOG.info("******") server = novaAdmin.servers.get(instance.uuid) LOG.info("******") server_dict = server.to_dict() LOG.info("******") server_host = server_dict['OS-EXT-SRV-ATTR:host'] server_status = server_dict['status'] LOG.info("******* server_status is *******" + str(server_status)) LOG.info("******* server_host is *******" + str(server_host)) host_ip = settings.COMPUTE_HOSTS[server_host] LOG.info("host ip is" + str(host_ip)) cmd="virsh -c qemu+tcp://" + host_ip + "/system vncdisplay " + instance.uuid LOG.info("cmd=" + cmd) p = subprocess.Popen("virsh -c qemu+tcp://" + host_ip + "/system vncdisplay " + instance.uuid, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) port = None for line in p.stdout.readlines(): port = line break LOG.info("host_ip=" + host_ip) LOG.info("port=" + str(port)) if "error" in str(port): return Response({"success": False, "msg": _('Failed to create flavor for unknown reason.')}) split_port = port.split(":") LOG.info(split_port) port_1 = split_port[1] LOG.info(port_1) port_2 = port_1.split("\\") LOG.info(port_2) port_3 = port_2[0] LOG.info(port_3) vnc_port = 5900 + int(port_3) LOG.info(vnc_port) spice_port = int(vnc_port) + 1 LOG.info(spice_port) spice_url = "spicevm:spice:" + host_ip + ":" + str(spice_port) LOG.info("*** spice_url is ***" + str(spice_url)) spice = {"url": spice_url, "type": "spice"} LOG.info(spice) return spice
def delete_user_instance_network(request, instance_id): # LOG.info("*** instance_id is ***" + str(instance_id)) novaAdmin = get_nova_admin(request) try: server_deleted = novaAdmin.servers.delete(str(instance_id)) LOG.info("*** delete success ***") except Exception: server_deleted = [] LOG.info("*** server delete failed ***") pass return True
def flavor_create(instance): assert instance def _generate_name(instance): name = u"%s.cpu-%s-ram-%s-disk-%s" % (settings.OS_NAME_PREFIX, instance.cpu, instance.memory, instance.sys_disk) return name def _get_flavor_by_name(instance, name): rc = create_rc_by_instance(instance) flavor = None novaAdmin = get_nova_admin(instance) try: flavors = novaAdmin.flavors.list(rc) except Exception: flavors = [] raise if flavors is not None: for f in flavors: if f.name == name: flavor = f break return flavor LOG.info(u"Flavor create start, [%s].", instance) begin = datetime.datetime.now() rc = create_rc_by_instance(instance) name = _generate_name(instance) flavor = _get_flavor_by_name(instance, name) if flavor is None: try: LOG.info(u"Flavor not exist, create new, [%s][%s].", instance, name) novaadmin = get_nova_admin(instance) flavor = novaadmin.flavors.create(ram=instance.memory, name=name, vcpus=instance.cpu, disk=instance.sys_disk, is_public=True) except nova.nova_exceptions.Conflict: LOG.info(u"Flavor name conflict, [%s][%s].", instance, name) flavor = _get_flavor_by_name(instance, name) except: raise end = datetime.datetime.now() LOG.info(u"Flavor create end, [%s][%s], apply [%s] seconds.", instance, name, (end-begin).seconds) return flavor
def _get_flavor_by_name(instance, name): rc = create_rc_by_instance(instance) flavor = None novaAdmin = get_nova_admin(instance) try: flavors = novaAdmin.flavors.list(rc) except Exception: flavors = [] raise if flavors is not None: for f in flavors: if f.name == name: flavor = f break return flavor
def stack_create_sync_status_task(stack_id, user, tenant_uuid, heat, retry_count=1): assert stack_id retry_count += 1 stack_ = None begin = datetime.datetime.now() for count in xrange(settings.MAX_COUNT_SYNC): stack_ = heatclient.stack_get(user, stack_id) status = stack_.status.upper() if stack_ else u"None" LOG.info("*** stack_ is ***" + str(stack_)) stack_name = stack_.stack_name LOG.info("*** status is ***" + str(status)) if status == "COMPLETE": heat.stack_id = stack_id heat.deploy_status = True heat.stack_name = stack_name if stack_: id_ = 0 UDC = UserDataCenter.objects.all().filter(user=user)[0] LOG.info("4") rc = create_rc_by_udc(UDC) stack = stack_ resources = None try: LOG.info("9999") stack_identifier = '%s/%s' % (stack.stack_name, stack.id) resources = heatclient.resources_list( user, stack_identifier) LOG.info("66666") LOG.info('got resources %s' % resources) # The stack id is needed to generate the resource URL. for r in resources: r.stack_id = stack.id except Exception: pass console_dic = {} if not resources: break for resource in resources: LOG.info("resources is oooooooooooooooooooooo" + str(resource)) if resource.resource_type == "OS::Heat::ResourceGroup": LOG.info("resource type is resource group") stack_ = None try: stack_ = heatclientstack_get( user, resource.physical_resource_id) except: pass if not stack_: break resources_ = None try: LOG.info("9999") stack_identifier_ = '%s/%s' % (stack_.stack_name, stack_.id) resources_ = heatclient.resources_list( user, stack_identifier_) LOG.info("66666") LOG.info('got resources %s' % resources_) # The stack id is needed to generate the resource URL. for r in resources_: r.stack_id = stack_.id except Exception: pass if resources_ == None: break for re in resources_: console_url_ = None instance_ = None LOG.info("instance id in group is" + str(re.physical_resource_id)) try: instance_ = nova.server_get( rc, re.physical_resource_id) #console_url_ = project_console.get_console(request, 'VNC', instance_) console_url_ = "http://test" except Exception: pass if instance_: data.append({ "id": instance_.id, "servicename": order_heatname, "instancename": instance_.name, "console": console_url_ }) id_ = id_ + 1 else: LOG.info("resource_tpye is server") console_url = None instance = None try: LOG.info("*** resource.physical_resource_id ***" + str(resource.physical_resource_id)) instance = nova.server_get( rc, resource.physical_resource_id) LOG.info("*** instance is ***" + str(instance)) console_url = "http://test" #project_console.get_console(request, 'VNC', instance) except Exception: pass #console_dic[resource.logical_resource_id] = console_url if instance: LOG.info("*** start to append data") LOG.info("*** instance is ***" + str(instance)) LOG.info("servicename " + str(instance.name)) id_ = id_ + 1 LOG.info("*** id_ is ***" + str(id_)) instance_id = instance.id instance_name = instance.name private_ip = None public_ip = None try: LOG.info("*** stack_name is ****" + str(stack_name)) LOG.info("*** stack_id is ****" + str(stack_id)) LOG.info("*** user is ****" + str(user)) LOG.info("*** instance_id is ***" + str(instance_id)) LOG.info("*** instance_name is ****" + str(instance_name)) addresses = instance.addresses LOG.info("*** addresses are ***" + str(addresses)) for key, value in addresses.items(): LOG.info("key is" + str(key)) LOG.info("value is" + str(value)) for v in value: if v['OS-EXT-IPS:type'] == "fixed": LOG.info("*** get fixed ip ****") private_ip = v['addr'] if v['OS-EXT-IPS:type'] == "floating": LOG.info("*** get public ip ****") public_ip = v['addr'] LOG.info("*****") flavor = instance.flavor LOG.info("*****") flavor_id = flavor['id'] LOG.info("*****") novaAdmin = get_nova_admin(instance) LOG.info("*****") try: flavor = novaAdmin.flavors.get(flavor_id) except Exception: flavor = {} raise real_flavor = flavor.to_dict() LOG.info("** real_flavor is **" + str(real_flavor)) LOG.info("***** flavor is " + str(real_flavor)) #flavor_ = flavor['flavor'] LOG.info("*****") cpu = real_flavor['vcpus'] LOG.info("*****") disk = real_flavor['disk'] LOG.info("*****") memory = real_flavor['ram'] LOG.info("*** cpu is ***" + str(cpu)) LOG.info("*** disk is ***" + str(disk)) LOG.info("*** memory is ***" + str(memory)) LOG.info("*** private_ip is ***" + str(private_ip)) LOG.info("*** public_ip is ***" + str(public_ip)) LOG.info("*** flavor is ***" + str(flavor)) user_data_center = UserDataCenter.objects.get( user=user) user_data_center_id = user_data_center.id LOG.info("*** user_data_center_id ***" + str(user_data_center_id)) LOG.info("*** tenant_uuid ***" + str(tenant_uuid)) instance_save = Instance( name=instance_name, status=1, user=user, uuid=instance_id, cpu=cpu, memory=memory, sys_disk=disk, network_id=1, flavor_id=flavor_id, private_ip=private_ip, public_ip=public_ip, user_data_center_id=user_data_center_id, policy=1, tenant_uuid=tenant_uuid) LOG.info("******************") instance_save.save() except: raise LOG.info("*** instances of this stack are ****" + str(id_)) heat.save() LOG.info("cccccccc") break if status == "FAILED": heat.stack_id = None heat.deploy_status = False heat.stack_status_reason = stack_.stack_status_reason heat.save() LOG.info("*** stack_status_reason is ***" + str(stack_.stack_status_reason)) break if status == "IN_PROGRESS": continue time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND) else: end = datetime.datetime.now() if retry_count <= 3: LOG.warn(u"Instance create timeout") stack_create_sync_status_task.delay(stack_id, user, tenant_uuid, heat) return False else: LOG.error(u"Stack create timeout") return False return True
def flavor_create(instance): assert instance def _generate_name(instance): gpu = 'no' if instance.gpu == True: gpu = 'yes' else: gpu = 'no' name = u"%s.cpu-%s-ram-%s-disk-%s-core-%s-socket-%s-gpu-%s" % (settings.OS_NAME_PREFIX, instance.cpu, instance.memory, instance.sys_disk, instance.core, instance.socket, gpu) return name def _get_flavor_by_name(instance, name): rc = create_rc_by_instance(instance) flavor = None novaAdmin = get_nova_admin(instance) try: flavors = novaAdmin.flavors.list(rc) except Exception: flavors = [] raise if flavors is not None: for f in flavors: if f.name == name: flavor = f break return flavor LOG.info(u"Flavor create start, [%s].", instance) begin = datetime.datetime.now() rc = create_rc_by_instance(instance) LOG.info("*** rc is ***" + str(rc)) name = _generate_name(instance) flavor = _get_flavor_by_name(instance, name) metadata = {"hw:cpu_cores":int(instance.core),"hw:cpu_sockets":int(instance.socket)} if instance.gpu: #metadata['pci_passthrough:alias'] = ['alias_1:1'] metadata['pci_passthrough:alias'] = settings.GPU LOG.info('pppppppppppppppppppppppccccccccccccccccccciiiiii') LOG.info('pppppppppppppppppppppppccccccccccccccccccciiiiii') LOG.info('pppppppppppppppppppppppccccccccccccccccccciiiiii') LOG.info('pppppppppppppppppppppppccccccccccccccccccciiiiii') LOG.info(metadata) if flavor is None: try: LOG.info(u"Flavor not exist, create new, [%s][%s].", instance, name) novaadmin = get_nova_admin(instance) flavor = novaadmin.flavors.create(ram=instance.memory, name=name, vcpus=instance.cpu, disk=instance.sys_disk, is_public=True) flavor.set_keys(metadata) #LOG.info(flavor.get_keys(flavor)) except nova.nova_exceptions.Conflict: LOG.info(u"Flavor name conflict, [%s][%s].", instance, name) flavor = _get_flavor_by_name(instance, name) except: raise end = datetime.datetime.now() LOG.info(u"Flavor create end, [%s][%s], apply [%s] seconds.", instance, name, (end-begin).seconds) return flavor
def stack_create_sync_status_task(stack_id, user, tenant_uuid, heat, retry_count=1): assert stack_id retry_count += 1 stack_ = None begin = datetime.datetime.now() for count in xrange(settings.MAX_COUNT_SYNC): stack_ = heatclient.stack_get(user, stack_id) status = stack_.status.upper() if stack_ else u"None" LOG.info("*** stack_ is ***" + str(stack_)) stack_name = stack_.stack_name LOG.info("*** status is ***" + str(status)) if status == "COMPLETE": heat.stack_id = stack_id heat.deploy_status = True heat.stack_name = stack_name if stack_: id_ = 0 UDC = UserDataCenter.objects.all().filter(user=user)[0] LOG.info("4") rc = create_rc_by_udc(UDC) stack = stack_ resources = None try: LOG.info("9999") stack_identifier = '%s/%s' % (stack.stack_name, stack.id) resources = heatclient.resources_list(user, stack_identifier) LOG.info("66666") LOG.info('got resources %s' % resources) # The stack id is needed to generate the resource URL. for r in resources: r.stack_id = stack.id except Exception: pass console_dic = {} if not resources: break for resource in resources: LOG.info("resources is oooooooooooooooooooooo" +str(resource)) if resource.resource_type == "OS::Heat::ResourceGroup": LOG.info("resource type is resource group") stack_ = None try: stack_ = heatclientstack_get(user, resource.physical_resource_id) except: pass if not stack_: break resources_ = None try: LOG.info("9999") stack_identifier_ = '%s/%s' % (stack_.stack_name, stack_.id) resources_ = heatclient.resources_list(user, stack_identifier_) LOG.info("66666") LOG.info('got resources %s' % resources_) # The stack id is needed to generate the resource URL. for r in resources_: r.stack_id = stack_.id except Exception: pass if resources_ == None: break for re in resources_: console_url_ = None instance_ = None LOG.info("instance id in group is" + str(re.physical_resource_id)) try: instance_ = nova.server_get(rc, re.physical_resource_id) #console_url_ = project_console.get_console(request, 'VNC', instance_) console_url_ = "http://test" except Exception: pass if instance_: data.append({"id": instance_.id, "servicename": order_heatname, "instancename": instance_.name, "console": console_url_}) id_ = id_ + 1 else: LOG.info("resource_tpye is server") console_url = None instance = None try: LOG.info("*** resource.physical_resource_id ***" + str(resource.physical_resource_id)) instance = nova.server_get(rc, resource.physical_resource_id) LOG.info("*** instance is ***" + str(instance)) console_url = "http://test"#project_console.get_console(request, 'VNC', instance) except Exception: pass #console_dic[resource.logical_resource_id] = console_url if instance: LOG.info("*** start to append data") LOG.info("*** instance is ***" + str(instance)) LOG.info("servicename " + str(instance.name)) id_ = id_ + 1 LOG.info("*** id_ is ***" + str(id_)) instance_id = instance.id instance_name = instance.name private_ip = None public_ip = None try: LOG.info("*** stack_name is ****" + str(stack_name)) LOG.info("*** stack_id is ****" + str(stack_id)) LOG.info("*** user is ****" + str(user)) LOG.info("*** instance_id is ***" + str(instance_id)) LOG.info("*** instance_name is ****" + str(instance_name)) addresses = instance.addresses LOG.info("*** addresses are ***" + str(addresses)) for key, value in addresses.items(): LOG.info("key is" + str(key)) LOG.info("value is" + str(value)) for v in value: if v['OS-EXT-IPS:type'] == "fixed": LOG.info("*** get fixed ip ****") private_ip = v['addr'] if v['OS-EXT-IPS:type'] == "floating": LOG.info("*** get public ip ****") public_ip = v['addr'] LOG.info("*****") flavor = instance.flavor LOG.info("*****") flavor_id = flavor['id'] LOG.info("*****") novaAdmin = get_nova_admin(instance) LOG.info("*****") try: flavor = novaAdmin.flavors.get(flavor_id) except Exception: flavor = {} raise real_flavor = flavor.to_dict() LOG.info("** real_flavor is **" + str(real_flavor)) LOG.info("***** flavor is " + str(real_flavor)) #flavor_ = flavor['flavor'] LOG.info("*****") cpu = real_flavor['vcpus'] LOG.info("*****") disk = real_flavor['disk'] LOG.info("*****") memory = real_flavor['ram'] LOG.info("*** cpu is ***" +str(cpu)) LOG.info("*** disk is ***" +str(disk)) LOG.info("*** memory is ***" +str(memory)) LOG.info("*** private_ip is ***" + str(private_ip)) LOG.info("*** public_ip is ***" + str(public_ip)) LOG.info("*** flavor is ***" + str(flavor)) user_data_center = UserDataCenter.objects.get(user=user) user_data_center_id = user_data_center.id LOG.info("*** user_data_center_id ***" + str(user_data_center_id)) LOG.info("*** tenant_uuid ***" + str(tenant_uuid)) instance_save = Instance(name=instance_name, status=1, user=user, uuid=instance_id, cpu=cpu, memory=memory, sys_disk=disk, network_id=1, flavor_id=flavor_id, private_ip=private_ip, public_ip=public_ip, user_data_center_id=user_data_center_id, policy=1, tenant_uuid=tenant_uuid) LOG.info("******************") instance_save.save() except: raise LOG.info("*** instances of this stack are ****" + str(id_)) heat.save() LOG.info("cccccccc") break if status == "FAILED": heat.stack_id = None heat.deploy_status = False heat.stack_status_reason = stack_.stack_status_reason heat.save() LOG.info("*** stack_status_reason is ***" + str(stack_.stack_status_reason)) break if status == "IN_PROGRESS": continue time.sleep(settings.INSTANCE_SYNC_INTERVAL_SECOND) else: end = datetime.datetime.now() if retry_count <= 3: LOG.warn(u"Instance create timeout") stack_create_sync_status_task.delay(stack_id, user, tenant_uuid, heat) return False else: LOG.error(u"Stack create timeout") return False return True