当前位置: 首页>>代码示例>>Python>>正文


Python shell.run_command函数代码示例

本文整理汇总了Python中st2common.util.green.shell.run_command函数的典型用法代码示例。如果您正苦于以下问题:Python run_command函数的具体用法?Python run_command怎么用?Python run_command使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了run_command函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: setUpClass

    def setUpClass(cls):
        super(SensorContainerTestCase, cls).setUpClass()

        st2tests.config.parse_args()

        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(
            cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
            username=username, password=password, ensure_indexes=False)

        # NOTE: We need to perform this patching because test fixtures are located outside of the
        # packs base paths directory. This will never happen outside the context of test fixtures.
        cfg.CONF.content.packs_base_paths = PACKS_BASE_PATH

        # Register sensors
        register_sensors(packs_base_paths=[PACKS_BASE_PATH], use_pack_cache=False)

        # Create virtualenv for examples pack
        virtualenv_path = '/tmp/virtualenvs/examples'

        run_command(cmd=['rm', '-rf', virtualenv_path])

        cmd = ['virtualenv', '--system-site-packages', '--python', PYTHON_BINARY, virtualenv_path]
        run_command(cmd=cmd)
开发者ID:StackStorm,项目名称:st2,代码行数:25,代码来源:test_sensor_container.py

示例2: _upload_file

    def _upload_file(self, local_path, base_path):
        """
        Upload provided file to the remote server in a temporary directory.

        :param local_path: Local path to the file to upload.
        :type local_path: ``str``

        :param base_path: Absolute base path for the share.
        :type base_path: ``str``
        """
        file_name = os.path.basename(local_path)

        temporary_directory_name = str(uuid.uuid4())
        command = 'mkdir %s' % (quote_windows(temporary_directory_name))

        # 1. Create a temporary dir for out scripts (ignore errors if it already exists)
        # Note: We don't necessary have access to $TEMP so we create a temporary directory for our
        # us in the root of the share we are using and have access to
        args = self._get_smbclient_command_args(host=self._host, username=self._username,
                                                password=self._password, command=command,
                                                share=self._share)

        LOG.debug('Creating temp directory "%s"' % (temporary_directory_name))

        exit_code, stdout, stderr, timed_out = run_command(cmd=args, stdout=subprocess.PIPE,
                                                           stderr=subprocess.PIPE, shell=False,
                                                           timeout=CREATE_DIRECTORY_TIMEOUT)

        extra = {'exit_code': exit_code, 'stdout': stdout, 'stderr': stderr}
        LOG.debug('Directory created', extra=extra)

        # 2. Upload file to temporary directory
        remote_path = PATH_SEPARATOR.join([temporary_directory_name, file_name])

        values = {
            'local_path': quote_windows(local_path),
            'remote_path': quote_windows(remote_path)
        }
        command = 'put %(local_path)s %(remote_path)s' % values
        args = self._get_smbclient_command_args(host=self._host, username=self._username,
                                                password=self._password, command=command,
                                                share=self._share)

        extra = {'local_path': local_path, 'remote_path': remote_path}
        LOG.debug('Uploading file to "%s"' % (remote_path))

        exit_code, stdout, stderr, timed_out = run_command(cmd=args, stdout=subprocess.PIPE,
                                                           stderr=subprocess.PIPE, shell=False,
                                                           timeout=UPLOAD_FILE_TIMEOUT)

        extra = {'exit_code': exit_code, 'stdout': stdout, 'stderr': stderr}
        LOG.debug('File uploaded to "%s"' % (remote_path), extra=extra)

        full_remote_file_path = base_path + '\\' + remote_path
        full_temporary_directory_path = base_path + '\\' + temporary_directory_name

        return full_remote_file_path, full_temporary_directory_path
开发者ID:Pulsant,项目名称:st2,代码行数:57,代码来源:windows_script_runner.py

示例3: _run_cli_command

    def _run_cli_command(self, command):
        exit_code, stdout, stderr, timed_out = run_command(
            cmd=command, stdin=None,
            stdout=subprocess.PIPE, stderr=subprocess.PIPE,
            shell=True, timeout=self._timeout, kill_func=kill_process)

        error = None
        if timed_out:
            error = 'Action failed to complete in %s seconds' % self._timeout
            exit_code = -9

        succeeded = (exit_code == 0)
        result = {
            'failed': not succeeded,
            'succeeded': succeeded,
            'return_code': exit_code,
            'stdout': stdout,
            'stderr': stderr
        }
        if error:
            result['error'] = error

        status = LIVEACTION_STATUS_SUCCEEDED if succeeded else LIVEACTION_STATUS_FAILED
        self._log_action_completion(logger=LOG, result=result, status=status, exit_code=exit_code)
        return result, status
开发者ID:meirwah,项目名称:st2,代码行数:25,代码来源:cloudslang_runner.py

示例4: _get_share_absolute_path

    def _get_share_absolute_path(self, share):
        """
        Retrieve full absolute path for a share with the provided name.

        :param share: Share name.
        :type share: ``str``
        """
        command = 'net share %s' % (quote_windows(share))
        args = self._get_winexe_command_args(host=self._host, username=self._username,
                                             password=self._password,
                                             command=command)

        LOG.debug('Retrieving full absolute path for share "%s"' % (share))
        exit_code, stdout, stderr, timed_out = run_command(cmd=args, stdout=subprocess.PIPE,
                                                           stderr=subprocess.PIPE, shell=False,
                                                           timeout=self._timeout)

        if exit_code != 0:
            msg = 'Failed to retrieve absolute path for share "%s"' % (share)
            raise Exception(msg)

        share_info = self._parse_share_information(stdout=stdout)
        share_path = share_info.get('path', None)

        if not share_path:
            msg = 'Failed to retrieve absolute path for share "%s"' % (share)
            raise Exception(msg)

        return share_path
开发者ID:Pulsant,项目名称:st2,代码行数:29,代码来源:windows_script_runner.py

示例5: _run_script

    def _run_script(self, script_path, arguments=None):
        """
        :param script_path: Full path to the script on the remote server.
        :type script_path: ``str``

        :param arguments: The arguments to pass to the script.
        :type arguments: ``str``
        """
        if arguments is not None:
            command = '%s %s %s' % (POWERSHELL_COMMAND, quote_windows(script_path), arguments)
        else:
            command = '%s %s' % (POWERSHELL_COMMAND, quote_windows(script_path))
        args = self._get_winexe_command_args(host=self._host, username=self._username,
                                             password=self._password,
                                             command=command)

        LOG.debug('Running script "%s"' % (script_path))

        # Note: We don't send anything over stdin, we just create an unused pipe
        # to avoid some obscure failures
        exit_code, stdout, stderr, timed_out = run_command(cmd=args,
                                                           stdin=subprocess.PIPE,
                                                           stdout=subprocess.PIPE,
                                                           stderr=subprocess.PIPE,
                                                           shell=False,
                                                           timeout=self._timeout)

        extra = {'exit_code': exit_code, 'stdout': stdout, 'stderr': stderr}
        LOG.debug('Command returned', extra=extra)

        return exit_code, stdout, stderr, timed_out
开发者ID:Pulsant,项目名称:st2,代码行数:31,代码来源:windows_script_runner.py

示例6: setUpClass

    def setUpClass(cls):
        super(SensorContainerTestCase, cls).setUpClass()
        return

        st2tests.config.parse_args()

        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(
            cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
            username=username, password=password, ensure_indexes=False)

        # Register sensors
        register_sensors(packs_base_paths=['/opt/stackstorm/packs'], use_pack_cache=False)

        # Create virtualenv for examples pack
        virtualenv_path = '/opt/stackstorm/virtualenvs/examples'
        cmd = ['virtualenv', '--system-site-packages', virtualenv_path]
        run_command(cmd=cmd)
开发者ID:AlexeyDeyneko,项目名称:st2,代码行数:19,代码来源:test_sensor_container.py

示例7: _apply_pack_permissions

    def _apply_pack_permissions(self, pack_path):
        """
        Will recursively apply permission 770 to pack and its contents.
        """
        # 1. switch owner group to configured group
        pack_group = utils.get_pack_group()
        if pack_group:
            shell.run_command(['sudo', 'chgrp', '-R', pack_group, pack_path])

        # 2. Setup the right permissions and group ownership
        # These mask is same as mode = 775
        mode = stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH
        os.chmod(pack_path, mode)

        # Yuck! Since os.chmod does not support chmod -R walk manually.
        for root, dirs, files in os.walk(pack_path):
            for d in dirs:
                os.chmod(os.path.join(root, d), mode)
            for f in files:
                os.chmod(os.path.join(root, f), mode)
开发者ID:Pulsant,项目名称:st2,代码行数:20,代码来源:download.py

