This commit is contained in:
Gilles Grandou 2023-12-04 21:02:22 +01:00
parent 61ab8938a4
commit bb8695119f
1 changed files with 37 additions and 24 deletions

View File

@ -12,32 +12,35 @@ import subprocess
from pprint import pprint
def read_yaml(conf_name):
yaml_list = [
def find_config_file(user_conf):
conf_list = [
'runon.yaml',
'.runon.yaml',
os.path.join(xdg.BaseDirectory.xdg_config_home, 'runon', 'runon.yaml'),
os.path.join(xdg.BaseDirectory.xdg_config_home, 'runon', 'runon.default.yaml')
]
if conf_name:
yaml_list.insert(0, conf_name)
for yaml_file in yaml_list:
if not os.path.exists(yaml_file):
continue
try:
with open(yaml_file, 'r') as file:
conf = yaml.safe_load(file)
conf['stamp'] = datetime.datetime.fromtimestamp(os.path.getmtime(yaml_file), tz=pytz.UTC)
return conf
except yaml.YAMLError as e:
print(f'ERROR: bad configuration file:')
print(e)
sys.exit(1)
if user_conf:
conf_list = [ user_conf ]
for conf in conf_list:
if os.path.exists(conf):
return conf
return None
def list_osnames(user_confname):
conf = read_yaml(user_confname)
def read_yaml(conf_file):
try:
with open(conf_file, 'r') as file:
conf = yaml.safe_load(file)
conf['stamp'] = datetime.datetime.fromtimestamp(os.path.getmtime(conf_file), tz=pytz.UTC)
return conf
except yaml.YAMLError as e:
print(f'ERROR: bad configuration file:')
print(e)
sys.exit(1)
return conf
def list_osnames(conf_file):
conf = read_yaml(conf_file)
osnames = []
for key in conf:
if (type(conf[key]) is dict) and conf[key].get('image'):
@ -45,14 +48,14 @@ def list_osnames(user_confname):
return osnames
def load_config(conf_name, osname):
def load_config(conf_file, osname):
user_vars = {
'osname': osname,
'user': getpass.getuser(),
'uid': os.getuid(),
'home': pathlib.Path.home(),
}
conf = read_yaml(conf_name)
conf = read_yaml(conf_file)
osconf = conf.get(osname)
if not osconf:
print(f"ERROR: cannot find configuration for distribution {osname}")
@ -181,7 +184,8 @@ def main():
parser.description = 'run commands on any distribution'
parser.add_argument('osname',
help = 'distribution name to run on, '
'"list" to dump all available distributions')
'"list" to dump all available distributions, '
'"edit" to open the current config file in a text editor.')
parser.epilog = '(c) 2021 Gilles Grandou <gilles@grandou.net>'
parser.add_argument('-v', '--verbose', action='store_true',
@ -199,18 +203,27 @@ def main():
if osname:
args.osname = osname
conf_file = find_config_file(args.config)
if not conf_file:
print('ERROR: config file not found')
sys.exit(1)
if args.osname == 'list':
osnames = list_osnames(args.config)
osnames = list_osnames(conf_file)
print('Available distributions:')
for o in sorted(osnames):
print(' {}'.format(o))
print()
return 0
elif args.osname == 'edit':
cmd = [ 'xdg-open', conf_file ]
ret = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL);
return 0
if args.link:
make_osname_link(sys.argv[0], args.osname)
conf = load_config(args.config, args.osname)
conf = load_config(conf_file, args.osname)
image_name = build_image(conf, args.update, args.verbose)
ret = run_image(image_name, conf, args.command, args.verbose)
return ret