linuxinstaller/libreticmenu/libreticmenu.py

175 lines
5.6 KiB
Python
Raw Normal View History

2024-09-30 19:32:52 +02:00
from InquirerPy import inquirer
from InquirerPy import prompt
from rich.console import Console
from rich.table import Table
2024-10-06 23:44:04 +02:00
import AnsibleActions
import PythonActions
import GitActions
2024-09-30 19:32:52 +02:00
import argparse
import yaml
import syslog
import os
### Cartouche de status
def print_status():
# Récupération des infos
2024-10-06 23:44:04 +02:00
hostname = PythonActions.getHostName()
domain = PythonActions.getDomain()
currentBranch = GitActions.getBranch()
2024-09-30 19:32:52 +02:00
# Affichage du statut
console = Console()
console.print()
2024-10-06 23:44:04 +02:00
console.print("=== Menu de configuration Libretic ===", style="bold red")
2024-09-30 19:32:52 +02:00
console.print()
table = Table(show_header=True, header_style="bold dim")
table.add_column("Paramètre", style="bold magenta")
table.add_column("Valeur")
table.add_column("Statut", justify="center")
table.add_row("Environnement du poste", currentBranch, "[green]OK" if currentBranch == "main" else "[yellow]Attention")
table.add_row("Hostname", hostname, "[green]OK" if hostname != "unassigned-hostname" else "[red]KO")
console.print(table)
console.print()
# Chargement du fichier de configuration
def readConfig(configFile):
try:
with open(configFile, "r") as file:
config = yaml.safe_load(file)
syslog.syslog(syslog.LOG_DEBUG, "Lecture du fichier de configuration {0}, contient: {1}".format(configFile, config))
return config
except:
syslog.syslog(syslog.LOG_DEBUG, "Impossible de lire le fichier de configuration {0}".format(configFile))
raise
2024-10-06 23:44:04 +02:00
### Menu du choix hostname
def hostnameChoice():
2024-09-30 19:32:52 +02:00
questions = [
{
"type": "input",
2024-10-06 23:44:04 +02:00
"message": "Renseigner le nom de la machine : ",
2024-09-30 19:32:52 +02:00
},
]
result = prompt(questions)
2024-10-06 23:44:04 +02:00
fqdn = result[0] + "." + result[1]
2024-09-30 19:32:52 +02:00
proceed = inquirer.confirm(
2024-10-06 23:44:04 +02:00
message ="Définir {} comme nom de machine ?".format(fqdn),
2024-09-30 19:32:52 +02:00
confirm_letter = "o",
default=False).execute()
if proceed:
2024-10-06 23:44:04 +02:00
PythonActions.setFQDN(fqdn)
2024-09-30 19:32:52 +02:00
else:
print("Annulation")
2024-10-07 14:05:53 +02:00
### Menu du choix playbook externe
def extPlaybookChoice():
url = inquirer.text(
2024-10-08 12:19:49 +02:00
message = "Renseigner l'url du projet git (exemple : https://adresse-serveur-git/projet.git) :",
2024-10-07 14:05:53 +02:00
validate = lambda url :
len(url) > 0 and
(url.startswith("http://") or url.startswith("https://")) and
2024-10-08 12:19:49 +02:00
url.endswith(".git"),
invalid_message = "Une url de projet git doit commencer par http(s):// et se terminer par .git"
2024-10-07 14:05:53 +02:00
).execute()
playbook = inquirer.text(
2024-10-08 12:19:49 +02:00
message = "Renseigner le nom du playbook :",
2024-10-07 14:05:53 +02:00
validate = lambda playbook :
len(playbook) > 0 and
2024-10-08 12:19:49 +02:00
(playbook.endswith(".yaml") or playbook.endswith(".yml")),
invalid_message = "Un nom de playbook doit se terminer par .yml ou .yaml"
2024-10-07 14:05:53 +02:00
).execute()
proceed = inquirer.confirm(
2024-10-08 12:19:49 +02:00
message ="Confirmer l'exécution du playbook {} du projet {} ?".format(playbook, url),
2024-10-07 14:05:53 +02:00
confirm_letter = "o",
default=False).execute()
if proceed:
2024-10-08 12:19:49 +02:00
AnsibleActions.pullAnsiblePlaybook(url, playbook)
2024-10-07 14:05:53 +02:00
else:
print("Annulation")
2024-10-06 23:44:04 +02:00
def branchChoice():
2024-09-30 19:32:52 +02:00
questions = [
{
"type": "list",
2024-10-06 23:44:04 +02:00
"message": "Choisir le type d'environnement de ce poste :",
"choices": GitActions.getBranchList(),
"default": "main",
2024-09-30 19:32:52 +02:00
},
]
result = prompt(questions)
proceed = inquirer.confirm(
2024-10-06 23:44:04 +02:00
message ="Définir {} comme environnement pour ce poste ?".format(result[0]),
2024-09-30 19:32:52 +02:00
confirm_letter = "o",
default=False).execute()
if proceed:
2024-10-06 23:44:04 +02:00
GitActions.setBranch(result[0])
console = Console()
console.print()
console.print("=== Relancer libreticmenu.sh pour bénéficier du changement d'environnement ===", style="bold red")
console.print()
exit()
2024-09-30 19:32:52 +02:00
else:
print("Annulation")
# Menu principal
def main():
2024-10-06 23:44:04 +02:00
config = readConfig("config.yml")
currentBranch = GitActions.getBranch()
parser = argparse.ArgumentParser(prog="libreticmenu.py", description="Menu de configuration Libretic")
for entry in config['ansiblemenu']:
parser.add_argument("--" + entry, help=config['ansiblemenu'][entry]['argument_help'], action='store_true')
2024-09-30 19:32:52 +02:00
args = parser.parse_args()
menu_main = {
"Définir le hostname": hostnameChoice,
}
2024-10-06 23:44:04 +02:00
for entry in config['ansiblemenu']:
if not config['ansiblemenu'][entry].get('menu_hidden', False):
menu_main = menu_main | {
2024-10-08 12:19:49 +02:00
config['ansiblemenu'][entry]['menu_title']: lambda e=entry: AnsibleActions.runAnsiblePlaybook(config, e)
2024-10-06 23:44:04 +02:00
}
menu_main = menu_main | {
2024-10-08 12:19:49 +02:00
"Exécuter un playbook d'un projet spécifique": extPlaybookChoice,
2024-10-06 23:44:04 +02:00
"Changer de branche": branchChoice,
"Quitter": None
}
2024-09-30 19:32:52 +02:00
2024-10-06 23:44:04 +02:00
# Exécute les options passées sur la ligne de commande
cmdlineOptionPassed = False
for entry in config['ansiblemenu']:
if entry in list(arg for arg in vars(args) if getattr(args, arg)):
2024-10-08 12:19:49 +02:00
AnsibleActions.runAnsiblePlaybook(config, entry)
2024-10-06 23:44:04 +02:00
cmdlineOptionPassed = True
2024-09-30 19:32:52 +02:00
2024-10-06 23:44:04 +02:00
while not cmdlineOptionPassed:
2024-09-30 19:32:52 +02:00
print_status()
console = Console()
console.print("Choisir une des options suivantes", style="bold blue")
action = inquirer.select(
message = "Que souhaitez-vous faire ?",
choices = menu_main.keys(),
default = None).execute()
if menu_main[action] != None:
2024-10-06 23:44:04 +02:00
menu_main[action]()
2024-09-30 19:32:52 +02:00
else:
break
if __name__ == "__main__":
main()