diff options
author | Christian Poessinger <christian@poessinger.com> | 2020-05-19 22:32:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-19 22:32:22 +0200 |
commit | b708a375010c7240d11412d92f8552dbba2abdc0 (patch) | |
tree | 01428d83fe5375545afee1f6359d35e0ab7ad2ba /python/vyos/util.py | |
parent | b645e28edb6e92fe13e781c0d29853aebaceb711 (diff) | |
parent | 43dd945c5a45c86c491892f30546d0179751bf4a (diff) | |
download | vyos-1x-b708a375010c7240d11412d92f8552dbba2abdc0.tar.gz vyos-1x-b708a375010c7240d11412d92f8552dbba2abdc0.zip |
Merge pull request #414 from thomas-mangin/T2467
util: T2467: automatically add sudo to known commands
Diffstat (limited to 'python/vyos/util.py')
-rw-r--r-- | python/vyos/util.py | 50 |
1 files changed, 36 insertions, 14 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py index 381cd0358..c2479aac4 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -21,12 +21,24 @@ import sys # where it is used so it is as local as possible to the execution # -# There is many (too many) ways to run command with python -# os.system, subprocess.Popen, subproces.{run,call,check_output} -# which all have slighty different behaviour -from subprocess import Popen, PIPE, STDOUT, DEVNULL + +def _need_sudo(command): + return os.path.basename(command.split()[0]) in ('systemctl', ) + + +def _add_sudo(command): + if _need_sudo(command): + return 'sudo ' + command + return command + + +from subprocess import Popen +from subprocess import PIPE +from subprocess import DEVNULL + + def popen(command, flag='', shell=None, input=None, timeout=None, env=None, - stdout=PIPE, stderr=PIPE, decode='utf-8'): + stdout=PIPE, stderr=PIPE, decode='utf-8', autosudo=True): """ popen is a wrapper helper aound subprocess.Popen with it default setting it will return a tuple (out, err) @@ -52,15 +64,22 @@ def popen(command, flag='', shell=None, input=None, timeout=None, env=None, the default is explicitely utf-8 which is python's own default usage: - to get both stdout, and stderr: popen('command', stdout=PIPE, stderr=STDOUT) - to discard stdout and get stderr: popen('command', stdout=DEVNUL, stderr=PIPE) + get both stdout and stderr: popen('command', stdout=PIPE, stderr=STDOUT) + discard stdout and get stderr: popen('command', stdout=DEVNUL, stderr=PIPE) """ + + # airbag must be left as an import in the function as otherwise we have a + # a circual import dependency from vyos import debug from vyos import airbag + # log if the flag is set, otherwise log if command is set if not debug.enabled(flag): flag = 'command' + if autosudo: + command = _add_sudo(command) + cmd_msg = f"cmd '{command}'" debug.message(cmd_msg, flag) @@ -72,9 +91,11 @@ def popen(command, flag='', shell=None, input=None, timeout=None, env=None, use_shell = True if env: use_shell = True + if input: stdin = PIPE input = input.encode() if type(input) is str else input + p = Popen( command, stdin=stdin, stdout=stdout, stderr=stderr, @@ -112,9 +133,9 @@ def popen(command, flag='', shell=None, input=None, timeout=None, env=None, def run(command, flag='', shell=None, input=None, timeout=None, env=None, - stdout=DEVNULL, stderr=PIPE, decode='utf-8'): + stdout=DEVNULL, stderr=PIPE, decode='utf-8', autosudo=True): """ - A wrapper around vyos.util.popen, which discard the stdout and + A wrapper around popen, which discard the stdout and will return the error code of a command """ _, code = popen( @@ -128,14 +149,15 @@ def run(command, flag='', shell=None, input=None, timeout=None, env=None, def cmd(command, flag='', shell=None, input=None, timeout=None, env=None, - stdout=PIPE, stderr=PIPE, decode='utf-8', + stdout=PIPE, stderr=PIPE, decode='utf-8', autosudo=True, raising=None, message='', expect=[0]): """ - A wrapper around vyos.util.popen, which returns the stdout and + A wrapper around popen, which returns the stdout and will raise the error code of a command - raising: specify which call should be used when raising (default is OSError) + raising: specify which call should be used when raising the class should only require a string as parameter + (default is OSError) with the error code expect: a list of error codes to consider as normal """ decoded, code = popen( @@ -159,9 +181,9 @@ def cmd(command, flag='', shell=None, input=None, timeout=None, env=None, def call(command, flag='', shell=None, input=None, timeout=None, env=None, - stdout=PIPE, stderr=PIPE, decode='utf-8'): + stdout=PIPE, stderr=PIPE, decode='utf-8', autosudo=True): """ - A wrapper around vyos.util.popen, which print the stdout and + A wrapper around popen, which print the stdout and will return the error code of a command """ out, code = popen( |