Improve option handling

- move bootstrap script to configuration option
- repair typo
- handle undefined but required options gracefully

Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
This commit is contained in:
Georg Pfuetzenreuter 2023-05-20 17:51:32 +02:00
parent 7c209509ed
commit 42235b9d62
Signed by: Georg
GPG Key ID: 1ED2F138E7E6FF57
2 changed files with 45 additions and 24 deletions

View File

@ -6,7 +6,8 @@ if ! ( ENV['SCULLERY_BOX_NAME'] && ENV['SCULLERY_BOX_IMAGE'] ) || ! ( ENV['SCULL
exit 1 exit 1
end end
salt_bootstrap = "test/bootstrap-salt-roots.sh" salt_bootstrap = ENV['SCULLERY_BOOTSTRAP']
salt_file_roots = 'file_roots:\n base:\n - /srv/salt\n - /srv/formulas\n'
Vagrant.configure("2") do |config| Vagrant.configure("2") do |config|
config.vm.provider "libvirt" config.vm.provider "libvirt"
@ -22,10 +23,12 @@ Vagrant.configure("2") do |config|
master_config.vm.provider :libvirt do |libvirt| master_config.vm.provider :libvirt do |libvirt|
libvirt.memory = 768 libvirt.memory = 768
end end
if salt_bootstrap
master_config.vm.provision "shell", path: salt_bootstrap master_config.vm.provision "shell", path: salt_bootstrap
master_config.vm.provision "shell", inline: <<-SHELL end
master_config.vm.provision "shell", env: {'SALT_FILE_ROOTS': salt_file_roots}, inline: <<-SHELL
printf 'auto_accept: True\n' > /etc/salt/master.d/notsecure.conf printf 'auto_accept: True\n' > /etc/salt/master.d/notsecure.conf
printf 'file_roots:\n base:\n - /srv/salt\n - /srv/formulas\n' > /etc/salt/master.d/roots.conf printf "$SALT_FILE_ROOTS" > /etc/salt/master.d/roots.conf
systemctl enable --now salt-master systemctl enable --now salt-master
SHELL SHELL
end end
@ -42,9 +45,14 @@ Vagrant.configure("2") do |config|
systemctl enable --now salt-minion systemctl enable --now salt-minion
SHELL SHELL
else else
minion_config.vm.provision "shell", env: {'SALT_FILE_ROOTS': salt_file_roots}, inline: <<-SHELL
printf "$SALT_FILE_ROOTS" > /etc/salt/minion.d/roots.conf
SHELL
if salt_bootstrap
minion_config.vm.provision "shell", path: salt_bootstrap minion_config.vm.provision "shell", path: salt_bootstrap
end end
end end
end end
end end
end
end end

51
scullery.py Normal file → Executable file
View File

@ -18,19 +18,13 @@ import logging
import os import os
import sys import sys
try:
import vagrant
except ImportError as myerror:
print('Could not load python-vagrant')
sys.exit(1)
argparser = ArgumentParser() argparser = ArgumentParser()
config = ConfigParser() config = ConfigParser()
env = os.environ.copy() env = os.environ.copy()
arggroup = argparser.add_mutually_exclusive_group() 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('--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('--config', help='Specify the configuration file to use', default='{}/scullery.ini'.format(os.getcwd()))
argparser.add_argument('--env', help='Write environment file for direct use of Vagrant', action='store_true') 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) 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('--stop', help='Stop running machines', action='store_true')
@ -38,7 +32,7 @@ arggroup.add_argument('--test', help='Start machines and run tests', action='sto
arggroup.add_argument('--status', help='Get Vagrant deployment status', action='store_true') arggroup.add_argument('--status', help='Get Vagrant deployment status', action='store_true')
args = argparser.parse_args() args = argparser.parse_args()
config.read(args.config) configfile = args.config
vmprefix = 'scullery' vmprefix = 'scullery'
@ -81,7 +75,7 @@ def _setenv(envmap, dump=False):
if dump: if dump:
log.debug('Writing environment variable file') log.debug('Writing environment variable file')
fh = open('.scullery_env', 'w') fh = open('.scullery_env', 'w')
for variable, vlaue in envmap.items(): for variable, value in envmap.items():
if value is not None: if value is not None:
if isinstance(value, list): if isinstance(value, list):
value = ','.join(value) value = ','.join(value)
@ -91,9 +85,9 @@ def _setenv(envmap, dump=False):
if dump: if dump:
fh.close() fh.close()
def vagrant_env(box_name, box_image, minions=None, masters=None, vagrantfile=None): def vagrant_env(box_name, box_image, minions=None, masters=None, vagrantfile=None, bootstrap=None):
envmap = {'VAGRANT_VAGRANTFILE': vagrantfile, 'SCULLERY_BOX_NAME': box_name, 'SCULLERY_BOX_IMAGE': box_image, envmap = {'VAGRANT_VAGRANTFILE': vagrantfile, 'SCULLERY_BOX_NAME': box_name, 'SCULLERY_BOX_IMAGE': box_image,
'SCULLERY_MASTERS': masters, 'SCULLERY_MINIONS': minions} 'SCULLERY_MASTERS': masters, 'SCULLERY_MINIONS': minions, 'SCULLERY_BOOTSTRAP': bootstrap}
log.debug('Environment variable map: {}'.format(str(envmap))) log.debug('Environment variable map: {}'.format(str(envmap)))
_setenv(envmap, args.env) _setenv(envmap, args.env)
v.env = env v.env = env
@ -118,6 +112,12 @@ def vagrant_isup(suite):
def main_interactive(): def main_interactive():
configmap = _config() configmap = _config()
box = configmap['box'] box = configmap['box']
box_name = box.get('name', None)
box_image = box.get('image', None)
box_file = box.get('file', '{}/Vagrantfile-Template'.format(os.path.abspath(os.path.dirname(__file__))))
if any([box_name, box_image, box_file]) is None:
_abort('Box configuration is incomplete')
box_bootstrap = box.get('bootstrap', None)
suites = configmap['suites'] suites = configmap['suites']
suite = args.suite suite = args.suite
if suite not in suites: if suite not in suites:
@ -129,12 +129,12 @@ def main_interactive():
minions = genvms('minion', suiteconf['minions']) minions = genvms('minion', suiteconf['minions'])
if suiteconf.get('masters', 0) > 0: if suiteconf.get('masters', 0) > 0:
masters = genvms('master', suiteconf['masters']) masters = genvms('master', suiteconf['masters'])
vagrant_env(box['name'], box['image'], minions, masters, box['file']) vagrant_env(box_name, box_image, minions, masters, box_file, box_bootstrap)
if args.status: if args.status:
log.info('Status report: {}'.format(v.status())) log.info('Status report: {}'.format(v.status()))
return True return True
status = vagrant_isup(suite) status = vagrant_isup(suite)
if status[0] is True and status[1] is None: if status[0] is True and status[1] is None or args.stop:
if args.stop is True: if args.stop is True:
log.info('Destroying machines ...') log.info('Destroying machines ...')
v.destroy() v.destroy()
@ -163,17 +163,30 @@ def main_interactive():
else: else:
_abort('Start failed') _abort('Start failed')
logging.basicConfig(format='%(asctime)s %(levelname)s - %(funcName)s: %(message)s', datefmt='%H:%M:%S')
log = logging.getLogger('scullery')
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s %(levelname)s - %(funcName)s: %(message)s', datefmt='%H:%M:%S')
log = logging.getLogger('scullery')
log.setLevel(args.loglevel) log.setLevel(args.loglevel)
log.debug(args) log.debug(args)
if args.loglevel == logging.INFO: if args.loglevel == logging.WARNING:
log_stderr = True quiet_stderr = True
else: else:
log_stderr = False quiet_stderr = False
v = _vagrant(log_stderr) log.debug('Vagrant stderr: {}'.format(str(quiet_stderr)))
try:
import vagrant
except ImportError as myerror:
_abort('Could not load python-vagrant')
if os.path.isfile(configfile):
config.read(configfile)
else:
_abort('Unable to locate configuration file at {}'.format(configfile))
if __name__ == '__main__':
v = _vagrant(quiet_stderr)
main_interactive() main_interactive()
else: else:
v = _vagrant() v = _vagrant()