|
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 os |
|
17
|
|
|
import pwd |
|
18
|
|
|
|
|
19
|
|
|
from st2common import log as logging |
|
20
|
|
|
from st2common.models.system.action import RemoteAction |
|
21
|
|
|
from st2common.models.system.action import SUDO_COMMON_OPTIONS |
|
22
|
|
|
from st2common.util.shell import quote_unix |
|
23
|
|
|
|
|
24
|
|
|
__all__ = [ |
|
25
|
|
|
'ParamikoRemoteCommandAction', |
|
26
|
|
|
] |
|
27
|
|
|
|
|
28
|
|
|
LOG = logging.getLogger(__name__) |
|
29
|
|
|
|
|
30
|
|
|
LOGGED_USER_USERNAME = pwd.getpwuid(os.getuid())[0] |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class ParamikoRemoteCommandAction(RemoteAction): |
|
34
|
|
|
|
|
35
|
|
|
def get_full_command_string(self): |
|
36
|
|
|
# Note: We pass -E to sudo because we want to preserve user provided environment variables |
|
37
|
|
|
env_str = self._get_env_vars_export_string() |
|
38
|
|
|
cwd = self.get_cwd() |
|
39
|
|
|
|
|
40
|
|
|
if self.sudo: |
|
41
|
|
|
if env_str: |
|
42
|
|
|
command = quote_unix('%s && cd %s && %s' % (env_str, cwd, self.command)) |
|
43
|
|
|
else: |
|
44
|
|
|
command = quote_unix('cd %s && %s' % (cwd, self.command)) |
|
45
|
|
|
|
|
46
|
|
|
sudo_arguments = ' '.join(self._get_common_sudo_arguments()) |
|
47
|
|
|
command = 'sudo %s -- bash -c %s' % (sudo_arguments, command) |
|
48
|
|
|
|
|
49
|
|
|
if self.sudo_password: |
|
50
|
|
|
command = 'echo -e %s | %s' % (quote_unix('%s\n' % (self.sudo_password)), command) |
|
51
|
|
|
else: |
|
52
|
|
|
if env_str: |
|
53
|
|
|
command = '%s && cd %s && %s' % (env_str, cwd, |
|
54
|
|
|
self.command) |
|
55
|
|
|
else: |
|
56
|
|
|
command = 'cd %s && %s' % (cwd, self.command) |
|
57
|
|
|
|
|
58
|
|
|
LOG.debug('Command to run on remote host will be: %s', command) |
|
59
|
|
|
return command |
|
60
|
|
|
|
|
61
|
|
|
def _get_common_sudo_arguments(self): |
|
62
|
|
|
""" |
|
63
|
|
|
Retrieve a list of flags which are passed to sudo on every invocation. |
|
64
|
|
|
|
|
65
|
|
|
:rtype: ``list`` |
|
66
|
|
|
""" |
|
67
|
|
|
flags = [] |
|
68
|
|
|
|
|
69
|
|
|
if self.sudo_password: |
|
70
|
|
|
flags.append('-S') |
|
71
|
|
|
|
|
72
|
|
|
flags = flags + SUDO_COMMON_OPTIONS |
|
73
|
|
|
|
|
74
|
|
|
return flags |
|
75
|
|
|
|