示例8: _delete_file

    def _delete_file(self, file_path):
        command = 'rm %(file_path)s' % {'file_path': quote_windows(file_path)}
        args = self._get_smbclient_command_args(host=self._host, username=self._username,
                                                password=self._password, command=command,
                                                share=self._share)

        exit_code, _, _, _ = run_command(cmd=args, stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE, shell=False,
                                         timeout=DELETE_FILE_TIMEOUT)

        return exit_code == 0
开发者ID:Pulsant,项目名称:st2,代码行数:11,代码来源:windows_script_runner.py

示例9: setUpClass

    def setUpClass(cls):
        super(SensorContainerTestCase, cls).setUpClass()

        st2tests.config.parse_args()

        username = cfg.CONF.database.username if hasattr(cfg.CONF.database, 'username') else None
        password = cfg.CONF.database.password if hasattr(cfg.CONF.database, 'password') else None
        cls.db_connection = db_setup(
            cfg.CONF.database.db_name, cfg.CONF.database.host, cfg.CONF.database.port,
            username=username, password=password, ensure_indexes=False)

        # Register sensors
        register_sensors(packs_base_paths=[PACKS_BASE_PATH], use_pack_cache=False)

        # Create virtualenv for examples pack
        virtualenv_path = '/tmp/virtualenvs/examples'

        run_command(cmd=['rm', '-rf', virtualenv_path])

        cmd = ['virtualenv', '--system-site-packages', '--python', PYTHON_BINARY, virtualenv_path]
        run_command(cmd=cmd)
开发者ID:lyandut,项目名称:st2,代码行数:21,代码来源:test_sensor_container.py

示例10: _delete_directory

    def _delete_directory(self, directory_path):
        command = 'rmdir %(directory_path)s' % {'directory_path': quote_windows(directory_path)}
        args = self._get_smbclient_command_args(host=self._host, username=self._username,
                                                password=self._password, command=command,
                                                share=self._share)

        LOG.debug('Removing directory "%s"' % (directory_path))
        exit_code, _, _, _ = run_command(cmd=args, stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE, shell=False,
                                         timeout=DELETE_DIRECTORY_TIMEOUT)

        return exit_code == 0
开发者ID:Pulsant,项目名称:st2,代码行数:12,代码来源:windows_script_runner.py

示例11: run

    def run(self, action_parameters):
        pack = self.get_pack_name()
        user = self.get_user()
        serialized_parameters = json.dumps(action_parameters) if action_parameters else ''
        virtualenv_path = get_sandbox_virtualenv_path(pack=pack)
        python_path = get_sandbox_python_binary_path(pack=pack)

        if virtualenv_path and not os.path.isdir(virtualenv_path):
            format_values = {'pack': pack, 'virtualenv_path': virtualenv_path}
            msg = PACK_VIRTUALENV_DOESNT_EXIST % format_values
            raise Exception(msg)

        if not self.entry_point:
            raise Exception('Action "%s" is missing entry_point attribute' % (self.action.name))

        args = [
            python_path,
            WRAPPER_SCRIPT_PATH,
            '--pack=%s' % (pack),
            '--file-path=%s' % (self.entry_point),
            '--parameters=%s' % (serialized_parameters),
            '--user=%s' % (user),
            '--parent-args=%s' % (json.dumps(sys.argv[1:]))
        ]

        # We need to ensure all the st2 dependencies are also available to the
        # subprocess
        env = os.environ.copy()
        env['PATH'] = get_sandbox_path(virtualenv_path=virtualenv_path)
        env['PYTHONPATH'] = get_sandbox_python_path(inherit_from_parent=True,
                                                    inherit_parent_virtualenv=True)

        # Include user provided environment variables (if any)
        user_env_vars = self._get_env_vars()
        env.update(user_env_vars)

        # Include common st2 environment variables
        st2_env_vars = self._get_common_action_env_variables()
        env.update(st2_env_vars)
        datastore_env_vars = self._get_datastore_access_env_vars()
        env.update(datastore_env_vars)

        exit_code, stdout, stderr, timed_out = run_command(cmd=args, stdout=subprocess.PIPE,
                                                           stderr=subprocess.PIPE, shell=False,
                                                           env=env, timeout=self._timeout)

        return self._get_output_values(exit_code, stdout, stderr, timed_out)
开发者ID:janjaapbos,项目名称:st2,代码行数:47,代码来源:pythonrunner.py

示例12: apply_pack_owner_group

