infra-as-code/service.py

89 lines
2.9 KiB
Python

#!/usr/bin/env python3
from __future__ import annotations
import subprocess as sub
import re
P_OPTIONS = """
Options:
0) Exit
1) Maak/Update klantomgeving(en)
2) Vernietig klantomgeving(en)
3) Recover klantomgeving(en)
4) List klantomgeving(en)
h) Help
"""
RE_TEXT = re.compile(r"^\w+$")
RE_NUM = re.compile(r"^\d+$")
def _take_input(prompt: str, pattern: re.Pattern, default: str="") -> str:
while True:
i = input(prompt)
if i == "":
i = default
if pattern.match(i):
return i
def main() -> int:
while True:
print(P_OPTIONS)
c = input("Input: ")
if c == "h":
continue
if c == "0":
break
if c == "1":
customer_name = _take_input("Customer name (example=opg): ",
RE_TEXT)
env_name = _take_input("Environment name (example=prod): ",
RE_TEXT)
amnt_nginx_web = _take_input(
"Number of nginx webservers (default=1): ", RE_NUM, default="1")
amnt_nginx_lb = _take_input(
"Number of nginx loadbalancers (default=1): ", RE_NUM, default="1")
amnt_psql = _take_input(
"Number of postgres instances (default=1): ", RE_NUM, default="1")
ip_format = _take_input(
"Format of ip (default=192.168.56.{}, `{}` will be replaced with the correct octet at runtime): ",
re.compile(r".+"), default="192.168.56.{}")
ip_int = _take_input(
"Number to start formatting the IP from (default=10): ",
RE_NUM, default="10")
sub.call([
"python3", "cli.py", "create", customer_name, env_name,
"--num-nginx-web", amnt_nginx_web, "--num-postgres", amnt_psql,
"--num-nginx-lb", amnt_nginx_lb, "--ip-format", ip_format,
"--ip-int", ip_int
])
if c == "2":
customer_name = _take_input("Customer name (example=opg): ",
RE_TEXT)
env_name = _take_input("Environment name (example=prod): ",
RE_TEXT)
sub.call(["python3", "cli.py", "delete", customer_name, env_name])
if c == "3":
customer_name = _take_input("Customer name (example=opg): ",
RE_TEXT)
env_name = _take_input("Environment name (example=prod): ",
RE_TEXT)
sub.call(["python3", "cli.py", "recover", customer_name, env_name])
if c == "4":
customer_name = _take_input("Customer name (example=opg): ",
RE_TEXT)
sub.call(["python3", "cli.py", "list", customer_name])
return 0
if __name__ == "__main__":
raise SystemExit(main())