diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-06-17 11:38:13 +0100 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-06-17 12:14:06 +0100 |
commit | 19ddeaafc678110dd6d9429fb11a57825f72650a (patch) | |
tree | 1b8f98efafb009d097fc55e7b41097e5971c446e | |
parent | 776019ae8df349cb35fdeb2c37e2e8561bcad31b (diff) | |
download | sandboxlib-19ddeaafc678110dd6d9429fb11a57825f72650a.tar.gz |
Allow overriding backend autodetection with SANDBOXLIB_BACKEND variable
This fixes https://github.com/CodethinkLabs/sandboxlib/issues/3
-rwxr-xr-x | run-sandbox | 17 | ||||
-rw-r--r-- | sandboxlib/__init__.py | 43 |
2 files changed, 42 insertions, 18 deletions
diff --git a/run-sandbox b/run-sandbox index 6bd44c0..aa7c713 100755 --- a/run-sandbox +++ b/run-sandbox @@ -31,21 +31,6 @@ def info(message, *args): logging.info(message % args) -def get_executor(name): - # Convert the name into a valid Python module name. This is a convenience - # for users just because '-' is easier to type than '_'. - name = name.replace('-', '_') - - try: - executor = getattr(sandboxlib, name) - except AttributeError: - raise RuntimeError( - "%s is not a known executor in this version of 'sandboxlib'." % - name) - - return executor - - def parse_args(): parser = argparse.ArgumentParser(description="Run something in a sandbox.") @@ -72,7 +57,7 @@ def parse_args(): def run(): args = parse_args() - executor = get_executor(args.executor) + executor = sandboxlib.get_executor(args.executor) if sandboxlib.load.appc.is_app_container_image(args.sandbox): info("%s is an App Container image." % args.sandbox) diff --git a/sandboxlib/__init__.py b/sandboxlib/__init__.py index 8179d72..11140b1 100644 --- a/sandboxlib/__init__.py +++ b/sandboxlib/__init__.py @@ -27,6 +27,7 @@ import os import platform import pipes import subprocess +import warnings class ProgramNotFound(Exception): @@ -123,14 +124,52 @@ def run_sandbox_with_redirection(command, **sandbox_config): raise NotImplementedError() +def get_executor(name): + '''Return the execution module with the given name. + + KeyError is raised if the backend isn't found. + + This function will, for convenience, convert '-' to '_'. This means + "linux-user-chroot" will return the "linux_user_chroot" backend, instead of + raising an error. + + ''' + + name = name.replace('-', '_') + + try: + executor = getattr(sandboxlib, name) + except AttributeError: + raise KeyError( + "%s is not a known executor in this version of 'sandboxlib'." % + name) + + return executor + + def executor_for_platform(): - '''Returns an execution module that will work on the current platform.''' + '''Returns an execution module that will work on the current platform. + + The autodetection can be overridden by setting SANDBOXLIB_BACKEND in the + environment of the process, which can be useful for testing and debugging. + + ''' log = logging.getLogger("sandboxlib") backend = None - if platform.uname()[0] == 'Linux': + if 'SANDBOXLIB_BACKEND' in os.environ: + backend_name = os.environ['SANDBOXLIB_BACKEND'] + logging.info("Got %s from SANDBOXLIB_BACKEND variable.", backend_name) + try: + backend = get_executor(backend_name) + except KeyError: + warnings.warn( + "SANDBOXLIB_BACKEND environment variable is set to an invalid " + "value %s." % backend_name) + + if backend is None and platform.uname()[0] == 'Linux': log.info("Linux detected, looking for 'linux-user-chroot'.") try: program = sandboxlib.linux_user_chroot.linux_user_chroot_program() |