From 32514c8f4f46e0f2156df80897c97e437335f483 Mon Sep 17 00:00:00 2001 From: Ryan Fairfax Date: Wed, 23 Feb 2022 02:02:01 +0100 Subject: [PATCH] shell: Add option to keep current environment When doing interactive development via kas shell it is often desirable to keep the user's customized configuration. The new --preserve-env argument has been added to support this scenario via an opt in flag. This flag is blocked when not running from a TTY or via kas-container and kas issues a warning to the user about potential unintended side effects when invoked. Signed-off-by: Ryan Fairfax Signed-off-by: Jan Kiszka --- kas-container | 4 ++++ kas/libcmds.py | 8 ++++++++ kas/plugins/shell.py | 25 ++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/kas-container b/kas-container index dff106b..094a89b 100755 --- a/kas-container +++ b/kas-container @@ -289,6 +289,10 @@ while [ $# -gt 0 ] && [ $KAS_EXTRA_BITBAKE_ARGS -eq 0 ]; do KAS_BITBAKE_C_OPTION_ARGS="$2" shift 2 ;; + -E|--preserve-env) + echo "$1 is not supported with kas-container" + exit 1 + ;; --) KAS_EXTRA_BITBAKE_ARGS=$# ;; diff --git a/kas/libcmds.py b/kas/libcmds.py index 35668fc..4a646f3 100644 --- a/kas/libcmds.py +++ b/kas/libcmds.py @@ -148,6 +148,14 @@ class SetupHome(Command): Sets up the home directory of kas. """ + # A list of environment variables that SETUP_HOME uses + # This should be kept up to date with any code in execute() + ENV_VARS = [ + 'GIT_CREDENTIAL_HELPER', + 'AWS_CONFIG_FILE', + 'AWS_SHARED_CREDENTIALS_FILE', + ] + def __init__(self): super().__init__() self.tmpdirname = tempfile.mkdtemp() diff --git a/kas/plugins/shell.py b/kas/plugins/shell.py index 2299618..5564da2 100644 --- a/kas/plugins/shell.py +++ b/kas/plugins/shell.py @@ -38,11 +38,12 @@ """ import logging +import os import subprocess import sys from kas.context import create_global_context from kas.config import Config -from kas.libcmds import Macro, Command +from kas.libcmds import Macro, Command, SetupHome from kas.libkas import setup_parser_common_args __license__ = 'MIT' @@ -70,6 +71,9 @@ class Shell: parser.add_argument('-c', '--command', help='Run command', default='') + parser.add_argument('-E', '--preserve-env', + help='Keep current user enviornment block', + action='store_true') def run(self, args): """ @@ -79,6 +83,25 @@ class Shell: ctx = create_global_context(args) ctx.config = Config(ctx, args.config) + if args.preserve_env: + # Warn if there's any settings that setup_home would apply + # but are now ignored + for var in SetupHome.ENV_VARS: + if var in os.environ: + logging.warning('Environment variable "%s" ignored ' + 'because user environment is being used', + var) + + if not os.isatty(sys.stdout.fileno()): + logging.error("Error: --preserve-env can only be " + "run from a tty") + sys.exit(1) + + ctx.environ = os.environ.copy() + + logging.warning("Preserving the current environment block may " + "have unintended side effects on the build.") + if args.keep_config_unchanged: # Skip the tasks which would change the config of the build # environment