def test_get_command_string(self):
        local_script_path = '/opt/stackstorm/packs/fixtures/actions/remote_script.sh'
        script_action = ParamikoRemoteScriptAction('fixtures.remote_script',
                                                   '55ce39d532ed3543aecbe71d',
                                                   local_script_path,
                                                   '/opt/stackstorm/packs/fixtures/actions/lib/',
                                                   named_args={'song': 'b s'},
                                                   positional_args=['taylor swift'],
                                                   env_vars={},
                                                   on_behalf_user='******',
                                                   user='******',
                                                   private_key='/home/vagrant/.ssh/stanley_rsa',
                                                   remote_dir='/tmp',
                                                   hosts=['localhost'],
                                                   parallel=True,
                                                   sudo=False,
                                                   timeout=60)
        expected = '/tmp/remote_script.sh song=\'b s\' \'taylor swift\''
        self.assertEqual(script_action.get_full_command_string(), expected)

        # Test with sudo
        script_action.sudo = True
        ex = 'sudo -E -- bash -c ' + \
             '\'/tmp/remote_script.sh song=\'"\'"\'b s\'"\'"\' \'"\'"\'taylor swift\'"\'"\'\''
        self.assertEqual(script_action.get_full_command_string(), ex)
    def test_get_command_string(self):
        local_script_path = '/opt/stackstorm/packs/fixtures/actions/remote_script.sh'
        script_action = ParamikoRemoteScriptAction(
            'fixtures.remote_script',
            '55ce39d532ed3543aecbe71d',
            local_script_path,
            '/opt/stackstorm/packs/fixtures/actions/lib/',
            named_args={'song': 'b s'},
            positional_args=['taylor swift'],
            env_vars={},
            on_behalf_user='******',
            user='******',
            private_key='/home/vagrant/.ssh/stanley_rsa',
            remote_dir='/tmp',
            hosts=['localhost'],
            parallel=True,
            sudo=False,
            timeout=60)
        expected = '/tmp/remote_script.sh song=\'b s\' \'taylor swift\''
        self.assertEqual(script_action.get_full_command_string(), expected)

        # Test with sudo
        script_action.sudo = True
        ex = 'sudo -E -- bash -c ' + \
             '\'/tmp/remote_script.sh song=\'"\'"\'b s\'"\'"\' \'"\'"\'taylor swift\'"\'"\'\''
        self.assertEqual(script_action.get_full_command_string(), ex)
Ejemplo n.º 3
0
    def _get_remote_action(self, action_parameters):
        # remote script actions without entry_point don't make sense, user probably wanted to use
        # "remote-shell-cmd" action
        if not self.entry_point:
            msg = ('Action "%s" is missing "entry_point" attribute. Perhaps wanted to use '
                   '"remote-shell-script" runner?' % (self.action_name))
            raise Exception(msg)

        script_local_path_abs = self.entry_point
        pos_args, named_args = self._get_script_args(action_parameters)
        named_args = self._transform_named_args(named_args)
        env_vars = self._get_env_vars()
        remote_dir = self.runner_parameters.get(RUNNER_REMOTE_DIR,
                                                cfg.CONF.ssh_runner.remote_dir)
        remote_dir = os.path.join(remote_dir, self.liveaction_id)
        return ParamikoRemoteScriptAction(self.action_name,
                                          str(self.liveaction_id),
                                          script_local_path_abs,
                                          self.libs_dir_path,
                                          named_args=named_args,
                                          positional_args=pos_args,
                                          env_vars=env_vars,
                                          on_behalf_user=self._on_behalf_user,
                                          user=self._username,
                                          password=self._password,
                                          private_key=self._private_key,
                                          remote_dir=remote_dir,
                                          hosts=self._hosts,
                                          parallel=self._parallel,
                                          sudo=self._sudo,
                                          sudo_password=self._sudo_password,
                                          timeout=self._timeout,
                                          cwd=self._cwd)
 def test_cwd_used_correctly(self):
     remote_action = ParamikoRemoteScriptAction(
         'foo-script', bson.ObjectId(),
         script_local_path_abs='/home/stanley/shiz_storm.py',
         script_local_libs_path_abs=None,
         named_args={}, positional_args=['blank space'], env_vars={},
         on_behalf_user='******', user='******',
         private_key='---SOME RSA KEY---',
         remote_dir='/tmp', hosts=['127.0.0.1'], cwd='/test/cwd/'
     )
     paramiko_runner = ParamikoRemoteScriptRunner('runner_1')
     paramiko_runner._parallel_ssh_client = ParallelSSHClient(['127.0.0.1'], 'stanley')
     paramiko_runner._run_script_on_remote_host(remote_action)
     exp_cmd = "cd /test/cwd/ && /tmp/shiz_storm.py 'blank space'"
     ParallelSSHClient.run.assert_called_with(exp_cmd,
                                              timeout=None)
