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
|
|
|
|
import PosteLinuxMint
|
|
|
|
import libreticmenuBranch
|
|
|
|
import argparse
|
|
|
|
import yaml
|
|
|
|
import syslog
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
### Cartouche de status
|
|
|
|
|
|
|
|
def print_status():
|
|
|
|
# Récupération des infos
|
|
|
|
hostname = PosteLinuxMint.getHostName()
|
|
|
|
domain = PosteLinuxMint.getDomain()
|
|
|
|
isCompliant = PosteLinuxMint.isCompliant()
|
|
|
|
currentBranch = libreticmenuBranch.getlibreticmenuBranch()
|
|
|
|
|
|
|
|
# Affichage du statut
|
|
|
|
console = Console()
|
|
|
|
console.print()
|
|
|
|
console.print("=== Menu de configuration - poste Linux Mint ===", 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")
|
|
|
|
table.add_row("Poste configuré", str(isCompliant), "[green]OK" if isCompliant else "[red]KO")
|
|
|
|
console.print(table)
|
|
|
|
console.print()
|
|
|
|
|
|
|
|
|
|
|
|
### Menu du choix hostname
|
|
|
|
def hostnameChoice(config, branch):
|
|
|
|
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:
|
|
|
|
PosteLinuxMint.setFQDN(fqdn)
|
|
|
|
else:
|
|
|
|
print("Annulation")
|
|
|
|
|
|
|
|
def complianceChoice(config, branch):
|
|
|
|
PosteLinuxMint.runCompliance(config, branch)
|
|
|
|
|
|
|
|
def libreticmenuBranchChoice(config, branch):
|
|
|
|
questions = [
|
|
|
|
{
|
|
|
|
"type": "list",
|
|
|
|
"message": "Choisir le type d'environnement de ce poste :",
|
|
|
|
"choices": ["main", "preprod"],
|
|
|
|
"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:
|
|
|
|
libreticmenuBranch.setlibreticmenuBranch(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")
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
|
|
def addLocalAdminChoice(config, branch):
|
|
|
|
questions = [
|
|
|
|
{
|
|
|
|
"type": "input",
|
|
|
|
"message": "Utilisateur devant être administrateur local : ",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
result = prompt(questions)
|
|
|
|
|
|
|
|
proceed = inquirer.confirm(
|
|
|
|
message ="Définir {} comme administrateur de ce poste ?".format(result[0]),
|
|
|
|
confirm_letter = "o",
|
|
|
|
default=False).execute()
|
|
|
|
if proceed:
|
|
|
|
PosteLinuxMint.addLocalAdmin(config, result[0])
|
|
|
|
else:
|
|
|
|
print("Annulation")
|
|
|
|
|
|
|
|
def removeLocalAdminChoice(config, branch):
|
|
|
|
userlist = PosteLinuxMint.getLocalAdmins(config)
|
|
|
|
|
|
|
|
if len(userlist) == 0:
|
|
|
|
console = Console()
|
|
|
|
console.print("=== Aucun administrateur local à supprimer ===", style="bold red")
|
|
|
|
return
|
|
|
|
|
|
|
|
questions = [
|
|
|
|
{
|
|
|
|
"type": "list",
|
|
|
|
"message": "Utilisateur ne devant plus être administrateur local : ",
|
|
|
|
"choices": userlist,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
result = prompt(questions)
|
|
|
|
|
|
|
|
proceed = inquirer.confirm(
|
|
|
|
message ="Retirer {} des administrateurs de ce poste ?".format(result[0]),
|
|
|
|
confirm_letter = "o",
|
|
|
|
default=False).execute()
|
|
|
|
if proceed:
|
|
|
|
PosteLinuxMint.removeLocalAdmin(config, result[0])
|
|
|
|
else:
|
|
|
|
print("Annulation")
|
|
|
|
|
|
|
|
|
|
|
|
# Menu principal
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(prog="libreticmenu.py", description="Menu de configuration poste Linux Mint")
|
|
|
|
parser.add_argument("-c", "--config", help="Fichier de configuration (config.yml par défaut)", default="config.yml")
|
|
|
|
parser.add_argument("--runCompliance", help="Ne présente pas le menu et exécuter la conformité", action=argparse.BooleanOptionalAction)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2024-10-02 08:23:24 +02:00
|
|
|
config = readConfig(args.config)
|
2024-09-30 19:32:52 +02:00
|
|
|
|
|
|
|
menu_main = {
|
|
|
|
"Définir le hostname": hostnameChoice,
|
|
|
|
"Exécuter la configuration du poste": complianceChoice,
|
|
|
|
"Changer le type d'environnement": libreticmenuBranchChoice,
|
|
|
|
"Ajouter sudoer": addLocalAdminChoice,
|
|
|
|
"Supprimer sudoer": removeLocalAdminChoice,
|
|
|
|
"Quitter": None
|
|
|
|
}
|
|
|
|
|
|
|
|
currentBranch = libreticmenuBranch.getlibreticmenuBranch()
|
|
|
|
|
|
|
|
if args.runCompliance:
|
|
|
|
PosteLinuxMint.runCompliance(config, currentBranch)
|
|
|
|
exit()
|
|
|
|
|
|
|
|
while True:
|
|
|
|
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](config, currentBranch)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|