ParamikoRemoteScriptAction._format_command()   F
last analyzed

Complexity

Conditions 9

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
c 2
b 0
f 0
dl 0
loc 42
rs 3
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 = ('set +o history ; echo -e %s | %s' %
58
                          (quote_unix('%s\n' % (self.sudo_password)), command))
59
        else:
60
            if script_arguments:
61
                if env_str:
62
                    command = '%s && cd %s && %s %s' % (env_str, cwd,
63
                                                        script_path, script_arguments)
64
                else:
65
                    command = 'cd %s && %s %s' % (cwd, script_path, script_arguments)
66
            else:
67
                if env_str:
68
                    command = '%s && cd %s && %s' % (env_str, cwd, script_path)
69
                else:
70
                    command = 'cd %s && %s' % (cwd, script_path)
71
72
        return command
73
74
    def _get_common_sudo_arguments(self):
75
        """
76
        Retrieve a list of flags which are passed to sudo on every invocation.
77
78
        :rtype: ``list``
79
        """
80
        flags = []
81
82
        if self.sudo_password:
83
            flags.append('-S')
84
85
        flags = flags + SUDO_COMMON_OPTIONS
86
87
        return flags
88