Esempio n. 1
0
    def _test_get_python_dir(self, mock_cbinit_dir, mock_join, python_dir):
        mock_cbinit_dir.return_value = "fake dir"
        mock_join.return_value = mock.sentinel

        mock_stdout = mock.Mock()
        if python_dir:
            mock_stdout.splitlines.return_value = [
                "fake_line", "pYthOn27", "python"
            ]
        else:
            mock_stdout.splitlines.return_value = [
                "fake_line", "fake_line 2", "fake line 3"
            ]

        mock_strip = mock.Mock()
        mock_strip.strip.return_value = mock_stdout

        execute_function = mock.Mock()
        execute_function.return_value = mock_strip

        result = windows.get_python_dir(execute_function)

        mock_cbinit_dir.assert_called_once_with(execute_function)
        mock_strip.strip.assert_called_once_with()
        command = 'dir "{}" /b'.format(mock_cbinit_dir.return_value)
        execute_function.assert_called_with(command, command_type=util.CMD)
        if python_dir:
            mock_join.assert_called_once_with(mock_cbinit_dir.return_value,
                                              "pYthOn27")
            self.assertEqual(result, mock_join.return_value)
        else:
            self.assertEqual(mock_join.call_count, 0)
            self.assertEqual(result, None)
Esempio n. 2
0
    def pre_sysprep(self):
        """Disable first_logon_behaviour for testing purposes.

        Because first_logon_behaviour will control how the password
        should work on next logon, we could have troubles in tests,
        so this is always disabled, excepting tests which sets
        it manual to whatever they want.
        """
        introspection.set_config_option(
            option="first_logon_behaviour", value="no",
            execute_function=self._execute)

        # Patch the installation of cloudbaseinit in order to create
        # a file when the execution ends. We're doing this instead of
        # monitoring the service, because on some OSes, just checking
        # if the service is stopped leads to errors, due to the
        # fact that the service starts later on.
        python_dir = introspection.get_python_dir(self._execute)
        cbinit = ntpath.join(python_dir, 'Lib', 'site-packages',
                             'cloudbaseinit')

        # Get the shell patching script and patch the installation.
        resource_location = "windows/patch_shell.ps1"
        params = r' "{}"'.format(cbinit)
        self._backend.remote_client.manager.execute_powershell_resource_script(
            resource_location=resource_location, parameters=params)

        # Prepare Something specific for the OS
        self._backend.remote_client.manager.specific_prepare()
    def _test_get_python_dir(self, mock_cbinit_dir, mock_join, python_dir):
        mock_cbinit_dir.return_value = "fake dir"
        mock_join.return_value = mock.sentinel

        mock_stdout = mock.Mock()
        if python_dir:
            mock_stdout.splitlines.return_value = [
                "fake_line", "pYthOn27", "python"]
        else:
            mock_stdout.splitlines.return_value = [
                "fake_line", "fake_line 2", "fake line 3"]

        mock_strip = mock.Mock()
        mock_strip.strip.return_value = mock_stdout

        execute_function = mock.Mock()
        execute_function.return_value = mock_strip

        result = windows.get_python_dir(execute_function)

        mock_cbinit_dir.assert_called_once_with(execute_function)
        mock_strip.strip.assert_called_once_with()
        command = 'dir "{}" /b'.format(mock_cbinit_dir.return_value)
        execute_function.assert_called_with(command, command_type=util.CMD)
        if python_dir:
            mock_join.assert_called_once_with(mock_cbinit_dir.return_value,
                                              "pYthOn27")
            self.assertEqual(result, mock_join.return_value)
        else:
            self.assertEqual(mock_join.call_count, 0)
            self.assertEqual(result, None)
Esempio n. 4
0
    def pre_sysprep(self):
        """Disable first_logon_behaviour for testing purposes.

        Because first_logon_behaviour will control how the password
        should work on next logon, we could have troubles in tests,
        so this is always disabled, excepting tests which sets
        it manual to whatever they want.
        """
        introspection.set_config_option(
            option="first_logon_behaviour", value="no",
            execute_function=self._execute)

        # Patch the installation of cloudbaseinit in order to create
        # a file when the execution ends. We're doing this instead of
        # monitoring the service, because on some OSes, just checking
        # if the service is stopped leads to errors, due to the
        # fact that the service starts later on.
        python_dir = introspection.get_python_dir(self._execute)
        cbinit = ntpath.join(python_dir, 'Lib', 'site-packages',
                             'cloudbaseinit')

        # Get the shell patching script and patch the installation.
        cmd = ("powershell Invoke-Webrequest -uri "
               "{}/windows/patch_shell.ps1 -outfile "
               "C:\\patch_shell.ps1"
               .format(self._conf.argus.resources))
        self._execute(cmd)

        escaped = introspection.escape_path(cbinit)
        self._execute('powershell "C:\\\\patch_shell.ps1 \"{}\""'
                      .format(escaped))
