change cloner info path
[sdnvpn.git] / odl-pipeline / lib / utils / processutils.py
1 #
2 # Copyright (c) 2015 All rights reserved
3 # This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #
10 import utils_log as log
11 import os
12 import six
13 import re
14 import signal
15 import subprocess
16 from time import sleep
17 from threading import Thread
18 try:
19     from Queue import Queue
20 except ImportError:
21     from queue import Queue  # python 3.x
22
23 LOG = log.LOG
24 LOG_LEVEL = log.LOG_LEVEL
25
26
27 def _subprocess_setup():
28     # Python installs a SIGPIPE handler by default. This is usually not what
29     # non-Python subprocesses expect.
30     signal.signal(signal.SIGPIPE, signal.SIG_DFL)
31
32 # NOTE(flaper87): The following globals are used by `mask_password`
33 _SANITIZE_KEYS = ['adminPass', 'admin_pass', 'password', 'admin_password']
34
35 # NOTE(ldbragst): Let's build a list of regex objects using the list of
36 # _SANITIZE_KEYS we already have. This way, we only have to add the new key
37 # to the list of _SANITIZE_KEYS and we can generate regular expressions
38 # for XML and JSON automatically.
39 _SANITIZE_PATTERNS_2 = []
40 _SANITIZE_PATTERNS_1 = []
41
42
43 def mask_password(message, secret="***"):
44     """Replace password with 'secret' in message.
45
46     :param message: The string which includes security information.
47     :param secret: value with which to replace passwords.
48     :returns: The unicode value of message with the password fields masked.
49
50     For example:
51
52     >>> mask_password("'adminPass' : 'aaaaa'")
53     "'adminPass' : '***'"
54     >>> mask_password("'admin_pass' : 'aaaaa'")
55     "'admin_pass' : '***'"
56     >>> mask_password('"password" : "aaaaa"')
57     '"password" : "***"'
58     >>> mask_password("'original_password' : 'aaaaa'")
59     "'original_password' : '***'"
60     >>> mask_password("u'original_password' :   u'aaaaa'")
61     "u'original_password' :   u'***'"
62     """
63     try:
64         message = six.text_type(message)
65     except UnicodeDecodeError:
66         # NOTE(jecarey): Temporary fix to handle cases where message is a
67         # byte string.   A better solution will be provided in Kilo.
68         pass
69
70     # NOTE(ldbragst): Check to see if anything in message contains any key
71     # specified in _SANITIZE_KEYS, if not then just return the message since
72     # we don't have to mask any passwords.
73     if not any(key in message for key in _SANITIZE_KEYS):
74         return message
75
76     substitute = r'\g<1>' + secret + r'\g<2>'
77     for pattern in _SANITIZE_PATTERNS_2:
78         message = re.sub(pattern, substitute, message)
79
80     substitute = r'\g<1>' + secret
81     for pattern in _SANITIZE_PATTERNS_1:
82         message = re.sub(pattern, substitute, message)
83
84     return message
85
86
87 class ProcessExecutionError(Exception):
88     def __init__(self, stdout=None, stderr=None, exit_code=None, cmd=None,
89                  description=None):
90         self.exit_code = exit_code
91         self.stderr = stderr
92         self.stdout = stdout
93         self.cmd = cmd
94         self.description = description
95
96         if description is None:
97             description = "Unexpected error while running command."
98         if exit_code is None:
99             exit_code = '-'
100         message = ("%s\nCommand: %s\nExit code: %s\nStdout: %r\nStderr: %r"
101                    % (description, cmd, exit_code, stdout, stderr))
102         super(ProcessExecutionError, self).__init__(message)
103
104
105 def enqueue_output(out, queue):
106     for line in iter(out.readline, b''):
107         queue.put(line)
108     queue.put("##Finished##")
109     out.close()
110
111
112 def execute(cmd, **kwargs):
113     """Helper method to shell out and execute a command through subprocess.
114
115     Allows optional retry.
116
117     :param cmd:             Passed to subprocess.Popen.
118     :type cmd:              list - will be converted if needed
119     :param process_input:   Send to opened process.
120     :type proces_input:     string
121     :param check_exit_code: Single bool, int, or list of allowed exit
122                             codes.  Defaults to [0].  Raise
123                             :class:`ProcessExecutionError` unless
124                             program exits with one of these code.
125     :type check_exit_code:  boolean, int, or [int]
126     :param delay_on_retry:  True | False. Defaults to True. If set to True,
127                             wait a short amount of time before retrying.
128     :type delay_on_retry:   boolean
129     :param attempts:        How many times to retry cmd.
130     :type attempts:         int
131     :param run_as_root:     True | False. Defaults to False. If set to True,
132         or as_root          the command is prefixed by the command specified
133                             in the root_helper kwarg.
134                             execute this command. Defaults to false.
135     :param shell:           whether or not there should be a shell used to
136     :type shell:            boolean
137     :param loglevel:        log level for execute commands.
138     :type loglevel:         int.  (Should be logging.DEBUG or logging.INFO)
139     :param non_blocking     Execute in background.
140     :type non_blockig:      boolean
141     :returns:               (stdout, (stderr, returncode)) from process
142                             execution
143     :raises:                :class:`UnknownArgumentError` on
144                             receiving unknown arguments
145     :raises:                :class:`ProcessExecutionError`
146     """
147     process_input = kwargs.pop('process_input', None)
148     check_exit_code = kwargs.pop('check_exit_code', [0])
149     ignore_exit_code = False
150     attempts = kwargs.pop('attempts', 1)
151     run_as_root = kwargs.pop('run_as_root', False) or kwargs.pop('as_root',
152                                                                  False)
153     shell = kwargs.pop('shell', False)
154     loglevel = kwargs.pop('loglevel', LOG_LEVEL)
155     non_blocking = kwargs.pop('non_blocking', False)
156
157     if not isinstance(cmd, list):
158         cmd = cmd.split(' ')
159
160     if run_as_root:
161         cmd = ['sudo'] + cmd
162     if shell:
163         cmd = ' '.join(cmd)
164     if isinstance(check_exit_code, bool):
165         ignore_exit_code = not check_exit_code
166         check_exit_code = [0]
167     elif isinstance(check_exit_code, int):
168         check_exit_code = [check_exit_code]
169
170     if kwargs:
171         raise Exception(('Got unknown keyword args '
172                          'to utils.execute: %r') % kwargs)
173
174     while attempts > 0:
175         attempts -= 1
176         try:
177             LOG.log(loglevel, ('Running cmd (subprocess): %s'), cmd)
178             _PIPE = subprocess.PIPE  # pylint: disable=E1101
179
180             if os.name == 'nt':
181                 preexec_fn = None
182                 close_fds = False
183             else:
184                 preexec_fn = _subprocess_setup
185                 close_fds = True
186
187             obj = subprocess.Popen(cmd,
188                                    stdin=_PIPE,
189                                    stdout=_PIPE,
190                                    stderr=_PIPE,
191                                    close_fds=close_fds,
192                                    preexec_fn=preexec_fn,
193                                    shell=shell)
194             result = None
195             if process_input is not None:
196                 result = obj.communicate(process_input)
197             else:
198                 if non_blocking:
199                     queue = Queue()
200                     thread = Thread(target=enqueue_output, args=(obj.stdout,
201                                                                  queue))
202                     thread.deamon = True
203                     thread.start()
204                     # If you want to read this output later:
205                     # try:
206                     #     from Queue import Queue, Empty
207                     # except ImportError:
208                     #     from queue import Queue, Empty  # python 3.x
209                     # try:  line = q.get_nowait() # or q.get(timeout=.1)
210                     # except Empty:
211                     #     print('no output yet')
212                     # else: # got line
213                     # ... do something with line
214                     return queue
215                 result = obj.communicate()
216             obj.stdin.close()  # pylint: disable=E1101
217             _returncode = obj.returncode  # pylint: disable=E1101
218             LOG.log(loglevel, ('Result was %s') % _returncode)
219             if not ignore_exit_code and _returncode not in check_exit_code:
220                 (stdout, stderr) = result
221                 sanitized_stdout = mask_password(stdout)
222                 sanitized_stderr = mask_password(stderr)
223                 raise ProcessExecutionError(
224                     exit_code=_returncode,
225                     stdout=sanitized_stdout,
226                     stderr=sanitized_stderr,
227                     cmd=(' '.join(cmd)) if isinstance(cmd, list) else cmd)
228             (stdout, stderr) = result
229             return (stdout, (stderr, _returncode))
230         except ProcessExecutionError:
231             raise
232         finally:
233             sleep(0)