示例#1
0
    def download_template_scripts(self,
                                  template_name,
                                  revision,
                                  force_overwrite=False):
        """Download all scripts of a template to local scripts folder.

        :param str template_name:
        "param str revision:
        :param bool force_overwrite: if True, will download the script even if
            it already exists.
        """
        for script_file in ScriptFile:
            remote_script_url = \
                self._get_remote_script_url(template_name, revision,
                                            script_file)

            local_script_filepath = ltm.get_script_filepath(
                template_name, revision, script_file)
            download_file(url=remote_script_url,
                          filepath=local_script_filepath,
                          force_overwrite=force_overwrite,
                          logger=self.logger,
                          msg_update_callback=self.msg_update_callback)

            # Set Read,Write permission only for the owner
            if os.name != 'nt':
                os.chmod(local_script_filepath, stat.S_IRUSR | stat.S_IWUSR)
示例#2
0
    def _get_init_script(self):
        """Read the initialization script from disk to create temp vApp.

        :return: content of the initialization script.

        :rtype: str
        """
        init_script_filepath = ltm.get_script_filepath(
            self.template_name, self.template_revision, ScriptFile.INIT)
        init_script = read_data_file(
            init_script_filepath, logger=self.logger,
            msg_update_callback=self.msg_update_callback)
        if self.ssh_key is not None:
            init_script += \
                f"mkdir -p /root/.ssh\n" \
                f"echo '{self.ssh_key}' >> /root/.ssh/authorized_keys\n" \
                f"chmod -R go-rwx /root/.ssh"
        return init_script
    def _customize_vm(self, vapp, vm_name):
        """Customize a vm in a VApp using customization script.

        :param pyvcloud.vcd.vapp.VApp vapp:
        :param str vm_name:

        :raises Exception: if unable to execute the customization script in
            the vm.
        """
        msg = f"Customizing vApp '{self.temp_vapp_name}', vm '{vm_name}'"
        self.msg_update_callback.general(msg)
        self.logger.info(msg)

        cust_script_filepath = ltm.get_script_filepath(self.template_name,
                                                       self.template_revision,
                                                       ScriptFile.CUST)
        cust_script = read_data_file(
            cust_script_filepath,
            logger=self.logger,
            msg_update_callback=self.msg_update_callback)

        vs = get_vsphere(self.sys_admin_client,
                         vapp,
                         vm_name,
                         logger=self.logger)
        callback = vgr_callback(
            prepend_msg='Waiting for guest tools, status: "',
            logger=self.logger,
            msg_update_callback=self.msg_update_callback)
        wait_until_tools_ready(vapp, vm_name, vs, callback=callback)
        password_auto = vapp.get_admin_password(vm_name)

        try:
            result = vs.execute_script_in_guest(
                vs.get_vm_by_moid(vapp.get_vm_moid(vm_name)),
                'root',
                password_auto,
                cust_script,
                target_file=None,
                wait_for_completion=True,
                wait_time=10,
                get_output=True,
                delete_script=True,
                callback=vgr_callback(
                    logger=self.logger,
                    msg_update_callback=self.msg_update_callback))
        except Exception as err:
            # TODO() replace raw exception with specific exception
            # unsure all errors execute_script_in_guest can result in
            # Docker TLS handshake timeout can occur when internet is slow
            self.msg_update_callback.error(
                "Failed VM customization. Check CSE install log")
            self.logger.error(f"Failed VM customization with error: {err}",
                              exc_info=True)
            raise

        if len(result) > 0:
            msg = f'Result: {result}'
            self.msg_update_callback.general_no_color(msg)
            self.logger.debug(msg)

            result_stdout = result[1].content.decode()
            result_stderr = result[2].content.decode()

            msg = 'stderr:'
            self.msg_update_callback.general_no_color(msg)
            self.logger.debug(msg)
            if len(result_stderr) > 0:
                self.msg_update_callback.general_no_color(result_stderr)
                self.logger.debug(result_stderr)

            msg = 'stdout:'
            self.msg_update_callback.general_no_color(msg)
            self.logger.debug(msg)
            if len(result_stdout) > 0:
                self.msg_update_callback.general_no_color(result_stdout)
                self.logger.debug(result_stdout)

        if len(result) == 0 or result[0] != 0:
            msg = "Failed VM customization"
            self.msg_update_callback.error(f"{msg}. Please check logs.")
            self.logger.error(
                f"{msg}\nResult start===\n{result}\n===Result end",
                exc_info=True)
            # TODO: replace raw exception with specific exception
            raise Exception(f"{msg}; Result: {result}")

        # Do not reboot VM after customization. Reboot will generate a new
        # machine-id, and once we capture the VM, all VMs deployed from the
        # template will have the same machine-id, which can lead to
        # unpredictable behavior

        msg = f"Customized vApp '{self.temp_vapp_name}', vm '{vm_name}'"
        self.msg_update_callback.general(msg)
        self.logger.info(msg)