174 lines
5.6 KiB
Python
174 lines
5.6 KiB
Python
from InquirerPy import inquirer
|
|
from InquirerPy import prompt
|
|
from rich.console import Console
|
|
from rich.table import Table
|
|
import AnsibleActions
|
|
import PythonActions
|
|
import GitActions
|
|
import argparse
|
|
import yaml
|
|
import syslog
|
|
import os
|
|
|
|
|
|
### Cartouche de status
|
|
|
|
def print_status():
|
|
# Récupération des infos
|
|
hostname = PythonActions.getHostName()
|
|
domain = PythonActions.getDomain()
|
|
currentBranch = GitActions.getBranch()
|
|
|
|
# Affichage du statut
|
|
console = Console()
|
|
console.print()
|
|
console.print("=== Menu de configuration Libretic ===", style="bold red")
|
|
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
|
|
|
|
|
|
### Menu du choix hostname
|
|
def hostnameChoice():
|
|
questions = [
|
|
{
|
|
"type": "input",
|
|
"message": "Renseigner le nom de la machine : ",
|
|
},
|
|
]
|
|
result = prompt(questions)
|
|
fqdn = result[0] + "." + result[1]
|
|
|
|
proceed = inquirer.confirm(
|
|
message ="Définir {} comme nom de machine ?".format(fqdn),
|
|
confirm_letter = "o",
|
|
default=False).execute()
|
|
if proceed:
|
|
PythonActions.setFQDN(fqdn)
|
|
else:
|
|
print("Annulation")
|
|
|
|
### Menu du choix playbook externe
|
|
def extPlaybookChoice():
|
|
url = inquirer.text(
|
|
message = "Renseigner l'url du projet git (exemple : https://adresse-serveur-git/projet.git) :",
|
|
validate = lambda url :
|
|
len(url) > 0 and
|
|
(url.startswith("http://") or url.startswith("https://")) and
|
|
url.endswith(".git"),
|
|
invalid_message = "Une url de projet git doit commencer par http(s):// et se terminer par .git"
|
|
).execute()
|
|
|
|
playbook = inquirer.text(
|
|
message = "Renseigner le nom du playbook :",
|
|
validate = lambda playbook :
|
|
len(playbook) > 0 and
|
|
(playbook.endswith(".yaml") or playbook.endswith(".yml")),
|
|
invalid_message = "Un nom de playbook doit se terminer par .yml ou .yaml"
|
|
).execute()
|
|
|
|
proceed = inquirer.confirm(
|
|
message ="Confirmer l'exécution du playbook {} du projet {} ?".format(playbook, url),
|
|
confirm_letter = "o",
|
|
default=False).execute()
|
|
if proceed:
|
|
AnsibleActions.pullAnsiblePlaybook(url, playbook)
|
|
else:
|
|
print("Annulation")
|
|
|
|
def branchChoice():
|
|
questions = [
|
|
{
|
|
"type": "list",
|
|
"message": "Choisir le type d'environnement de ce poste :",
|
|
"choices": GitActions.getBranchList(),
|
|
"default": "main",
|
|
},
|
|
]
|
|
result = prompt(questions)
|
|
|
|
proceed = inquirer.confirm(
|
|
message ="Définir {} comme environnement pour ce poste ?".format(result[0]),
|
|
confirm_letter = "o",
|
|
default=False).execute()
|
|
if proceed:
|
|
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()
|
|
else:
|
|
print("Annulation")
|
|
|
|
|
|
# Menu principal
|
|
def main():
|
|
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')
|
|
args = parser.parse_args()
|
|
|
|
menu_main = {
|
|
"Définir le hostname": hostnameChoice,
|
|
}
|
|
|
|
for entry in config['ansiblemenu']:
|
|
if not config['ansiblemenu'][entry].get('menu_hidden', False):
|
|
menu_main = menu_main | {
|
|
config['ansiblemenu'][entry]['menu_title']: lambda e=entry: AnsibleActions.runAnsiblePlaybook(config, e)
|
|
}
|
|
|
|
menu_main = menu_main | {
|
|
"Exécuter un playbook d'un projet spécifique": extPlaybookChoice,
|
|
"Changer de branche": branchChoice,
|
|
"Quitter": None
|
|
}
|
|
|
|
# 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)):
|
|
AnsibleActions.runAnsiblePlaybook(config, entry)
|
|
cmdlineOptionPassed = True
|
|
|
|
|
|
while not cmdlineOptionPassed:
|
|
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:
|
|
menu_main[action]()
|
|
else:
|
|
break
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|