How to use server_host method in ATX

Best Python code snippet using ATX

esmon_virt.py

Source:esmon_virt.py Github

copy

Full Screen

1# Copyright (c) 2017 DataDirect Networks, Inc.2# All Rights Reserved.3# Author: lixi@ddn.com4"""5Library for installing virtual machines6"""7# pylint: disable=too-many-lines8import sys9import logging10import traceback11import os12import shutil13import random14import yaml15import filelock16# Local libs17from pyesmon import utils18from pyesmon import time_util19from pyesmon import ssh_host20from pyesmon import esmon_common21ESMON_VIRT_CONFIG_FNAME = "esmon_virt.conf"22ESMON_VIRT_CONFIG = "/etc/" + ESMON_VIRT_CONFIG_FNAME23ESMON_VIRT_LOG_DIR = "/var/log/esmon_virt"24class VirtTemplate(object):25 """26 Each virtual machine template has an object of this type27 """28 # pylint: disable=too-few-public-methods,too-many-instance-attributes29 # pylint: disable=too-many-arguments30 def __init__(self, server_host, iso, template_hostname, internet,31 network_configs, image_dir, distro, ram_size, disk_sizes):32 self.vt_server_host = server_host33 self.vt_iso = iso34 self.vt_template_hostname = template_hostname35 self.vt_internet = internet36 self.vt_network_configs = network_configs37 self.vt_image_dir = image_dir38 self.vt_distro = distro39 self.vt_ram_size = ram_size40 self.vt_disk_sizes = disk_sizes41def random_mac():42 """43 Generate random MAC address44 """45 mac_parts = [random.randint(0x00, 0x7f),46 random.randint(0x00, 0xff),47 random.randint(0x00, 0xff)]48 mac_string = "52:54:00"49 for mac_part in mac_parts:50 mac_string += ":" + ("%02x" % mac_part)51 return mac_string52def vm_is_shut_off(server_host, hostname):53 """54 Check whether vm is shut off55 """56 state = server_host.sh_virsh_dominfo_state(hostname)57 if state is None:58 return False59 elif state == "shut off":60 return True61 return False62def vm_check_shut_off(args):63 """64 Check whether vm is shut off65 """66 server_host = args[0]67 hostname = args[1]68 off = vm_is_shut_off(server_host, hostname)69 if off:70 return 071 return -172def vm_delete(server_host, hostname):73 """74 Delete a virtual machine75 """76 existed = True77 active = True78 state = server_host.sh_virsh_dominfo_state(hostname)79 if state is None:80 existed = False81 active = False82 elif state == "shut off":83 active = False84 if active:85 command = ("virsh destroy %s" % hostname)86 retval = server_host.sh_run(command)87 if retval.cr_exit_status:88 logging.error("failed to run command [%s] on host [%s], "89 "ret = [%d], stdout = [%s], stderr = [%s]",90 command,91 server_host.sh_hostname,92 retval.cr_exit_status,93 retval.cr_stdout,94 retval.cr_stderr)95 return -196 if existed:97 command = ("virsh undefine %s" % hostname)98 retval = server_host.sh_run(command)99 if retval.cr_exit_status:100 logging.error("failed to run command [%s] on host [%s], "101 "ret = [%d], stdout = [%s], stderr = [%s]",102 command,103 server_host.sh_hostname,104 retval.cr_exit_status,105 retval.cr_stdout,106 retval.cr_stderr)107 return -1108 return 0109def vm_clone(workspace, server_host, hostname, network_configs, ips,110 template_hostname, image_dir, distro, internet, disk_number):111 """112 Create virtual machine113 """114 # pylint: disable=too-many-arguments,too-many-locals,too-many-return-statements115 # pylint: disable=too-many-branches,too-many-statements116 host_ip = ips[0]117 ret = vm_delete(server_host, hostname)118 if ret:119 return -1120 command = ("ping -c 1 %s" % host_ip)121 retval = server_host.sh_run(command)122 if retval.cr_exit_status == 0:123 logging.error("IP [%s] already used by a host", host_ip)124 return -1125 command = ("ping -c 1 %s" % hostname)126 retval = server_host.sh_run(command)127 if retval.cr_exit_status == 0:128 logging.error("host [%s] already up", hostname)129 return -1130 active = True131 state = server_host.sh_virsh_dominfo_state(template_hostname)132 if state is None:133 logging.error("template [%s] doesn't exist on host [%s]",134 template_hostname, server_host.sh_hostname)135 return -1136 elif state == "shut off":137 active = False138 if active:139 command = ("virsh destroy %s" % template_hostname)140 retval = server_host.sh_run(command)141 if retval.cr_exit_status:142 logging.error("failed to run command [%s] on host [%s], "143 "ret = [%d], stdout = [%s], stderr = [%s]",144 command,145 server_host.sh_hostname,146 retval.cr_exit_status,147 retval.cr_stdout,148 retval.cr_stderr)149 return -1150 file_options = ""151 for disk_index in range(disk_number):152 file_options += (" --file %s/%s_%d.img" %153 (image_dir, hostname, disk_index))154 command = ("rm -f %s/%s_%d.img" %155 (image_dir, hostname, disk_index))156 retval = server_host.sh_run(command)157 if retval.cr_exit_status:158 logging.error("failed to run command [%s] on host [%s], "159 "ret = [%d], stdout = [%s], stderr = [%s]",160 command,161 server_host.sh_hostname,162 retval.cr_exit_status,163 retval.cr_stdout,164 retval.cr_stderr)165 return -1166 command = ("virt-clone --original %s --name %s%s" %167 (template_hostname, hostname, file_options))168 retval = server_host.sh_run(command)169 if retval.cr_exit_status:170 logging.error("failed to run command [%s] on host [%s], "171 "ret = [%d], stdout = [%s], stderr = [%s]",172 command,173 server_host.sh_hostname,174 retval.cr_exit_status,175 retval.cr_stdout,176 retval.cr_stderr)177 return -1178 local_host_dir = workspace + "/" + hostname179 os.mkdir(local_host_dir)180 # net.ifnames=0 biosdevname=0 has been added to grub, so the interface181 # name will always be eth*182 eth_number = 0183 for eth_ip in ips:184 network_config = network_configs[eth_number]185 ifcfg = 'DEVICE="eth%d"\n' % eth_number186 ifcfg += 'IPADDR="%s"\n' % eth_ip187 ifcfg += 'NETMASK="%s"\n' % network_config["netmask"]188 if "gateway" in network_config:189 ifcfg += 'GATEWAY=\"%s"\n' % network_config["gateway"]190 ifcfg += """ONBOOT=yes191BOOTPROTO="static"192TYPE=Ethernet193IPV6INIT=no194NM_CONTROLLED=no195"""196 ifcfg_fname = "ifcfg-eth%d" % eth_number197 ifcfg_fpath = local_host_dir + "/" + ifcfg_fname198 with open(ifcfg_fpath, "wt") as fout:199 fout.write(ifcfg)200 host_ifcfg_fpath = workspace + "/" + ifcfg_fname201 ret = server_host.sh_send_file(ifcfg_fpath, workspace)202 if ret:203 logging.error("failed to send file [%s] on local host to "204 "directory [%s] on host [%s]",205 ifcfg_fpath, workspace,206 server_host.sh_hostname)207 return -1208 ret = server_host.sh_run("which virt-copy-in")209 if ret.cr_exit_status != 0:210 command = ("yum install libguestfs-tools-c -y")211 retval = server_host.sh_run(command)212 if retval.cr_exit_status:213 logging.error("failed to run command [%s] on host [%s], "214 "ret = [%d], stdout = [%s], stderr = [%s]",215 command,216 server_host.sh_hostname,217 retval.cr_exit_status,218 retval.cr_stdout,219 retval.cr_stderr)220 return -1221 command = ("virt-copy-in -d %s %s "222 "/etc/sysconfig/network-scripts" % (hostname, host_ifcfg_fpath))223 retval = server_host.sh_run(command)224 if retval.cr_exit_status:225 logging.error("failed to run command [%s] on host [%s], "226 "ret = [%d], stdout = [%s], stderr = [%s]",227 command,228 server_host.sh_hostname,229 retval.cr_exit_status,230 retval.cr_stdout,231 retval.cr_stderr)232 return -1233 eth_number += 1234 host_rules_fpath = workspace + "/70-persistent-net.rules"235 command = ("> %s" % host_rules_fpath)236 retval = server_host.sh_run(command)237 if retval.cr_exit_status:238 logging.error("failed to run command [%s] on host [%s], "239 "ret = [%d], stdout = [%s], stderr = [%s]",240 command,241 server_host.sh_hostname,242 retval.cr_exit_status,243 retval.cr_stdout,244 retval.cr_stderr)245 return -1246 command = ("virt-copy-in -d %s %s "247 "/etc/udev/rules.d" % (hostname, host_rules_fpath))248 retval = server_host.sh_run(command)249 if retval.cr_exit_status:250 logging.error("failed to run command [%s] on host [%s], "251 "ret = [%d], stdout = [%s], stderr = [%s]",252 command,253 server_host.sh_hostname,254 retval.cr_exit_status,255 retval.cr_stdout,256 retval.cr_stderr)257 return -1258 if distro == ssh_host.DISTRO_RHEL6:259 network_string = 'NETWORKING=yes\n'260 network_string += 'HOSTNAME=%s\n' % hostname261 network_fname = "network"262 network_fpath = local_host_dir + "/" + network_fname263 with open(network_fpath, "wt") as fout:264 fout.write(network_string)265 host_network_fpath = workspace + "/" + network_fname266 ret = server_host.sh_send_file(network_fpath, workspace)267 if ret:268 logging.error("failed to send file [%s] on local host to "269 "directory [%s] on host [%s]",270 network_fpath, workspace,271 server_host.sh_hostname)272 return -1273 command = ("virt-copy-in -d %s %s "274 "/etc/sysconfig" % (hostname, host_network_fpath))275 retval = server_host.sh_run(command)276 if retval.cr_exit_status:277 logging.error("failed to run command [%s] on host [%s], "278 "ret = [%d], stdout = [%s], stderr = [%s]",279 command,280 server_host.sh_hostname,281 retval.cr_exit_status,282 retval.cr_stdout,283 retval.cr_stderr)284 return -1285 else:286 host_hostname_fpath = workspace + "/hostname"287 command = ("echo %s > %s" % (hostname, host_hostname_fpath))288 retval = server_host.sh_run(command)289 if retval.cr_exit_status:290 logging.error("failed to run command [%s] on host [%s], "291 "ret = [%d], stdout = [%s], stderr = [%s]",292 command,293 server_host.sh_hostname,294 retval.cr_exit_status,295 retval.cr_stdout,296 retval.cr_stderr)297 return -1298 command = ("virt-copy-in -d %s %s "299 "/etc" % (hostname, host_hostname_fpath))300 retval = server_host.sh_run(command)301 if retval.cr_exit_status:302 logging.error("failed to run command [%s] on host [%s], "303 "ret = [%d], stdout = [%s], stderr = [%s]",304 command,305 server_host.sh_hostname,306 retval.cr_exit_status,307 retval.cr_stdout,308 retval.cr_stderr)309 return -1310 command = ("virsh start %s" % hostname)311 retval = server_host.sh_run(command)312 if retval.cr_exit_status:313 logging.error("failed to run command [%s] on host [%s], "314 "ret = [%d], stdout = [%s], stderr = [%s]",315 command,316 server_host.sh_hostname,317 retval.cr_exit_status,318 retval.cr_stdout,319 retval.cr_stderr)320 return -1321 # Remove the record in known_hosts, otherwise ssh will fail322 command = ('sed -i "/%s /d" /root/.ssh/known_hosts' % (host_ip))323 retval = server_host.sh_run(command)324 if retval.cr_exit_status:325 logging.error("failed to run command [%s] on host [%s], "326 "ret = [%d], stdout = [%s], stderr = [%s]",327 command,328 server_host.sh_hostname,329 retval.cr_exit_status,330 retval.cr_stdout,331 retval.cr_stderr)332 return -1333 # Remove the record in known_hosts, otherwise ssh will fail334 command = ('sed -i "/%s /d" /root/.ssh/known_hosts' % (hostname))335 retval = server_host.sh_run(command)336 if retval.cr_exit_status:337 logging.error("failed to run command [%s] on host [%s], "338 "ret = [%d], stdout = [%s], stderr = [%s]",339 command,340 server_host.sh_hostname,341 retval.cr_exit_status,342 retval.cr_stdout,343 retval.cr_stderr)344 return -1345 vm_host = ssh_host.SSHHost(host_ip)346 ret = vm_host.sh_wait_up()347 if ret:348 logging.error("failed to wait host [%s] up",349 host_ip)350 return -1351 ret = vm_check(hostname, host_ip, distro, internet)352 if ret:353 return -1354 return 0355def vm_check(hostname, host_ip, distro, internet):356 """357 Check whether virtual machine is up and fine358 """359 # pylint: disable=too-many-return-statements360 vm_host = ssh_host.SSHHost(host_ip)361 command = "hostname"362 retval = vm_host.sh_run(command)363 if retval.cr_exit_status:364 logging.error("failed to run command [%s] on host [%s], "365 "ret = [%d], stdout = [%s], stderr = [%s]",366 command,367 host_ip,368 retval.cr_exit_status,369 retval.cr_stdout,370 retval.cr_stderr)371 return -1372 current_hostname = retval.cr_stdout.strip()373 if current_hostname != hostname:374 logging.error("wrong host name of the virtual machine [%s], expected "375 "[%s], got [%s]", host_ip, hostname, current_hostname)376 return -1377 vm_host = ssh_host.SSHHost(hostname)378 command = "hostname"379 retval = vm_host.sh_run(command)380 if retval.cr_exit_status:381 logging.error("failed to run command [%s] on host [%s], "382 "ret = [%d], stdout = [%s], stderr = [%s]",383 command,384 hostname,385 retval.cr_exit_status,386 retval.cr_stdout,387 retval.cr_stderr)388 return -1389 current_hostname = retval.cr_stdout.strip()390 if current_hostname != hostname:391 logging.error("wrong host name of the virtual machine [%s], expected "392 "[%s], got [%s]", hostname, hostname, current_hostname)393 return -1394 if vm_host.sh_distro() != distro:395 logging.error("wrong distro of the virtual machine [%s], expected "396 "[%s], got [%s]", hostname, distro, vm_host.sh_distro())397 return -1398 if internet:399 if vm_host.sh_check_internet():400 logging.error("virtual machine [%s] can not access Internet",401 hostname)402 return -1403 return 0404def vm_start(workspace, server_host, hostname, network_configs, ips,405 template_hostname, image_dir, distro, internet, disk_number):406 """407 Start virtual machine, if vm is bad, clone it408 """409 # pylint: disable=too-many-arguments410 host_ip = ips[0]411 ret = vm_check(hostname, host_ip, distro, internet)412 if ret == 0:413 return 0414 if vm_is_shut_off(server_host, hostname):415 command = ("virsh start %s" % (hostname))416 retval = server_host.sh_run(command)417 if retval.cr_exit_status:418 logging.error("failed to run command [%s] on host [%s], "419 "ret = [%d], stdout = [%s], stderr = [%s]",420 command,421 server_host.sh_hostname,422 retval.cr_exit_status,423 retval.cr_stdout,424 retval.cr_stderr)425 return -1426 vm_host = ssh_host.SSHHost(hostname)427 ret = vm_host.sh_wait_up()428 if ret == 0:429 ret = vm_check(hostname, host_ip, distro, internet)430 if ret == 0:431 return 0432 ret = vm_clone(workspace, server_host, hostname, network_configs, ips,433 template_hostname, image_dir, distro, internet, disk_number)434 if ret:435 logging.error("failed to create virtual machine [%s] based on "436 "template [%s]", hostname, template_hostname)437 return -1438 return 0439def vm_install(workspace, server_host, iso_path, hostname,440 internet, network_configs, image_dir, distro,441 ram_size, disk_sizes):442 """443 Install virtual machine from ISO444 """445 # pylint: disable=too-many-arguments,too-many-locals446 # pylint: disable=too-many-return-statements,too-many-statements447 # pylint: disable=too-many-branches448 ret = vm_delete(server_host, hostname)449 if ret:450 return -1451 network_config = network_configs[0]452 host_ip = network_config["ip"]453 command = ("ping -c 1 %s" % host_ip)454 retval = server_host.sh_run(command)455 if retval.cr_exit_status == 0:456 logging.error("IP [%s] is already used by a host", host_ip)457 return -1458 command = ("ping -c 1 %s" % hostname)459 retval = server_host.sh_run(command)460 if retval.cr_exit_status == 0:461 logging.error("host [%s] is already up", hostname)462 return -1463 mnt_path = "/mnt/" + utils.random_word(8)464 command = ("mkdir -p %s && mount -o loop %s %s" %465 (mnt_path, iso_path, mnt_path))466 retval = server_host.sh_run(command)467 if retval.cr_exit_status:468 logging.error("failed to run command [%s] on host [%s], "469 "ret = [%d], stdout = [%s], stderr = [%s]",470 command,471 server_host.sh_hostname,472 retval.cr_exit_status,473 retval.cr_stdout,474 retval.cr_stderr)475 return -1476 ks_config = """# Kickstart file automatically generated by ESMON.477install478reboot479cdrom480lang en_US.UTF-8481keyboard us482"""483 ks_config += """rootpw password484firewall --disabled485authconfig --enableshadow --passalgo=sha512486selinux --disabled487timezone --utc Asia/Shanghai488bootloader --location=mbr --driveorder=sda --append="crashkernel=auto net.ifnames=0 biosdevname=0"489zerombr490clearpart --all --initlabel491part / --fstype=ext4 --grow --size=500 --ondisk=sda --asprimary492repo --name="Media" --baseurl=file:///mnt/source --cost=100493%packages494@Core495%end496%post --log=/var/log/anaconda/post-install.log497#!/bin/bash498# Configure hostname, somehow virt-install --name doesn't work499"""500 if distro == ssh_host.DISTRO_RHEL6:501 ks_config += 'echo NETWORKING=yes > /etc/sysconfig/network\n'502 ks_config += ('echo HOSTNAME=%s >> /etc/sysconfig/network\n' %503 (hostname))504 elif distro == ssh_host.DISTRO_RHEL7:505 ks_config += "echo %s > /etc/hostname\n" % (hostname)506 else:507 logging.error("wrong distro [%s]", distro)508 return -1509 ks_config += "# Configure network\n"510 eth_number = 0511 ens_number = 3512 for network_config in network_configs:513 # net.ifnames=0 biosdevname=0 will be added to GRUB_CMDLINE_LINUX, so the514 # interface name will always be eth*515 ks_config += "# Network eth%d\n" % eth_number516 ks_config += ("rm -f /etc/sysconfig/network-scripts/ifcfg-ens%d\n" %517 ens_number)518 ks_config += ("cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth%d\n" %519 eth_number)520 ks_config += "DEVICE=eth%d\n" % eth_number521 ks_config += 'IPADDR="%s"\n' % network_config["ip"]522 ks_config += 'NETMASK="%s"\n' % network_config["netmask"]523 if "gateway" in network_config:524 ks_config += 'GATEWAY=\"%s"\n' % network_config["gateway"]525 ks_config += """ONBOOT=yes526BOOTPROTO="static"527TYPE=Ethernet528IPV6INIT=no529NM_CONTROLLED=no530EOF531"""532 eth_number += 1533 ens_number += 1534 ks_config += "%end\n"535 local_host_dir = workspace + "/" + hostname536 os.mkdir(local_host_dir)537 ks_fname = "%s.ks" % hostname538 ks_fpath = local_host_dir + "/" + ks_fname539 with open(ks_fpath, "wt") as fout:540 fout.write(ks_config)541 host_ks_fpath = workspace + "/" + ks_fname542 ret = server_host.sh_send_file(ks_fpath, workspace)543 if ret:544 logging.error("failed to send file [%s] on local host to "545 "directory [%s] on host [%s]",546 ks_fpath, workspace,547 server_host.sh_hostname)548 return -1549 command = ("virt-install --vcpus=1 --os-type=linux "550 "--hvm --connect=qemu:///system "551 "--accelerate --serial pty -v --nographics --noautoconsole --wait=-1 ")552 command += "--ram=%s " % ram_size553 for network_config in network_configs:554 command += ("--network=%s " % (network_config["virt_install_option"]))555 command += ("--name=%s " % (hostname))556 command += ("--initrd-inject=%s " % (host_ks_fpath))557 disk_index = 0558 for disk_size in disk_sizes:559 command += ("--disk path=%s/%s_%d.img,size=%s " %560 (image_dir, hostname, disk_index, disk_size))561 disk_index += 1562 command += ("--location %s " % (mnt_path))563 command += ("--disk=%s,device=cdrom,perms=ro " % (iso_path))564 command += ("--extra-args='console=tty0 console=ttyS0,115200n8 "565 "ks=file:/%s'" % (ks_fname))566 if distro == ssh_host.DISTRO_RHEL6:567 install_timeout = 300568 elif distro == ssh_host.DISTRO_RHEL7:569 install_timeout = 1200570 retval = server_host.sh_run(command, timeout=install_timeout)571 if retval.cr_exit_status:572 logging.error("failed to run command [%s] on host [%s], "573 "ret = [%d], stdout = [%s], stderr = [%s]",574 command,575 server_host.sh_hostname,576 retval.cr_exit_status,577 retval.cr_stdout,578 retval.cr_stderr)579 return -1580 ret = server_host.sh_run("which sshpass")581 if ret.cr_exit_status != 0:582 # sshpass rely on epel-release on centos6583 command = ("yum install epel-release -y")584 retval = server_host.sh_run(command)585 if retval.cr_exit_status:586 logging.error("failed to run command [%s] on host [%s], "587 "ret = [%d], stdout = [%s], stderr = [%s]",588 command,589 server_host.sh_hostname,590 retval.cr_exit_status,591 retval.cr_stdout,592 retval.cr_stderr)593 return -1594 command = ("yum install sshpass -y")595 retval = server_host.sh_run(command)596 if retval.cr_exit_status:597 logging.error("failed to run command [%s] on host [%s], "598 "ret = [%d], stdout = [%s], stderr = [%s]",599 command,600 server_host.sh_hostname,601 retval.cr_exit_status,602 retval.cr_stdout,603 retval.cr_stderr)604 return -1605 # Remove the record in known_hosts, otherwise ssh will fail606 command = ('sed -i "/%s /d" /root/.ssh/known_hosts' % (host_ip))607 retval = server_host.sh_run(command)608 if retval.cr_exit_status:609 logging.error("failed to run command [%s] on host [%s], "610 "ret = [%d], stdout = [%s], stderr = [%s]",611 command,612 server_host.sh_hostname,613 retval.cr_exit_status,614 retval.cr_stdout,615 retval.cr_stderr)616 return -1617 # When virt-install finished, the virtual machine starts to reboot618 # so wait a little bit here until the host is up. Need619 # StrictHostKeyChecking=no, otherwise exit code will be 6 (ENOENT)620 expect_stdout = hostname + "\n"621 command = ("sshpass -p password ssh -o StrictHostKeyChecking=no "622 "root@%s hostname" % (host_ip))623 ret = server_host.sh_wait_update(command, expect_exit_status=0,624 expect_stdout=expect_stdout)625 if ret:626 logging.error("failed to wait host [%s] up", hostname)627 return -1628 command = ("sshpass -p password ssh root@%s "629 "\"mkdir /root/.ssh && chmod 600 /root/.ssh\"" % (host_ip))630 retval = server_host.sh_run(command)631 if retval.cr_exit_status:632 logging.error("failed to run command [%s] on host [%s], "633 "ret = [%d], stdout = [%s], stderr = [%s]",634 command,635 server_host.sh_hostname,636 retval.cr_exit_status,637 retval.cr_stdout,638 retval.cr_stderr)639 return -1640 command = ("sshpass -p password scp /root/.ssh/* root@%s:/root/.ssh" % (host_ip))641 retval = server_host.sh_run(command)642 if retval.cr_exit_status:643 logging.error("failed to run command [%s] on host [%s], "644 "ret = [%d], stdout = [%s], stderr = [%s]",645 command,646 server_host.sh_hostname,647 retval.cr_exit_status,648 retval.cr_stdout,649 retval.cr_stderr)650 return -1651 vm_host = ssh_host.SSHHost(host_ip)652 command = "> /root/.ssh/known_hosts"653 retval = vm_host.sh_run(command)654 if retval.cr_exit_status:655 logging.error("failed to run command [%s] on host [%s], "656 "ret = [%d], stdout = [%s], stderr = [%s]",657 command,658 vm_host.sh_hostname,659 retval.cr_exit_status,660 retval.cr_stdout,661 retval.cr_stderr)662 return -1663 command = "hostname"664 retval = vm_host.sh_run(command)665 if retval.cr_exit_status:666 logging.error("failed to run command [%s] on host [%s], "667 "ret = [%d], stdout = [%s], stderr = [%s]",668 command,669 vm_host.sh_hostname,670 retval.cr_exit_status,671 retval.cr_stdout,672 retval.cr_stderr)673 return -1674 real_hostname = retval.cr_stdout.strip()675 if real_hostname != hostname:676 logging.error("wrong hostname, expected [%s], got [%s]",677 hostname, real_hostname)678 return -1679 if internet:680 ret = vm_host.sh_enable_dns()681 if ret:682 logging.error("failed to enable dns on host [%s]")683 return -1684 command = "yum install rsync -y"685 retval = vm_host.sh_run(command)686 if retval.cr_exit_status:687 logging.error("failed to run command [%s] on host [%s], "688 "ret = [%d], stdout = [%s], stderr = [%s]",689 command,690 vm_host.sh_hostname,691 retval.cr_exit_status,692 retval.cr_stdout,693 retval.cr_stderr)694 return -1695 # Do not check the return status, because the connection could be stopped696 command = "init 0"697 vm_host.sh_run(command)698 command = ("umount %s" % (mnt_path))699 retval = server_host.sh_run(command)700 if retval.cr_exit_status:701 logging.error("failed to run command [%s] on host [%s], "702 "ret = [%d], stdout = [%s], stderr = [%s]",703 command,704 server_host.sh_hostname,705 retval.cr_exit_status,706 retval.cr_stdout,707 retval.cr_stderr)708 ret = -1709 command = ("rmdir %s" % (mnt_path))710 retval = server_host.sh_run(command)711 if retval.cr_exit_status:712 logging.error("failed to run command [%s] on host [%s], "713 "ret = [%d], stdout = [%s], stderr = [%s]",714 command,715 server_host.sh_hostname,716 retval.cr_exit_status,717 retval.cr_stdout,718 retval.cr_stderr)719 return -1720 # Need to wait until VM shut off, otherwise "virsh change-media" won't721 # change the XML file722 ret = utils.wait_condition(vm_check_shut_off, [server_host, hostname])723 if ret:724 logging.error("failed when waiting host [%s] on [%s] shut off",725 hostname, server_host.sh_hostname)726 return ret727 # Find the CDROM device728 command = ("virsh domblklist %s --details | grep cdrom | "729 "awk '{print $3}'" % (hostname))730 retval = server_host.sh_run(command)731 if retval.cr_exit_status:732 logging.error("failed to run command [%s] on host [%s], "733 "ret = [%d], stdout = [%s], stderr = [%s]",734 command,735 server_host.sh_hostname,736 retval.cr_exit_status,737 retval.cr_stdout,738 retval.cr_stderr)739 return -1740 cdroms = retval.cr_stdout.splitlines()741 if len(cdroms) != 1:742 logging.error("unexpected cdroms: [%s]",743 retval.cr_stdout)744 return -1745 cdrom = cdroms[0]746 command = ("virsh change-media %s %s --eject" % (hostname, cdrom))747 retval = server_host.sh_run(command)748 if retval.cr_exit_status:749 logging.error("failed to run command [%s] on host [%s], "750 "ret = [%d], stdout = [%s], stderr = [%s]",751 command,752 server_host.sh_hostname,753 retval.cr_exit_status,754 retval.cr_stdout,755 retval.cr_stderr)756 return -1757 return 0758def esmon_vm_install(workspace, config, config_fpath):759 """760 Start to test with ESMON761 """762 # pylint: disable=too-many-return-statements,too-many-locals763 # pylint: disable=too-many-branches,too-many-statements764 ssh_host_configs = esmon_common.config_value(config, esmon_common.CSTR_SSH_HOSTS)765 if ssh_host_configs is None:766 logging.error("can NOT find [%s] in the config file, "767 "please correct file [%s]",768 esmon_common.CSTR_SSH_HOSTS, config_fpath)769 return -1770 hosts = {}771 for host_config in ssh_host_configs:772 host_id = host_config[esmon_common.CSTR_HOST_ID]773 if host_id is None:774 logging.error("can NOT find [%s] in the config of a "775 "SSH host, please correct file [%s]",776 esmon_common.CSTR_HOST_ID, config_fpath)777 return -1778 hostname = esmon_common.config_value(host_config,779 esmon_common.CSTR_HOSTNAME)780 if hostname is None:781 logging.error("can NOT find [%s] in the config of SSH host "782 "with ID [%s], please correct file [%s]",783 esmon_common.CSTR_HOSTNAME, host_id, config_fpath)784 return -1785 local = esmon_common.config_value(host_config,786 esmon_common.CSTR_LOCAL_HOST)787 if local is None:788 logging.debug("can NOT find [%s] in the config of SSH host "789 "with ID [%s], use [false] as default value",790 esmon_common.CSTR_LOCAL_HOST, host_id)791 local = False792 mapping_dict = {esmon_common.ESMON_CONFIG_CSTR_NONE: None}793 ssh_identity_file = esmon_common.config_value(host_config,794 esmon_common.CSTR_SSH_IDENTITY_FILE,795 mapping_dict=mapping_dict)796 if host_id in hosts:797 logging.error("multiple SSH hosts with the same ID [%s], please "798 "correct file [%s]", host_id, config_fpath)799 return -1800 host = ssh_host.SSHHost(hostname, ssh_identity_file, local=local)801 hosts[host_id] = host802 template_configs = esmon_common.config_value(config, esmon_common.CSTR_TEMPLATES)803 if template_configs is None:804 logging.error("can NOT find [%s] in the config file, "805 "please correct file [%s]",806 esmon_common.CSTR_TEMPLATES, config_fpath)807 return -1808 templates = {}809 for template_config in template_configs:810 template_hostname = esmon_common.config_value(template_config,811 esmon_common.CSTR_HOSTNAME)812 if template_hostname is None:813 logging.error("can NOT find [%s] in the config of a "814 "SSH host, please correct file [%s]",815 esmon_common.CSTR_HOSTNAME, config_fpath)816 return -1817 reinstall = esmon_common.config_value(template_config,818 esmon_common.CSTR_REINSTALL)819 if reinstall is None:820 logging.error("no [%s] is configured, please correct file [%s]",821 esmon_common.CSTR_REINSTALL, config_fpath)822 return -1823 internet = esmon_common.config_value(template_config,824 esmon_common.CSTR_INTERNET)825 if internet is None:826 internet = False827 logging.debug("no [%s] is configured, will "828 "not add internet support", esmon_common.CSTR_INTERNET)829 ram_size = esmon_common.config_value(template_config, esmon_common.CSTR_RAM_SIZE)830 if ram_size is None:831 logging.error("no [%s] is configured, please correct file [%s]",832 esmon_common.CSTR_RAM_SIZE, config_fpath)833 return -1834 disk_sizes = esmon_common.config_value(template_config,835 esmon_common.CSTR_DISK_SIZES)836 if disk_sizes is None:837 logging.error("no [%s] is configured, please correct file [%s]",838 esmon_common.CSTR_DISK_SIZES, config_fpath)839 return -1840 network_configs = esmon_common.config_value(template_config,841 esmon_common.CSTR_NETWORK_CONFIGS)842 if network_configs is None:843 logging.error("no [%s] is configured, please correct file [%s]",844 esmon_common.CSTR_NETWORK_CONFIGS, config_fpath)845 return -1846 iso = esmon_common.config_value(template_config, esmon_common.CSTR_ISO)847 if iso is None:848 logging.error("no [%s] is configured, please correct file [%s]",849 esmon_common.CSTR_ISO, config_fpath)850 return -1851 distro = esmon_common.config_value(template_config, esmon_common.CSTR_DISTRO)852 if distro is None:853 logging.error("no [%s] is configured, please correct file [%s]",854 esmon_common.CSTR_DISTRO, config_fpath)855 return -1856 image_dir = esmon_common.config_value(template_config, esmon_common.CSTR_IMAGE_DIR)857 if image_dir is None:858 logging.error("no [%s] is configured, please correct file [%s]",859 esmon_common.CSTR_IMAGE_DIR, config_fpath)860 return -1861 server_host_id = esmon_common.config_value(template_config,862 esmon_common.CSTR_SERVER_HOST_ID)863 if server_host_id is None:864 logging.error("no [%s] is configured, please correct file [%s]",865 esmon_common.CSTR_SERVER_HOST_ID, config_fpath)866 return -1867 if server_host_id not in hosts:868 logging.error("SSH host with ID [%s] is NOT configured in "869 "[%s], please correct file [%s]",870 server_host_id, esmon_common.CSTR_SSH_HOSTS,871 config_fpath)872 return -1873 server_host = hosts[server_host_id]874 command = "mkdir -p %s" % workspace875 retval = server_host.sh_run(command)876 if retval.cr_exit_status:877 logging.error("failed to run command [%s] on host [%s], "878 "ret = [%d], stdout = [%s], stderr = [%s]",879 command,880 server_host.sh_hostname,881 retval.cr_exit_status,882 retval.cr_stdout,883 retval.cr_stderr)884 return -1885 template = VirtTemplate(server_host, iso, template_hostname, internet,886 network_configs, image_dir, distro, ram_size,887 disk_sizes)888 templates[template_hostname] = template889 state = server_host.sh_virsh_dominfo_state(template_hostname)890 if not reinstall and state is not None:891 logging.debug("skipping reinstall of template [%s] according to config",892 template_hostname)893 continue894 ret = vm_install(workspace, server_host, iso,895 template_hostname, internet,896 network_configs, image_dir,897 distro, ram_size, disk_sizes)898 if ret:899 logging.error("failed to create virtual machine template [%s]",900 template_hostname)901 return -1902 vm_host_configs = esmon_common.config_value(config, "vm_hosts")903 if vm_host_configs is None:904 logging.error("no [vm_hosts] is configured, "905 "please correct file [%s]", config_fpath)906 return -1907 vm_hosts = []908 hosts_string = """127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4909::1 localhost localhost.localdomain localhost6 localhost6.localdomain6910"""911 for vm_host_config in vm_host_configs:912 hostname = esmon_common.config_value(vm_host_config, esmon_common.CSTR_HOSTNAME)913 if hostname is None:914 logging.error("no [hostname] is configured for a vm_host, "915 "please correct file [%s]", config_fpath)916 return -1917 ips = esmon_common.config_value(vm_host_config, esmon_common.CSTR_HOST_IPS)918 if ips is None:919 logging.error("no [%s] is configured for a vm_host, "920 "please correct file [%s]", esmon_common.CSTR_HOST_IPS,921 config_fpath)922 return -1923 template_hostname = esmon_common.config_value(vm_host_config,924 esmon_common.CSTR_TEMPLATE_HOSTNAME)925 if template_hostname is None:926 logging.error("can NOT find [%s] in the config of a "927 "SSH host, please correct file [%s]",928 esmon_common.CSTR_TEMPLATE_HOSTNAME, config_fpath)929 return -1930 if template_hostname not in templates:931 logging.error("template with hostname [%s] is NOT configured in "932 "[%s], please correct file [%s]",933 template_hostname, esmon_common.CSTR_TEMPLATES, config_fpath)934 return -1935 template = templates[template_hostname]936 reinstall = esmon_common.config_value(vm_host_config, "reinstall")937 state = template.vt_server_host.sh_virsh_dominfo_state(hostname)938 if reinstall is None:939 reinstall = False940 if state is None:941 reinstall = True942 if not reinstall:943 ret = vm_start(workspace,944 template.vt_server_host,945 hostname,946 template.vt_network_configs,947 ips,948 template.vt_template_hostname,949 template.vt_image_dir,950 template.vt_distro,951 template.vt_internet,952 len(template.vt_disk_sizes))953 if ret:954 logging.error("virtual machine [%s] can't be started",955 hostname)956 return -1957 else:958 ret = vm_clone(workspace,959 template.vt_server_host,960 hostname,961 template.vt_network_configs,962 ips,963 template.vt_template_hostname,964 template.vt_image_dir,965 template.vt_distro,966 template.vt_internet,967 len(template.vt_disk_sizes))968 if ret:969 logging.error("failed to create virtual machine [%s] based on "970 "template [%s]", hostname,971 template.vt_template_hostname)972 return -1973 host_ip = ips[0]974 vm_host = ssh_host.SSHHost(hostname)975 hosts_string += ("%s %s\n" % (host_ip, hostname))976 vm_hosts.append(vm_host)977 host_configs = esmon_common.config_value(config, esmon_common.CSTR_HOSTS)978 if host_configs is None:979 logging.error("can NOT find [%s] in the config file, "980 "please correct file [%s]",981 esmon_common.CSTR_HOSTS, config_fpath)982 return -1983 for host_config in host_configs:984 hostname = esmon_common.config_value(host_config, esmon_common.CSTR_HOSTNAME)985 if hostname is None:986 logging.error("can NOT find [%s] in the config file, "987 "please correct file [%s]",988 esmon_common.CSTR_HOSTNAME, config_fpath)989 return -1990 host_ip = esmon_common.config_value(host_config, esmon_common.CSTR_IP)991 if host_ip is None:992 logging.error("can NOT find [%s] in the config file, "993 "please correct file [%s]",994 esmon_common.CSTR_IP, config_fpath)995 return -1996 hosts_string += ("%s %s\n" % (host_ip, hostname))997 hosts_fpath = workspace + "/hosts"998 with open(hosts_fpath, "wt") as hosts_file:999 hosts_file.write(hosts_string)1000 for host in vm_hosts:1001 ret = host.sh_send_file(hosts_fpath, "/etc")1002 if ret:1003 logging.error("failed to send hosts file [%s] on local host to "1004 "directory [%s] on host [%s]",1005 hosts_fpath, workspace,1006 host.sh_hostname)1007 return -11008 # Clear the known_hosts, otherwise the reinstalled hosts can't be1009 # accessed by other hosts1010 command = "> /root/.ssh/known_hosts"1011 retval = host.sh_run(command)1012 if retval.cr_exit_status:1013 logging.error("failed to run command [%s] on host [%s], "1014 "ret = [%d], stdout = [%s], stderr = [%s]",1015 command,1016 host.sh_hostname,1017 retval.cr_exit_status,1018 retval.cr_stdout,1019 retval.cr_stderr)1020 return -11021 return 01022def esmon_virt_locked(workspace, config_fpath):1023 """1024 Start to test holding the confiure lock1025 """1026 # pylint: disable=too-many-branches,bare-except,too-many-locals1027 # pylint: disable=too-many-statements1028 config_fd = open(config_fpath)1029 ret = 01030 try:1031 config = yaml.load(config_fd)1032 except:1033 logging.error("not able to load [%s] as yaml file: %s", config_fpath,1034 traceback.format_exc())1035 ret = -11036 config_fd.close()1037 if ret:1038 return -11039 try:1040 ret = esmon_vm_install(workspace, config, config_fpath)1041 except:1042 ret = -11043 logging.error("exception: %s", traceback.format_exc())1044 return ret1045def esmon_virt(workspace, config_fpath):1046 """1047 Start to install virtual machines1048 """1049 # pylint: disable=bare-except1050 lock_file = config_fpath + ".lock"1051 lock = filelock.FileLock(lock_file)1052 try:1053 with lock.acquire(timeout=0):1054 try:1055 ret = esmon_virt_locked(workspace, config_fpath)1056 except:1057 ret = -11058 logging.error("exception: %s", traceback.format_exc())1059 lock.release()1060 except filelock.Timeout:1061 ret = -11062 logging.error("someone else is holding lock of file [%s], aborting "1063 "to prevent conflicts", lock_file)1064 return ret1065def usage():1066 """1067 Print usage string1068 """1069 utils.eprint("Usage: %s <config_file>" %1070 sys.argv[0])1071def main():1072 """1073 Install virtual machines1074 """1075 # pylint: disable=unused-variable1076 reload(sys)1077 sys.setdefaultencoding("utf-8")1078 config_fpath = ESMON_VIRT_CONFIG1079 if len(sys.argv) == 2:1080 config_fpath = sys.argv[1]1081 elif len(sys.argv) > 2:1082 usage()1083 sys.exit(-1)1084 identity = time_util.local_strftime(time_util.utcnow(), "%Y-%m-%d-%H_%M_%S")1085 workspace = ESMON_VIRT_LOG_DIR + "/" + identity1086 if not os.path.exists(ESMON_VIRT_LOG_DIR):1087 os.mkdir(ESMON_VIRT_LOG_DIR)1088 elif not os.path.isdir(ESMON_VIRT_LOG_DIR):1089 logging.error("[%s] is not a directory", ESMON_VIRT_LOG_DIR)1090 sys.exit(-1)1091 if not os.path.exists(workspace):1092 os.mkdir(workspace)1093 elif not os.path.isdir(workspace):1094 logging.error("[%s] is not a directory", workspace)1095 sys.exit(-1)1096 print("Started installing virtual machines using config [%s], "1097 "please check [%s] for more log" %1098 (config_fpath, workspace))1099 utils.configure_logging(workspace)1100 console_handler = utils.LOGGING_HANLDERS["console"]1101 console_handler.setLevel(logging.DEBUG)1102 save_fpath = workspace + "/" + ESMON_VIRT_CONFIG_FNAME1103 logging.debug("copying config file from [%s] to [%s]", config_fpath,1104 save_fpath)1105 shutil.copyfile(config_fpath, save_fpath)1106 ret = esmon_virt(workspace, config_fpath)1107 if ret:1108 logging.error("installation failed, please check [%s] for more log\n",1109 workspace)1110 sys.exit(ret)1111 logging.info("Installed the virtual machines, please check [%s] "1112 "for more log", workspace)...