def apply_pack_owner_group(pack_path):
    """
    Switch owner group of the pack / virtualenv directory to the configured
    group.

    NOTE: This requires sudo access.
    """
    pack_group = utils.get_pack_group()

    if pack_group:
        LOG.debug('Changing owner group of "%s" directory to %s' % (pack_path, pack_group))
        exit_code, _, stderr, _ = shell.run_command(['sudo', 'chgrp', '-R', pack_group, pack_path])

        if exit_code != 0:
            # Non fatal, but we still log it
            LOG.debug('Failed to change owner group on directory "%s" to "%s": %s' %
                      (pack_path, pack_group, stderr))

    return True
开发者ID:nzlosh,项目名称:st2,代码行数:19,代码来源:pack_management.py

示例13: run

    def run(self, action_parameters):
        # Make sure the dependencies are available
        self._verify_winexe_exists()

        args = self._get_winexe_command_args(host=self._host, username=self._username,
                                             password=self._password,
                                             command=self._command)

        # Note: We don't send anything over stdin, we just create an unused pipe
        # to avoid some obscure failures
        exit_code, stdout, stderr, timed_out = run_command(cmd=args,
                                                           stdin=subprocess.PIPE,
                                                           stdout=subprocess.PIPE,
                                                           stderr=subprocess.PIPE,
                                                           shell=False,
                                                           timeout=self._timeout)

        if timed_out:
            error = 'Action failed to complete in %s seconds' % (self._timeout)
        else:
            error = None

        if exit_code != 0:
            error = self._parse_winexe_error(stdout=stdout, stderr=stderr)

        result = stdout

        output = {
            'stdout': stdout,
            'stderr': stderr,
            'exit_code': exit_code,
            'result': result
        }

        if error:
            output['error'] = error

        status = LIVEACTION_STATUS_SUCCEEDED if exit_code == 0 else LIVEACTION_STATUS_FAILED
        self._log_action_completion(logger=LOG, result=output, status=status, exit_code=exit_code)
        return (status, output, None)
开发者ID:Kailashkatheth1,项目名称:st2,代码行数:40,代码来源:windows_command_runner.py

示例14: _run

    def _run(self, action):
        env_vars = self._env

        if not self.entry_point:
            script_action = False
        else:
            script_action = True

        args = action.get_full_command_string()
        sanitized_args = action.get_sanitized_full_command_string()

        # For consistency with the old Fabric based runner, make sure the file is executable
        if script_action:
            script_local_path_abs = self.entry_point
            args = 'chmod +x %s ; %s' % (script_local_path_abs, args)
            sanitized_args = 'chmod +x %s ; %s' % (script_local_path_abs, sanitized_args)

        env = os.environ.copy()

        # Include user provided env vars (if any)
        env.update(env_vars)

        # Include common st2 env vars
        st2_env_vars = self._get_common_action_env_variables()
        env.update(st2_env_vars)

        LOG.info('Executing action via LocalRunner: %s', self.runner_id)
        LOG.info('[Action info] name: %s, Id: %s, command: %s, user: %s, sudo: %s' %
                 (action.name, action.action_exec_id, sanitized_args, action.user, action.sudo))

        stdout = StringIO()
        stderr = StringIO()

        store_execution_stdout_line = functools.partial(store_execution_output_data,
                                                        output_type='stdout')
        store_execution_stderr_line = functools.partial(store_execution_output_data,
                                                        output_type='stderr')

        read_and_store_stdout = make_read_and_store_stream_func(execution_db=self.execution,
            action_db=self.action, store_data_func=store_execution_stdout_line)
        read_and_store_stderr = make_read_and_store_stream_func(execution_db=self.execution,
            action_db=self.action, store_data_func=store_execution_stderr_line)

        # If sudo password is provided, pass it to the subprocess via stdin>
        # Note: We don't need to explicitly escape the argument because we pass command as a list
        # to subprocess.Popen and all the arguments are escaped by the function.
        if self._sudo_password:
            LOG.debug('Supplying sudo password via stdin')
            echo_process = subprocess.Popen(['echo', self._sudo_password + '\n'],
                                            stdout=subprocess.PIPE)
            stdin = echo_process.stdout
        else:
            stdin = None

        # Make sure os.setsid is called on each spawned process so that all processes
        # are in the same group.

        # Process is started as sudo -u {{system_user}} -- bash -c {{command}}. Introduction of the
        # bash means that multiple independent processes are spawned without them being
        # children of the process we have access to and this requires use of pkill.
        # Ideally os.killpg should have done the trick but for some reason that failed.
        # Note: pkill will set the returncode to 143 so we don't need to explicitly set
        # it to some non-zero value.
        exit_code, stdout, stderr, timed_out = shell.run_command(cmd=args,
                                                                 stdin=stdin,
                                                                 stdout=subprocess.PIPE,
                                                                 stderr=subprocess.PIPE,
                                                                 shell=True,
                                                                 cwd=self._cwd,
                                                                 env=env,
                                                                 timeout=self._timeout,
                                                                 preexec_func=os.setsid,
                                                                 kill_func=kill_process,
                                                           read_stdout_func=read_and_store_stdout,
                                                           read_stderr_func=read_and_store_stderr,
                                                           read_stdout_buffer=stdout,
                                                           read_stderr_buffer=stderr)

        error = None

        if timed_out:
            error = 'Action failed to complete in %s seconds' % (self._timeout)
            exit_code = -1 * exit_code_constants.SIGKILL_EXIT_CODE

        # Detect if user provided an invalid sudo password or sudo is not configured for that user
        if self._sudo_password:
            if re.search(r'sudo: \d+ incorrect password attempts', stderr):
                match = re.search(r'\[sudo\] password for (.+?)\:', stderr)

                if match:
                    username = match.groups()[0]
                else:
                    username = 'unknown'

                error = ('Invalid sudo password provided or sudo is not configured for this user '
                        '(%s)' % (username))
                exit_code = -1

        succeeded = (exit_code == exit_code_constants.SUCCESS_EXIT_CODE)