Ejemplo n.º 5
0
 def _get_test_script_action():
     local_script_path = '/opt/stackstorm/packs/fixtures/actions/remote_script.sh'
     script_action = ParamikoRemoteScriptAction('fixtures.remote_script',
                                                '55ce39d532ed3543aecbe71d',
                                                local_script_path,
                                                '/opt/stackstorm/packs/fixtures/actions/lib/',
                                                named_args={'song': 'b s'},
                                                positional_args=['taylor swift'],
                                                env_vars={},
                                                on_behalf_user='******',
                                                user='******',
                                                private_key='/home/vagrant/.ssh/stanley_rsa',
                                                remote_dir='/tmp',
                                                hosts=['127.0.0.1'],
                                                parallel=True,
                                                sudo=False,
                                                timeout=60, cwd='/tmp')
     return script_action
 def _get_test_script_action():
     local_script_path = "/opt/stackstorm/packs/fixtures/actions/remote_script.sh"
     script_action = ParamikoRemoteScriptAction(
         "fixtures.remote_script",
         "55ce39d532ed3543aecbe71d",
         local_script_path,
         "/opt/stackstorm/packs/fixtures/actions/lib/",
         named_args={"song": "b s"},
         positional_args=["taylor swift"],
         env_vars={},
         on_behalf_user="******",
         user="******",
         private_key="/home/vagrant/.ssh/stanley_rsa",
         remote_dir="/tmp",
         hosts=["127.0.0.1"],
         parallel=True,
         sudo=False,
         timeout=60,
         cwd="/tmp",
     )
     return script_action
 def test_cwd_used_correctly(self):
     remote_action = ParamikoRemoteScriptAction(
         "foo-script",
         bson.ObjectId(),
         script_local_path_abs="/home/stanley/shiz_storm.py",
         script_local_libs_path_abs=None,
         named_args={},
         positional_args=["blank space"],
         env_vars={},
         on_behalf_user="******",
         user="******",
         private_key="---SOME RSA KEY---",
         remote_dir="/tmp",
         hosts=["127.0.0.1"],
         cwd="/test/cwd/",
     )
     paramiko_runner = ParamikoRemoteScriptRunner("runner_1")
     paramiko_runner._parallel_ssh_client = ParallelSSHClient(["127.0.0.1"],
                                                              "stanley")
     paramiko_runner._run_script_on_remote_host(remote_action)
     exp_cmd = "cd /test/cwd/ && /tmp/shiz_storm.py 'blank space'"
     ParallelSSHClient.run.assert_called_with(exp_cmd, timeout=None)
    def test_command_construction_correct_default_parameter_values_are_used(self):
        runner_parameters = {}
        action_db_parameters = {
            'project': {
                'type': 'string',
                'default': 'st2',
                'position': 0,
            },
            'version': {
                'type': 'string',
                'position': 1,
                'required': True
            },
            'fork': {
                'type': 'string',
                'position': 2,
                'default': 'StackStorm',
            },
            'branch': {
                'type': 'string',
                'position': 3,
                'default': 'master',
            },
            'update_mistral': {
                'type': 'boolean',
                'position': 4,
                'default': False
            },
            'update_changelog': {
                'type': 'boolean',
                'position': 5,
                'default': False
            },
            'local_repo': {
                'type': 'string',
                'position': 6,
            }
        }
        context = {}

        action_db = ActionDB(pack='dummy', name='action')

        runner = ParamikoRemoteScriptRunner('id')
        runner.runner_parameters = {}
        runner.action = action_db

        # 1. All default values used
        live_action_db_parameters = {
            'project': 'st2flow',
            'version': '3.0.0',
            'fork': 'StackStorm',
            'local_repo': '/tmp/repo'
        }

        runner_params, action_params = param_utils.render_final_params(runner_parameters,
                                                action_db_parameters,
                                                live_action_db_parameters,
                                                context)

        self.assertDictEqual(action_params, {
            'project': 'st2flow',
            'version': '3.0.0',
            'fork': 'StackStorm',
            'branch': 'master',  # default value used
            'update_mistral': False,  # default value used
            'update_changelog': False,  # default value used
            'local_repo': '/tmp/repo'
        })

        action_db.parameters = action_db_parameters
        positional_args, named_args = runner._get_script_args(action_params)
        named_args = runner._transform_named_args(named_args)

        remote_action = ParamikoRemoteScriptAction(
            'foo-script', 'id',
            script_local_path_abs='/tmp/script.sh',
            script_local_libs_path_abs=None,
            named_args=named_args, positional_args=positional_args, env_vars={},
            on_behalf_user='******', user='******',
            remote_dir='/tmp', hosts=['127.0.0.1'], cwd='/test/cwd/'
        )

        command_string = remote_action.get_full_command_string()
        expected = 'cd /test/cwd/ && /tmp/script.sh st2flow 3.0.0 StackStorm master 0 0 /tmp/repo'
        self.assertEqual(command_string, expected)

        # 2. Some default values used
        live_action_db_parameters = {
            'project': 'st2web',
            'version': '3.1.0',
            'fork': 'StackStorm1',
            'update_changelog': True,
            'local_repo': '/tmp/repob'
        }

        runner_params, action_params = param_utils.render_final_params(runner_parameters,
                                                action_db_parameters,
                                                live_action_db_parameters,
                                                context)

        self.assertDictEqual(action_params, {
            'project': 'st2web',
            'version': '3.1.0',
            'fork': 'StackStorm1',
            'branch': 'master',  # default value used
            'update_mistral': False,  # default value used
            'update_changelog': True,  # default value used
            'local_repo': '/tmp/repob'
        })

        action_db.parameters = action_db_parameters
        positional_args, named_args = runner._get_script_args(action_params)
        named_args = runner._transform_named_args(named_args)

        remote_action = ParamikoRemoteScriptAction(
            'foo-script', 'id',
            script_local_path_abs='/tmp/script.sh',
            script_local_libs_path_abs=None,
            named_args=named_args, positional_args=positional_args, env_vars={},
            on_behalf_user='******', user='******',
            remote_dir='/tmp', hosts=['127.0.0.1'], cwd='/test/cwd/'
        )

        command_string = remote_action.get_full_command_string()
        expected = 'cd /test/cwd/ && /tmp/script.sh st2web 3.1.0 StackStorm1 master 0 1 /tmp/repob'
        self.assertEqual(command_string, expected)

        # 3. None is specified for a boolean parameter, should use a default
        live_action_db_parameters = {
            'project': 'st2rbac',
            'version': '3.2.0',
            'fork': 'StackStorm2',
            'update_changelog': None,
            'local_repo': '/tmp/repoc'
        }

        runner_params, action_params = param_utils.render_final_params(runner_parameters,
                                                action_db_parameters,
                                                live_action_db_parameters,
                                                context)

        self.assertDictEqual(action_params, {
            'project': 'st2rbac',
            'version': '3.2.0',
            'fork': 'StackStorm2',
            'branch': 'master',  # default value used
            'update_mistral': False,  # default value used
            'update_changelog': False,  # default value used
            'local_repo': '/tmp/repoc'
        })

        action_db.parameters = action_db_parameters
        positional_args, named_args = runner._get_script_args(action_params)
        named_args = runner._transform_named_args(named_args)

        remote_action = ParamikoRemoteScriptAction(
            'foo-script', 'id',
            script_local_path_abs='/tmp/script.sh',
            script_local_libs_path_abs=None,
            named_args=named_args, positional_args=positional_args, env_vars={},
            on_behalf_user='******', user='******',
            remote_dir='/tmp', hosts=['127.0.0.1'], cwd='/test/cwd/'
        )

        command_string = remote_action.get_full_command_string()
        expected = 'cd /test/cwd/ && /tmp/script.sh st2rbac 3.2.0 StackStorm2 master 0 0 /tmp/repoc'
        self.assertEqual(command_string, expected)
    def test_command_construction_correct_default_parameter_values_are_used(
            self):
        runner_parameters = {}
        action_db_parameters = {
            "project": {
                "type": "string",
                "default": "st2",
                "position": 0,
            },
            "version": {
                "type": "string",
                "position": 1,
                "required": True
            },
            "fork": {
                "type": "string",
                "position": 2,
                "default": "StackStorm",
            },
            "branch": {
                "type": "string",
                "position": 3,
                "default": "master",
            },
            "update_changelog": {
                "type": "boolean",
                "position": 4,
                "default": False
            },
            "local_repo": {
                "type": "string",
                "position": 5,
            },
        }
        context = {}

        action_db = ActionDB(pack="dummy", name="action")

        runner = ParamikoRemoteScriptRunner("id")
        runner.runner_parameters = {}
        runner.action = action_db

        # 1. All default values used
        live_action_db_parameters = {
            "project": "st2flow",
            "version": "3.0.0",
            "fork": "StackStorm",
            "local_repo": "/tmp/repo",
        }

        runner_params, action_params = param_utils.render_final_params(
            runner_parameters, action_db_parameters, live_action_db_parameters,
            context)

        self.assertDictEqual(
            action_params,
            {
                "project": "st2flow",
                "version": "3.0.0",
                "fork": "StackStorm",
                "branch": "master",  # default value used
                "update_changelog": False,  # default value used
                "local_repo": "/tmp/repo",
            },
        )

        action_db.parameters = action_db_parameters
        positional_args, named_args = runner._get_script_args(action_params)
        named_args = runner._transform_named_args(named_args)

        remote_action = ParamikoRemoteScriptAction(
            "foo-script",
            "id",
            script_local_path_abs="/tmp/script.sh",
            script_local_libs_path_abs=None,
            named_args=named_args,
            positional_args=positional_args,
            env_vars={},
            on_behalf_user="******",
            user="******",
            remote_dir="/tmp",
            hosts=["127.0.0.1"],
            cwd="/test/cwd/",
        )

        command_string = remote_action.get_full_command_string()
        expected = "cd /test/cwd/ && /tmp/script.sh st2flow 3.0.0 StackStorm master 0 /tmp/repo"
        self.assertEqual(command_string, expected)

        # 2. Some default values used
        live_action_db_parameters = {
            "project": "st2web",
            "version": "3.1.0",
            "fork": "StackStorm1",
            "update_changelog": True,
            "local_repo": "/tmp/repob",
        }

        runner_params, action_params = param_utils.render_final_params(
            runner_parameters, action_db_parameters, live_action_db_parameters,
            context)

        self.assertDictEqual(
            action_params,
            {
                "project": "st2web",
                "version": "3.1.0",
                "fork": "StackStorm1",
                "branch": "master",  # default value used
                "update_changelog": True,  # default value used
                "local_repo": "/tmp/repob",
            },
        )

        action_db.parameters = action_db_parameters
        positional_args, named_args = runner._get_script_args(action_params)
        named_args = runner._transform_named_args(named_args)

        remote_action = ParamikoRemoteScriptAction(
            "foo-script",
            "id",
            script_local_path_abs="/tmp/script.sh",
            script_local_libs_path_abs=None,
            named_args=named_args,
            positional_args=positional_args,
            env_vars={},
            on_behalf_user="******",
            user="******",
            remote_dir="/tmp",
            hosts=["127.0.0.1"],
            cwd="/test/cwd/",
        )

        command_string = remote_action.get_full_command_string()
        expected = "cd /test/cwd/ && /tmp/script.sh st2web 3.1.0 StackStorm1 master 1 /tmp/repob"
        self.assertEqual(command_string, expected)

        # 3. None is specified for a boolean parameter, should use a default
        live_action_db_parameters = {
            "project": "st2rbac",
            "version": "3.2.0",
            "fork": "StackStorm2",
            "update_changelog": None,
            "local_repo": "/tmp/repoc",
        }

        runner_params, action_params = param_utils.render_final_params(
            runner_parameters, action_db_parameters, live_action_db_parameters,
            context)

        self.assertDictEqual(
            action_params,
            {
                "project": "st2rbac",
                "version": "3.2.0",
                "fork": "StackStorm2",
                "branch": "master",  # default value used
                "update_changelog": False,  # default value used
                "local_repo": "/tmp/repoc",
            },
        )

        action_db.parameters = action_db_parameters
        positional_args, named_args = runner._get_script_args(action_params)
        named_args = runner._transform_named_args(named_args)

        remote_action = ParamikoRemoteScriptAction(
            "foo-script",
            "id",
            script_local_path_abs="/tmp/script.sh",
            script_local_libs_path_abs=None,
            named_args=named_args,
            positional_args=positional_args,
            env_vars={},
            on_behalf_user="******",
            user="******",
            remote_dir="/tmp",
            hosts=["127.0.0.1"],
            cwd="/test/cwd/",
        )

        command_string = remote_action.get_full_command_string()
        expected = "cd /test/cwd/ && /tmp/script.sh st2rbac 3.2.0 StackStorm2 master 0 /tmp/repoc"
        self.assertEqual(command_string, expected)
    def test_command_construction_correct_default_parameter_values_are_used(
            self):
        runner_parameters = {}
        action_db_parameters = {
            'project': {
                'type': 'string',
                'default': 'st2',
                'position': 0,
            },
            'version': {
                'type': 'string',
                'position': 1,
                'required': True
            },
            'fork': {
                'type': 'string',
                'position': 2,
                'default': 'StackStorm',
            },
            'branch': {
                'type': 'string',
                'position': 3,
                'default': 'master',
            },
            'update_mistral': {
                'type': 'boolean',
                'position': 4,
                'default': False
            },
            'update_changelog': {
                'type': 'boolean',
                'position': 5,
                'default': False
            },
            'local_repo': {
                'type': 'string',
                'position': 6,
            }
        }
        context = {}

        action_db = ActionDB(pack='dummy', name='action')

        runner = ParamikoRemoteScriptRunner('id')
        runner.runner_parameters = {}
        runner.action = action_db

        # 1. All default values used
        live_action_db_parameters = {
            'project': 'st2flow',
            'version': '3.0.0',
            'fork': 'StackStorm',
            'local_repo': '/tmp/repo'
        }

        runner_params, action_params = param_utils.render_final_params(
            runner_parameters, action_db_parameters, live_action_db_parameters,
            context)

        self.assertDictEqual(
            action_params,
            {
                'project': 'st2flow',
                'version': '3.0.0',
                'fork': 'StackStorm',
                'branch': 'master',  # default value used
                'update_mistral': False,  # default value used
                'update_changelog': False,  # default value used
                'local_repo': '/tmp/repo'
            })

        action_db.parameters = action_db_parameters
        positional_args, named_args = runner._get_script_args(action_params)
        named_args = runner._transform_named_args(named_args)

        remote_action = ParamikoRemoteScriptAction(
            'foo-script',
            'id',
            script_local_path_abs='/tmp/script.sh',
            script_local_libs_path_abs=None,
            named_args=named_args,
            positional_args=positional_args,
            env_vars={},
            on_behalf_user='******',
            user='******',
            remote_dir='/tmp',
            hosts=['127.0.0.1'],
            cwd='/test/cwd/')

        command_string = remote_action.get_full_command_string()
        expected = 'cd /test/cwd/ && /tmp/script.sh st2flow 3.0.0 StackStorm master 0 0 /tmp/repo'
        self.assertEqual(command_string, expected)

        # 2. Some default values used
        live_action_db_parameters = {
            'project': 'st2web',
            'version': '3.1.0',
            'fork': 'StackStorm1',
            'update_changelog': True,
            'local_repo': '/tmp/repob'
        }

        runner_params, action_params = param_utils.render_final_params(
            runner_parameters, action_db_parameters, live_action_db_parameters,
            context)

        self.assertDictEqual(
            action_params,
            {
                'project': 'st2web',
                'version': '3.1.0',
                'fork': 'StackStorm1',
                'branch': 'master',  # default value used
                'update_mistral': False,  # default value used
                'update_changelog': True,  # default value used
                'local_repo': '/tmp/repob'
            })

        action_db.parameters = action_db_parameters
        positional_args, named_args = runner._get_script_args(action_params)
        named_args = runner._transform_named_args(named_args)

        remote_action = ParamikoRemoteScriptAction(
            'foo-script',
            'id',
            script_local_path_abs='/tmp/script.sh',
            script_local_libs_path_abs=None,
            named_args=named_args,
            positional_args=positional_args,
            env_vars={},
            on_behalf_user='******',
            user='******',
            remote_dir='/tmp',
            hosts=['127.0.0.1'],
            cwd='/test/cwd/')

        command_string = remote_action.get_full_command_string()
        expected = 'cd /test/cwd/ && /tmp/script.sh st2web 3.1.0 StackStorm1 master 0 1 /tmp/repob'
        self.assertEqual(command_string, expected)

        # 3. None is specified for a boolean parameter, should use a default
        live_action_db_parameters = {
            'project': 'st2rbac',
            'version': '3.2.0',
            'fork': 'StackStorm2',
            'update_changelog': None,
            'local_repo': '/tmp/repoc'
        }

        runner_params, action_params = param_utils.render_final_params(
            runner_parameters, action_db_parameters, live_action_db_parameters,
            context)

        self.assertDictEqual(
            action_params,
            {
                'project': 'st2rbac',
                'version': '3.2.0',
                'fork': 'StackStorm2',
                'branch': 'master',  # default value used
                'update_mistral': False,  # default value used
                'update_changelog': False,  # default value used
                'local_repo': '/tmp/repoc'
            })

        action_db.parameters = action_db_parameters
        positional_args, named_args = runner._get_script_args(action_params)
        named_args = runner._transform_named_args(named_args)

        remote_action = ParamikoRemoteScriptAction(
            'foo-script',
            'id',
            script_local_path_abs='/tmp/script.sh',
            script_local_libs_path_abs=None,
            named_args=named_args,
            positional_args=positional_args,
            env_vars={},
            on_behalf_user='******',
            user='******',
            remote_dir='/tmp',
            hosts=['127.0.0.1'],
            cwd='/test/cwd/')

        command_string = remote_action.get_full_command_string()
        expected = 'cd /test/cwd/ && /tmp/script.sh st2rbac 3.2.0 StackStorm2 master 0 0 /tmp/repoc'
        self.assertEqual(command_string, expected)