Esempio n. 5
0
    def replace_code(self):
        """Replace the code of Cloudbase-Init."""
        if not CONFIG.argus.git_command:
            # Nothing to replace.
            return

        LOG.info("Replacing Cloudbase-Init's code "
                 "with %s", CONFIG.argus.git_command)

        LOG.debug("Getting Cloudbase-Init location...")
        # Get Cloudbase-Init python location.
        python_dir = introspection.get_python_dir(self._execute)

        # Remove everything from the Cloudbase-Init installation.
        LOG.debug("Recursively removing Cloudbase-Init...")
        cloudbaseinit = ntpath.join(python_dir, "Lib", "site-packages",
                                    "cloudbaseinit")
        self._execute('rmdir "{}" /S /q'.format(cloudbaseinit),
                      command_type=util.CMD)

        # Clone the repository
        clone_res = self._backend.remote_client.manager.git_clone(
            repo_url=_CBINIT_REPO, location=_CBINIT_TARGET_LOCATION)
        if not clone_res:
            raise exceptions.ArgusError('Code repository could not '
                                        'be cloned.')
        # Run the command provided at cli.
        LOG.debug("Applying cli patch...")
        self._execute("cd {location} && {command}".format(
            location=_CBINIT_TARGET_LOCATION,
            command=CONFIG.argus.git_command),
                      command_type=util.CMD)

        # Replace the code, by moving the code from Cloudbase-Init
        # to the installed location.
        LOG.debug("Replacing code...")
        self._execute('Copy-Item {location}\\cloudbaseinit \'{folder}\''
                      '-Recurse'.format(location=_CBINIT_TARGET_LOCATION,
                                        folder=cloudbaseinit),
                      command_type=util.POWERSHELL)

        # Auto-install packages from the new requirements.txt
        python = ntpath.join(python_dir, "python.exe")
        command = '"{folder}" -m pip install -r {location}\\requirements.txt'
        self._execute(command.format(folder=python,
                                     location=_CBINIT_TARGET_LOCATION),
                      command_type=util.CMD)

        command = '"{folder}" -m pip install {location}'
        self._execute(command.format(folder=python,
                                     location=_CBINIT_TARGET_LOCATION),
                      command_type=util.CMD)
    def replace_code(self):
        """Replace the code of Cloudbase-Init."""
        if not CONFIG.argus.git_command:
            # Nothing to replace.
            return

        LOG.info("Replacing Cloudbase-Init's code "
                 "with %s", CONFIG.argus.git_command)

        LOG.debug("Getting Cloudbase-Init location...")
        # Get Cloudbase-Init python location.
        python_dir = introspection.get_python_dir(self._execute)

        # Remove everything from the Cloudbase-Init installation.
        LOG.debug("Recursively removing Cloudbase-Init...")
        cloudbaseinit = ntpath.join(
            python_dir,
            "Lib",
            "site-packages",
            "cloudbaseinit")
        self._execute('rmdir "{}" /S /q'.format(cloudbaseinit),
                      command_type=util.CMD)

        # Clone the repository
        clone_res = self._backend.remote_client.manager.git_clone(
            repo_url=_CBINIT_REPO, location=_CBINIT_TARGET_LOCATION)
        if not clone_res:
            raise exceptions.ArgusError('Code repository could not '
                                        'be cloned.')
        # Run the command provided at cli.
        LOG.debug("Applying cli patch...")
        self._execute("cd {location} && {command}".format(
            location=_CBINIT_TARGET_LOCATION,
            command=CONFIG.argus.git_command), command_type=util.CMD)

        # Replace the code, by moving the code from Cloudbase-Init
        # to the installed location.
        LOG.debug("Replacing code...")
        self._execute('Copy-Item {location}\\cloudbaseinit \'{folder}\''
                      '-Recurse'.format(location=_CBINIT_TARGET_LOCATION,
                                        folder=cloudbaseinit),
                      command_type=util.POWERSHELL)

        # Auto-install packages from the new requirements.txt
        python = ntpath.join(python_dir, "python.exe")
        command = '"{folder}" -m pip install -r {location}\\requirements.txt'
        self._execute(command.format(folder=python,
                                     location=_CBINIT_TARGET_LOCATION),
                      command_type=util.CMD)
