2022-07-31 02:33:31 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import subprocess # nosec 404
|
|
|
|
import sys
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
from pathlib import Path
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
# Variables
|
|
|
|
# You might want to change some.
|
|
|
|
|
|
|
|
# Volumes
|
2022-07-31 02:33:31 +00:00
|
|
|
VOLUMES_DIR = Path.home() / "volumes"
|
2022-08-02 11:39:44 +00:00
|
|
|
# AdvLabDB
|
|
|
|
ADVLABDB_REPO_LINK = "https://gitlab.rlp.net/mobitar/advlabdb.git"
|
2022-07-31 02:33:31 +00:00
|
|
|
ADVLABDB_VOLUMES_DIR = VOLUMES_DIR / "advlabdb"
|
|
|
|
ADVLABDB_REPO_DIR = ADVLABDB_VOLUMES_DIR / "repo"
|
|
|
|
ADVLABDB_LOGS_DIR = ADVLABDB_VOLUMES_DIR / "logs"
|
2022-08-02 11:39:44 +00:00
|
|
|
# Traefik
|
2022-07-31 02:33:31 +00:00
|
|
|
TRAEFIK_VOLUMES_DIR = VOLUMES_DIR / "traefik"
|
|
|
|
TRAEFIK_ETC_DIR = TRAEFIK_VOLUMES_DIR / "etc"
|
|
|
|
TRAEFIK_LOGS_DIR = TRAEFIK_VOLUMES_DIR / "logs"
|
|
|
|
TRAEFIK_CERTS_DIR = TRAEFIK_VOLUMES_DIR / "certs"
|
2022-08-02 11:39:44 +00:00
|
|
|
# Nginx
|
|
|
|
NGINX_CONF_D_DIR = VOLUMES_DIR / "nginx/conf.d"
|
|
|
|
# Systemd
|
2022-07-31 02:33:31 +00:00
|
|
|
SYSTEMD_USER_DIR = Path.home() / ".config/systemd/user"
|
|
|
|
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
# Parse script arguments
|
|
|
|
|
|
|
|
parser = ArgumentParser(
|
|
|
|
description="Build the AdvLabDB image and deploy related containers.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--skip-traefik",
|
|
|
|
action="store_true",
|
|
|
|
help="Do not deploy the Traefik container.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--skip-nginx",
|
|
|
|
action="store_true",
|
|
|
|
help="Do not deploy the Nginx container.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-n",
|
|
|
|
"--network",
|
|
|
|
default="traefik",
|
|
|
|
help="Traefik's network. Default: traefik.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-v",
|
|
|
|
"--verbose",
|
|
|
|
action="store_true",
|
|
|
|
help="Show more information while running.",
|
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Functions
|
|
|
|
|
|
|
|
|
2022-07-31 02:33:31 +00:00
|
|
|
def run(command: str, **kwargs):
|
2022-08-02 11:39:44 +00:00
|
|
|
if args.verbose:
|
|
|
|
print("Running command:")
|
|
|
|
print(f"\t{command}")
|
2022-07-31 02:33:31 +00:00
|
|
|
return subprocess.run(command, shell=True, **kwargs) # nosec B602
|
|
|
|
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
# Needed for create_container
|
|
|
|
if args.verbose:
|
|
|
|
print(f"Making sure that the systemd directory for user services {SYSTEMD_USER_DIR} exists.")
|
2022-07-31 02:33:31 +00:00
|
|
|
SYSTEMD_USER_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
def create_container(container_name: str, podman_args: str):
|
2022-08-02 11:39:44 +00:00
|
|
|
"""
|
|
|
|
Create a container with a systemd service.
|
|
|
|
"""
|
|
|
|
print(f"Creating container: {container_name}.")
|
2022-07-31 02:33:31 +00:00
|
|
|
run(
|
|
|
|
f"""podman create \
|
|
|
|
--name {container_name} \
|
2022-08-02 12:33:20 +00:00
|
|
|
--tz local \
|
2022-07-31 02:33:31 +00:00
|
|
|
{podman_args}""",
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
print(f"Generating a systemd service then enabling and starting it for the container {container_name}.")
|
|
|
|
print(f"The service is a user service and named container-{container_name}.")
|
|
|
|
print("You can check its status with the following command:")
|
|
|
|
print(f"\tsystemctl --user status container-{container_name}")
|
2022-07-31 02:33:31 +00:00
|
|
|
run(
|
|
|
|
f"podman generate systemd --new --files --name {container_name}",
|
|
|
|
cwd=SYSTEMD_USER_DIR,
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
run(
|
|
|
|
f"systemctl --user enable --now container-{container_name}",
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
# Checking requirements
|
2022-07-31 02:33:31 +00:00
|
|
|
|
|
|
|
env_file = ADVLABDB_REPO_DIR / ".env"
|
|
|
|
|
|
|
|
if ADVLABDB_REPO_DIR.is_dir():
|
2022-08-02 11:39:44 +00:00
|
|
|
print("Pulling AdvLabDB repository.")
|
2022-07-31 02:33:31 +00:00
|
|
|
run(
|
2022-08-02 11:59:26 +00:00
|
|
|
"git pull origin main",
|
2022-07-31 02:33:31 +00:00
|
|
|
cwd=ADVLABDB_REPO_DIR,
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not env_file.is_file():
|
|
|
|
sys.exit(f"{env_file} missing!")
|
|
|
|
else:
|
2022-08-02 11:39:44 +00:00
|
|
|
if args.verbose:
|
|
|
|
print(f"Making sure that the volumes directory {ADVLABDB_VOLUMES_DIR} exists.")
|
2022-07-31 02:33:31 +00:00
|
|
|
ADVLABDB_VOLUMES_DIR.mkdir(parents=True, exist_ok=True)
|
2022-08-02 11:39:44 +00:00
|
|
|
|
|
|
|
print("Cloning AdvLabDB repository")
|
|
|
|
print(f"From:\t{ADVLABDB_REPO_LINK}")
|
|
|
|
print(f"Into:\t{ADVLABDB_REPO_DIR}")
|
2022-07-31 02:33:31 +00:00
|
|
|
run(
|
|
|
|
f"git clone {ADVLABDB_REPO_LINK} {ADVLABDB_REPO_DIR}",
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
sys.exit(f"{env_file} missing!")
|
|
|
|
|
|
|
|
if not args.skip_traefik:
|
|
|
|
if not TRAEFIK_ETC_DIR.is_dir():
|
|
|
|
sys.exit(f"{TRAEFIK_ETC_DIR} missing!")
|
|
|
|
|
|
|
|
if not args.skip_nginx:
|
|
|
|
if not NGINX_CONF_D_DIR.is_dir():
|
|
|
|
sys.exit(f"{NGINX_CONF_D_DIR} missing!")
|
|
|
|
|
|
|
|
if run(f"podman network exists {args.network}").returncode != 0:
|
|
|
|
if args.skip_traefik:
|
|
|
|
sys.exit(f"Skipped Traefik's deployment although Traefik's network {args.network} does not exist!")
|
|
|
|
else:
|
2022-08-02 11:39:44 +00:00
|
|
|
print(f"Creating network {args.network}.")
|
2022-07-31 02:33:31 +00:00
|
|
|
run(
|
|
|
|
f"podman network create {args.network}",
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
# Create/update the AdvLabDB image and container
|
|
|
|
|
|
|
|
# Make sure that the builder container does not exist.
|
2022-07-31 02:33:31 +00:00
|
|
|
run(
|
|
|
|
"buildah rm builder",
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
)
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
print("Creating AdvLabDB image.")
|
2022-07-31 02:33:31 +00:00
|
|
|
commands = [
|
2022-08-02 11:39:44 +00:00
|
|
|
# TODO: Use fixed version tag 3.10-slim
|
2022-07-31 02:33:31 +00:00
|
|
|
"buildah from --pull --name builder docker.io/library/python:slim",
|
2022-08-02 11:39:44 +00:00
|
|
|
# Copy repo into container
|
2022-07-31 02:33:31 +00:00
|
|
|
f"buildah copy builder {ADVLABDB_REPO_DIR} /volumes/repo",
|
|
|
|
"buildah config --workingdir /volumes/repo builder",
|
2022-08-02 11:39:44 +00:00
|
|
|
# Install Python requirements in the container
|
2022-07-31 02:33:31 +00:00
|
|
|
"buildah run builder -- pip3 install -r requirements.txt",
|
|
|
|
# TODO: Only when database does not exist
|
|
|
|
"buildah run builder -- python3 -m advlabdb.scripts.setup.init_database",
|
|
|
|
"buildah config --cmd 'gunicorn --bind 0.0.0.0:80 --workers 5 --log-file /volumes/logs/gunicorn.log run:app' builder",
|
|
|
|
]
|
|
|
|
|
|
|
|
for command in commands:
|
|
|
|
run(command, check=True)
|
|
|
|
|
|
|
|
if run("systemctl --user is-enabled container-advlabdb").returncode == 0:
|
2022-08-02 11:39:44 +00:00
|
|
|
print("Disabling and deleting existing container advlabdb.")
|
2022-07-31 02:33:31 +00:00
|
|
|
run(
|
|
|
|
"systemctl --user disable --now container-advlabdb",
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
if run("podman image exists advlabdb").returncode == 0:
|
2022-08-02 11:39:44 +00:00
|
|
|
print("Deleting existing image advlabdb")
|
2022-07-31 02:33:31 +00:00
|
|
|
run(
|
|
|
|
"podman rmi advlabdb",
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
# Save new image
|
2022-07-31 02:33:31 +00:00
|
|
|
run(
|
|
|
|
"buildah commit --rm builder advlabdb",
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
if args.verbose:
|
|
|
|
print(f"Making sure that the logs directory {ADVLABDB_LOGS_DIR} exists.")
|
2022-07-31 02:33:31 +00:00
|
|
|
ADVLABDB_LOGS_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
print("Creating container advlabdb.")
|
2022-07-31 02:33:31 +00:00
|
|
|
create_container(
|
|
|
|
"advlabdb",
|
|
|
|
# TODO: Add database as volume
|
|
|
|
f"""--network {args.network} \
|
|
|
|
-v {ADVLABDB_LOGS_DIR}:/volumes/logs:Z \
|
|
|
|
localhost/advlabdb:latest""",
|
|
|
|
)
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
# Create Traefik container if needed
|
|
|
|
|
2022-07-31 02:33:31 +00:00
|
|
|
if not args.skip_traefik and run("systemctl --user is-enabled container-traefik").returncode != 0:
|
2022-08-02 11:39:44 +00:00
|
|
|
if args.verbose:
|
|
|
|
print(f"Making sure that the logs directory {TRAEFIK_LOGS_DIR} exists.")
|
2022-07-31 02:33:31 +00:00
|
|
|
TRAEFIK_LOGS_DIR.mkdir(parents=True, exist_ok=True)
|
2022-08-02 11:39:44 +00:00
|
|
|
|
|
|
|
if args.verbose:
|
|
|
|
print(f"Making sure that the certificates directory {TRAEFIK_CERTS_DIR} exists.")
|
2022-07-31 02:33:31 +00:00
|
|
|
TRAEFIK_CERTS_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
print("Creating container traefik.")
|
2022-07-31 02:33:31 +00:00
|
|
|
create_container(
|
|
|
|
"traefik",
|
|
|
|
f"""--label "io.containers.autoupdate=registry" \
|
|
|
|
--network {args.network} \
|
|
|
|
-p 80:80 \
|
|
|
|
-p 443:443 \
|
|
|
|
-v {TRAEFIK_ETC_DIR}:/etc/traefik:ro:Z,ro \
|
|
|
|
-v {TRAEFIK_LOGS_DIR}:/volumes/logs:Z \
|
|
|
|
-v {TRAEFIK_CERTS_DIR}:/volumes/certs:Z \
|
|
|
|
docker.io/library/traefik:latest""",
|
|
|
|
)
|
|
|
|
|
2022-08-02 11:39:44 +00:00
|
|
|
# Create Nginx container if needed
|
|
|
|
|
2022-07-31 02:33:31 +00:00
|
|
|
if not args.skip_nginx and run("systemctl --user is-enabled container-nginx").returncode != 0:
|
2022-08-02 11:39:44 +00:00
|
|
|
print("Creating container nginx.")
|
2022-07-31 02:33:31 +00:00
|
|
|
create_container(
|
|
|
|
"nginx",
|
|
|
|
f"""--label "io.containers.autoupdate=registry" \
|
|
|
|
--network {args.network} \
|
|
|
|
-v {NGINX_CONF_D_DIR}:/etc/nginx/conf.d:Z,ro \
|
|
|
|
docker.io/library/nginx:alpine""",
|
|
|
|
)
|
|
|
|
|
|
|
|
print("\nDone!\n")
|