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:
Ryan Fairfax 2022-02-23 02:02:01 +01:00 committed by Jan Kiszka
parent 3d35fe2f60
commit 32514c8f4f
3 changed files with 36 additions and 1 deletions

View File

@ -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=$#
;;

View File

@ -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()

View File

@ -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