#.........这里部分代码省略.........
开发者ID:StackStorm,项目名称:st2,代码行数:101,代码来源:base.py

示例15: run

    def run(self, action_parameters):
        pack = self.get_pack_name()
        serialized_parameters = json.dumps(action_parameters) if action_parameters else ''
        virtualenv_path = get_sandbox_virtualenv_path(pack=pack)
        python_path = get_sandbox_python_binary_path(pack=pack)

        if virtualenv_path and not os.path.isdir(virtualenv_path):
            format_values = {'pack': pack, 'virtualenv_path': virtualenv_path}
            msg = PACK_VIRTUALENV_DOESNT_EXIST % format_values
            raise Exception(msg)

        if not self.entry_point:
            raise Exception('Action "%s" is missing entry_point attribute' % (self.action.name))

        args = [
            python_path,
            WRAPPER_SCRIPT_PATH,
            '--pack=%s' % (pack),
            '--file-path=%s' % (self.entry_point),
            '--parameters=%s' % (serialized_parameters),
            '--parent-args=%s' % (json.dumps(sys.argv[1:]))
        ]

        # We need to ensure all the st2 dependencies are also available to the
        # subprocess
        env = os.environ.copy()
        env['PATH'] = get_sandbox_path(virtualenv_path=virtualenv_path)
        env['PYTHONPATH'] = get_sandbox_python_path(inherit_from_parent=True,
                                                    inherit_parent_virtualenv=True)

        # Include user provided environment variables (if any)
        user_env_vars = self._get_env_vars()
        env.update(user_env_vars)

        # Include common st2 environment variables
        st2_env_vars = self._get_common_action_env_variables()
        env.update(st2_env_vars)

        exit_code, stdout, stderr, timed_out = run_command(cmd=args, stdout=subprocess.PIPE,
                                                           stderr=subprocess.PIPE, shell=False,
                                                           env=env, timeout=self._timeout)

        if timed_out:
            error = 'Action failed to complete in %s seconds' % (self._timeout)
        else:
            error = None

        if ACTION_OUTPUT_RESULT_DELIMITER in stdout:
            split = stdout.split(ACTION_OUTPUT_RESULT_DELIMITER)
            assert len(split) == 3
            result = split[1].strip()
            stdout = split[0] + split[2]
        else:
            result = None

        try:
            result = json.loads(result)
        except:
            pass

        output = {
            'stdout': stdout,
            'stderr': stderr,
            'exit_code': exit_code,
            'result': result
        }

        if error:
            output['error'] = error

        if exit_code == 0:
            status = LIVEACTION_STATUS_SUCCEEDED
        elif timed_out:
            status = LIVEACTION_STATUS_TIMED_OUT
        else:
            status = LIVEACTION_STATUS_FAILED

        return (status, output, None)
开发者ID:MohammadHabbab,项目名称:st2,代码行数:78,代码来源:pythonrunner.py


注:本文中的st2common.util.green.shell.run_command函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。