From 3df365739c5102e162a3d686fa66ec72cd9c40b8 Mon Sep 17 00:00:00 2001 From: James Falcon Date: Mon, 16 Nov 2020 15:57:38 -0600 Subject: [PATCH 1/2] Add sudo option to instance execute --- pycloudlib/instance.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pycloudlib/instance.py b/pycloudlib/instance.py index f8923f0a..ac23fced 100644 --- a/pycloudlib/instance.py +++ b/pycloudlib/instance.py @@ -138,7 +138,8 @@ def _run_command(self, command, stdin): """Run command in the instance.""" return self._ssh(list(command), stdin=stdin) - def execute(self, command, stdin=None, description=None, **kwargs): + def execute(self, command, stdin=None, description=None, *, use_sudo=False, + **kwargs): """Execute command in instance, recording output, error and exit code. Assumes functional networking and execution with the target filesystem @@ -150,6 +151,7 @@ def execute(self, command, stdin=None, description=None, **kwargs): `['sh', '-c', command]` stdin: bytes content for standard in description: purpose of command + use_sudo: boolean to run the command as sudo Returns: Result object @@ -157,6 +159,8 @@ def execute(self, command, stdin=None, description=None, **kwargs): """ if isinstance(command, str): command = ['sh', '-c', command] + if use_sudo: + command.insert(0, 'sudo') self._log.info('executing: %s', shell_quote(command)) if description: From e9791bae7cfece5f73dbfec895d331a653dde986 Mon Sep 17 00:00:00 2001 From: James Falcon Date: Tue, 17 Nov 2020 09:17:35 -0600 Subject: [PATCH 2/2] [squash] Adding -- to sudo per review --- pycloudlib/instance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pycloudlib/instance.py b/pycloudlib/instance.py index ac23fced..68b99233 100644 --- a/pycloudlib/instance.py +++ b/pycloudlib/instance.py @@ -160,7 +160,7 @@ def execute(self, command, stdin=None, description=None, *, use_sudo=False, if isinstance(command, str): command = ['sh', '-c', command] if use_sudo: - command.insert(0, 'sudo') + command = ['sudo', '--'] + command self._log.info('executing: %s', shell_quote(command)) if description: