def test_create_volume_multiple(self): ''' 创建多块磁盘 :return: ''' self.test_login() volume = Volume() volume.create_multiple("test_disk", 3, 10, username=USER_LOGIN)
def test_create_volume(self): ''' 创建volume :return: ''' self.test_login() volume = Volume() volume.create(10, name="test_a")
def test_list_volume(self): ''' :return: ''' self.test_login() volume = Volume() msg = volume.list(username=USER_LOGIN) prints(msg)
def test_list_volume(self): ''' :return: ''' self.test_login() volume = Volume() msg = volume.list() prints(msg)
def test_create_volume_multiple(self): ''' 创建多块磁盘 :return: ''' self.test_login() volume = Volume() volume.create_multiple("test_disk",3,10,username=USER_LOGIN)
def test_show_volume(self): ''' :return: ''' self.test_login() volume_id = "" volume = Volume() msg = volume.show_detail(volume_id) prints(msg)
def test_show_volume(self): ''' :return: ''' self.test_login() volume_id = VOLUME_ID volume = Volume() msg = volume.show_detail(volume_id, username=USER_LOGIN) prints(msg)
def test_delete_volume(self): ''' :return: ''' self.test_login() volume_id = VOLUME_ID volume = Volume() msg = volume.delete(volume_id, username=USER_LOGIN) prints(msg)
def test_delete_volume(self): ''' :return: ''' self.test_login() volume_id = VOLUME_ID volume = Volume() msg = volume.delete(volume_id,username=USER_LOGIN) prints(msg)
def test_show_volume(self): ''' :return: ''' self.test_login() volume_id = VOLUME_ID volume = Volume() msg = volume.show_detail(volume_id,username=USER_LOGIN) prints(msg)
def test_extend_volume(self): ''' :return: ''' self.test_login() volume_id = VOLUME_ID size = 11 volume = Volume() msg = volume.extend(volume_id,size,username=USER_LOGIN) prints(msg)
def test_create_volume(self): ''' 创建volume :return: ''' self.test_login() volume = Volume() ret = volume.create(10, name="test_a", username=USER_LOGIN) prints(ret) return ret
def test_extend_volume(self): ''' :return: ''' self.test_login() volume_id = "" size = "" volume = Volume() msg = volume.extend(volume_id, size) prints(msg)
def test_create_volume(self): ''' 创建volume :return: ''' self.test_login() volume = Volume() ret = volume.create(10, name="test_a",username=USER_LOGIN) prints(ret) return ret
def test_extend_volume(self): ''' :return: ''' self.test_login() volume_id = VOLUME_ID size = 11 volume = Volume() msg = volume.extend(volume_id, size, username=USER_LOGIN) prints(msg)
def volumes_deal(host,disk_list,volumes_id,username): try: volume_s = Volume() volumes_list = disk_list['os-extended-volumes:volumes_attached'] volumes_name_list = [] for volmes_dict in volumes_list: volumes_name = {} volumes_details = volume_s.show_detail(volmes_dict['id'],username=username) if volumes_id != volumes_details['volume']['id']: if not volumes_details['volume']['displayName']: volumes_name['disk_name'] = volumes_details['volume']['id'] else: volumes_name['disk_name'] = volumes_details['volume']['displayName'] volumes_name['disk_id'] = volumes_details['volume']['id'] volumes_name['size'] = volumes_details['volume']['size'] volumes_name['voumetype'] = 'ceph' volumes_name['device'] = volumes_details['volume']['attachments'][0]['device'] volumes_name_list.append(volumes_name) openstack_log.info("虚拟机磁盘:{0}..{1}".format(host, volumes_name_list)) except Exception, e: openstack_log.error("虚拟机磁盘:{0}".format(e))
def volumes_deal(host, disk_list, volumes_id, username): try: volume_s = Volume() volumes_list = disk_list['os-extended-volumes:volumes_attached'] volumes_name_list = [] for volmes_dict in volumes_list: volumes_name = {} volumes_details = volume_s.show_detail(volmes_dict['id'], username=username) if volumes_id != volumes_details['volume']['id']: if not volumes_details['volume']['displayName']: volumes_name['disk_name'] = volumes_details['volume']['id'] else: volumes_name['disk_name'] = volumes_details['volume'][ 'displayName'] volumes_name['disk_id'] = volumes_details['volume']['id'] volumes_name['size'] = volumes_details['volume']['size'] volumes_name['voumetype'] = 'ceph' volumes_name['device'] = volumes_details['volume'][ 'attachments'][0]['device'] volumes_name_list.append(volumes_name) openstack_log.info("虚拟机磁盘:{0}..{1}".format(host, volumes_name_list)) except Exception, e: openstack_log.error("虚拟机磁盘:{0}".format(e))
def create(self, name, flavor, image, password, userdata, key_name="",disk=None,username=""): ''' 创建虚拟机,创建的接口在后台应该是异步执行的,当创建的请求发送过去后很快会有结果返回,但是虚拟机实际可能还没有创建成功 所以需要先判断虚拟机的创建状态,如果是完成的再绑定磁盘 :param name: :param flavor: :param image: :param password: :param userdata: :param disk:如果创建时需要选择磁盘则传,格式为: [ { "name":"",可选 "size":"", "availability_zone":"",#可选 "des":"",#可选 "metadata":{},#可选 "volume_type":""#可选, "snapshot_id":""#可选, "dev_name":"连接虚拟机后的盘符名"#可选 } ] :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] cache(del_cache=get_origin_addr(self.list)) if disk is None: disk = [] self.result.update({name: {"name": name, "id": "", "status_vm": 0, "status_disk": {}}}) # 虚拟机创建状态,0表示创建中,1表示成功,2表示失败 assert token != "", "not login" path = url_vm_create.format(project_id=project_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} avzone = self.get_avzone() params = {"server": {"name": name, "flavorRef": flavor, "imageRef": image, "adminPass": password, "availability_zone":avzone}} if userdata: params["server"].update({"user_data":userdata}) if key_name: params["server"].update({"key_name": key_name}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) vm_id = ret["server"]["id"] vm_snap = Vm_snap() vm_snap.set_vm(vm_id) vm_snap.create_root_snap(image) self.result[name]["id"] = vm_id if disk: volume = Volume() volume_attach = Volume_attach() vm_compele_flag = 0 # 判断虚拟机是否创建完成的标志,如果置1则下面不再判断创建的状态 for tmp_dict in disk: time_int = int(time.time()) name_disk = tmp_dict.get("name", "%s_%s" % (name, time_int)) self.result[name]["status_disk"].update({name_disk: 0}) size = tmp_dict["size"] availability_zone = tmp_dict.get("availability_zone", "") des = tmp_dict.get("des", "") metadata = tmp_dict.get("metadata", "") volume_type = tmp_dict.get("volume_type", "") snapshot_id = tmp_dict.get("snapshot_id", "") tmpret = volume.create(size, availability_zone, name_disk, des, metadata, volume_type, snapshot_id,username=username) dev_name = tmp_dict.get("dev_name", "") volume_id = tmpret["volume"]["id"] if not vm_compele_flag: t1 = run_in_thread(self.wait_complete, vm_id,username,timeout=TIMEOUT) if t1 == 0: vm_compele_flag = 1 t2 = run_in_thread(volume.wait_complete, volume_id, ["available"],username, timeout=TIMEOUT) # assert vm_compele_flag == 1, "vm status is not activate" if not vm_compele_flag: self.result[name]["status_vm"] = 2 ret = 1 break # assert t2 == 0, "volume status is not available" if t2 != 0: self.result[name]["status_disk"][name_disk] = 2 continue self.result[name]["status_disk"][name_disk] = 1 volume_attach.attach(vm_id, volume_id, dev_name,username) self.result[name]["status_vm"] = 1 else: t = run_in_thread(self.wait_complete, vm_id,username,timeout=TIMEOUT) self.result[name]["status_vm"] = 1 if t == 0 else 2 return ret
from common import json_data, volumes_deal, time_handle, size_handle, ReturnImages, ReturnFlavor, ReturnVm from openstack.middleware.vm.vm import Vm_manage, Vm_control from openstack.middleware.volume.volume import Volume, Volume_attach import time from openstack.service import user_login, user_login_tmp from django.views.decorators.csrf import csrf_exempt import base64 from decimal import * import json import logging openstack_log = logging.getLogger("openstack_log") imagess = Image() flavorss = Flavor() vm_manage = Vm_manage() volume_s = Volume() host_vm_start = Vm_control() volume_attach = Volume_attach() @csrf_exempt @user_login_tmp() def instances(request): global imagess global flavorss global vm_manage global volume_s global host_vm_start ret = {} disk = [] sys = {}
def create(self, name, flavor, image, password, userdata, key_name="", disk=None, username=""): ''' 创建虚拟机,创建的接口在后台应该是异步执行的,当创建的请求发送过去后很快会有结果返回,但是虚拟机实际可能还没有创建成功 所以需要先判断虚拟机的创建状态,如果是完成的再绑定磁盘 :param name: :param flavor: :param image: :param password: :param userdata: :param disk:如果创建时需要选择磁盘则传,格式为: [ { "name":"",可选 "size":"", "availability_zone":"",#可选 "des":"",#可选 "metadata":{},#可选 "volume_type":""#可选, "snapshot_id":""#可选, "dev_name":"连接虚拟机后的盘符名"#可选 } ] :return: ''' ret = 0 token = self.token_dict[username] project_id = self.project_id_dict[username] cache(del_cache=get_origin_addr(self.list)) if disk is None: disk = [] self.result.update({ name: { "name": name, "id": "", "status_vm": 0, "status_disk": {} } }) # 虚拟机创建状态,0表示创建中,1表示成功,2表示失败 assert token != "", "not login" path = url_vm_create.format(project_id=project_id) method = "POST" head = {"Content-Type": "application/json", "X-Auth-Token": token} avzone = self.get_avzone() params = { "server": { "name": name, "flavorRef": flavor, "imageRef": image, "adminPass": password, "availability_zone": avzone } } if userdata: params["server"].update({"user_data": userdata}) if key_name: params["server"].update({"key_name": key_name}) ret = send_request(method, IP_nova, PORT_nova, path, params, head) vm_id = ret["server"]["id"] vm_snap = Vm_snap() vm_snap.set_vm(vm_id) vm_snap.create_root_snap(image) self.result[name]["id"] = vm_id if disk: volume = Volume() volume_attach = Volume_attach() vm_compele_flag = 0 # 判断虚拟机是否创建完成的标志,如果置1则下面不再判断创建的状态 for tmp_dict in disk: time_int = int(time.time()) name_disk = tmp_dict.get("name", "%s_%s" % (name, time_int)) self.result[name]["status_disk"].update({name_disk: 0}) size = tmp_dict["size"] availability_zone = tmp_dict.get("availability_zone", "") des = tmp_dict.get("des", "") metadata = tmp_dict.get("metadata", "") volume_type = tmp_dict.get("volume_type", "") snapshot_id = tmp_dict.get("snapshot_id", "") tmpret = volume.create(size, availability_zone, name_disk, des, metadata, volume_type, snapshot_id, username=username) dev_name = tmp_dict.get("dev_name", "") volume_id = tmpret["volume"]["id"] if not vm_compele_flag: t1 = run_in_thread(self.wait_complete, vm_id, username, timeout=TIMEOUT) if t1 == 0: vm_compele_flag = 1 t2 = run_in_thread( volume.wait_complete, volume_id, ["available"], username, timeout=TIMEOUT ) # assert vm_compele_flag == 1, "vm status is not activate" if not vm_compele_flag: self.result[name]["status_vm"] = 2 ret = 1 break # assert t2 == 0, "volume status is not available" if t2 != 0: self.result[name]["status_disk"][name_disk] = 2 continue self.result[name]["status_disk"][name_disk] = 1 volume_attach.attach(vm_id, volume_id, dev_name, username) self.result[name]["status_vm"] = 1 else: t = run_in_thread(self.wait_complete, vm_id, username, timeout=TIMEOUT) self.result[name]["status_vm"] = 1 if t == 0 else 2 return ret
from openstack.middleware.flavor.flavor import Flavor from common import json_data,volumes_deal,time_handle,size_handle,sendhttp,sendhttpdata,sendhttpdate,ReturnVolume from openstack.middleware.vm.vm import Vm_manage, Vm_control, Vm_snap from openstack.middleware.volume.volume import Volume, Volume_attach, Volume_snaps,Volume_backup from openstack.middleware.user.user import User from django.http import HttpResponse from Aries.settings import MONITOR_URL from user_auth.models import Account import json import logging import traceback import time import md5 openstack_log = logging.getLogger("openstack_log") volume = Volume() volume_snaps = Volume_snaps() volume_backup=Volume_backup() vm_manage = Vm_manage() volume_attach = Volume_attach() image = Image() last_login_time = 0 space_change = False #是否切换了space,如果切换了需要重新登入openstack获取token def set_space_change(): global space_change space_change = True def json_data(json_status): if len(json_status) == 0: