Completed
Pull Request — master (#2596)
by Edward
06:08 queued 22s
created

st2actions.runners.ParamikoRemoteCommandRunner   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %
Metric Value
wmc 4
dl 0
loc 39
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A _run() 0 3 1
A run() 0 9 1
B _get_remote_action() 0 24 2
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import uuid
17
18
from oslo_config import cfg
19
20
from st2common import log as logging
21
from st2actions.runners.ssh.fabric_runner import BaseFabricRunner
22
from st2actions.runners.ssh.fabric_runner import RUNNER_COMMAND
23
from st2actions.runners.ssh.paramiko_ssh_runner import BaseParallelSSHRunner
24
from st2common.models.system.action import FabricRemoteAction
25
from st2common.models.system.paramiko_command_action import ParamikoRemoteCommandAction
26
27
__all__ = [
28
    'get_runner',
29
    'ParamikoRemoteCommandRunner',
30
    'RemoteCommandRunner'
31
]
32
33
LOG = logging.getLogger(__name__)
34
35
36
def get_runner():
37
    if cfg.CONF.ssh_runner.use_paramiko_ssh_runner:
38
        return ParamikoRemoteCommandRunner(str(uuid.uuid4()))
39
    return RemoteCommandRunner(str(uuid.uuid4()))
40
41
42
class RemoteCommandRunner(BaseFabricRunner):
43
    def run(self, action_parameters):
44
        remote_action = self._get_remote_action(action_parameters)
45
46
        LOG.debug('Will execute remote_action: %s.', str(remote_action))
47
        result = self._run(remote_action)
48
        LOG.debug('Executed remote_action: %s. Result is: %s.', remote_action, result)
49
        status = self._get_result_status(result, cfg.CONF.ssh_runner.allow_partial_failure)
50
51
        return (status, result, None)
52
53
    def _get_remote_action(self, action_paramaters):
54
        # remote script actions with entry_point don't make sense, user probably wanted to use
55
        # "remote-shell-script" action
56
        if self.entry_point:
57
            msg = ('Action "%s" specified "entry_point" attribute. Perhaps wanted to use '
58
                   '"remote-shell-script" runner?' % (self.action_name))
59
            raise Exception(msg)
60
61
        command = self.runner_parameters.get(RUNNER_COMMAND, None)
62
        env_vars = self._get_env_vars()
63
        return FabricRemoteAction(self.action_name,
64
                                  str(self.liveaction_id),
65
                                  command,
66
                                  env_vars=env_vars,
67
                                  on_behalf_user=self._on_behalf_user,
68
                                  user=self._username,
69
                                  password=self._password,
70
                                  private_key=self._private_key,
71
                                  hosts=self._hosts,
72
                                  parallel=self._parallel,
73
                                  sudo=self._sudo,
74
                                  timeout=self._timeout,
75
                                  cwd=self._cwd)
76
77
78
class ParamikoRemoteCommandRunner(BaseParallelSSHRunner):
79
    def run(self, action_parameters):
80
        remote_action = self._get_remote_action(action_parameters)
81
82
        LOG.debug('Executing remote command action.', extra={'_action_params': remote_action})
83
        result = self._run(remote_action)
84
        LOG.debug('Executed remote_action.', extra={'_result': result})
85
        status = self._get_result_status(result, cfg.CONF.ssh_runner.allow_partial_failure)
86
87
        return (status, result, None)
88
89
    def _run(self, remote_action):
90
        command = remote_action.get_full_command_string()
91
        return self._parallel_ssh_client.run(command, timeout=remote_action.get_timeout())
92
93
    def _get_remote_action(self, action_paramaters):
94
        # remote script actions with entry_point don't make sense, user probably wanted to use
95
        # "remote-shell-script" action
96
        if self.entry_point:
97
            msg = ('Action "%s" specified "entry_point" attribute. Perhaps wanted to use '
98
                   '"remote-shell-script" runner?' % (self.action_name))
99
            raise Exception(msg)
100
101
        command = self.runner_parameters.get(RUNNER_COMMAND, None)
102
        env_vars = self._get_env_vars()
103
        return ParamikoRemoteCommandAction(self.action_name,
104
                                           str(self.liveaction_id),
105
                                           command,
106
                                           env_vars=env_vars,
107
                                           on_behalf_user=self._on_behalf_user,
108
                                           user=self._username,
109
                                           password=self._password,
110
                                           private_key=self._private_key,
111
                                           passphrase=self._passphrase,
112
                                           hosts=self._hosts,
113
                                           parallel=self._parallel,
114
                                           sudo=self._sudo,
115
                                           timeout=self._timeout,
116
                                           cwd=self._cwd)
117