Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sudo_options & 'env' parameters to run_command #167

Merged
merged 9 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ The client here will eventually be released as "spython" (and eventually to
singularity on pypi), and the versions here will coincide with these releases.

## [master](https://github.com/singularityhub/singularity-cli/tree/master)
- add sudo_options option to Instance.start/stop and Client.execute (0.0.85)
- add environ option to Instance.start and Client.execute for passing env variables
- Small bugfix for docker writer and adding pyflakes for unused imports (0.0.84)
- Adding support for multistage build parsing (0.0.83)
- Singularity Python does not yet support multistage builds (0.0.82)
Expand Down
11 changes: 10 additions & 1 deletion spython/instance/cmd/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ def start(
name=None,
args=None,
sudo=False,
sudo_options=None,
options=None,
capture=False,
singularity_options=None,
environ=None,
):
"""start an instance. This is done by default when an instance is created.

Expand Down Expand Up @@ -77,7 +79,14 @@ def start(
self.args = args
self.cmd = cmd

output = run_command(cmd, sudo=sudo, quiet=True, capture=capture)
output = run_command(
cmd,
sudo=sudo,
sudo_options=sudo_options,
quiet=True,
capture=capture,
environ=environ,
)

if output["return_code"] == 0:
self._update_metadata()
Expand Down
11 changes: 9 additions & 2 deletions spython/instance/cmd/stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
from spython.logger import bot


def stop(self, name=None, sudo=False, timeout=None, singularity_options=None):
def stop(
self,
name=None,
sudo=False,
sudo_options=None,
timeout=None,
singularity_options=None,
):
"""stop an instance. This is done by default when an instance is created.

Parameters
Expand Down Expand Up @@ -42,7 +49,7 @@ def stop(self, name=None, sudo=False, timeout=None, singularity_options=None):
instance_name = name
cmd = cmd + [instance_name]

output = run_command(cmd, sudo=sudo, quiet=True)
output = run_command(cmd, sudo=sudo, sudo_options=sudo_options, quiet=True)

if output["return_code"] != 0:
message = "%s : return code %s" % (output["message"], output["return_code"])
Expand Down
8 changes: 7 additions & 1 deletion spython/main/base/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def run_command(
quiet=None,
return_result=False,
sudo_options=None,
environ=None,
):

"""run_command is a wrapper for the global run_command, checking first
Expand All @@ -130,7 +131,12 @@ def run_command(
quiet = self.quiet

result = run_cmd(
cmd, sudo=sudo, capture=capture, quiet=quiet, sudo_options=sudo_options
cmd,
sudo=sudo,
capture=capture,
quiet=quiet,
sudo_options=sudo_options,
environ=environ,
)

# If one line is returned, squash dimension
Expand Down
11 changes: 9 additions & 2 deletions spython/main/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def execute(
options=None,
singularity_options=None,
sudo=False,
sudo_options=None,
quiet=True,
environ=None,
):
""" execute: send a command to a container

Expand Down Expand Up @@ -99,9 +101,14 @@ def execute(

if not stream:
return self._run_command(
cmd, sudo=sudo, return_result=return_result, quiet=quiet
cmd,
sudo=sudo,
sudo_options=sudo_options,
return_result=return_result,
quiet=quiet,
environ=environ,
)
return stream_command(cmd, sudo=sudo)
return stream_command(cmd, sudo=sudo, sudo_options=sudo_options)

bot.exit("Please include a command (list) to execute.")

Expand Down
3 changes: 2 additions & 1 deletion spython/main/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def list_instances(
return_json=False,
quiet=False,
sudo=False,
sudo_options=None,
singularity_options=None,
):
"""list instances. For Singularity, this is provided as a command sub
Expand Down Expand Up @@ -53,7 +54,7 @@ def list_instances(
if name is not None:
cmd.append(name)

output = run_command(cmd, quiet=True, sudo=sudo)
output = run_command(cmd, quiet=True, sudo=sudo, sudo_options=sudo_options)
instances = []

# Success, we have instances
Expand Down
4 changes: 3 additions & 1 deletion spython/utils/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def run_command(
no_newline_regexp="Progess",
quiet=False,
sudo_options=None,
environ=None,
):

"""run_command uses subprocess to send a command to the terminal. If
Expand All @@ -179,7 +180,8 @@ def run_command(
stdout = subprocess.PIPE

# Use the parent stdout and stderr
process = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=stdout)

process = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=stdout, env=environ)
lines = []
found_match = False

Expand Down
2 changes: 1 addition & 1 deletion spython/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.


__version__ = "0.0.84"
__version__ = "0.0.85"
AUTHOR = "Vanessa Sochat"
AUTHOR_EMAIL = "vsochat@stanford.edu"
NAME = "spython"
Expand Down