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 <rfairfax@linux.microsoft.com> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
		| @@ -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() | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user