Compare commits
3 Commits
b546e03e37
...
main
Author | SHA1 | Date | |
---|---|---|---|
c81588265b | |||
55a487fed3 | |||
777d0cfbd7 |
47
service.py
47
service.py
@ -5,6 +5,7 @@ from os import path
|
|||||||
import subprocess as sub
|
import subprocess as sub
|
||||||
import re
|
import re
|
||||||
from typing import Any, Callable
|
from typing import Any, Callable
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
P_BANNER = """
|
P_BANNER = """
|
||||||
██╗ ██╗███████╗██╗ ██╗ ██╗ ██████╗ ███╗ ███╗
|
██╗ ██╗███████╗██╗ ██╗ ██╗ ██████╗ ███╗ ███╗
|
||||||
@ -23,6 +24,13 @@ Opties:
|
|||||||
3) Recover omgeving(en)
|
3) Recover omgeving(en)
|
||||||
4) List 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_TEXT = re.compile(r"^\w+$")
|
||||||
RE_NUM = re.compile(r"^\d+$")
|
RE_NUM = re.compile(r"^\d+$")
|
||||||
@ -49,6 +57,11 @@ class Prompter:
|
|||||||
prompt = self._build_prompt(prompt)
|
prompt = self._build_prompt(prompt)
|
||||||
return Prompter.input(prompt, lambda x: x in options)
|
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
|
@staticmethod
|
||||||
def input(prompt: str,
|
def input(prompt: str,
|
||||||
validate: Callable[[str], bool],
|
validate: Callable[[str], bool],
|
||||||
@ -60,7 +73,6 @@ class Prompter:
|
|||||||
if validate(i):
|
if validate(i):
|
||||||
return i
|
return i
|
||||||
|
|
||||||
|
|
||||||
def get_env_list(customer: str) -> list[str]:
|
def get_env_list(customer: str) -> list[str]:
|
||||||
"""Fetches and parses a list of a customer's environments
|
"""Fetches and parses a list of a customer's environments
|
||||||
|
|
||||||
@ -89,16 +101,16 @@ def main() -> int:
|
|||||||
p = Prompter(ps1=f"{customer_name} > ")
|
p = Prompter(ps1=f"{customer_name} > ")
|
||||||
while True:
|
while True:
|
||||||
print(P_OPTIONS)
|
print(P_OPTIONS)
|
||||||
c = p.take_input("Keuze: ", RE_ANY)
|
choice = p.take_input("Keuze: ", RE_ANY)
|
||||||
print(end="\n")
|
print(end="\n")
|
||||||
|
|
||||||
if c == "0":
|
if choice == PortalOptions.EXIT.value:
|
||||||
"""
|
"""
|
||||||
Used to break out of the loop and exit the portal.
|
Used to break out of the loop and exit the portal.
|
||||||
"""
|
"""
|
||||||
break
|
break
|
||||||
|
|
||||||
if c == "1":
|
if choice == PortalOptions.CREATE.value:
|
||||||
"""
|
"""
|
||||||
Used to either update or create an environment.
|
Used to either update or create an environment.
|
||||||
"""
|
"""
|
||||||
@ -109,10 +121,11 @@ def main() -> int:
|
|||||||
fmt = "Omgevingsnaam: "
|
fmt = "Omgevingsnaam: "
|
||||||
env_name = p.take_input(fmt, RE_TEXT)
|
env_name = p.take_input(fmt, RE_TEXT)
|
||||||
|
|
||||||
|
# Loop until customer has confirmed their desired combination of machines
|
||||||
|
while True:
|
||||||
templates = ["production", "acceptance", "test", "custom"]
|
templates = ["production", "acceptance", "test", "custom"]
|
||||||
fmt = f"Het type omgeving {templates}: "
|
fmt = f"Het type omgeving {templates}: "
|
||||||
template_name = p.take_choice(fmt, templates)
|
template_name = p.take_choice(fmt, templates)
|
||||||
|
|
||||||
if template_name == "custom":
|
if template_name == "custom":
|
||||||
"""
|
"""
|
||||||
Asks for all machine's individually
|
Asks for all machine's individually
|
||||||
@ -129,6 +142,7 @@ def main() -> int:
|
|||||||
amnt_nginx_web = "2"
|
amnt_nginx_web = "2"
|
||||||
amnt_nginx_lb = "1"
|
amnt_nginx_lb = "1"
|
||||||
amnt_psql = "1"
|
amnt_psql = "1"
|
||||||
|
|
||||||
elif template_name == "acceptance":
|
elif template_name == "acceptance":
|
||||||
amnt_nginx_web = "1"
|
amnt_nginx_web = "1"
|
||||||
amnt_nginx_lb = "0"
|
amnt_nginx_lb = "0"
|
||||||
@ -138,6 +152,23 @@ def main() -> int:
|
|||||||
amnt_nginx_web = "1"
|
amnt_nginx_web = "1"
|
||||||
amnt_nginx_lb = "0"
|
amnt_nginx_lb = "0"
|
||||||
amnt_psql = "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
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Define the format for templating ip-addresses.`{}` is required
|
Define the format for templating ip-addresses.`{}` is required
|
||||||
and will be subbed at runtime with the correct octet.
|
and will be subbed at runtime with the correct octet.
|
||||||
@ -159,7 +190,7 @@ def main() -> int:
|
|||||||
])
|
])
|
||||||
print(f"Omgeving `{env_name}` successvol gemaakt.")
|
print(f"Omgeving `{env_name}` successvol gemaakt.")
|
||||||
|
|
||||||
if c == "2":
|
if choice == PortalOptions.DESTROY.value:
|
||||||
"""
|
"""
|
||||||
Deletes all traces of the chosen environment.
|
Deletes all traces of the chosen environment.
|
||||||
"""
|
"""
|
||||||
@ -168,7 +199,7 @@ def main() -> int:
|
|||||||
env_name = p.take_choice(fmt, envs)
|
env_name = p.take_choice(fmt, envs)
|
||||||
sub.call(["python3", "cli.py", "delete", customer_name, env_name])
|
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`.
|
Allows the customer to "force" their environment into the desired `up-state`.
|
||||||
This `up-state` is a representation of the Vagrantfile, Ansible inventory and
|
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)
|
env_name = p.take_choice(fmt, envs)
|
||||||
sub.call(["python3", "cli.py", "recover", customer_name, env_name])
|
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
|
This branch displays the customer's existing environments
|
||||||
"""
|
"""
|
||||||
|
Reference in New Issue
Block a user