Compare commits

...

3 Commits

Author SHA1 Message Date
c81588265b Small changes 2022-04-09 19:01:24 +02:00
55a487fed3 Confirmation of chosen preset self-service portal 2022-04-09 11:08:46 +02:00
777d0cfbd7 Added enum for self-service options 2022-04-09 10:54:13 +02:00

View File

@ -5,6 +5,7 @@ from os import path
import subprocess as sub
import re
from typing import Any, Callable
from enum import Enum
P_BANNER = """
██╗ ██╗███████╗██╗ ██╗ ██╗ ██████╗ ███╗ ███╗
@ -23,6 +24,13 @@ Opties:
3) Recover omgeving(en)
4) List omgeving(en)
"""
class PortalOptions(Enum):
EXIT = "0"
CREATE = "1"
DESTROY = "2"
RECOVER = "3"
LIST = "4"
RE_TEXT = re.compile(r"^\w+$")
RE_NUM = re.compile(r"^\d+$")
@ -49,6 +57,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],
@ -60,7 +73,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
@ -89,16 +101,16 @@ def main() -> int:
p = Prompter(ps1=f"{customer_name} > ")
while True:
print(P_OPTIONS)
c = p.take_input("Keuze: ", RE_ANY)
choice = p.take_input("Keuze: ", RE_ANY)
print(end="\n")
if c == "0":
if choice == PortalOptions.EXIT.value:
"""
Used to break out of the loop and exit the portal.
"""
break
if c == "1":
if choice == PortalOptions.CREATE.value:
"""
Used to either update or create an environment.
"""
@ -106,38 +118,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)
# Loop until customer has confirmed their desired combination of machines
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")
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.
@ -159,7 +190,7 @@ def main() -> int:
])
print(f"Omgeving `{env_name}` successvol gemaakt.")
if c == "2":
if choice == PortalOptions.DESTROY.value:
"""
Deletes all traces of the chosen environment.
"""
@ -168,7 +199,7 @@ def main() -> int:
env_name = p.take_choice(fmt, envs)
sub.call(["python3", "cli.py", "delete", customer_name, env_name])
if c == "3":
if choice == PortalOptions.RECOVER.value:
"""
Allows the customer to "force" their environment into the desired `up-state`.
This `up-state` is a representation of the Vagrantfile, Ansible inventory and
@ -181,7 +212,7 @@ def main() -> int:
env_name = p.take_choice(fmt, envs)
sub.call(["python3", "cli.py", "recover", customer_name, env_name])
if c == "4":
if choice == PortalOptions.LIST.value:
"""
This branch displays the customer's existing environments
"""