From 7c209509ed02df960868c209b330b9bb8fe7ea04 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Sat, 20 May 2023 15:02:28 +0200 Subject: [PATCH] Refactor environment handling Signed-off-by: Georg Pfuetzenreuter --- scullery.py | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/scullery.py b/scullery.py index 41ad893..3af7d84 100644 --- a/scullery.py +++ b/scullery.py @@ -31,6 +31,7 @@ env = os.environ.copy() arggroup = argparser.add_mutually_exclusive_group() argparser.add_argument('--debug', help='Print extremely verbose output', action='store_const', dest='loglevel', const=logging.DEBUG, default=logging.INFO) argparser.add_argument('--config', help='Specify the configuration file to use', default='{}/scullery.ini'.format(os.path.abspath(os.path.dirname(__file__)))) +argparser.add_argument('--env', help='Write environment file for direct use of Vagrant', action='store_true') argparser.add_argument('--suite', help='Specify the suite to run', required=True) arggroup.add_argument('--stop', help='Stop running machines', action='store_true') arggroup.add_argument('--test', help='Start machines and run tests', action='store_true') @@ -76,25 +77,25 @@ def genvms(flavor, amount): vms.append('{}-{}{}'.format(vmprefix, flavor, i)) return vms -def vagrant_env(box_name, box_image, minions=None, masters=None, vagrantfile=None): - fh = open('.scullery_env', 'w') - if vagrantfile is not None: - env['VAGRANT_VAGRANTFILE'] = vagrantfile - fh.write('VAGRANT_VAGRANTFILE={}\n'.format(vagrantfile)) - env['SCULLERY_BOX_NAME'] = box_name - env['SCULLERY_BOX_IMAGE'] = box_image - fh.write('SCULLERY_BOX_NAME={}\nSCULLERY_BOX_IMAGE={}\n'.format(box_name, box_image)) - if masters is not None: - env_masters = ','.join(masters) - env['SCULLERY_MASTERS'] = env_masters - fh.write('SCULLERY_MASTERS={}\n'.format(env_masters)) - if minions is not None: - env_minions = ','.join(minions) - env['SCULLERY_MINIONS'] = env_minions - fh.write('SCULLERY_MINIONS={}\n'.format(env_minions)) - #log.debug('Environment is: {}'.format(str(env))) - fh.close() +def _setenv(envmap, dump=False): + if dump: + log.debug('Writing environment variable file') + fh = open('.scullery_env', 'w') + for variable, vlaue in envmap.items(): + if value is not None: + if isinstance(value, list): + value = ','.join(value) + env[variable] = value + if dump: + fh.write(f'{variable}={value}\n') + if dump: + fh.close() +def vagrant_env(box_name, box_image, minions=None, masters=None, vagrantfile=None): + envmap = {'VAGRANT_VAGRANTFILE': vagrantfile, 'SCULLERY_BOX_NAME': box_name, 'SCULLERY_BOX_IMAGE': box_image, + 'SCULLERY_MASTERS': masters, 'SCULLERY_MINIONS': minions} + log.debug('Environment variable map: {}'.format(str(envmap))) + _setenv(envmap, args.env) v.env = env def vagrant_isup(suite):