Esempio n. 7
0
    def pre_sysprep(self):
        # Patch the installation of Cloudbase-Init in order to create
        # a file when the execution ends. We're doing this instead of
        # monitoring the service, because on some OSes, just checking
        # if the service is stopped leads to errors, due to the
        # fact that the service starts later on.
        python_dir = introspection.get_python_dir(self._execute)
        cbinit = ntpath.join(python_dir, 'Lib', 'site-packages',
                             'cloudbaseinit')

        # Get the shell patching script and patch the installation.
        resource_location = "windows/patch_shell.ps1"
        params = r' "{}"'.format(cbinit)
        self._backend.remote_client.manager.execute_powershell_resource_script(
            resource_location=resource_location, parameters=params)
    def pre_sysprep(self):
        # Patch the installation of Cloudbase-Init in order to create
        # a file when the execution ends. We're doing this instead of
        # monitoring the service, because on some OSes, just checking
        # if the service is stopped leads to errors, due to the
        # fact that the service starts later on.
        python_dir = introspection.get_python_dir(self._execute)
        cbinit = ntpath.join(python_dir, 'Lib', 'site-packages',
                             'cloudbaseinit')

        # Get the shell patching script and patch the installation.
        resource_location = "windows/patch_shell.ps1"
        params = r' "{}"'.format(cbinit)
        self._backend.remote_client.manager.execute_powershell_resource_script(
            resource_location=resource_location, parameters=params)
Esempio n. 9
0
    def get_agent_command(self, agent_action,
                          agent_path=None, **kwargs):
        """Command builder for the Argus utilitary agent.

        Returns a command string formed by the given action and its
        required arguments.
        """
        agent_path = agent_path or self._ARGUS_AGENT_SCRIPT
        python_dir = introspection.get_python_dir(self._execute)
        cmd = (r'& "{pydir}\python.exe" {agent_path} --{agent_action} '
               ' "{source}" "{location}"'.format(
                   pydir=python_dir, agent_path=agent_path,
                   agent_action=agent_action,
                   source=kwargs.get('source', ''),
                   location=kwargs.get('location', '')))
        return cmd
Esempio n. 10
0
    def get_agent_command(self, agent_action,
                          agent_path=None, **kwargs):
        """Command builder for the argus utilitary agent.

        Returns a command string formed by the given action and its
        required arguments.
        """
        agent_path = agent_path or self._ARGUS_AGENT_SCRIPT
        python_dir = introspection.get_python_dir(self._execute)
        cmd = (r'& "{pydir}\python.exe" {agent_path} --{agent_action} '
               ' "{source}" "{location}"'.format(
                   pydir=python_dir, agent_path=agent_path,
                   agent_action=agent_action,
                   source=kwargs.get('source', ''),
                   location=kwargs.get('location', '')))
        return cmd
Esempio n. 11
0
    def pre_sysprep(self):
        super(CloudbaseinitCloudstackRecipe, self).pre_sysprep()

        python_dir = introspection.get_python_dir(self._execute)
        cbinit = ntpath.join(python_dir, 'Lib', 'site-packages',
                             'cloudbaseinit')

        # Install mock
        python = ntpath.join(python_dir, "python.exe")
        command = '"{}" -m pip install mock'
        self._execute(command.format(python), command_type=util.CMD)

        # Get the cloudstack patching script and patch the installation.
        resource_location = "windows/patch_cloudstack.ps1"
        params = r'"{}"'.format(cbinit)
        self._backend.remote_client.manager.execute_powershell_resource_script(
            resource_location=resource_location, parameters=params)
Esempio n. 12
0
    def replace_code(self):
        """Replace the code of cloudbaseinit."""
        if not self._conf.argus.git_command:
            # Nothing to replace.
            return

        LOG.info("Replacing cloudbaseinit's code...")

        LOG.info("Getting cloudbase-init location...")
        # Get cb-init python location.
        python_dir = introspection.get_python_dir(self._execute)

        # Remove everything from the cloudbaseinit installation.
        LOG.info("Removing recursively cloudbaseinit...")
        cloudbaseinit = ntpath.join(
            python_dir,
            "Lib",
            "site-packages",
            "cloudbaseinit")
        self._execute('rmdir "{}" /S /q'.format(cloudbaseinit),
                      command_type=util.CMD)

        # Clone the repo
        LOG.info("Cloning the cloudbaseinit repo...")
        self._backend.remote_client.manager.git_clone(
            repo_url=_CBINIT_REPO,
            location=r"C:\cloudbaseinit")

        # Run the command provided at cli.
        LOG.info("Applying cli patch...")
        self._execute("cd C:\\cloudbaseinit && {}".format(
            self._conf.argus.git_command), command_type=util.CMD)

        # Replace the code, by moving the code from cloudbaseinit
        # to the installed location.
        LOG.info("Replacing code...")
        self._execute('Copy-Item C:\\cloudbaseinit\\cloudbaseinit '
                      '\'{}\' -Recurse'.format(cloudbaseinit),
                      command_type=util.POWERSHELL)

        # Autoinstall packages from the new requirements.txt
        python = ntpath.join(python_dir, "python.exe")
        command = '"{}" -m pip install -r C:\\cloudbaseinit\\requirements.txt'
        self._execute(command.format(python), command_type=util.CMD)
