def deletevm(conn, phy, name): ''' : 1. destory vm : 2. undefine vm : 3. delete img :param name: :return: ''' try: ret_message = [] # destory @qemu_isalive(host=phy) def destory(conn, name): return destoryvm(conn, name) err, message = destory(name) assert err is None, '{} shutdown force error!'.format(name) ret_message.append(message) # undefine dom = conn.lookupByName(name) ret = dom.undefine() assert ret == 0, '{} undefine error!'.format(name) ret_message.append({ "error": 0, "message": "{} undefine!".format(name) }) # delete img_path = os.path.join(settings.IMG_PATH, name + '.qcow2') err, host = get_host() assert err is None, "GET HOST ERROR" host_list = '{},'.format(host) task_list = [ dict(action=dict(module='file', args="name={} state=absent".format(img_path))), dict(action=dict( module='file', args="path=/ddhome/kvm/config/{} state=absent".format(name))) ] ans = AnsibleRun(host_list, task_list) ans.task_run() msg = ans.get_result() if msg["failed"]: raise OSError({"error": 1, "message": msg.get('failed')}) elif msg['unreachable']: raise OSError({"error": 1, "message": msg.get('unreachable')}) elif msg['ok']: ret_message.append({"error": 0, "message": msg.get('ok')}) return None, ret_message except Exception as e: return True, {"error": 1, 'message': "{}".format(e)}
def createvm(conn, xmldesc, ifdesc, name): ''' : 1. define : 2. copy ifcfg-eth0 : 3. virt-copy-in :param conn: :param xmldesc: :param rcdesc: :param name: :return: ''' try: ret_message = [] # define dom = conn.defineXML(xmldesc) assert dom.name() == name, '{} define error'.format(name) ret_message.append({"error": 0, "message": "{} define!".format(name)}) # copy ifcfg-eth0 and virt-copy-in with open('templates/{}_ifcfg-eth0'.format(name), 'w') as fp: fp.write(ifdesc) err, host = get_host() assert err is None, "GET HOST ERROR" host_list = '{},'.format(host) task_list = [ dict(action=dict(module="file", args="path=/ddhome/kvm/config/{} state=directory". format(name))), dict( action=dict(module='copy', args='src=templates/{name}_ifcfg-eth0 ' 'dest=/ddhome/kvm/config/{name}/ifcfg-eth0'.format( name=name))), dict(action=dict( module='shell', args= '/usr/bin/virt-copy-in -d {name} /ddhome/kvm/config/{name}/ifcfg-eth0 ' '/etc/sysconfig/network-scripts/'.format(name=name))) ] ans = AnsibleRun(host_list, task_list) ans.task_run() msg = ans.get_result() if msg["failed"]: raise OSError({"error": 1, "message": msg.get('failed')}) elif msg['unreachable']: raise OSError({"error": 1, "message": msg.get('unreachable')}) elif msg['ok']: ret_message.append({"error": 0, "message": msg.get('ok')}) return None, ret_message except Exception as e: return True, {"error": 1, "message": "{}".format(e)}
def qemu_connect(host=None): if host is None: try: err, host = get_host() assert err is None, "GET HOST ERROR" conn = libvirt.open('qemu+tcp://{}:16509/system'.format(host)) return None, conn except libvirt.libvirtError as e: return True, {"error": 1, "message": "{}".format(e)} else: try: conn = libvirt.open('qemu+tcp://{}:16509/system'.format(host)) return None, conn except libvirt.libvirtError as e: return True, {"error": 1, "message": "{}".format(e)}
def get(self, request, phy=None, format=None): ''' : ansible facts :param request: :param format: :return: ''' err, host = get_host() assert err is None, 'GET HOST ERROR' host_list = '{},'.format(phy or host) task_list = [ dict(action=dict(module='script', args='templates/sys_info.py')), ] ans = AnsibleRun(host_list, task_list) ans.task_run() return Response(ans.get_result(), status=status.HTTP_200_OK)
def delete_no_destroyvm(conn, name): ''' : :param conn: :param name: :return: ''' try: ret_message = [] # undefine dom = conn.lookupByName(name) ret = dom.undefine() assert ret == 0, '{} undefine error!'.format(name) ret_message.append({ "error": 0, "message": "{} undefine!".format(name) }) # delete img_path = os.path.join(settings.IMG_PATH, name + '.qcow2') err, host = get_host() assert err is None, "GET HOST ERROR" host_list = '{},'.format(host) task_list = [ dict(action=dict(module='file', args="name={} state=absent".format(img_path))), ] ans = AnsibleRun(host_list, task_list) ans.task_run() msg = ans.get_result() if msg["failed"]: raise OSError({"error": 1, "message": msg.get('failed')}) elif msg['unreachable']: raise OSError({"error": 1, "message": msg.get('unreachable')}) elif msg['ok']: ret_message.append({"error": 0, "message": msg.get('ok')}) return None, ret_message except Exception as e: print(e) return True, {"error": 1, 'message': "{}".format(e)}
def get(self, request, format=None): ''' :param request: :param format: :return: ''' # 获取默认的宿主机 err, host = get_host() assert err is None, 'GET HOST ERROR' @qemu_isalive(host=host) def lst(conn): return domlist(conn) err, vm_list = lst() if err: return Response(vm_list, status=status.HTTP_500_INTERNAL_SERVER_ERROR) serializer = VMListSerializer(data=vm_list, many=True) if serializer.is_valid(): return Response(serializer.validated_data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)