Compare commits
13 Commits
a330bb17d1
...
main
Author | SHA1 | Date | |
---|---|---|---|
c81588265b | |||
55a487fed3 | |||
777d0cfbd7 | |||
b546e03e37 | |||
6608e1216b | |||
61428cf80a | |||
db13dfb04b | |||
b99fe97267 | |||
acda51a6b4 | |||
cd736a2606 | |||
a1c7052e85 | |||
efe5901a55 | |||
7ed3be574a |
378
cli.py
Executable file
378
cli.py
Executable file
@ -0,0 +1,378 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
from __future__ import annotations
|
||||||
|
import argparse
|
||||||
|
import dataclasses
|
||||||
|
import sys
|
||||||
|
from os import path
|
||||||
|
from pathlib import Path
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
from typing import Any, DefaultDict, Iterable, Mapping
|
||||||
|
from jinja2 import Template
|
||||||
|
import itertools
|
||||||
|
import subprocess as sub
|
||||||
|
import shutil
|
||||||
|
import string
|
||||||
|
import secrets
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
TEMPLATE_DIR = "./templates"
|
||||||
|
|
||||||
|
|
||||||
|
#region util
|
||||||
|
def gen_pass(pass_length: int = 20) -> str:
|
||||||
|
"""Generates a simple password with the provided length
|
||||||
|
|
||||||
|
Args:
|
||||||
|
pass_length (int, optional): Length of password. Defaults to 20.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The generated password.
|
||||||
|
"""
|
||||||
|
alphabet = string.ascii_letters + string.digits
|
||||||
|
password = "".join(secrets.choice(alphabet) for _ in range(pass_length))
|
||||||
|
return password
|
||||||
|
|
||||||
|
|
||||||
|
def check_positive(value: str) -> int:
|
||||||
|
"""`argparse` type helper to check whether a given input is a positive integer.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
value (str): Input to validate
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Exception: Input is neither a decimal or positive
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
int: The parsed input if valid
|
||||||
|
"""
|
||||||
|
ivalue = int(value)
|
||||||
|
if ivalue < 0:
|
||||||
|
raise Exception(f"Supplied number must be >= 0")
|
||||||
|
return ivalue
|
||||||
|
|
||||||
|
|
||||||
|
def encode_member(member: str, mapping: Mapping[str, Any]) -> str:
|
||||||
|
"""Encodes the member-entry of an inventory file with additional mappings.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
member (str)
|
||||||
|
mapping (Mapping[str, Any])
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: member with mapping encoded
|
||||||
|
"""
|
||||||
|
return member + " " + " ".join([f"{k}={v}" for k, v in mapping.items()])
|
||||||
|
|
||||||
|
|
||||||
|
def iter_ips(ip_format: str, start_octet: int):
|
||||||
|
"""Simple iterator for generating ip's
|
||||||
|
|
||||||
|
Args:
|
||||||
|
ip_format (str)
|
||||||
|
start_octet (int)
|
||||||
|
|
||||||
|
Yields:
|
||||||
|
Yeah idk too lazy too look up what the type annotation for a generator is
|
||||||
|
"""
|
||||||
|
ip_int = start_octet
|
||||||
|
ip_fmt = ip_format
|
||||||
|
while ip_int < 255:
|
||||||
|
yield ip_fmt.format(ip_int)
|
||||||
|
ip_int += 1
|
||||||
|
|
||||||
|
|
||||||
|
def copy_template(src: str, dest: str, mapping: Mapping[str, Any] = {}):
|
||||||
|
"""Templates and writes a template file using Jinja as templating engine.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
src (str): content of the file
|
||||||
|
dest (str): place to write templated file to
|
||||||
|
mapping (Mapping[str, Any], optional): datamapping. Defaults to {}.
|
||||||
|
"""
|
||||||
|
c = Path(src).read_text()
|
||||||
|
t: Template = Template(c)
|
||||||
|
r = t.render(mapping)
|
||||||
|
Path(dest).write_text(r)
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region models
|
||||||
|
@dataclass
|
||||||
|
class MachineResources:
|
||||||
|
cpus: int
|
||||||
|
mem: int
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_prompt() -> "MachineResources":
|
||||||
|
"""Generate `MachineResources` from prompt.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Exception
|
||||||
|
Exception
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
MachineResources
|
||||||
|
"""
|
||||||
|
cpus = input(
|
||||||
|
"How many processors would you like to assign (default=1): ")
|
||||||
|
if not cpus:
|
||||||
|
cpus = "1"
|
||||||
|
|
||||||
|
if not cpus.isdigit() or int(cpus) < 0:
|
||||||
|
raise Exception("Expected a postive amount of processors")
|
||||||
|
|
||||||
|
mem = input(
|
||||||
|
"How many megabytes of RAM would you like to assign (default=1024): "
|
||||||
|
)
|
||||||
|
if not mem:
|
||||||
|
mem = "1024"
|
||||||
|
|
||||||
|
if not mem.isdigit() or int(mem) < 0:
|
||||||
|
raise Exception("Expected a postive amount of memory")
|
||||||
|
|
||||||
|
return MachineResources(cpus=int(cpus), mem=int(mem))
|
||||||
|
|
||||||
|
|
||||||
|
class InventoryWriter:
|
||||||
|
"""
|
||||||
|
Helper class for generating Ansible inventory files.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, location: str) -> None:
|
||||||
|
self._file_handle = Path(location)
|
||||||
|
self._groups: dict[str, set[str]] = DefaultDict(set)
|
||||||
|
|
||||||
|
def add(self, name: str, members: Iterable[str]):
|
||||||
|
self._groups[name] |= set(members)
|
||||||
|
|
||||||
|
def _build_group(self, name: str, members: set[str]):
|
||||||
|
fmt = f"[{name}]\n" + "\n".join(members)
|
||||||
|
return fmt
|
||||||
|
|
||||||
|
def flush(self):
|
||||||
|
txt = ""
|
||||||
|
for name, members in self._groups.items():
|
||||||
|
txt += self._build_group(name, members) + "\n\n"
|
||||||
|
self._file_handle.write_text(txt, encoding="utf8")
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region CLI positional flows
|
||||||
|
def list_envs(args: argparse.Namespace):
|
||||||
|
try:
|
||||||
|
customer_path = path.join("customers", args.customer_name, "envs")
|
||||||
|
print(" ".join(os.listdir(customer_path)))
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"Customer `{args.customer_name}` does not exist.")
|
||||||
|
|
||||||
|
|
||||||
|
def delete_env(args: argparse.Namespace):
|
||||||
|
for env in args.env_names:
|
||||||
|
env_path = path.join("customers", args.customer_name, "envs", env)
|
||||||
|
sub.call(["vagrant", "destroy", "-f"], cwd=env_path)
|
||||||
|
shutil.rmtree(env_path)
|
||||||
|
print(f"Deleted `{env}` from customer `{ args.customer_name}`")
|
||||||
|
|
||||||
|
|
||||||
|
def recover_env(args: argparse.Namespace):
|
||||||
|
for env in args.env_names:
|
||||||
|
env_path = path.join("customers", args.customer_name, "envs", env)
|
||||||
|
sub.call(["vagrant", "up"], cwd=env_path)
|
||||||
|
|
||||||
|
# Artificial sleep waiting for sshd on VM to correctly start
|
||||||
|
print("Waiting on virtual machines...")
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
|
sub.call(["ansible-playbook", "../../../../site.yml"], cwd=env_path)
|
||||||
|
print(f"Recovered `{env}` from customer `{ args.customer_name}`")
|
||||||
|
|
||||||
|
|
||||||
|
def create_env(args: argparse.Namespace):
|
||||||
|
"""
|
||||||
|
Managing environments works in a declarative way and thus it can
|
||||||
|
also be used for updating.
|
||||||
|
"""
|
||||||
|
if (args.num_nginx_web + args.num_nginx_lb + args.num_postgres) == 0:
|
||||||
|
raise Exception("At least one item should be deployed")
|
||||||
|
|
||||||
|
env_path = path.join("customers", args.customer_name, "envs",
|
||||||
|
args.env_name)
|
||||||
|
Path(env_path).mkdir(exist_ok=True, parents=True)
|
||||||
|
vagrant_mapping: dict[str, Any] = {
|
||||||
|
"webserver_specs": None,
|
||||||
|
"loadbalancers_specs": None,
|
||||||
|
"postgres_specs": None,
|
||||||
|
"num_webserver": args.num_nginx_web,
|
||||||
|
"num_loadbalancers": args.num_nginx_lb,
|
||||||
|
"num_postgres": args.num_postgres,
|
||||||
|
"env": args.env_name,
|
||||||
|
"customer_name": args.customer_name,
|
||||||
|
"ip_int": args.ip_int,
|
||||||
|
"ip_format": args.ip_format.replace("{}", "%d"),
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.num_nginx_web > 0:
|
||||||
|
print("\nNginx webserver resources:")
|
||||||
|
web_specs = MachineResources.from_prompt()
|
||||||
|
vagrant_mapping["webserver_specs"] = dataclasses.asdict(web_specs)
|
||||||
|
|
||||||
|
if args.num_nginx_lb > 0:
|
||||||
|
print("\nNginx loadbalancer resources: ")
|
||||||
|
lb_specs = MachineResources.from_prompt()
|
||||||
|
vagrant_mapping["loadbalancers_specs"] = dataclasses.asdict(lb_specs)
|
||||||
|
|
||||||
|
if args.num_postgres > 0:
|
||||||
|
print("\nPostgresql machine resources: ")
|
||||||
|
psql_specs = MachineResources.from_prompt()
|
||||||
|
vagrant_mapping["postgres_specs"] = dataclasses.asdict(psql_specs)
|
||||||
|
|
||||||
|
# Template `ansible.cfg`
|
||||||
|
src = path.join(TEMPLATE_DIR, "ansible.cfg.template")
|
||||||
|
dest = path.join(env_path, "ansible.cfg")
|
||||||
|
copy_template(src=src, dest=dest)
|
||||||
|
|
||||||
|
# Create inventory file
|
||||||
|
inv_path = path.join(env_path, "inventory")
|
||||||
|
iw = InventoryWriter(inv_path)
|
||||||
|
ip_generator = iter_ips(args.ip_format, args.ip_int)
|
||||||
|
|
||||||
|
web_ips = itertools.islice(ip_generator, args.num_nginx_web)
|
||||||
|
iw.add("webserver", web_ips)
|
||||||
|
|
||||||
|
lb_ips = itertools.islice(ip_generator, args.num_nginx_lb)
|
||||||
|
iw.add("loadbalancer", lb_ips)
|
||||||
|
|
||||||
|
def psql_gen_pass(x: str) -> str:
|
||||||
|
return encode_member(x, {"psql_pass": gen_pass()})
|
||||||
|
|
||||||
|
psql_ips = list(itertools.islice(ip_generator, args.num_postgres))
|
||||||
|
psql_ips = map(psql_gen_pass, psql_ips)
|
||||||
|
iw.add("postgresql", psql_ips)
|
||||||
|
|
||||||
|
iw.flush()
|
||||||
|
|
||||||
|
# Template `Vagrantfile`
|
||||||
|
src = path.join(TEMPLATE_DIR, "Vagrantfile.template")
|
||||||
|
dest = path.join(env_path, "Vagrantfile")
|
||||||
|
should_reload = Path(dest).exists()
|
||||||
|
copy_template(src=src, dest=dest, mapping=vagrant_mapping)
|
||||||
|
|
||||||
|
# Generate .ssh if it doesn't exist already
|
||||||
|
ssh_dir = path.join(env_path, ".ssh")
|
||||||
|
Path(ssh_dir).mkdir(exist_ok=True)
|
||||||
|
rsa_path = path.join(ssh_dir, "id_rsa")
|
||||||
|
if not Path(rsa_path).exists():
|
||||||
|
print(end="\n")
|
||||||
|
sub.call([
|
||||||
|
"ssh-keygen",
|
||||||
|
"-t",
|
||||||
|
"rsa",
|
||||||
|
"-b",
|
||||||
|
"2048",
|
||||||
|
"-f",
|
||||||
|
rsa_path,
|
||||||
|
])
|
||||||
|
|
||||||
|
# Provision and configure machines
|
||||||
|
# Create VM's that do not exist yet
|
||||||
|
sub.call(["vagrant", "up"], cwd=env_path)
|
||||||
|
|
||||||
|
# Update VM's that already existed
|
||||||
|
if should_reload:
|
||||||
|
sub.call(["vagrant", "reload", "--provision"], cwd=env_path)
|
||||||
|
|
||||||
|
# Artificial sleep waiting for sshd on VM to correctly start
|
||||||
|
print("Waiting on virtual machines...")
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
|
sub.call(["ansible-playbook", "../../../../site.yml"], cwd=env_path)
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
sub_parser = parser.add_subparsers()
|
||||||
|
|
||||||
|
# CLI definition for positional arg "list"
|
||||||
|
list_parser = sub_parser.add_parser(
|
||||||
|
"list", help="list customer-owned environments")
|
||||||
|
list_parser.add_argument("customer_name",
|
||||||
|
type=str,
|
||||||
|
help="name of the customer")
|
||||||
|
list_parser.set_defaults(func=list_envs)
|
||||||
|
|
||||||
|
# CLI definition for positional arg "create"
|
||||||
|
cenv_parser = sub_parser.add_parser("create",
|
||||||
|
help="create/update an environment")
|
||||||
|
cenv_parser.add_argument("customer_name",
|
||||||
|
type=str,
|
||||||
|
help="name of the customer")
|
||||||
|
cenv_parser.add_argument("env_name",
|
||||||
|
type=str,
|
||||||
|
help="name of the environment")
|
||||||
|
cenv_parser.add_argument(
|
||||||
|
"--num-postgres",
|
||||||
|
type=check_positive,
|
||||||
|
help="number of postgres databases",
|
||||||
|
default=0,
|
||||||
|
)
|
||||||
|
cenv_parser.add_argument(
|
||||||
|
"--num-nginx-web",
|
||||||
|
type=check_positive,
|
||||||
|
help="number of nginx webservers",
|
||||||
|
default=0,
|
||||||
|
)
|
||||||
|
cenv_parser.add_argument(
|
||||||
|
"--num-nginx-lb",
|
||||||
|
type=check_positive,
|
||||||
|
help="number of nginx loadbalancers",
|
||||||
|
default=0,
|
||||||
|
)
|
||||||
|
cenv_parser.add_argument("--ip-format",
|
||||||
|
type=str,
|
||||||
|
help="format of ip",
|
||||||
|
default="192.168.56.{}")
|
||||||
|
cenv_parser.add_argument("--ip-int",
|
||||||
|
type=check_positive,
|
||||||
|
help="4th octet to start at",
|
||||||
|
default="10")
|
||||||
|
cenv_parser.set_defaults(func=create_env)
|
||||||
|
|
||||||
|
# CLI definition for positional arg "delete"
|
||||||
|
denv_parser = sub_parser.add_parser("delete", help="delete an environment")
|
||||||
|
denv_parser.add_argument("customer_name",
|
||||||
|
type=str,
|
||||||
|
help="name of the customer")
|
||||||
|
denv_parser.add_argument("env_names",
|
||||||
|
type=str,
|
||||||
|
nargs="+",
|
||||||
|
help="name of one or more environments")
|
||||||
|
denv_parser.set_defaults(func=delete_env)
|
||||||
|
|
||||||
|
# CLI definition for positional arg "recover"
|
||||||
|
denv_parser = sub_parser.add_parser("recover",
|
||||||
|
help="attempts to recover an env")
|
||||||
|
denv_parser.add_argument("customer_name",
|
||||||
|
type=str,
|
||||||
|
help="name of the customer")
|
||||||
|
denv_parser.add_argument("env_names",
|
||||||
|
type=str,
|
||||||
|
nargs="+",
|
||||||
|
help="name of one or more environments")
|
||||||
|
denv_parser.set_defaults(func=recover_env)
|
||||||
|
|
||||||
|
# Parse args
|
||||||
|
args = parser.parse_args(sys.argv[1:])
|
||||||
|
args.func(args)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
sudo apt update --yes && sudo apt upgrade --yes
|
sudo apt update --yes && sudo apt upgrade --yes
|
||||||
sudo apt install --yes virtualbox vagrant ansible
|
sudo apt install --yes virtualbox vagrant ansible
|
||||||
ansible-galaxy install -r ./requirements.yml
|
ansible-galaxy collection install -r requirements.yml
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
---
|
---
|
||||||
# ansible-galaxy requirements for this repository
|
# ansible-galaxy requirements for this repository
|
||||||
collections:
|
collections:
|
||||||
- 'community.postgresql'
|
- community.postgresql
|
||||||
|
432
service.py
Executable file → Normal file
432
service.py
Executable file → Normal file
@ -1,255 +1,225 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import argparse
|
|
||||||
import dataclasses
|
|
||||||
import sys
|
|
||||||
from os import path
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import os
|
from os import path
|
||||||
import time
|
|
||||||
from typing import Any, Callable, DefaultDict, Iterable, Mapping
|
|
||||||
from jinja2 import Template
|
|
||||||
import itertools
|
|
||||||
import subprocess as sub
|
import subprocess as sub
|
||||||
import shutil
|
import re
|
||||||
import string
|
from typing import Any, Callable
|
||||||
import secrets
|
from enum import Enum
|
||||||
from dataclasses import dataclass
|
|
||||||
|
P_BANNER = """
|
||||||
|
██╗ ██╗███████╗██╗ ██╗ ██╗ ██████╗ ███╗ ███╗
|
||||||
|
██║ ██║██╔════╝██║ ██║ ██╔╝██╔═══██╗████╗ ████║
|
||||||
|
██║ █╗ ██║█████╗ ██║ █████╔╝ ██║ ██║██╔████╔██║
|
||||||
|
██║███╗██║██╔══╝ ██║ ██╔═██╗ ██║ ██║██║╚██╔╝██║
|
||||||
|
╚███╔███╔╝███████╗███████╗██║ ██╗╚██████╔╝██║ ╚═╝ ██║
|
||||||
|
╚══╝╚══╝ ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
|
||||||
|
"""
|
||||||
|
|
||||||
|
P_OPTIONS = """
|
||||||
|
Opties:
|
||||||
|
0) Afsluiten
|
||||||
|
1) Maak/Wijzig omgeving(en)
|
||||||
|
2) Vernietig omgeving(en)
|
||||||
|
3) Recover omgeving(en)
|
||||||
|
4) List omgeving(en)
|
||||||
|
"""
|
||||||
|
class PortalOptions(Enum):
|
||||||
|
EXIT = "0"
|
||||||
|
CREATE = "1"
|
||||||
|
DESTROY = "2"
|
||||||
|
RECOVER = "3"
|
||||||
|
LIST = "4"
|
||||||
|
|
||||||
|
|
||||||
TEMPLATE_DIR = "./templates"
|
RE_TEXT = re.compile(r"^\w+$")
|
||||||
|
RE_NUM = re.compile(r"^\d+$")
|
||||||
|
RE_ANY = re.compile(r".+")
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
class Prompter:
|
||||||
class MachineResources:
|
|
||||||
cpus: int
|
def __init__(self, ps1: str = "") -> None:
|
||||||
mem: int
|
self.ps1 = ps1
|
||||||
|
|
||||||
|
def _build_prompt(self, prompt: str) -> str:
|
||||||
|
return self.ps1 + prompt
|
||||||
|
|
||||||
|
def take_input(self,
|
||||||
|
prompt: str,
|
||||||
|
pattern: re.Pattern[Any],
|
||||||
|
default: str = "") -> str:
|
||||||
|
prompt = self._build_prompt(prompt)
|
||||||
|
return Prompter.input(prompt, lambda x: bool(pattern.match(x)),
|
||||||
|
default)
|
||||||
|
|
||||||
|
def take_choice(self, prompt: str, options: list[str]) -> str:
|
||||||
|
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
|
@staticmethod
|
||||||
def from_prompt() -> "MachineResources":
|
def input(prompt: str,
|
||||||
cpus = input("How many processors would you like to assign (default=1): ")
|
validate: Callable[[str], bool],
|
||||||
if not cpus:
|
default: str = "") -> str:
|
||||||
cpus = "1"
|
while True:
|
||||||
|
i = input(prompt)
|
||||||
|
if i == "":
|
||||||
|
i = default
|
||||||
|
if validate(i):
|
||||||
|
return i
|
||||||
|
|
||||||
if not cpus.isdigit() or int(cpus) < 0:
|
def get_env_list(customer: str) -> list[str]:
|
||||||
raise Exception("Expected a postive amount of processors")
|
"""Fetches and parses a list of a customer's environments
|
||||||
|
|
||||||
mem = input(
|
Args:
|
||||||
"How many megabytes of RAM would you like to assign (default=1024): "
|
customer (str): The customer to fetch environments from
|
||||||
)
|
|
||||||
if not mem:
|
|
||||||
mem = "1024"
|
|
||||||
|
|
||||||
if not mem.isdigit() or int(mem) < 0:
|
Returns:
|
||||||
raise Exception("Expected a postive amount of memory")
|
list[str]: The parsed environments
|
||||||
|
"""
|
||||||
return MachineResources(cpus=int(cpus), mem=int(mem))
|
return sub.check_output(["python3", "cli.py", "list",
|
||||||
|
customer]).decode().strip("\n").split(" ")
|
||||||
|
|
||||||
def gen_pass() -> str:
|
|
||||||
alphabet = string.ascii_letters + string.digits
|
|
||||||
password = "".join(secrets.choice(alphabet) for _ in range(20))
|
|
||||||
return password
|
|
||||||
|
|
||||||
|
|
||||||
def check_positive(value: str):
|
|
||||||
ivalue = int(value)
|
|
||||||
if ivalue <= 0:
|
|
||||||
raise Exception("Supplied number must be >= 0")
|
|
||||||
return ivalue
|
|
||||||
|
|
||||||
|
|
||||||
def encode_member(member: str, mapping: Mapping[str, Any]) -> str:
|
|
||||||
return member + " " + " ".join([f"{k}={v}" for k, v in mapping.items()])
|
|
||||||
|
|
||||||
|
|
||||||
def iter_ips(ip_format: str, start_octet: int):
|
|
||||||
ip_int = start_octet
|
|
||||||
ip_fmt = ip_format
|
|
||||||
while ip_int < 255:
|
|
||||||
yield ip_fmt.format(ip_int)
|
|
||||||
ip_int += 1
|
|
||||||
|
|
||||||
|
|
||||||
class InventoryWriter:
|
|
||||||
def __init__(self, location: str) -> None:
|
|
||||||
self._file_handle = Path(location)
|
|
||||||
self._groups: dict[str, set[str]] = DefaultDict(set)
|
|
||||||
|
|
||||||
def add(self, name: str, members: Iterable[str]):
|
|
||||||
self._groups[name] |= set(members)
|
|
||||||
|
|
||||||
def _build_group(self, name: str, members: set[str]):
|
|
||||||
fmt = f"[{name}]\n" + "\n".join(members)
|
|
||||||
return fmt
|
|
||||||
|
|
||||||
def flush(self):
|
|
||||||
txt = ""
|
|
||||||
for name, members in self._groups.items():
|
|
||||||
txt += self._build_group(name, members) + "\n\n"
|
|
||||||
self._file_handle.write_text(txt, encoding="utf8")
|
|
||||||
|
|
||||||
|
|
||||||
def copy_template(src: str, dest: str, mapping: Mapping[str, Any] = {}):
|
|
||||||
c = Path(src).read_text()
|
|
||||||
t: Template = Template(c)
|
|
||||||
r = t.render(mapping)
|
|
||||||
Path(dest).write_text(r)
|
|
||||||
|
|
||||||
|
|
||||||
def list_envs(args: argparse.Namespace):
|
|
||||||
try:
|
|
||||||
customer_path = path.join("customers", args.customer_name, "envs")
|
|
||||||
print(" ".join(os.listdir(customer_path)))
|
|
||||||
except FileNotFoundError:
|
|
||||||
raise Exception(f"Customer `{args.customer_name}` does not exist.")
|
|
||||||
|
|
||||||
|
|
||||||
def delete_env(args: argparse.Namespace):
|
|
||||||
for env in args.env_names:
|
|
||||||
env_path = path.join("customers", args.customer_name, "envs", env)
|
|
||||||
sub.call(["vagrant", "destroy", "-f"], cwd=env_path)
|
|
||||||
shutil.rmtree(env_path)
|
|
||||||
print(f"Deleted `{env}` from customer `{ args.customer_name}`")
|
|
||||||
|
|
||||||
|
|
||||||
def create_env(args: argparse.Namespace):
|
|
||||||
if (args.num_nginx_web + args.num_nginx_lb + args.num_postgres) == 0:
|
|
||||||
raise Exception("At least one item should be deployed")
|
|
||||||
|
|
||||||
env_path = path.join("customers", args.customer_name, "envs", args.env_name)
|
|
||||||
Path(env_path).mkdir(exist_ok=True, parents=True)
|
|
||||||
|
|
||||||
web_specs = None
|
|
||||||
if args.num_nginx_web > 0:
|
|
||||||
print("\nNginx webserver resources:")
|
|
||||||
web_specs = MachineResources.from_prompt()
|
|
||||||
|
|
||||||
lb_specs = None
|
|
||||||
if args.num_nginx_lb > 0:
|
|
||||||
print("\nNginx loadbalancer resources: ")
|
|
||||||
lb_specs = MachineResources.from_prompt()
|
|
||||||
|
|
||||||
psql_specs = None
|
|
||||||
if args.num_postgres > 0:
|
|
||||||
print("\nPostgresql machine resources: ")
|
|
||||||
psql_specs = MachineResources.from_prompt()
|
|
||||||
|
|
||||||
# Template `ansible.cfg`
|
|
||||||
src = path.join(TEMPLATE_DIR, "ansible.cfg.template")
|
|
||||||
dest = path.join(env_path, "ansible.cfg")
|
|
||||||
copy_template(src=src, dest=dest)
|
|
||||||
|
|
||||||
# Create inventory file
|
|
||||||
inv_path = path.join(env_path, "inventory")
|
|
||||||
iw = InventoryWriter(inv_path)
|
|
||||||
ip_generator = iter_ips(args.ip_format, args.ip_int)
|
|
||||||
|
|
||||||
web_ips = itertools.islice(ip_generator, args.num_nginx_web)
|
|
||||||
iw.add("webserver", web_ips)
|
|
||||||
|
|
||||||
lb_ips = itertools.islice(ip_generator, args.num_nginx_lb)
|
|
||||||
iw.add("loadbalancer", lb_ips)
|
|
||||||
|
|
||||||
psql_gen_pass: Callable[[str], str] = lambda x: encode_member(
|
|
||||||
x, {"psql_pass": gen_pass()}
|
|
||||||
)
|
|
||||||
|
|
||||||
psql_ips = list(itertools.islice(ip_generator, args.num_postgres))
|
|
||||||
psql_ips = map(psql_gen_pass, psql_ips)
|
|
||||||
iw.add("postgresql", psql_ips)
|
|
||||||
|
|
||||||
iw.flush()
|
|
||||||
|
|
||||||
# Template `Vagrantfile`
|
|
||||||
src = path.join(TEMPLATE_DIR, "Vagrantfile.template")
|
|
||||||
dest = path.join(env_path, "Vagrantfile")
|
|
||||||
|
|
||||||
mapping = {
|
|
||||||
"env": args.env_name,
|
|
||||||
"customer_name": args.customer_name,
|
|
||||||
"ip_int": args.ip_int,
|
|
||||||
"ip_format": args.ip_format.replace("{}", "%d"),
|
|
||||||
"num_webserver": args.num_nginx_web,
|
|
||||||
"num_loadbalancers": args.num_nginx_lb,
|
|
||||||
"num_postgres": args.num_postgres,
|
|
||||||
"webserver_specs": dataclasses.asdict(web_specs),
|
|
||||||
"loadbalancers_specs": dataclasses.asdict(lb_specs),
|
|
||||||
"postgres_specs": dataclasses.asdict(psql_specs),
|
|
||||||
}
|
|
||||||
copy_template(src=src, dest=dest, mapping=mapping)
|
|
||||||
|
|
||||||
# Generate .ssh
|
|
||||||
ssh_dir = path.join(env_path, ".ssh")
|
|
||||||
Path(ssh_dir).mkdir(exist_ok=True)
|
|
||||||
rsa_path = path.join(ssh_dir, "id_rsa")
|
|
||||||
if not Path(rsa_path).exists():
|
|
||||||
print(end="\n")
|
|
||||||
ssh_key_cmd = [
|
|
||||||
"ssh-keygen",
|
|
||||||
"-t",
|
|
||||||
"rsa",
|
|
||||||
"-b",
|
|
||||||
"2048",
|
|
||||||
"-f",
|
|
||||||
rsa_path,
|
|
||||||
]
|
|
||||||
sub.call(ssh_key_cmd)
|
|
||||||
|
|
||||||
# Provision and configure machines
|
|
||||||
sub.call(["vagrant", "up"], cwd=env_path)
|
|
||||||
print("Waiting on virtual machines...")
|
|
||||||
time.sleep(5)
|
|
||||||
sub.call(["ansible-playbook", "../../../../site.yml"], cwd=env_path)
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
def main() -> int:
|
||||||
parser = argparse.ArgumentParser()
|
print(P_BANNER)
|
||||||
sub_parser = parser.add_subparsers()
|
customer_name = Prompter.input("Wat is uw naam: ",
|
||||||
|
lambda x: bool(RE_TEXT.match(x)))
|
||||||
|
"""
|
||||||
|
Ensures the necessary customer setup.
|
||||||
|
|
||||||
|
NOTE: The customer and the `envs` directory inside of it never get removed.
|
||||||
|
"""
|
||||||
|
envs_dir = path.join("customers", customer_name, "envs")
|
||||||
|
Path(envs_dir).mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
list_parser = sub_parser.add_parser("list", help="list customer-owned environments")
|
p = Prompter(ps1=f"{customer_name} > ")
|
||||||
list_parser.add_argument("customer_name", type=str, help="name of the customer")
|
while True:
|
||||||
list_parser.set_defaults(func=list_envs)
|
print(P_OPTIONS)
|
||||||
|
choice = p.take_input("Keuze: ", RE_ANY)
|
||||||
|
print(end="\n")
|
||||||
|
|
||||||
cenv_parser = sub_parser.add_parser("create", help="create a new environment")
|
if choice == PortalOptions.EXIT.value:
|
||||||
cenv_parser.add_argument("customer_name", type=str, help="name of the customer")
|
"""
|
||||||
cenv_parser.add_argument("env_name", type=str, help="name of the environment")
|
Used to break out of the loop and exit the portal.
|
||||||
cenv_parser.add_argument(
|
"""
|
||||||
"--num-postgres",
|
break
|
||||||
type=check_positive,
|
|
||||||
help="number of postgres databases",
|
|
||||||
default=0,
|
|
||||||
)
|
|
||||||
cenv_parser.add_argument(
|
|
||||||
"--num-nginx-web",
|
|
||||||
type=check_positive,
|
|
||||||
help="number of nginx webservers",
|
|
||||||
default=0,
|
|
||||||
)
|
|
||||||
cenv_parser.add_argument(
|
|
||||||
"--num-nginx-lb",
|
|
||||||
type=check_positive,
|
|
||||||
help="number of nginx loadbalancers",
|
|
||||||
default=0,
|
|
||||||
)
|
|
||||||
cenv_parser.add_argument(
|
|
||||||
"--ip-format", type=str, help="format of ip", default="192.168.56.{}"
|
|
||||||
)
|
|
||||||
cenv_parser.add_argument(
|
|
||||||
"--ip-int", type=check_positive, help="4th octet to start at", default=10
|
|
||||||
)
|
|
||||||
cenv_parser.set_defaults(func=create_env)
|
|
||||||
|
|
||||||
denv_parser = sub_parser.add_parser("delete", help="delete an environment")
|
if choice == PortalOptions.CREATE.value:
|
||||||
denv_parser.add_argument("customer_name", type=str, help="name of the customer")
|
"""
|
||||||
denv_parser.add_argument(
|
Used to either update or create an environment.
|
||||||
"env_names", type=str, nargs="+", help="name of one or more environments"
|
"""
|
||||||
)
|
envs = get_env_list(customer_name)
|
||||||
|
print(
|
||||||
|
f"NOTE: Kies een albestaande omgevingen {envs} of iets nieuws..."
|
||||||
|
)
|
||||||
|
fmt = "Omgevingsnaam: "
|
||||||
|
env_name = p.take_input(fmt, RE_TEXT)
|
||||||
|
|
||||||
denv_parser.set_defaults(func=delete_env)
|
# 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")
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
"""
|
||||||
|
Define the format for templating ip-addresses.`{}` is required
|
||||||
|
and will be subbed at runtime with the correct octet.
|
||||||
|
"""
|
||||||
|
print(end="\n")
|
||||||
|
fmt = "NOTE: `{}` will be replaced with the correct octet at runtime"
|
||||||
|
print(fmt)
|
||||||
|
fmt = "Format of ip (default=192.168.56.{}): "
|
||||||
|
ip_format = p.take_input(fmt, RE_ANY, default="192.168.56.{}")
|
||||||
|
|
||||||
|
fmt = "Number to start formatting the IP from (default=10): "
|
||||||
|
ip_int = p.take_input(fmt, 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
|
||||||
|
])
|
||||||
|
print(f"Omgeving `{env_name}` successvol gemaakt.")
|
||||||
|
|
||||||
|
if choice == PortalOptions.DESTROY.value:
|
||||||
|
"""
|
||||||
|
Deletes all traces of the chosen environment.
|
||||||
|
"""
|
||||||
|
envs = get_env_list(customer_name)
|
||||||
|
fmt = f"Omgevingsnaam {envs}: "
|
||||||
|
env_name = p.take_choice(fmt, envs)
|
||||||
|
sub.call(["python3", "cli.py", "delete", customer_name, env_name])
|
||||||
|
|
||||||
|
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
|
||||||
|
.ssh directory of that specific environment. The .ssh directory is only
|
||||||
|
necessary in case of an existing storage-volume. Otherwise a new one
|
||||||
|
keypair can be manually created or created using option 1.
|
||||||
|
"""
|
||||||
|
envs = get_env_list(customer_name)
|
||||||
|
fmt = f"Omgevingsnaam {envs}: "
|
||||||
|
env_name = p.take_choice(fmt, envs)
|
||||||
|
sub.call(["python3", "cli.py", "recover", customer_name, env_name])
|
||||||
|
|
||||||
|
if choice == PortalOptions.LIST.value:
|
||||||
|
"""
|
||||||
|
This branch displays the customer's existing environments
|
||||||
|
"""
|
||||||
|
print("De volgende omgeving(en) zijn aanwezig: ")
|
||||||
|
for env in get_env_list(customer_name):
|
||||||
|
print(f" - {env}")
|
||||||
|
|
||||||
args = parser.parse_args(sys.argv[1:])
|
|
||||||
args.func(args)
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,11 +10,10 @@ Vagrant.configure("2") do |config|
|
|||||||
config.ssh.insert_key = false
|
config.ssh.insert_key = false
|
||||||
config.ssh.private_key_path = ["./.ssh/id_rsa","~/.vagrant.d/insecure_private_key"]
|
config.ssh.private_key_path = ["./.ssh/id_rsa","~/.vagrant.d/insecure_private_key"]
|
||||||
|
|
||||||
num_webserver = {{ num_webserver }}
|
|
||||||
num_loadbalancer = {{ num_loadbalancers }}
|
|
||||||
num_postgresql = {{ num_postgres }}
|
num_postgresql = {{ num_postgres }}
|
||||||
|
|
||||||
(1..num_webserver).each do |nth|
|
{% if webserver_specs is not none %}
|
||||||
|
(1..{{ num_webserver }}).each do |nth|
|
||||||
machine_id = "{{ customer_name }}-{{ env }}-web%d" % [nth]
|
machine_id = "{{ customer_name }}-{{ env }}-web%d" % [nth]
|
||||||
machine_ip = increment_ip()
|
machine_ip = increment_ip()
|
||||||
|
|
||||||
@ -33,8 +32,10 @@ Vagrant.configure("2") do |config|
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
(1..num_loadbalancer).each do |nth|
|
{% if loadbalancers_specs is not none %}
|
||||||
|
(1..{{ num_loadbalancers }}).each do |nth|
|
||||||
machine_id = "{{ customer_name }}-{{ env }}-lb%d" % [nth]
|
machine_id = "{{ customer_name }}-{{ env }}-lb%d" % [nth]
|
||||||
machine_ip = increment_ip()
|
machine_ip = increment_ip()
|
||||||
|
|
||||||
@ -53,8 +54,10 @@ Vagrant.configure("2") do |config|
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
(1..num_postgresql).each do |nth|
|
{% if postgres_specs is not none %}
|
||||||
|
(1..{{ num_postgres }}).each do |nth|
|
||||||
machine_id = "{{ customer_name }}-{{ env }}-db%d" % [nth]
|
machine_id = "{{ customer_name }}-{{ env }}-db%d" % [nth]
|
||||||
machine_ip = increment_ip()
|
machine_ip = increment_ip()
|
||||||
|
|
||||||
@ -73,4 +76,5 @@ Vagrant.configure("2") do |config|
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
{% endif %}
|
||||||
end
|
end
|
||||||
|
@ -3,3 +3,6 @@ private_key_file = "./.ssh/id_rsa"
|
|||||||
remote_user = "vagrant"
|
remote_user = "vagrant"
|
||||||
inventory = ./inventory
|
inventory = ./inventory
|
||||||
host_key_checking = False
|
host_key_checking = False
|
||||||
|
|
||||||
|
[ssh_connection]
|
||||||
|
retries=2
|
||||||
|
Reference in New Issue
Block a user