risotto/sbin/risotto_display

236 lines
8.8 KiB
Text
Raw Permalink Normal View History

2022-12-21 16:35:58 +01:00
#!/usr/bin/env python3
from tabulate import tabulate
from argparse import ArgumentParser
from rougail.utils import normalize_family
from tiramisu.error import PropertiesOptionError
from risotto.machine import load, remove_cache, ROUGAIL_NAMESPACE
HIDE_SECRET = True
def list_to_string(lst):
if isinstance(lst, list):
return "\n".join([str(val) for val in lst])
return lst
2023-06-22 16:19:44 +02:00
def get_files_subelements(type_name, element, files_subelement, files_cols):
2022-12-21 16:35:58 +01:00
data = {}
2023-06-22 16:19:44 +02:00
if not element.option('activate').value.get():
2022-12-21 16:35:58 +01:00
return data
for subelement in files_subelement.values():
if subelement['type'] == 'subelement':
try:
2023-06-22 16:19:44 +02:00
value = list_to_string(element.option(subelement['key']).value.get())
2022-12-21 16:35:58 +01:00
# FIXME except AttributeError:
except Exception:
value = ''
elif subelement['type'] == 'information':
2023-06-22 16:19:44 +02:00
value = element.information.get(subelement['key'], '')
2022-12-21 16:35:58 +01:00
elif subelement['type'] == 'none':
value = subelement['value']
else:
raise Exception('unknown subelement')
if value != '':
files_cols.add(subelement['key'])
data[subelement['key']] = value
if type_name == 'overrides':
data['name'] = f'/systemd/system/{data["source"]}.d/rougail.conf'
if not data['engine']:
data['engine'] = 'none'
elif not data['engine']:
data['engine'] = 'cheetah'
return data
2023-06-22 16:19:44 +02:00
def services(config, values):
2022-12-21 16:35:58 +01:00
files_subelement = {'Source': {'key': 'source', 'type': 'information'},
'Nom': {'key': 'name', 'type': 'subelement'},
'Variable': {'key': 'variable', 'type': 'subelement'},
'Propriétaire': {'key': 'owner', 'type': 'subelement'},
'Groupe': {'key': 'group', 'type': 'subelement'},
'Mode': {'key': 'mode', 'type': 'subelement'},
'Moteur': {'key': 'engine', 'type': 'information'},
}
disabled_services = []
2023-06-22 16:19:44 +02:00
for service in config.option.list(type="all"):
doc = service.option.doc()
2022-12-21 16:35:58 +01:00
files_lst = []
files_cols = set()
2023-06-22 16:19:44 +02:00
if not service.option('manage').value.get():
2022-12-21 16:35:58 +01:00
doc += " - unmanaged"
2023-06-22 16:19:44 +02:00
if not service.option('activate').value.get():
2022-12-21 16:35:58 +01:00
disabled_services.append([doc])
else:
2023-06-22 16:19:44 +02:00
for type in service.list(type="all"):
type_name = type.option.doc()
2022-12-21 16:35:58 +01:00
if type_name in ['files', 'overrides']:
2023-06-22 16:19:44 +02:00
for element in type.list(type="all"):
data = get_files_subelements(type_name, element, files_subelement, files_cols)
2022-12-21 16:35:58 +01:00
if data:
files_lst.append(data)
elif type_name == 'manage':
pass
elif type_name == 'activate':
2023-06-22 16:19:44 +02:00
if not type.value.get():
2022-12-21 16:35:58 +01:00
doc += " - unactivated"
else:
print("FIXME " + type_name)
if files_lst:
keys = [key for key, val in files_subelement.items() if val['key'] in files_cols]
values[doc] = {'keys': keys, 'lst': []}
for lst in files_lst:
values[doc]['lst'].append([val for key, val in lst.items() if key in files_cols])
if disabled_services:
values["Services désactivés"] = {'keys': ['Nom'], 'lst': disabled_services}
2023-06-22 16:19:44 +02:00
def table_leader(config, read_only):
2022-12-21 16:35:58 +01:00
keys = ['Description']
if read_only:
keys.append('Cachée')
2023-06-22 16:19:44 +02:00
leadership_lst = config.list(type="all")
2022-12-21 16:35:58 +01:00
leader = leadership_lst.pop(0)
2023-06-22 16:19:44 +02:00
leader_owner = leader.owner.get()
follower_names = [follower.option.name() for follower in leadership_lst]
doc = leader.option.doc()
properties = leader.property.get()
2022-12-21 16:35:58 +01:00
if 'mandatory' in properties:
doc += '*'
2023-06-22 16:19:44 +02:00
name = leader.option.name()
2022-12-21 16:35:58 +01:00
lst = [[f'{doc} ({name})']]
if read_only:
if 'hidden' in properties:
hidden = 'oui'
else:
hidden = ''
lst[0].append(hidden)
2023-06-22 16:19:44 +02:00
for idx, leader_value in enumerate(leader.value.get()):
2022-12-21 16:35:58 +01:00
keys.append(f'Valeur {idx}')
keys.append(f'Utilisateur {idx}')
lst[0].append(leader_value)
lst[0].append(leader_owner)
for follower_idx, follower_name in enumerate(follower_names):
follower_option = config.option(follower_name, idx)
if idx == 0:
2023-06-22 16:19:44 +02:00
doc = follower_option.option.doc()
properties = follower_option.property.get()
2022-12-21 16:35:58 +01:00
if 'mandatory' in properties:
doc += '*'
2023-06-22 16:19:44 +02:00
name = follower_option.option.name()
2022-12-21 16:35:58 +01:00
lst.append([f'{doc} ({name})'])
if read_only:
if 'hidden' in properties:
hidden = 'oui'
else:
hidden = ''
lst[-1].append(hidden)
try:
2023-06-22 16:19:44 +02:00
lst[follower_idx + 1].append(list_to_string(follower_option.value.get()))
lst[follower_idx + 1].append(follower_option.owner.get())
2022-12-21 16:35:58 +01:00
except PropertiesOptionError:
pass
# leader = next leader_iter
# if master_values is None:
2023-06-22 16:19:44 +02:00
# master_values = subconfig.value.get()
2022-12-21 16:35:58 +01:00
return {'keys': keys, 'lst': lst}
2023-06-22 16:19:44 +02:00
def table(config, prefix_len, values, read_only):
2022-12-21 16:35:58 +01:00
lst = []
2023-06-22 16:19:44 +02:00
for subconfig in config.option.list(type="all"):
2022-12-21 16:35:58 +01:00
# prefix = prefix_len * 2 * ' '
2023-06-22 16:19:44 +02:00
# if subconfig.option.isoptiondescription():
2022-12-21 16:35:58 +01:00
# prefix += '=>'
# else:
# prefix += '-'
# display_str = f'{prefix} {description}'
# if name != description:
# display_str = f'{display_str} ({name})'
2023-06-22 16:19:44 +02:00
name = subconfig.option.name()
doc = subconfig.option.doc()
2022-12-21 16:35:58 +01:00
if prefix_len == 0 and ROUGAIL_NAMESPACE != name:
doc = doc.capitalize()
if prefix_len == 0 and name == 'services':
values['Services'] = {}
2023-06-22 16:19:44 +02:00
services(subconfig, values['Services'])
elif subconfig.option.isoptiondescription():
od_name = f'{doc} ({(subconfig.option.path()).split(".", 1)[1]})'
2022-12-21 16:35:58 +01:00
values[od_name] = None
2023-06-22 16:19:44 +02:00
if subconfig.option.isleadership():
values[od_name] = table_leader(subconfig, read_only)
2022-12-21 16:35:58 +01:00
else:
2023-06-22 16:19:44 +02:00
values[od_name] = table(subconfig, prefix_len + 1, values, read_only)
2022-12-21 16:35:58 +01:00
else:
2023-06-22 16:19:44 +02:00
value = list_to_string(subconfig.value.get())
doc = subconfig.option.doc()
properties = subconfig.property.get()
2022-12-21 16:35:58 +01:00
if 'mandatory' in properties:
doc += '*'
2023-06-22 16:19:44 +02:00
name = subconfig.option.name()
2022-12-21 16:35:58 +01:00
lst.append([f'{doc} ({name})', value])
if read_only:
if 'hidden' in properties:
hidden = 'oui'
else:
hidden = ''
lst[-1].append(hidden)
2023-06-22 16:19:44 +02:00
lst[-1].append(subconfig.owner.get())
2022-12-21 16:35:58 +01:00
keys = ['Description', 'Valeur']
if read_only:
keys.append('Cachée')
keys.append('Utilisateur')
return {'keys': keys, 'lst': lst}
2023-06-22 16:19:44 +02:00
def main():
2022-12-21 16:35:58 +01:00
parser = ArgumentParser()
parser.add_argument('server_name')
parser.add_argument('--read_only', action='store_true')
parser.add_argument('--nocache', action='store_true')
2023-02-27 14:03:56 +01:00
parser.add_argument('--debug', action='store_true')
2022-12-21 16:35:58 +01:00
args = parser.parse_args()
if args.nocache:
remove_cache()
values = {}
server_name = args.server_name
2023-06-22 16:19:44 +02:00
config = load(hide_secret=HIDE_SECRET,
2022-12-21 16:35:58 +01:00
original_display_name=True,
valid_mandatories=args.read_only,
)
if not args.read_only:
2023-06-22 16:19:44 +02:00
config.property.read_write()
2022-12-21 16:35:58 +01:00
root_option = config.option(normalize_family(server_name))
try:
2023-06-22 16:19:44 +02:00
root_option.option.get()
2022-12-21 16:35:58 +01:00
except AttributeError:
2023-06-22 16:19:44 +02:00
exit(f'Unable to find {server_name} configuration: {[o.option.description() for o in config.option.list(type="optiondescription")]}')
table(root_option, 0, values, args.read_only)
2022-12-21 16:35:58 +01:00
for title, dico in values.items():
if title == 'Services':
if not dico:
continue
print()
print(title)
print('=' * len(title))
print()
for subtitle, dic in dico.items():
print()
print(' ' + subtitle)
print(' ' + '-' * len(subtitle))
print()
print(tabulate(dic['lst'], headers=dic['keys'], tablefmt="fancy_grid"))
elif dico['lst']:
print()
print(title)
print('=' * len(title))
print()
print(tabulate(dico['lst'], headers=dico['keys'], tablefmt="fancy_grid"))
2023-06-22 16:19:44 +02:00
main()