Full Screen

Full Screen

service_request_body.py

Source:service_request_body.py Github

copy

Full Screen

...50 :type service_name: str51 """52 self._service_name = service_name53 @property54 def server_host(self):55 """Gets the server_host of this ServiceRequestBody.56 server_host是由用户提供的域名。 我们会通过此域名进行接口调用,请以https/http开头,长度小于等于128位字符。 TestHub将会通过此域名下的接口,保证云测数据与用户系统数据的一致性。57 :return: The server_host of this ServiceRequestBody.58 :rtype: str59 """60 return self._server_host61 @server_host.setter62 def server_host(self, server_host):63 """Sets the server_host of this ServiceRequestBody.64 server_host是由用户提供的域名。 我们会通过此域名进行接口调用,请以https/http开头,长度小于等于128位字符。 TestHub将会通过此域名下的接口,保证云测数据与用户系统数据的一致性。65 :param server_host: The server_host of this ServiceRequestBody.66 :type server_host: str67 """68 self._server_host = server_host69 def to_dict(self):70 """Returns the model properties as a dict"""71 result = {}72 for attr, _ in six.iteritems(self.openapi_types):73 value = getattr(self, attr)74 if isinstance(value, list):75 result[attr] = list(map(76 lambda x: x.to_dict() if hasattr(x, "to_dict") else x,...

Full Screen

Full Screen

vpn.py

Source:vpn.py Github

copy

Full Screen

1import os2class VPN:3 def __init__(self, server_host, remote_dir, local_dir):4 self.server_host = server_host5 self.remote_dir = remote_dir6 self.local_dir = local_dir7 def add_vpn_user(self, user_name):8 os.system(f'ssh {self.server_host} "sudo ikev2.sh --addclient {user_name}"')9 def copy_sert(self, user_name):10 os.system(f'ssh {self.server_host} "sudo ikev2.sh --addclient {user_name}"')11 os.system(f'ssh {self.server_host} "sudo ikev2.sh --exportclient {user_name}"')12 os.system(f'scp {self.server_host}:{self.remote_dir}/{user_name}.mobileconfig '13 f'{self.local_dir}/{self.server_host}-{user_name}.mobileconfig')14 os.system(f'scp {self.server_host}:{self.remote_dir}/{user_name}.p12 '15 f'{self.local_dir}/{self.server_host}-{user_name}.p12')16 os.system(f'scp {self.server_host}:{self.remote_dir}/{user_name}.sswan '17 f'{self.local_dir}/{self.server_host}-{user_name}.sswan')18 def delete_sert_files(self, user_name):19 os.system(f'ssh {self.server_host} "rm {user_name}*"')20def main():21 pass22if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ATX automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful