Пример #1
0
    def start_vm(self):
        if self._type == "VMWare":
            # Load VMWare
            print("Starting VM session at " + str(datetime.now()) + "...")
            self.host = VixHost()

            # Using the VM directory, identify the valid VMs and add them to a dictionary {name: file}
            vm_dir = str(Path.home() / "Documents" / "Virtual Machines")
            files = [(root, file) for root, _, flist in os.walk(vm_dir)
                     for file in flist if file.endswith(".vmx")]
            vm_dict = {
                file.rsplit('.', 1)[0]: os.path.join(root, file)
                for root, file in files
            }
            self._vm_file = vm_dict[self._name]
Пример #2
0
class BaseVixVMTest(unittest.TestCase):
    def setUp(self):
        self._host = VixHost()
        items = self._host.find_items()
        self._vm = items[0]

        self.assertIsInstance(self._vm, VixVM)
Пример #3
0
class VixHostTest(unittest.TestCase):
    def setUp(self):
        self._conn = VixHost()

    def test_host_info(self):
        host_info = self._conn.host_info

        self.assertIn(host_info.host_type, (
            VixHost.VIX_SERVICEPROVIDER_VMWARE_SERVER,
            VixHost.VIX_SERVICEPROVIDER_VMWARE_WORKSTATION,
            VixHost.VIX_SERVICEPROVIDER_VMWARE_PLAYER,
            VixHost.VIX_SERVICEPROVIDER_VMWARE_VI_SERVER,
            VixHost.VIX_SERVICEPROVIDER_VMWARE_WORKSTATION_SHARED,
        ), 'host_type is unknown')

        self.assertIsNot(host_info.api_version, None, 'invalid api_version')
        self.assertIsInstance(host_info.software_version, str,
                              'software_version is not a string.')

    def test_find_item_names(self):
        items = self._conn.find_items(names_only=True)

        self.assertIsInstance(items, list, 'Expected list')
        self.assertGreater(len(items), 0, 'No VMS running. Cant test...')

        for item in items:
            self.assertIsInstance(item, str, 'Expected string')

    def test_find_item_instances(self):
        items = self._conn.find_items(names_only=False)

        self.assertIsInstance(items, list, 'Expected list')
        self.assertGreater(len(items), 0, 'No VMS running. Cant test...')

        for item in items:
            self.assertIsInstance(item, VixVM, 'Expected VixVM')

    def tearDown(self):
        del self._conn
Пример #4
0
    def run(self):
        try:
            done = 0
            get_allconfig()
            GlobalLogging.getInstance().info('-' * 50)
            GlobalLogging.getInstance().info('Copying the ' + build_number +
                                             ' build to host.')

            release_package_full_path = os.path.join(
                '{rp}\{rpd}\ArchiveManagerInstaller.msi'.format(
                    rp=release_path, rpd=build_number))
            # Copy the latest mis packet from share path to host,if fail,it will retry 3 times
            i = 0
            while i < 3:
                copy_return = shell.SHFileOperation(
                    (0, shellcon.FO_COPY, release_package_full_path,
                     host_os_files_path_for_am,
                     shellcon.FOF_NOCONFIRMATION | shellcon.FOF_SILENT, None,
                     None))
                if copy_return[0] == 0:
                    GlobalLogging.getInstance().info(
                        "Copied the " + build_number + " build from " +
                        release_package_full_path + " to " +
                        host_os_files_path_for_am)
                    break
                i += 1
                GlobalLogging.getInstance().error(
                    "Copy error with error code:{error}".format(
                        error=copy_return))

            # new a VixHost instance
            _vm_host = VixHost()

            # open the dc vm
            GlobalLogging.getInstance().info('Opening the dc vm.')
            vm_dc = _vm_host.open_vm(dc_vmx_path)
            GlobalLogging.getInstance().info('Power on the dc vm.')
            vm_dc.power_on(launch_gui=True)
            GlobalLogging.getInstance().info('Power on the dc vm - Done.')
            # revert to a snapshot
            GlobalLogging.getInstance().info('Reverting to snapshot.')
            vm_dc_snapshot = vm_dc.snapshot_get_named(snapshot_name)
            vm_dc.snapshot_revert(snapshot=vm_dc_snapshot,
                                  options=VixVM.VIX_VMPOWEROP_LAUNCH_GUI)
            GlobalLogging.getInstance().info(
                'Reverted to snapshot Base - Done.')

            # Login to dc guest for guest operation
            GlobalLogging.getInstance().info('Waiting for tools.')
            vm_dc.wait_for_tools()
            GlobalLogging.getInstance().info('Login dc guest.')
            vm_dc.login(guest_login_name,
                        guest_login_password,
                        require_interactive=True)
            time.sleep(2.0)
            GlobalLogging.getInstance().info(
                'Login to dc guest for guest operation - Done.')

            # copy vix folder from host to guest for dc
            GlobalLogging.getInstance().info(
                'Copying vix folder from host to dc guest.')
            vm_dc.copy_host_to_guest(host_os_files_path_for_dc,
                                     guest_os_files_path)
            GlobalLogging.getInstance().info(
                'Copied vix folder from host to dc guest - Done.')

            # run program on am dc
            dc_program_name = program_run_on_dc.split(',')
            dc_program_block = not (program_run_on_dc_imme == str(True))
            for dc_program in dc_program_name:
                if dc_program is not '':
                    GlobalLogging.getInstance().info('Running program ' +
                                                     dc_program + '.')
                    vm_dc.proc_run(guest_os_files_path + "\\" + dc_program,
                                   None, dc_program_block)
                    GlobalLogging.getInstance().info('Running program ' +
                                                     dc_program + ' - Done.')

            # run script on am dc
            dc_script = script_run_on_dc.split(',')
            dc_scr_block = not (script_run_on_dc_imme == str(True))
            for dc_scr in dc_script:
                if dc_scr is not '':
                    GlobalLogging.getInstance().info('Running script ' +
                                                     dc_scr + '.')
                    vm_dc.run_script(dc_scr, None, dc_scr_block)
                    GlobalLogging.getInstance().info('Running script ' +
                                                     dc_scr + ' - Done.')

            # logout guest of dc
            vm_dc.logout()
            GlobalLogging.getInstance().info('Logout dc guest - Done.')

            # open the am vm
            GlobalLogging.getInstance().info('Opening the am vm.')
            vm_am = _vm_host.open_vm(am_vmx_path)
            GlobalLogging.getInstance().info('Power on the am vm.')
            vm_am.power_on(launch_gui=True)
            GlobalLogging.getInstance().info('Power on the am vm - Done.')
            # revert to a snapshot
            GlobalLogging.getInstance().info('Reverting to snapshot.')
            vm_am_snapshot = vm_am.snapshot_get_named(snapshot_name)
            vm_am.snapshot_revert(snapshot=vm_am_snapshot,
                                  options=VixVM.VIX_VMPOWEROP_LAUNCH_GUI)
            GlobalLogging.getInstance().info(
                'Reverted to snapshot Base - Done.')

            # Login to am guest for guest operation
            GlobalLogging.getInstance().info('Waiting for tools.')
            vm_am.wait_for_tools()

            GlobalLogging.getInstance().info('Login am guest.')
            vm_am.login(guest_login_name,
                        guest_login_password,
                        require_interactive=True)
            time.sleep(2.0)
            GlobalLogging.getInstance().info(
                'Login to am guest for guest operation - Done.')

            # copy vix folder from host to guest for am
            GlobalLogging.getInstance().info(
                'Copying vix folder from host to am guest.')
            vm_am.copy_host_to_guest(host_os_files_path_for_am,
                                     guest_os_files_path)
            GlobalLogging.getInstance().info(
                'Copy vix folder from host to am guest - Done.')

            # run program on am vm
            am_program_name = program_run_on_am.split(',')
            am_program_block = not (program_run_on_am_imme == str(True))
            for am_program in am_program_name:
                if am_program is not '':
                    GlobalLogging.getInstance().info('Running program ' +
                                                     am_program + '.')
                    vm_am.proc_run(guest_os_files_path + "\\" + am_program,
                                   None, am_program_block)
                    GlobalLogging.getInstance().info('Running program ' +
                                                     am_program + ' - Done.')

            # run script on am vm
            am_script = script_run_on_am.split(',')
            am_scr_block = not (script_run_on_am_imme == str(True))
            for am_scr in am_script:
                if am_scr is not '':
                    GlobalLogging.getInstance().info('Running script ' +
                                                     am_scr + '.')
                    vm_am.run_script(am_scr, None, am_scr_block)
                    GlobalLogging.getInstance().info('Running script ' +
                                                     am_scr + ' - Done.')

            # logout guest of am
            vm_am.logout()
            GlobalLogging.getInstance().info('Logout am guest - Done.')
            done = 1

        except Exception as ex:
            GlobalLogging.getInstance().exception("Catch a exception.")
        finally:
            _vm_host.disconnect()
            self.run_button.setEnabled(True)
            self.run_button.setText("Run")
            if done == 1:
                self._change_logcolour_signal.emit("green")
                GlobalLogging.getInstance().info(build_number +
                                                 " installed successfully.")
            else:
                self._change_logcolour_signal.emit("red")
                GlobalLogging.getInstance().info(build_number +
                                                 " install failed.")
Пример #5
0
from flask import Flask, render_template, redirect, url_for, request
from vix import VixHost
import subprocess

app = Flask(__name__, template_folder='templates')

host = VixHost()
vm = host.open_vm(r'C:\Users\Raynold kennedy L\Documents\UKP\semester 6\Tekvir\CentOS.vmx')

@app.route("/", methods=["POST","GET"])
def home():
    return render_template("index.html")

@app.route("/poweron", methods=["POST","GET"])
def poweron():
    if request.method == "POST":
        vm.power_on(launch_gui=True)
        return render_template("poweron.html")
    else :
        return render_template("poweron.html")

@app.route("/poweroff", methods=["POST","GET"])
def poweroff():
    if request.method == "POST":
        vm.power_off(from_guest=False)
        return render_template("index.html")
    else :
        return render_template("index.html")

@app.route("/suspend", methods=["POST","GET"])
def suspend():
Пример #6
0
    def setUp(self):
        self._host = VixHost()
        items = self._host.find_items()
        self._vm = items[0]

        self.assertIsInstance(self._vm, VixVM)
Пример #7
0
 def setUp(self):
     self._conn = VixHost()
Пример #8
0
class VmWrapper:
    def __init__(self, settings):
        # Currently only supports VMWare and VirtualBox
        if settings.type == "VMWare" or settings.type == "VirtualBox":
            self._type = settings.type
        else:
            # TODO - make this more robust
            print("Unsupported VM type!")
            return

        # Setup other settings
        self._name = settings.name
        self._snapshot = settings.snapshot
        self._ip = settings.ip
        self._port = settings.port
        self._user = settings.user
        self._passwd = settings.passwd
        self._boot_time = settings.boot_time
        self._staging_files = settings.staging_files
        self._payload_files = settings.payload_files
        self._result_files = settings.result_files
        self._staging_dir = settings.staging_dir
        self._payload_dir = settings.payload_dir
        self._result_dir = settings.result_dir
        self._build_cmd = settings.build_cmd
        self._stage_cmd = settings.stage_cmd
        self._remote_proj_dir = settings.remote_proj_dir
        self._remote_staging_dir = settings.remote_staging_dir
        self._remote_payload_dir = settings.remote_payload_dir
        self._remote_result_dir = settings.remote_result_dir

    # Method to start the VM software, only needs to be done once
    def start_vm(self):
        if self._type == "VMWare":
            # Load VMWare
            print("Starting VM session at " + str(datetime.now()) + "...")
            self.host = VixHost()

            # Using the VM directory, identify the valid VMs and add them to a dictionary {name: file}
            vm_dir = str(Path.home() / "Documents" / "Virtual Machines")
            files = [(root, file) for root, _, flist in os.walk(vm_dir)
                     for file in flist if file.endswith(".vmx")]
            vm_dict = {
                file.rsplit('.', 1)[0]: os.path.join(root, file)
                for root, file in files
            }
            self._vm_file = vm_dict[self._name]
        # else self._type == "VirtualBox":
        #     self.vm =

    # Method to boot up the VM
    def boot_vm(self):
        self.vm = self.host.open_vm(self._vm_file)

        if self._snapshot != None:
            snapshot = self.vm.snapshot_get_named(self._snapshot)
            self.vm.snapshot_revert(snapshot=snapshot)

        self.vm.power_on()
        time.sleep(self._boot_time)

    # Method to send the necessary files over to reptilian, make the project, then reboot
    def make_vm(self, submission):
        if submission[0] == '.':
            # Split off . if it exists
            submission = submission[1:]
        if submission[0] == '\\':
            # Split off \ if it exists
            submission = submission[1:]

        # Current project directory
        submission_dir = os.path.join(self._payload_dir, submission)

        target = os.path.basename(submission)

        # Boot the VM
        self.boot_vm()

        # Connect to the remote server
        print("Connecting via SSH...")
        ssh = self.loop_for_shell()
        if not ssh:
            print("Error setting up SSH connection! Exiting...")
            self.dirty_shutdown()
            return

        # Run the staging phase.
        print("Setting the stage for the payload...")
        self.run_staging(ssh, target)

        # Run the build phase.
        print("Beginning build cycle...")
        self.run_build(ssh, target, submission_dir)

        print("Rebooting post build...")
        ssh.exec_command(self._remote_staging_dir + "/" + 'reboot.sh',
                         timeout=120000)
        time.sleep(self._boot_time)
        ssh.close()

        # Return the location of the staging and build error logs to populate the errors into error.log
        return (self._result_dir + STAGING_LOG + ERR_LOG,
                self._result_dir + BUILD_LOG + ERR_LOG)

    # Method to run tests, then shut down the VM once completed
    def run_tests(self, submission):
        # Run the test phase.
        print("Beginning test cycle...")

        target = os.path.basename(submission)

        ssh = self.loop_for_shell()
        if not ssh:
            print("Error setting up SSH connection! Exiting...")
            self.dirty_shutdown()
            return

        # Limit to 2 min test
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(
            self._remote_staging_dir + "/" + 'run.sh', timeout=120000)
        time.sleep(2)
        gen_errors = ssh_stderr.readlines()
        print("Tests complete. Fetching results.")
        sftp = ssh.open_sftp()
        for filename in self._result_files:
            print("Getting " + filename + "...")
            try:
                sftp.get(self._remote_result_dir + "/" + filename,
                         self._result_dir + "/" + target + "/" + filename)
            except:
                print("Error: could not grab " + filename + " for target " +
                      target + ". Skipping.")

        sftp.close()
        self.write_to_file(
            gen_errors,
            self._result_dir + "/" + target + "/" + RUN_LOG + ERR_LOG)

        ssh.close()

        # Shut down the VM for this target
        print("Shutting down VM...")
        self.dirty_shutdown()

        run_log = self._result_dir + "/" + target + "/" + RUN_LOG
        # Return the file location of the run.log file
        return run_log

    def loop_for_shell(self):
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        connected = False
        failures = 0

        while not (connected) and failures < MAX_RETRIES:
            try:
                ssh.connect(self._ip,
                            username=self._user,
                            password=self._passwd,
                            port=self._port)
                connected = True
            except (socket.timeout,
                    paramiko.ssh_exception.SSHException) as err:
                print("Error connecting. Retrying...")
                failures = failures + 1

        if not (connected):
            print("Could not connect to guest: retries exceeded.")
            return None
        return ssh

    def dirty_shutdown(self):
        self.vm.power_off(from_guest=False)
        time.sleep(5)

    def graceful_shutdown(self):
        # Make sure we are connected with ADB
        # TODO - ADB is broken, investigate
        subprocess.call([ADB, "connect", self._name])
        # Send the shutdown signal via ADB and wait for the machine to finish
        subprocess.call([ADB, "shell", "su -c 'svc power shutdown'"])
        time.sleep(VM_SLEEP_TIME)
        self.vm.power_off(from_guest=True)

    def write_to_file(self, lines, filename):
        with open(filename, 'w+') as file:
            for line in lines:
                file.write(line + "\n")
            file.close()

    def run_staging(self, ssh, target):
        # Make sure the directory is there for staging.
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(
            "mkdir " + self._remote_staging_dir)
        build_errors = ssh_stderr.readlines()

        # Push files via SFTP.
        sftp = ssh.open_sftp()

        for filename in self._staging_files:
            if not os.path.isfile(self._staging_dir + "/" + filename):
                print("Error - file " + filename +
                      " does not exist in staging area. Skipping.")
                continue

            print("Pushing " + filename + "...")
            try:
                sftp.put(self._staging_dir + "/" + filename,
                         self._remote_staging_dir + "/" + filename)
            except:
                print("Error: could not upload " + filename + ".")
                self.dirty_shutdown()

        sftp.close()

        # Run the staging script.
        print("Running staging script...")
        time.sleep(2)

        # Make the script executable
        build_errors.extend(ssh_stderr.readlines())
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(
            "chmod +x " + self._remote_staging_dir + "/" + self._stage_cmd)
        time.sleep(2)

        # Run the staging script
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(
            self._remote_staging_dir + "/" + self._stage_cmd)
        build_errors.extend(ssh_stderr.readlines())
        self.write_to_file(
            build_errors,
            self._result_dir + "/" + target + "/" + STAGING_LOG + ERR_LOG)

    def run_build(self, ssh, target, target_dir):
        # Push files via SFTP.
        sftp = ssh.open_sftp()

        for filename in self._payload_files:
            if not os.path.isfile(target_dir + "/" + filename):
                print("Error - file " + filename +
                      " does not exist in payload area for target " + target +
                      ". Skipping.")
                continue

            print("Pushing " + filename + "...")
            try:
                sftp.put(target_dir + "/" + filename,
                         self._remote_payload_dir + "/" + filename)
            except:
                print("Error: could not upload " + filename + ".")

        # Run the build script; power down when completed.
        print("Executing build....")
        # Limit to 20 min build
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(
            "chmod +x " + self._remote_staging_dir + "/" + self._build_cmd)
        time.sleep(2)
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(
            self._remote_staging_dir + "/" + self._build_cmd, timeout=1200000)
        time.sleep(2)
        build_errors = ssh_stderr.readlines()

        print("Build complete. Fetching logs.")
        sftp.get(self._remote_result_dir + "/" + BUILD_LOG,
                 self._result_dir + "/" + target + "/" + BUILD_LOG)
        sftp.close()
        self.write_to_file(
            build_errors,
            self._result_dir + "/" + target + "/" + BUILD_LOG + ERR_LOG)