Passed
Push — master ( 6aeca2...dec5f2 )
by
unknown
03:57
created

ParamikoRemoteScriptAction   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
F _format_command() 0 41 9
A _get_common_sudo_arguments() 0 14 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
from st2common import log as logging
17
from st2common.models.system.action import RemoteScriptAction
18
from st2common.models.system.action import SUDO_COMMON_OPTIONS
19
from st2common.util.shell import quote_unix
20
21
__all__ = [
22
    'ParamikoRemoteScriptAction',
23
]
24
25
26
LOG = logging.getLogger(__name__)
27
28
29
class ParamikoRemoteScriptAction(RemoteScriptAction):
30
31
    def _format_command(self):
32
        script_arguments = self._get_script_arguments(named_args=self.named_args,
33
                                                      positional_args=self.positional_args)
34
        env_str = self._get_env_vars_export_string()
35
        cwd = quote_unix(self.get_cwd())
36
        script_path = quote_unix(self.remote_script)
37
38
        if self.sudo:
39
            if script_arguments:
40
                if env_str:
41
                    command = quote_unix('%s && cd %s && %s %s' % (
42
                        env_str, cwd, script_path, script_arguments))
43
                else:
44
                    command = quote_unix('cd %s && %s %s' % (
45
                        cwd, script_path, script_arguments))
46
            else:
47
                if env_str:
48
                    command = quote_unix('%s && cd %s && %s' % (
49
                        env_str, cwd, script_path))
50
                else:
51
                    command = quote_unix('cd %s && %s' % (cwd, script_path))
52
53
            sudo_arguments = ' '.join(self._get_common_sudo_arguments())
54
            command = 'sudo %s -- bash -c %s' % (sudo_arguments, command)
55
56
            if self.sudo_password:
57
                command = 'echo -e %s | %s' % (quote_unix('%s\n' % (self.sudo_password)), command)
58
        else:
59
            if script_arguments:
60
                if env_str:
61
                    command = '%s && cd %s && %s %s' % (env_str, cwd,
62
                                                        script_path, script_arguments)
63
                else:
64
                    command = 'cd %s && %s %s' % (cwd, script_path, script_arguments)
65
            else:
66
                if env_str:
67
                    command = '%s && cd %s && %s' % (env_str, cwd, script_path)
68
                else:
69
                    command = 'cd %s && %s' % (cwd, script_path)
70
71
        return command
72
73
    def _get_common_sudo_arguments(self):
74
        """
75
        Retrieve a list of flags which are passed to sudo on every invocation.
76
77
        :rtype: ``list``
78
        """
79
        flags = []
80
81
        if self.sudo_password:
82
            flags.append('-S')
83
84
        flags = flags + SUDO_COMMON_OPTIONS
85
86
        return flags
87