From 55a487fed3aa9673df39294139ffe61c33fbe538 Mon Sep 17 00:00:00 2001 From: strNophix Date: Sat, 9 Apr 2022 11:08:46 +0200 Subject: [PATCH] Confirmation of chosen preset self-service portal --- service.py | 80 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 28 deletions(-) diff --git a/service.py b/service.py index e35d12e..825d975 100644 --- a/service.py +++ b/service.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 from __future__ import annotations +from email.policy import default from pathlib import Path from os import path import subprocess as sub @@ -57,6 +58,11 @@ class Prompter: prompt = self._build_prompt(prompt) return Prompter.input(prompt, lambda x: x in options) + def take_confirmation(self, prompt: str, default: str = "y") -> bool: + prompt = self._build_prompt(prompt) + i = Prompter.input(prompt, lambda x: x.lower() in ["y", "n"], default=default) + return i == "y" + @staticmethod def input(prompt: str, validate: Callable[[str], bool], @@ -68,7 +74,6 @@ class Prompter: if validate(i): return i - def get_env_list(customer: str) -> list[str]: """Fetches and parses a list of a customer's environments @@ -114,38 +119,57 @@ def main() -> int: print( f"NOTE: Kies een albestaande omgevingen {envs} of iets nieuws..." ) - fmt = "Omgevingsnaam:" + fmt = "Omgevingsnaam: " env_name = p.take_input(fmt, RE_TEXT) - templates = ["production", "acceptance", "test", "custom"] - fmt = f"Het type omgeving {templates}: " - template_name = p.take_choice(fmt, templates) - if template_name == "custom": - """ - Asks for all machine's individually - """ - fmt = "Aantal nginx webservers (default=1): " - amnt_nginx_web = p.take_input(fmt, RE_NUM, default="1") + while True: + templates = ["production", "acceptance", "test", "custom"] + fmt = f"Het type omgeving {templates}: " + template_name = p.take_choice(fmt, templates) + if template_name == "custom": + """ + Asks for all machine's individually + """ + fmt = "Aantal nginx webservers (default=1): " + amnt_nginx_web = p.take_input(fmt, RE_NUM, default="1") - fmt = "Aantal nginx loadbalancers (default=1): " - amnt_nginx_lb = p.take_input(fmt, RE_NUM, default="1") + fmt = "Aantal nginx loadbalancers (default=1): " + amnt_nginx_lb = p.take_input(fmt, RE_NUM, default="1") + + fmt = "Aantal postgres instances (default=1): " + amnt_psql = p.take_input(fmt, RE_NUM, default="1") + elif template_name == "production": + amnt_nginx_web = "2" + amnt_nginx_lb = "1" + amnt_psql = "1" + + elif template_name == "acceptance": + amnt_nginx_web = "1" + amnt_nginx_lb = "0" + amnt_psql = "1" + # elif template_name == "test": + else: + amnt_nginx_web = "1" + amnt_nginx_lb = "0" + amnt_psql = "0" + + # TODO: migrate different machine types to a dict model + print(end="\n") + print("Deze omgeving bevat:") + if amnt_nginx_web > "0": + print(f" - {amnt_nginx_web} Nginx webserver(s)") + + if amnt_nginx_lb > "0": + print(f" - {amnt_nginx_lb} Nginx loadbalancer(s)") + + if amnt_psql > "0": + print(f" - {amnt_psql} Postgres instance(s)") + print(end="\n") + + if p.take_confirmation("Bevestig (Y/n): "): + break - fmt = "Aantal postgres instances (default=1): " - amnt_psql = p.take_input(fmt, RE_NUM, default="1") - elif template_name == "production": - amnt_nginx_web = "2" - amnt_nginx_lb = "1" - amnt_psql = "1" - elif template_name == "acceptance": - amnt_nginx_web = "1" - amnt_nginx_lb = "0" - amnt_psql = "1" - # elif template_name == "test": - else: - amnt_nginx_web = "1" - amnt_nginx_lb = "0" - amnt_psql = "0" """ Define the format for templating ip-addresses.`{}` is required and will be subbed at runtime with the correct octet.