166 lines
5.4 KiB
Python
166 lines
5.4 KiB
Python
from InquirerPy import inquirer
|
|
from InquirerPy import prompt
|
|
from rich.console import Console
|
|
import AnsibleActions
|
|
import PythonActions
|
|
import GitActions
|
|
import argparse
|
|
import yaml
|
|
import syslog
|
|
import os
|
|
|
|
|
|
# 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]
|
|
|
|
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.runPulledAnsiblePlaybook(playbook, url)
|
|
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 linuxinstaller.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()
|
|
|
|
description="Menu de configuration linuxinstaller"
|
|
parser = argparse.ArgumentParser(description=description)
|
|
parser.add_argument("--tags", help="Exécute les playbooks portant ces étiquettes", action="extend", nargs="+", type=str)
|
|
for entry in config['ansible_playbooks']:
|
|
if 'argument_help' in config['ansible_playbooks'][entry]:
|
|
parser.add_argument("--" + entry, help=config['ansible_playbooks'][entry]['argument_help'], action='store_true')
|
|
|
|
args = parser.parse_args()
|
|
|
|
menu_main = {
|
|
"Définir le hostname": hostnameChoice,
|
|
}
|
|
|
|
for entry in config['ansible_playbooks']:
|
|
if 'menu_title' in config['ansible_playbooks'][entry]:
|
|
menu_main = menu_main | {
|
|
config['ansible_playbooks'][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['ansible_playbooks']:
|
|
mustRun = False
|
|
|
|
# Teste si la clé est dans la liste des arguments
|
|
if entry in list(arg for arg in vars(args) if getattr(args, arg)):
|
|
mustRun = True
|
|
|
|
# Teste si un élément commun entre les tags mentionnés dans la conf et les tags en argument
|
|
if 'tags' in config['ansible_playbooks'][entry] and \
|
|
args.tags != None and \
|
|
len(set(args.tags).intersection(config['ansible_playbooks'][entry]['tags'])) > 0:
|
|
mustRun = True
|
|
|
|
if mustRun == True:
|
|
AnsibleActions.runAnsiblePlaybook(config, entry)
|
|
cmdlineOptionPassed = True
|
|
|
|
|
|
while not cmdlineOptionPassed:
|
|
PythonActions.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()
|