예제 #1
0
 def test_env_to_file_nt(self):
     file_path = utils.env_to_file({'key': 'value', 'key2': 'value2'},
                                   posix=False)
     with open(file_path) as f:
         content = f.read()
     self.assertIn('set key=value', content)
     self.assertIn('set key2=value2', content)
예제 #2
0
 def test_env_to_file_nt(self):
     file_path = utils.env_to_file({'key': 'value', 'key2': 'value2'},
                                   posix=False)
     with open(file_path) as f:
         content = f.read()
     self.assertIn('set key=value', content)
     self.assertIn('set key2=value2', content)
예제 #3
0
    def run(self,
            command,
            raise_on_failure=True,
            execution_env=None,
            powershell=False):
        """
        :param command: The command to execute.
        :param raise_on_failure: by default, this will raise an exception
                                 if the command fails. You can use
                                 raise_on_failure=False to just log the
                                 error and not raise an exception.
        :param execution_env: environment variables to be applied before
                              running the command
        :param powershell: Determines where to run command as a powershell
                           script.

        :return a response object with information about the execution
        :rtype WinRMCommandExecutionResponse.
        """

        if execution_env is None:
            execution_env = {}

        remote_env_file = None
        if execution_env:
            env_file = utils.env_to_file(execution_env, posix=False)
            remote_env_file = self.put_file(src=env_file,
                                            dst='{0}.bat'.format(
                                                self.mktemp()))

        def _chk(res):
            if res.status_code == 0:
                return WinRMCommandExecutionResponse(
                    command=command,
                    std_err=res.std_err,
                    std_out=res.std_out,
                    return_code=res.status_code)
            else:
                error = WinRMCommandExecutionException(command=command,
                                                       code=res.status_code,
                                                       error=res.std_err,
                                                       output=res.std_out)
                if raise_on_failure:
                    raise error
                self.logger.error(error)

        self.logger.debug('[{0}] run: {1}'.format(self.session_config['host'],
                                                  command))

        if remote_env_file:
            command = 'call {0} & {1}'.format(remote_env_file, command)
        try:
            if powershell:
                response = self.session.run_ps(command)
            else:
                response = self.session.run_cmd(command)
        except BaseException as e:
            raise WinRMCommandExecutionError(command=command, error=str(e))
        return _chk(response)
    def run(self, command, raise_on_failure=True, execution_env=None):

        """
        :param command: The command to execute.
        :param raise_on_failure: by default, this will raise an exception
                                 if the command fails. You can use
                                 raise_on_failure=False to just log the
                                 error and not raise an exception.
        :param execution_env: environment variables to be applied before
                              running the command

        :return a response object with information about the execution
        :rtype WinRMCommandExecutionResponse.
        """

        if execution_env is None:
            execution_env = {}

        remote_env_file = None
        if execution_env:
            env_file = utils.env_to_file(execution_env, posix=False)
            remote_env_file = self.put_file(src=env_file,
                                            dst='{0}.bat'.format(
                                                self.mktemp()))

        def _chk(res):
            if res.status_code == 0:
                return WinRMCommandExecutionResponse(
                    command=command,
                    std_err=res.std_err,
                    std_out=res.std_out,
                    return_code=res.status_code)
            else:
                error = WinRMCommandExecutionException(
                    command=command,
                    code=res.status_code,
                    error=res.std_err,
                    output=res.std_out)
                if raise_on_failure:
                    raise error
                self.logger.error(error)

        self.logger.debug(
            '[{0}] run: {1}'.format(
                self.session_config['host'],
                command))

        if remote_env_file:
            command = 'call {0} & {1}'.format(remote_env_file, command)
        try:
            response = self.session.run_cmd(command)
        except BaseException as e:
            raise WinRMCommandExecutionError(
                command=command,
                error=str(e)
            )
        return _chk(response)
예제 #5
0
def test_env_to_file_nt():
    file_path = utils.env_to_file({
        'key': 'value',
        'key2': 'value2'
    },
                                  posix=False)
    with open(file_path) as f:
        content = f.read()
    assert 'set key=value' in content
    assert 'set key2=value2' in content
예제 #6
0
 def test_env_to_file(self):
     file_path = utils.env_to_file({'key': 'value', 'key2': 'value2'})
     with open(file_path) as f:
         content = f.read()
     self.assertIn('export key=value', content)
     self.assertIn('export key2=value2', content)
예제 #7
0
def test_env_to_file():
    file_path = utils.env_to_file({'key': 'value', 'key2': 'value2'})
    with open(file_path) as f:
        content = f.read()
    assert 'export key=value' in content
    assert 'export key2=value2' in content
예제 #8
0
 def test_env_to_file(self):
     file_path = utils.env_to_file({'key': 'value', 'key2': 'value2'})
     with open(file_path) as f:
         content = f.read()
     self.assertIn('export key=value', content)
     self.assertIn('export key2=value2', content)