Esempio n. 13
0
    def replace_code(self):
        """Replace the code of cloudbaseinit."""
        if not self._conf.argus.git_command:
            # Nothing to replace.
            return

        LOG.info("Replacing cloudbaseinit's code...")

        LOG.info("Getting cloudbase-init location...")
        # Get cb-init python location.
        python_dir = introspection.get_python_dir(self._execute)

        # Remove everything from the cloudbaseinit installation.
        LOG.info("Removing recursively cloudbaseinit...")
        cloudbaseinit = ntpath.join(
            python_dir,
            "Lib",
            "site-packages",
            "cloudbaseinit")
        self._execute('rmdir "{}" /S /q'.format(cloudbaseinit))

        # Clone the repo
        LOG.info("Cloning the cloudbaseinit repo...")
        self._execute("git clone https://github.com/stackforge/"
                      "cloudbase-init C:\\cloudbaseinit")

        # Run the command provided at cli.
        LOG.info("Applying cli patch...")
        self._execute("cd C:\\cloudbaseinit && {}".format(
            self._conf.argus.git_command))

        # Replace the code, by moving the code from cloudbaseinit
        # to the installed location.
        LOG.info("Replacing code...")
        self._execute('powershell "Copy-Item C:\\cloudbaseinit\\cloudbaseinit '
                      '\'{}\' -Recurse"'.format(cloudbaseinit))

        # Autoinstall packages from the new requirements.txt
        python = ntpath.join(python_dir, "python.exe")
        command = '"{}" -m pip install -r C:\\cloudbaseinit\\requirements.txt'
        self._execute(command.format(python))
Esempio n. 14
0
    def check_cbinit_installation(self):
        """Check if Cloudbase-Init was installed successfully."""
        LOG.info("Checking Cloudbase-Init installation.")

        try:
            python_dir = introspection.get_python_dir(self._execute)
        except exceptions.ArgusError as exc:
            LOG.warning("Could not check Cloudbase-Init installation: %s", exc)
            return False

        check_cmd = r'& "{}\python.exe" -c "import cloudbaseinit"'.format(
            python_dir)
        try:
            self._client.run_remote_cmd(
                cmd=check_cmd, command_type=util.POWERSHELL)
        except exceptions.ArgusError as exc:
            LOG.debug("Cloudbase-Init installation failed: %s", exc)
            return False

        LOG.info("Cloudbase-Init was successfully installed!")
        return True
Esempio n. 15
0
    def check_cbinit_installation(self):
        """Check if Cloudbase-Init was installed successfully."""
        LOG.info("Checking Cloudbase-Init installation.")

        try:
            python_dir = introspection.get_python_dir(self._execute)
        except exceptions.ArgusError as exc:
            LOG.warning("Could not check Cloudbase-Init installation: %s", exc)
            return False

        check_cmd = r'& "{}\python.exe" -c "import cloudbaseinit"'.format(
            python_dir)
        try:
            self._client.run_remote_cmd(cmd=check_cmd,
                                        command_type=util.POWERSHELL)
        except exceptions.ArgusError as exc:
            LOG.debug("Cloudbase-Init installation failed: %s", exc)
            return False

        LOG.info("Cloudbase-Init was successfully installed!")
        return True
Esempio n. 16
0
    def pre_sysprep(self):
        super(CloudbaseinitCloudstackRecipe, self).pre_sysprep()

        python_dir = introspection.get_python_dir(self._execute)
        cbinit = ntpath.join(python_dir, 'Lib', 'site-packages',
                             'cloudbaseinit')

        # Install mock
        python = ntpath.join(python_dir, "python.exe")
        command = '"{}" -m pip install mock'
        self._execute(command.format(python))

        # Get the cloudstack patching script and patch the installation.
        cmd = ("powershell Invoke-Webrequest -uri "
               "{}/windows/patch_cloudstack.ps1 -outfile "
               "C:\\patch_cloudstack.ps1"
               .format(self._conf.argus.resources))
        self._execute(cmd)

        escaped = introspection.escape_path(cbinit)
        self._execute('powershell "C:\\\\patch_cloudstack.ps1 \"{}\""'
                      .format(escaped))