def tcp_setup(params): """ configure libvirt and connect to it through TCP socket""" logger = params["logger"] target_machine = params["target_machine"] username = params["username"] password = params["password"] listen_tcp = params["listen_tcp"] auth_tcp = params["auth_tcp"] uri = "qemu+tcp://%s/system" % target_machine logger.info("the hostname of server is %s" % target_machine) logger.info("the value of listen_tcp is %s" % listen_tcp) logger.info("the value of auth_tcp is %s" % auth_tcp) if not utils.do_ping(target_machine, 0): logger.error("failed to ping host %s" % target_machine) return 1 if auth_tcp == "sasl": if sasl_user_add(target_machine, username, password, logger): return 1 if tcp_libvirtd_set(target_machine, username, password, listen_tcp, auth_tcp, logger): return 1 if listen_tcp == "disable": if hypervisor_connecting_test(uri, auth_tcp, username, password, logger, "fail"): return 1 elif listen_tcp == "enable": if hypervisor_connecting_test(uri, auth_tcp, username, password, logger, "success"): return 1 return 0
def shutdown(params): """Shutdown domain Argument is a dictionary with two keys: {'logger': logger, 'guestname': guestname} logger -- an object of utils/log.py guestname -- same as the domain name Return 0 on SUCCESS or 1 on FAILURE """ domname = params['guestname'] logger = params['logger'] conn = sharedmod.libvirtobj['conn'] domobj = conn.lookupByName(domname) timeout = 600 logger.info('shutdown domain') mac = utils.get_dom_mac_addr(domname) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180) logger.info("the ip address of guest is %s" % ip) # Shutdown domain try: domobj.shutdown() except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("shutdown failed") return 1 # Check domain status by ping ip while timeout: time.sleep(10) timeout -= 10 logger.info(str(timeout) + "s left") state = domobj.info()[0] if state == libvirt.VIR_DOMAIN_SHUTOFF: break if timeout <= 0: logger.error('The domain state is not equal to "shutoff"') return 1 logger.info('ping guest') if utils.do_ping(ip, 300): logger.error('The guest is still active, IP: ' + str(ip)) return 1 else: logger.info("domain %s shutdown successfully" % domname) return 0
def ifstats(params): """Domain interface statistic""" logger = params['logger'] guestname = params['guestname'] conn = sharedmod.libvirtobj['conn'] domobj = conn.lookupByName(guestname) if check_guest_status(domobj): pass else: try: logger.info("%s is not running , power on it" % guestname) domobj.create() except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("start failed") return 1 mac = utils.get_dom_mac_addr(guestname) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180) logger.info('ping guest') if not utils.do_ping(ip, 300): logger.error('Failed on ping guest, IP: ' + str(ip)) return 1 xml = domobj.XMLDesc(0) doc = libxml2.parseDoc(xml) ctx = doc.xpathNewContext() devs = ctx.xpathEval("/domain/devices/interface/target/@dev") path = devs[0].content ifstats = domobj.interfaceStats(path) if ifstats: # check_interface_stats() logger.debug(ifstats) logger.info("%s rx_bytes %s" % (path, ifstats[0])) logger.info("%s rx_packets %s" % (path, ifstats[1])) logger.info("%s rx_errs %s" % (path, ifstats[2])) logger.info("%s rx_drop %s" % (path, ifstats[3])) logger.info("%s tx_bytes %s" % (path, ifstats[4])) logger.info("%s tx_packets %s" % (path, ifstats[5])) logger.info("%s tx_errs %s" % (path, ifstats[6])) logger.info("%s tx_drop %s" % (path, ifstats[7])) else: logger.error("fail to get domain interface statistics\n") return 1 return 0
def suspend(params): """Suspend domain Argument is a dictionary with two keys: {'logger': logger, 'guestname': guestname} logger -- an object of utils/log.py guestname -- same as the domain name Return 0 on SUCCESS or 1 on FAILURE """ domname = params['guestname'] logger = params['logger'] conn = sharedmod.libvirtobj['conn'] domobj = conn.lookupByName(domname) # Suspend domain logger.info('suspend domain') try: domobj.suspend() except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) return 1 time.sleep(1) state = domobj.info()[0] if state != libvirt.VIR_DOMAIN_PAUSED: logger.error('The domain state is not equal to "paused"') return 1 mac = utils.get_dom_mac_addr(domname) time.sleep(3) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 10) time.sleep(10) logger.info('ping guest') if utils.do_ping(ip, 20): logger.error('The guest is still active, IP: ' + str(ip)) return 1 logger.info('PASS') return 0
def get_guest_ipaddr(*args): """Get guest ip address""" (guestname, logger) = args mac = utils.get_dom_mac_addr(guestname) logger.debug("guest mac address: %s" % mac) ipaddr = utils.mac_to_ip(mac, 15) logger.debug("guest ip address: %s" % ipaddr) if utils.do_ping(ipaddr, 20) == 1: logger.info("ping current guest successfull") return ipaddr else: logger.error("Error: can't ping current guest") return None
def resume(params): """Resume domain Argument is a dictionary with two keys: {'logger': logger, 'guestname': guestname} logger -- an object of utils/log.py guestname -- same as the domain name Return 0 on SUCCESS or 1 on FAILURE """ domname = params['guestname'] logger = params['logger'] # Resume domain conn = sharedmod.libvirtobj['conn'] domobj = conn.lookupByName(domname) logger.info('resume domain') try: domobj.resume() except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("resume failed") return 1 state = domobj.info()[0] expect_states = [ libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_NOSTATE, libvirt.VIR_DOMAIN_BLOCKED ] if state not in expect_states: logger.error('The domain state is not equal to "paused"') return 1 mac = utils.get_dom_mac_addr(domname) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 120) logger.info('ping guest') if not utils.do_ping(ip, 300): logger.error('Failed on ping guest, IP: ' + str(ip)) return 1 logger.info("PASS") return 0
def resume(params): """Resume domain Argument is a dictionary with two keys: {'logger': logger, 'guestname': guestname} logger -- an object of utils/log.py guestname -- same as the domain name Return 0 on SUCCESS or 1 on FAILURE """ domname = params['guestname'] logger = params['logger'] # Resume domain conn = sharedmod.libvirtobj['conn'] domobj = conn.lookupByName(domname) logger.info('resume domain') try: domobj.resume() except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("resume failed") return 1 state = domobj.info()[0] expect_states = [ libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_NOSTATE, libvirt.VIR_DOMAIN_BLOCKED] if state not in expect_states: logger.error('The domain state is not equal to "paused"') return 1 mac = utils.get_dom_mac_addr(domname) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 120) logger.info('ping guest') if not utils.do_ping(ip, 300): logger.error('Failed on ping guest, IP: ' + str(ip)) return 1 logger.info("PASS") return 0
def tcp_setup(params): """ configure libvirt and connect to it through TCP socket""" logger = params['logger'] target_machine = params['target_machine'] username = params['username'] password = params['password'] listen_tcp = params['listen_tcp'] auth_tcp = params['auth_tcp'] uri = "qemu+tcp://%s/system" % target_machine logger.info("the hostname of server is %s" % target_machine) logger.info("the value of listen_tcp is %s" % listen_tcp) logger.info("the value of auth_tcp is %s" % auth_tcp) if not utils.do_ping(target_machine, 0): logger.error("failed to ping host %s" % target_machine) return 1 if auth_tcp == 'sasl': if sasl_user_add(target_machine, username, password, logger): return 1 if tcp_libvirtd_set(target_machine, username, password, listen_tcp, auth_tcp, logger): return 1 if listen_tcp == 'disable': if hypervisor_connecting_test(uri, auth_tcp, username, password, logger, 'fail'): return 1 elif listen_tcp == 'enable': if hypervisor_connecting_test(uri, auth_tcp, username, password, logger, 'success'): return 1 return 0
def restart(params): """restart libvirtd test""" logger = params['logger'] guestname = params['guestname'] conn = sharedmod.libvirtobj['conn'] logger.info("check the domain state") ret = check_domain_running(conn, guestname, logger) if ret: return 1 logger.info("check the libvirtd status:") ret = libvirtd_check(logger) if ret: return 1 # Get domain ip logger.info("get the mac address of domain %s" % guestname) mac = utils.get_dom_mac_addr(guestname) logger.info("the mac address of domain %s is %s" % (guestname, mac)) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180) logger.info("the ip address of domain %s is %s" % (guestname, ip)) logger.info("ping to domain %s" % guestname) if utils.do_ping(ip, 0): logger.info("Success ping domain %s" % guestname) else: logger.error("fail to ping domain %s" % guestname) return 1 ret, pid_before = get_domain_pid(logger, guestname) if ret: return 1 logger.info("restart libvirtd service:") ret, out = utils.exec_cmd(RESTART_CMD, shell=True) if ret != 0: logger.error("failed to restart libvirtd") for i in range(len(out)): logger.error(out[i]) return 1 else: for i in range(len(out)): logger.info(out[i]) logger.info("recheck libvirtd status:") ret = libvirtd_check(logger) if ret: return 1 logger.info("ping to domain %s again" % guestname) if utils.do_ping(ip, 0): logger.info("Success ping domain %s" % guestname) else: logger.error("fail to ping domain %s" % guestname) return 1 ret, pid_after = get_domain_pid(logger, guestname) if ret: return 1 if pid_before != pid_after: logger.error("%s pid changed during libvirtd restart" % \ guestname) return 1 else: logger.info("domain pid not change, %s keeps running during \ libvirtd restart" % guestname) return 0
def restart(params): """restart libvirtd test""" logger = params['logger'] guestname = params['guestname'] conn = sharedmod.libvirtobj['conn'] logger.info("check the domain state") ret = check_domain_running(conn, guestname, logger) if ret: return 1 logger.info("check the libvirtd status:") ret = libvirtd_check(logger) if ret: return 1 # Get domain ip logger.info("get the mac address of domain %s" % guestname) mac = utils.get_dom_mac_addr(guestname) logger.info("the mac address of domain %s is %s" % (guestname, mac)) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180) logger.info("the ip address of domain %s is %s" % (guestname, ip)) logger.info("ping to domain %s" % guestname) if utils.do_ping(ip, 0): logger.info("Success ping domain %s" % guestname) else: logger.error("fail to ping domain %s" % guestname) return 1 ret, pid_before = get_domain_pid(logger, guestname) if ret: return 1 logger.info("restart libvirtd service:") ret, out = utils.exec_cmd(RESTART_CMD, shell=True) if ret != 0: logger.error("failed to restart libvirtd") for i in range(len(out)): logger.error(out[i]) return 1 else: for i in range(len(out)): logger.info(out[i]) logger.info("recheck libvirtd status:") ret = libvirtd_check(logger) if ret: return 1 logger.info("ping to domain %s again" % guestname) if utils.do_ping(ip, 0): logger.info("Success ping domain %s" % guestname) else: logger.error("fail to ping domain %s" % guestname) return 1 ret, pid_after = get_domain_pid(logger, guestname) if ret: return 1 if pid_before != pid_after: logger.error("%s pid changed during libvirtd restart" % guestname) return 1 else: logger.info("domain pid not change, %s keeps running during \ libvirtd restart" % guestname) return 0
def managedsave_start(params): """ Start domain with managedsave image and check if its status is right according to given flags of running managedsave command.If it is correctly paused , resume it. Argument is a dictionary with two keys: {'logger': logger, 'guestname': guestname} logger -- an object of utils/log.py mandatory arguments : guestname -- same as the domain name optional arguments : flags -- domain create flags <none|start_paused |noping>.It allows only one flag be given. Return 0 on SUCCESS or 1 on FAILURE """ domname = params['guestname'] global logger logger = params['logger'] flags = params.get('flags', '') # Get given flags of managedsave if 'flagsave' in sharedmod.data: flagsave = sharedmod.data.get('flagsave') else: logger.error("Failed to get flags from managedsave") # Clean sharedmod.data sharedmod.data = {} conn = sharedmod.libvirtobj['conn'] domobj = conn.lookupByName(domname) timeout = 600 logger.info('start domain') # Check if guest has managedsave image before start if domobj.hasManagedSaveImage(0): logger.info("Domain has managedsave image") else: logger.info("Domain hasn't managedsave image") try: if "none" in flags: domobj.createWithFlags(NONE) elif "start_paused" in flags: domobj.createWithFlags(START_PAUSED) else: # this covers flags = None as well as flags = 'noping' domobj.create() except libvirtError as e: logger.error("API error message: %s, error code is %s" % e.message) logger.error("start failed") return 1 while timeout: state = domobj.info()[0] expect_states = [ libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_PAUSED, libvirt.VIR_DOMAIN_NOSTATE, libvirt.VIR_DOMAIN_BLOCKED] if state in expect_states: break time.sleep(10) timeout -= 10 logger.info(str(timeout) + "s left") if timeout <= 0: logger.error('The domain state is not as expected, state: ' + state) return 1 logger.info("Guest started") """If domain's current state is paused. Check if start command has --paused flag or managedsave has --paused flag (given flags in managedsave include '4'). If yes, it means domain successfully paused , then resume it. If not, throw error -guest state error.""" if state == libvirt.VIR_DOMAIN_PAUSED: if "start_paused" in flags or "4" in flagsave: logger.info("Guest paused successfully ") try: domobj.resume() except libvirtError as e: logger.error("API error message: %s, error code is %s" % e.message) logger.error("resume failed") return 1 stateresume = domobj.info()[0] expect_states = [libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_NOSTATE, libvirt.VIR_DOMAIN_BLOCKED] if stateresume not in expect_states: logger.error('The domain state is not equal to "paused"') return 1 else: logger.info('Domain resume successfully') return 0 else: logger.error("guest state error") return 1 # Get domain ip and ping ip to check domain's status if "noping" not in flags: mac = utils.get_dom_mac_addr(domname) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180) logger.info('ping guest') if not utils.do_ping(ip, 300): logger.error('Failed on ping guest, IP: ' + str(ip)) return 1 # Check if domain' managedsave image exists,if not, return 0. if not domobj.hasManagedSaveImage(0) and check_savefile_remove(domname): logger.info("Domain %s with managedsave image successfully start" % domname) return 0 else: logger.error("Fail to start domain %s with managedsave image" % domname) return 1
def reboot(params): """Reboot virtual machine Return 0 on SUCCESS or 1 on FAILURE """ # Initiate and check parameters global logger logger = params['logger'] params.pop('logger') domain_name = params['guestname'] # Connect to local hypervisor connection URI hypervisor = utils.get_hypervisor() if hypervisor == "kvm": logger.info("kvm hypervisor doesn't support the funtion now") return 0 conn = sharedmod.libvirtobj['conn'] domobj = conn.lookupByName(domain_name) # Get domain ip logger.info("get the mac address of vm %s" % domain_name) mac = utils.get_dom_mac_addr(domain_name) logger.info("the mac address of vm %s is %s" % (domain_name, mac)) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180) logger.info("the ip address of vm %s is %s" % (domain_name, ip)) timeout = 600 logger.info('reboot vm %s now' % domain_name) # Reboot domain try: domobj = reboot(0) except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("fail to reboot domain") return 1 logger.info("the vm %s is power off" % domain_name) # Check domain status by ping ip while timeout: time.sleep(10) timeout -= 10 if utils.do_ping(ip, 0): logger.info(str(timeout) + "s left") else: logger.info("vm %s power off successfully" % domain_name) break if timeout == 0: logger.info("fail to power off %s" % domain_name) return 1 timeout = 600 logger.info("the vm %s is power on" % domain_name) while timeout: time.sleep(10) timeout -= 10 if not utils.do_ping(ip, 0): logger.info(str(timeout) + "s left") else: logger.info("vm %s power on successfully") break if timeout == 0: logger.info("fail to power on vm %s" % domain_name) return 1 return 0
def install_linux_check(params): """check guest status after installation, including network ping, read/write option in guest. return value: 0 - ok; 1 - bad """ global logger logger = params['logger'] params.pop('logger') guestname = params.get('guestname') virt_type = params.get('virt_type') logger.info("the name of guest is %s" % guestname) # Connect to local hypervisor connection URI hypervisor = utils.get_hypervisor() logger.info("the type of hypervisor is %s" % hypervisor) conn = sharedmod.libvirtobj['conn'] domobj = conn.lookupByName(guestname) state = domobj.info()[0] if(state == libvirt.VIR_DOMAIN_SHUTOFF): logger.info("guest is shutoff, if u want to run this case, \ guest must be started") return 1 logger.info("get the mac address of vm %s" % guestname) mac = utils.get_dom_mac_addr(guestname) logger.info("the mac address of vm %s is %s" % (guestname, mac)) timeout = 300 while timeout: ipaddr = utils.mac_to_ip(mac, 180) if not ipaddr: logger.info(str(timeout) + "s left") time.sleep(10) timeout -= 10 else: logger.info("the ip address of vm %s is %s" % (guestname, ipaddr)) break if timeout == 0: logger.info("vm %s fail to get ip address" % guestname) return 1 time.sleep(120) logger.info("Now checking guest health after installation") domain_name=guestname blk_type=params['hddriver'] nic_type=params['nicdriver'] Test_Result = 0 # Ping guest from host logger.info("check point1: ping guest from host") if utils.do_ping(ipaddr, 20) == 1: logger.info("ping current guest successfull") else: logger.error("Error: can't ping current guest") Test_Result = 1 return Test_Result # Creat file and read file in guest. logger.info("check point2: creat and read dirctory/file in guest") if utils.create_dir(ipaddr, "root", "redhat") == 0: logger.info("create dir - /tmp/test successfully") if utils.write_file(ipaddr, "root", "redhat") == 0: logger.info("write and read file: /tmp/test/test.log successfully") else: logger.error("Error: fail to write/read file - /tmp/test/test.log") Test_Result = 1 return Test_Result else: logger.error("Error: fail to create dir - /tmp/test") Test_Result = 1 return Test_Result # Check whether vcpu equals the value set in geust config xml logger.info("check point3: check cpu number in guest equals to \ the value set in domain config xml") vcpunum_expect = int(utils.get_num_vcpus(domain_name)) logger.info("vcpu number in domain config xml - %s is %s" % \ (domain_name, vcpunum_expect)) vcpunum_actual = int(utils.get_remote_vcpus(ipaddr, "root", "redhat")) logger.info("The actual vcpu number in guest - %s is %s" % (domain_name, vcpunum_actual)) if vcpunum_expect == vcpunum_actual: logger.info("The actual vcpu number in guest is \ equal to the setting your domain config xml") else: logger.error("Error: The actual vcpu number in guest is \ NOT equal to the setting your domain config xml") Test_Result = 1 return Test_Result # Check whether mem in guest is equal to the value set in domain config xml logger.info("check point4: check whether mem in guest is equal to \ the value set in domain config xml") mem_expect = utils.get_size_mem(domain_name) logger.info("current mem size in domain config xml - %s is %s" % (domain_name, mem_expect)) mem_actual = utils.get_remote_memory(ipaddr, "root", "redhat") logger.info("The actual mem size in guest - %s is %s" % (domain_name, mem_actual)) diff_range = int(mem_expect) * 0.07 diff = int(mem_expect) - int(mem_actual) if int(math.fabs(diff)) < int(diff_range): logger.info("The actual mem size in guest is almost equal to \ the setting your domain config xml") else: logger.error("Error: The actual mem size in guest is NOT equal to \ the setting your domain config xml") Test_Result = 1 return Test_Result # Check app works fine in guest, such as: wget logger.info("check point5: check app works fine in guest, such as: wget") logger.info("get system environment information") envfile = os.path.join(HOME_PATH, 'global.cfg') logger.info("the environment file is %s" % envfile) envparser = env_parser.Envparser(envfile) file_url = envparser.get_value("other", "wget_url") if utils.run_wget_app(ipaddr, "root", "redhat", file_url, logger) == 0: logger.info("run wget successfully in guest.") else: logger.error("Error: fail to run wget in guest") Test_Result = 1 return Test_Result # Check nic and blk driver in guest if 'kvm' in virt_type or 'xenfv' in virt_type: logger.info("check point6: check nic and blk driver in guest is \ expected as your config:") if utils.validate_remote_nic_type(ipaddr, "root", "redhat", nic_type, logger) == 0 and \ utils.validate_remote_blk_type(ipaddr, "root", "redhat", blk_type, logger) == 0: logger.info("nic type - %s and blk type - %s check successfully" % (nic_type, blk_type)) else: logger.error("Error: nic type - %s or blk type - %s check failed" % (nic_type, blk_type)) Test_Result = 1 return Test_Result return Test_Result
def set_vcpus(domobj, domain_name, vcpu): """set the value of virtual machine to vcpu offline , then boot up the virtual machine """ timeout = 60 logger.info('destroy domain') logger.info("get the mac address of vm %s" % domain_name) mac = utils.get_dom_mac_addr(domain_name) logger.info("the mac address of vm %s is %s" % (domain_name, mac)) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180) logger.info("the ip address of vm %s is %s" % (domain_name, ip)) try: domobj.destroy() except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("fail to destroy domain") return 1 while timeout: time.sleep(10) timeout -= 10 logger.info(str(timeout) + "s left") logger.info('ping guest') if utils.do_ping(ip, 30): logger.error('The guest is still active, IP: ' + str(ip)) else: logger.info("domain %s is destroied successfully" % domain_name) break if timeout <= 0: logger.error("the domain couldn't be destroied within 60 secs.") return 1 newguestxml = redefine_vcpu_number(domobj, domain_name, vcpu) logger.debug('''new guest %s xml :\n%s''' % (domain_name, newguestxml)) logger.info("undefine the original guest") try: domobj.undefine() except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("fail to undefine guest %s" % domain_name) return 1 logger.info("define guest with new xml") try: conn = domobj._conn conn.defineXML(newguestxml) except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("fail to define guest %s" % domain_name) return 1 try: logger.info('boot guest up ...') domobj.create() except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("fail to start domain %s" % domain_name) return 1 timeout = 600 while timeout: time.sleep(10) timeout -= 10 ip = utils.mac_to_ip(mac, 180) if not ip: logger.info(str(timeout) + "s left") else: logger.info("vm %s power on successfully" % domain_name) logger.info("the ip address of vm %s is %s" % (domain_name, ip)) break if timeout <= 0: logger.info("fail to power on vm %s" % domain_name) return 1 return 0
def tls_setup(params): """ generate tls certificates and configure libvirt """ logger = params["logger"] target_machine = params["target_machine"] username = params["username"] password = params["password"] listen_tls = params["listen_tls"] auth_tls = params["auth_tls"] pkipath = "" if params.has_key("pkipath"): pkipath = params["pkipath"] if os.path.exists(pkipath): shutil.rmtree(pkipath) os.mkdir(pkipath) uri = "qemu://%s/system" % target_machine if pkipath: uri += "?pkipath=%s" % pkipath local_machine = utils.get_local_hostname() logger.info("the hostname of server is %s" % target_machine) logger.info("the hostname of local machine is %s" % local_machine) logger.info("the value of listen_tls is %s" % listen_tls) logger.info("the value of auth_tls is %s" % auth_tls) if not utils.do_ping(target_machine, 0): logger.error("failed to ping host %s" % target_machine) return 1 if os.path.exists(TEMP_TLS_FOLDER): shutil.rmtree(TEMP_TLS_FOLDER) os.mkdir(TEMP_TLS_FOLDER) if iptables_stop(target_machine, username, password, logger): return 1 if CA_setting_up(logger): return 1 if tls_server_cert(target_machine, logger): return 1 if tls_client_cert(local_machine, logger): return 1 if deliver_cert(target_machine, username, password, pkipath, logger): return 1 if auth_tls == "sasl": if sasl_user_add(target_machine, username, password, logger): return 1 if tls_libvirtd_set(target_machine, username, password, listen_tls, auth_tls, logger): return 1 if listen_tls == "disable": if hypervisor_connecting_test(uri, auth_tls, username, password, logger, "fail"): return 1 elif listen_tls == "enable": if hypervisor_connecting_test(uri, auth_tls, username, password, logger, "success"): return 1 return 0
# Reboot domain try: domobj = reboot(0) except libvirtError, e: logger.error("API error message: %s, error code is %s" \ % (e.message, e.get_error_code())) logger.error("fail to reboot domain") return 1 logger.info("the vm %s is power off" % domain_name) # Check domain status by ping ip while timeout: time.sleep(10) timeout -= 10 if utils.do_ping(ip, 0): logger.info(str(timeout) + "s left") else: logger.info("vm %s power off successfully" % domain_name) break if timeout == 0: logger.info("fail to power off %s" % domain_name) return 1 timeout = 600 logger.info("the vm %s is power on" % domain_name) while timeout: time.sleep(10) timeout -= 10 if not utils.do_ping(ip, 0):
# Suspend domain logger.info('suspend domain') try: domobj.suspend() except libvirtError, e: logger.error("API error message: %s, error code is %s" \ % (e.message, e.get_error_code())) return 1 time.sleep(1) state = domobj.info()[0] if state != libvirt.VIR_DOMAIN_PAUSED: logger.error('The domain state is not equal to "paused"') return 1 mac = utils.get_dom_mac_addr(domname) time.sleep(3) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 10) time.sleep(10) logger.info('ping guest') if utils.do_ping(ip, 20): logger.error('The guest is still active, IP: ' + str(ip)) return 1 logger.info('PASS') return 0
def start(params): """Start domain Argument is a dictionary with two keys: {'logger': logger, 'guestname': guestname} logger -- an object of utils/log.py mandatory arguments : guestname -- same as the domain name optional arguments : flags -- domain create flags <none|start_paused|noping> Return 0 on SUCCESS or 1 on FAILURE """ domname = params['guestname'] logger = params['logger'] flags = params.get('flags', '') if "none" in flags and "start_paused" in flags: logger.error( "Flags error: Can't specify none and start_paused simultaneously") return 1 conn = sharedmod.libvirtobj['conn'] domobj = conn.lookupByName(domname) timeout = 600 logger.info('start domain') try: if "none" in flags: domobj.createWithFlags(NONE) elif "start_paused" in flags: domobj.createWithFlags(START_PAUSED) else: # this covers flags = None as well as flags = 'noping' domobj.create() except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("start failed") return 1 if "start_paused" in flags: state = domobj.info()[0] if state == libvirt.VIR_DOMAIN_PAUSED: logger.info("guest start with state paused successfully") return 0 else: logger.error("guest state error") return 1 while timeout: state = domobj.info()[0] expect_states = [ libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_NOSTATE, libvirt.VIR_DOMAIN_BLOCKED] if state in expect_states: break time.sleep(10) timeout -= 10 logger.info(str(timeout) + "s left") if timeout <= 0: logger.error('The domain state is not as expected, state: ' + state) return 1 logger.info("Guest started") # Get domain ip and ping ip to check domain's status if "noping" not in flags: mac = utils.get_dom_mac_addr(domname) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180) logger.info('ping guest') if not utils.do_ping(ip, 300): logger.error('Failed on ping guest, IP: ' + str(ip)) return 1 logger.info("PASS") return 0
def managedsave_start(params): """ Start domain with managedsave image and check if its status is right according to given flags of running managedsave command.If it is correctly paused , resume it. Argument is a dictionary with two keys: {'logger': logger, 'guestname': guestname} logger -- an object of utils/log.py mandatory arguments : guestname -- same as the domain name optional arguments : flags -- domain create flags <none|start_paused |noping>.It allows only one flag be given. Return 0 on SUCCESS or 1 on FAILURE """ domname = params['guestname'] global logger logger = params['logger'] flags = params.get('flags', '') # Get given flags of managedsave if 'flagsave' in sharedmod.data: flagsave = sharedmod.data.get('flagsave') else: logger.error("Failed to get flags from managedsave") # Clean sharedmod.data sharedmod.data = {} conn = sharedmod.libvirtobj['conn'] domobj = conn.lookupByName(domname) timeout = 600 logger.info('start domain') # Check if guest has managedsave image before start if domobj.hasManagedSaveImage(0): logger.info("Domain has managedsave image") else: logger.info("Domain hasn't managedsave image") try: if "none" in flags: domobj.createWithFlags(NONE) elif "start_paused" in flags: domobj.createWithFlags(START_PAUSED) else: # this covers flags = None as well as flags = 'noping' domobj.create() except libvirtError as e: logger.error("API error message: %s, error code is %s" % e.message) logger.error("start failed") return 1 while timeout: state = domobj.info()[0] expect_states = [ libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_PAUSED, libvirt.VIR_DOMAIN_NOSTATE, libvirt.VIR_DOMAIN_BLOCKED ] if state in expect_states: break time.sleep(10) timeout -= 10 logger.info(str(timeout) + "s left") if timeout <= 0: logger.error('The domain state is not as expected, state: ' + state) return 1 logger.info("Guest started") """If domain's current state is paused. Check if start command has --paused flag or managedsave has --paused flag (given flags in managedsave include '4'). If yes, it means domain successfully paused , then resume it. If not, throw error -guest state error.""" if state == libvirt.VIR_DOMAIN_PAUSED: if "start_paused" in flags or "4" in flagsave: logger.info("Guest paused successfully ") try: domobj.resume() except libvirtError as e: logger.error("API error message: %s, error code is %s" % e.message) logger.error("resume failed") return 1 stateresume = domobj.info()[0] expect_states = [ libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_NOSTATE, libvirt.VIR_DOMAIN_BLOCKED ] if stateresume not in expect_states: logger.error('The domain state is not equal to "paused"') return 1 else: logger.info('Domain resume successfully') return 0 else: logger.error("guest state error") return 1 # Get domain ip and ping ip to check domain's status if "noping" not in flags: mac = utils.get_dom_mac_addr(domname) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180) logger.info('ping guest') if not utils.do_ping(ip, 300): logger.error('Failed on ping guest, IP: ' + str(ip)) return 1 # Check if domain' managedsave image exists,if not, return 0. if not domobj.hasManagedSaveImage(0) and check_savefile_remove(domname): logger.info("Domain %s with managedsave image successfully start" % domname) return 0 else: logger.error("Fail to start domain %s with managedsave image" % domname) return 1
if stateresume not in expect_states: logger.error('The domain state is not equal to "paused"') return 1 else: logger.info('Domain resume successfully') return 0 else: logger.error("guest state error") return 1 # Get domain ip and ping ip to check domain's status if not "noping" in flags: mac = utils.get_dom_mac_addr(domname) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180) logger.info('ping guest') if not utils.do_ping(ip, 300): logger.error('Failed on ping guest, IP: ' + str(ip)) return 1 # Check if domain' managedsave image exists,if not, return 0. if not domobj.hasManagedSaveImage(0) and check_savefile_remove(domname): logger.info("Domain %s with managedsave image successfully start" \ % domname) return 0 else: logger.error("Fail to start domain s% with managedsave image" \ % domname) return 1
def start(params): """Start domain Argument is a dictionary with two keys: {'logger': logger, 'guestname': guestname} logger -- an object of utils/log.py mandatory arguments : guestname -- same as the domain name optional arguments : flags -- domain create flags <none|start_paused|noping> Return 0 on SUCCESS or 1 on FAILURE """ domname = params['guestname'] logger = params['logger'] flags = params.get('flags', '') if "none" in flags and "start_paused" in flags: logger.error( "Flags error: Can't specify none and start_paused simultaneously") return 1 conn = sharedmod.libvirtobj['conn'] domobj = conn.lookupByName(domname) timeout = 600 logger.info('start domain') try: if "none" in flags: domobj.createWithFlags(NONE) elif "start_paused" in flags: domobj.createWithFlags(START_PAUSED) else: # this covers flags = None as well as flags = 'noping' domobj.create() except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("start failed") return 1 if "start_paused" in flags: state = domobj.info()[0] if state == libvirt.VIR_DOMAIN_PAUSED: logger.info("guest start with state paused successfully") return 0 else: logger.error("guest state error") return 1 while timeout: state = domobj.info()[0] expect_states = [ libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_NOSTATE, libvirt.VIR_DOMAIN_BLOCKED ] if state in expect_states: break time.sleep(10) timeout -= 10 logger.info(str(timeout) + "s left") if timeout <= 0: logger.error('The domain state is not as expected, state: ' + state) return 1 logger.info("Guest started") # Get domain ip and ping ip to check domain's status if "noping" not in flags: mac = utils.get_dom_mac_addr(domname) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180) logger.info('ping guest') if not utils.do_ping(ip, 300): logger.error('Failed on ping guest, IP: ' + str(ip)) return 1 logger.info("PASS") return 0
def tls_setup(params): """ generate tls certificates and configure libvirt """ logger = params['logger'] target_machine = params['target_machine'] username = params['username'] password = params['password'] listen_tls = params['listen_tls'] auth_tls = params['auth_tls'] pkipath = "" if 'pkipath' in params: pkipath = params['pkipath'] if os.path.exists(pkipath): shutil.rmtree(pkipath) os.mkdir(pkipath) uri = "qemu://%s/system" % target_machine if pkipath: uri += "?pkipath=%s" % pkipath local_machine = utils.get_local_hostname() logger.info("the hostname of server is %s" % target_machine) logger.info("the hostname of local machine is %s" % local_machine) logger.info("the value of listen_tls is %s" % listen_tls) logger.info("the value of auth_tls is %s" % auth_tls) if not utils.do_ping(target_machine, 0): logger.error("failed to ping host %s" % target_machine) return 1 if os.path.exists(TEMP_TLS_FOLDER): shutil.rmtree(TEMP_TLS_FOLDER) os.mkdir(TEMP_TLS_FOLDER) if iptables_stop(target_machine, username, password, logger): return 1 if CA_setting_up(logger): return 1 if tls_server_cert(target_machine, logger): return 1 if tls_client_cert(local_machine, logger): return 1 if deliver_cert(target_machine, username, password, pkipath, logger): return 1 if auth_tls == 'sasl': if sasl_user_add(target_machine, username, password, logger): return 1 if tls_libvirtd_set(target_machine, username, password, listen_tls, auth_tls, logger): return 1 if listen_tls == 'disable': if hypervisor_connecting_test(uri, auth_tls, username, password, logger, 'fail'): return 1 elif listen_tls == 'enable': if hypervisor_connecting_test(uri, auth_tls, username, password, logger, 'success'): return 1 return 0
def destroy(params): """destroy domain Argument is a dictionary with two keys: {'guestname': guestname} logger -- an object of utils/log.py guestname -- the domain name flags -- optional arguments: noping: Don't do the ping test Return 0 on SUCCESS or 1 on FAILURE """ # Initiate and check parameters global logger logger = params['logger'] params.pop('logger') guestname = params['guestname'] br = params.get('bridgename', 'virbr0') flags = "" if 'flags' in params: flags = params['flags'] conn = sharedmod.libvirtobj['conn'] # Get running domain by name guest_names = [] ids = conn.listDomainsID() for id in ids: obj = conn.lookupByID(id) guest_names.append(obj.name()) if guestname not in guest_names: logger.error("guest %s doesn't exist or isn't running." % guestname) return 1 domobj = conn.lookupByName(guestname) timeout = 60 logger.info('destroy domain') if "noping" not in flags: # Get domain ip mac = utils.get_dom_mac_addr(guestname) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180, br) logger.info("the ip address of guest is %s" % ip) # Destroy domain try: domobj.destroy() except libvirtError as e: logger.error("API error message: %s, error code is %s" % (e.message, e.get_error_code())) logger.error("failed to destroy domain") return 1 # Check domain status by ping ip if "noping" not in flags: while timeout: time.sleep(10) timeout -= 10 logger.info(str(timeout) + "s left") logger.info('ping guest') if utils.do_ping(ip, 30): logger.error('The guest is still active, IP: ' + str(ip)) return 1 else: logger.info("domain %s was destroyed successfully" % guestname) break if timeout <= 0: logger.error("the domain couldn't be destroyed within 60 seconds.") return 1 return 0
libvirt.VIR_DOMAIN_RUNNING, libvirt.VIR_DOMAIN_NOSTATE, libvirt.VIR_DOMAIN_BLOCKED ] if state in expect_states: break time.sleep(10) timeout -= 10 logger.info(str(timeout) + "s left") if timeout <= 0: logger.error('The domain state is not as expected, state: ' + state) return 1 logger.info("Guest started") # Get domain ip and ping ip to check domain's status if not "noping" in flags: mac = utils.get_dom_mac_addr(domname) logger.info("get ip by mac address") ip = utils.mac_to_ip(mac, 180) logger.info('ping guest') if not utils.do_ping(ip, 300): logger.error('Failed on ping guest, IP: ' + str(ip)) return 1 logger.info("PASS") return 0
try: domobj.destroy() except libvirtError, e: logger.error("API error message: %s, error code is %s" \ % (e.message, e.get_error_code())) logger.error("fail to destroy domain") return 1 while timeout: time.sleep(10) timeout -= 10 logger.info(str(timeout) + "s left") logger.info('ping guest') if utils.do_ping(ip, 30): logger.error('The guest is still active, IP: ' + str(ip)) else: logger.info("domain %s is destroied successfully" % domain_name) break if timeout <= 0: logger.error("the domain couldn't be destroied within 60 secs.") return 1 newguestxml = redefine_vcpu_number(domobj, domain_name, vcpu) logger.debug('''new guest %s xml :\n%s''' % (domain_name, newguestxml)) logger.info("undefine the original guest") try: domobj.undefine()
def tls_setup(params): """ generate tls certificates and configure libvirt """ logger = params['logger'] target_machine = params['target_machine'] username = params['username'] password = params['password'] listen_tls = params['listen_tls'] auth_tls = params['auth_tls'] pkipath = "" if params.has_key('pkipath'): pkipath = params['pkipath'] if os.path.exists(pkipath): shutil.rmtree(pkipath) os.mkdir(pkipath) uri = "qemu://%s/system" % target_machine if pkipath: uri += "?pkipath=%s" % pkipath local_machine = utils.get_local_hostname() logger.info("the hostname of server is %s" % target_machine) logger.info("the hostname of local machine is %s" % local_machine) logger.info("the value of listen_tls is %s" % listen_tls) logger.info("the value of auth_tls is %s" % auth_tls) if not utils.do_ping(target_machine, 0): logger.error("failed to ping host %s" % target_machine) return 1 if os.path.exists(TEMP_TLS_FOLDER): shutil.rmtree(TEMP_TLS_FOLDER) os.mkdir(TEMP_TLS_FOLDER) if iptables_stop(target_machine, username, password, logger): return 1 if CA_setting_up(logger): return 1 if tls_server_cert(target_machine, logger): return 1 if tls_client_cert(local_machine, logger): return 1 if deliver_cert(target_machine, username, password, pkipath, logger): return 1 if auth_tls == 'sasl': if sasl_user_add(target_machine, username, password, logger): return 1 if tls_libvirtd_set(target_machine, username, password, listen_tls, auth_tls, logger): return 1 if listen_tls == 'disable': if hypervisor_connecting_test(uri, auth_tls, username, password, logger, 'fail'): return 1 elif listen_tls == 'enable': if hypervisor_connecting_test(uri, auth_tls, username, password, logger, 'success'): return 1 return 0
try: domobj.destroy() except libvirtError, e: logger.error("API error message: %s, error code is %s" \ % (e.message, e.get_error_code())) logger.error("fail to destroy domain") return 1 while timeout: time.sleep(10) timeout -= 10 logger.info(str(timeout) + "s left") logger.info('ping guest') if utils.do_ping(ip, 30): logger.error('The guest is still active, IP: ' + str(ip)) else: logger.info("domain %s is destroied successfully" % domain_name) break if timeout <= 0: logger.error("the domain couldn't be destroied within 60 secs.") return 1 newguestxml = redefine_vcpu_number(domobj, domain_name, vcpu) logger.debug('''new guest %s xml :\n%s''' %(domain_name, newguestxml)) logger.info("undefine the original guest") try: domobj.undefine()