2023-08-10 17:38:07 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# Copyright (C) 2023 Sofus Albert Høgsbro Rose
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
if not all([
|
|
|
|
sys.version_info.major == 3,
|
|
|
|
sys.version_info.minor in [9, 10, 11, 12, 13],
|
|
|
|
]):
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
import platform
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import contextlib
|
|
|
|
|
|
|
|
####################
|
|
|
|
# - Constants
|
|
|
|
####################
|
|
|
|
SCRIPT_PATH = Path(__file__).resolve().parent
|
|
|
|
|
|
|
|
CMD_DEPENDENCIES = [
|
|
|
|
'podman',
|
|
|
|
'git',
|
|
|
|
'pre-commit'
|
|
|
|
]
|
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
IMAGE_NAME = "site-support"
|
2023-08-14 13:44:00 +02:00
|
|
|
IMAGE_VERSION = ("0", "2", "0")
|
2023-08-10 17:38:07 +02:00
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
REGISTRY_HOST = "git.sofus.io"
|
2023-08-13 22:51:26 +02:00
|
|
|
REGISTRY_USER = "python-support"
|
2023-08-13 04:37:36 +02:00
|
|
|
REGISTRY_PASSWORD = lambda: subprocess.check_output(
|
2023-08-13 01:22:05 +02:00
|
|
|
['pass', 'services/home/git.sofus.io/container-registry-token']
|
|
|
|
).decode('ascii').strip()
|
|
|
|
|
2023-08-10 17:38:07 +02:00
|
|
|
####################
|
|
|
|
# - Help Text
|
|
|
|
####################
|
|
|
|
def action_help() -> None:
|
2023-08-13 22:51:26 +02:00
|
|
|
print("""This script provides one-click development, CI, and deployment,
|
|
|
|
to support the use of the project.
|
2023-08-10 17:38:07 +02:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
echo -e "./run.py [OPTION] [EXTRAS]"
|
2023-08-13 04:37:36 +02:00
|
|
|
|
2023-08-10 17:38:07 +02:00
|
|
|
The following commands must be available:
|
|
|
|
podman
|
2023-08-13 04:37:36 +02:00
|
|
|
=> This project is developed and run in podman containers.
|
|
|
|
=> https://podman.io/
|
2023-08-10 17:38:07 +02:00
|
|
|
git
|
|
|
|
=> This project uses git for versioning, and collaboration.
|
2023-08-13 04:37:36 +02:00
|
|
|
=> https://git-scm.com/
|
2023-08-10 17:38:07 +02:00
|
|
|
pre-commit
|
|
|
|
=> Enforces that certain checks pass at each commit.
|
2023-08-13 04:37:36 +02:00
|
|
|
=> https://pre-commit.com/
|
2023-08-10 17:38:07 +02:00
|
|
|
|
|
|
|
Options:
|
2023-08-13 01:22:05 +02:00
|
|
|
./run.py
|
|
|
|
=> Equivilant to run.py help.
|
2023-08-10 17:38:07 +02:00
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
./run.py -h|h|help
|
2023-08-10 17:38:07 +02:00
|
|
|
=> Shows this text.
|
|
|
|
|
|
|
|
Options, Run Locally:
|
2023-08-13 01:22:05 +02:00
|
|
|
./run.py app
|
2023-08-13 04:37:36 +02:00
|
|
|
=> Will run the app live on port 8787.
|
2023-08-10 17:38:07 +02:00
|
|
|
|
|
|
|
Options, Check:
|
2023-08-13 01:22:05 +02:00
|
|
|
./run.py check
|
2023-08-10 17:38:07 +02:00
|
|
|
=> Will run all checks, including static analysis and tests.
|
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
./run.py analyze-quality [OPTIONS]
|
2023-08-13 04:37:36 +02:00
|
|
|
=> Not yet implemented...
|
|
|
|
=> Potentially, could run ruff on Python snippets in the book.
|
2023-08-10 17:38:07 +02:00
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
./run.py analyze-types [OPTIONS]
|
2023-08-13 04:37:36 +02:00
|
|
|
=> Not yet implemented...
|
|
|
|
=> Potentially, could run mypy on Python snippets in the book.
|
2023-08-10 17:38:07 +02:00
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
./run.py analyze-security
|
2023-08-10 17:38:07 +02:00
|
|
|
=> Will run the static security analysis suite.
|
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
./run.py analyze-format [--overwrite] [OPTIONS]
|
2023-08-13 04:37:36 +02:00
|
|
|
=> Not yet implemented...
|
|
|
|
=> Potentially, could format Python snippets in the book.
|
2023-08-10 17:38:07 +02:00
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
./run.py test
|
2023-08-13 04:37:36 +02:00
|
|
|
=> Not yet impelmented...
|
|
|
|
=> Potentially, would test distro commands in an appropriate container.
|
|
|
|
=> Potentially, would turn Python snippets into pytest units.
|
|
|
|
- See https://github.com/modal-labs/pytest-markdown-docs
|
2023-08-13 01:22:05 +02:00
|
|
|
=> Will run all Markdown Python snippets as tests.
|
2023-08-10 17:38:07 +02:00
|
|
|
|
|
|
|
Options, Build & Deploy:
|
2023-08-13 01:22:05 +02:00
|
|
|
./run.py build
|
2023-08-13 04:37:36 +02:00
|
|
|
=> Will build a docker image, and tag it :dev.
|
2023-08-10 17:38:07 +02:00
|
|
|
|
|
|
|
Options, Housekeeping:
|
2023-08-13 01:22:05 +02:00
|
|
|
./run.py clean
|
2023-08-13 04:37:36 +02:00
|
|
|
=> Not yet implemented...
|
2023-08-10 17:38:07 +02:00
|
|
|
=> Will delete all data caused by this project's presence on your system.
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
|
|
####################
|
|
|
|
# - Utilities
|
|
|
|
####################
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def cd_script_dir() -> None:
|
|
|
|
cwd_orig = Path.cwd()
|
|
|
|
|
|
|
|
os.chdir(SCRIPT_PATH)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.chdir(cwd_orig)
|
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
def get_git_revision_hash(short = True) -> str:
|
|
|
|
commit_id = subprocess.check_output(
|
2023-08-10 17:38:07 +02:00
|
|
|
['git', 'rev-parse', 'HEAD']
|
|
|
|
).decode('ascii').strip()
|
2023-08-13 01:22:05 +02:00
|
|
|
|
|
|
|
return commit_id[:16] if short else commit_id
|
2023-08-10 17:38:07 +02:00
|
|
|
|
|
|
|
def cmd_exists(cmd: str) -> bool:
|
|
|
|
"""Checks if a command exists. Supports Linux, Mac, Windows.
|
|
|
|
"""
|
|
|
|
return shutil.which(cmd) is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
####################
|
2023-08-13 01:22:05 +02:00
|
|
|
# - Actions - Build
|
2023-08-10 17:38:07 +02:00
|
|
|
####################
|
2023-08-13 01:22:05 +02:00
|
|
|
def action_build(release = False) -> None:
|
|
|
|
tag = get_git_revision_hash() if release else "dev"
|
2023-08-10 17:38:07 +02:00
|
|
|
subprocess.run([
|
|
|
|
"podman", "build",
|
|
|
|
".",
|
|
|
|
|
|
|
|
# :<commit_id> - Tag Commit ID
|
|
|
|
"--tag",
|
2023-08-13 01:22:05 +02:00
|
|
|
f"{IMAGE_NAME}:{tag}",
|
2023-08-10 17:38:07 +02:00
|
|
|
|
|
|
|
])
|
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
def action_publish() -> None:
|
|
|
|
action_build(release = True)
|
|
|
|
|
|
|
|
# Login to Registry
|
2023-08-10 17:38:07 +02:00
|
|
|
subprocess.run([
|
2023-08-13 01:22:05 +02:00
|
|
|
"podman", "login", REGISTRY_HOST,
|
|
|
|
"--username", REGISTRY_USER,
|
2023-08-13 04:37:36 +02:00
|
|
|
"--password", REGISTRY_PASSWORD(),
|
2023-08-10 17:38:07 +02:00
|
|
|
])
|
2023-08-13 01:22:05 +02:00
|
|
|
|
|
|
|
# Tag & Publish Image @ :<commit_id>
|
|
|
|
subprocess.run([
|
|
|
|
"podman", "image", "push",
|
2023-08-13 04:37:36 +02:00
|
|
|
f"{IMAGE_NAME}:{get_git_revision_hash()}",
|
2023-08-13 01:22:05 +02:00
|
|
|
f"{REGISTRY_HOST}/{REGISTRY_USER}/{IMAGE_NAME}:{get_git_revision_hash()}",
|
|
|
|
])
|
2023-08-10 17:38:07 +02:00
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
# Publish Image @ :M, :M.m, :M.m.p
|
2023-08-10 17:38:07 +02:00
|
|
|
for image_tag in [
|
|
|
|
f"{IMAGE_NAME}:{'.'.join(IMAGE_VERSION)}",
|
|
|
|
f"{IMAGE_NAME}:{'.'.join(IMAGE_VERSION[:2])}",
|
|
|
|
f"{IMAGE_NAME}:{IMAGE_VERSION[0]}",
|
|
|
|
]:
|
2023-08-13 01:22:05 +02:00
|
|
|
# Tag Image
|
2023-08-10 17:38:07 +02:00
|
|
|
subprocess.run([
|
|
|
|
"podman", "tag",
|
2023-08-13 04:37:36 +02:00
|
|
|
f"{IMAGE_NAME}:{get_git_revision_hash()}",
|
2023-08-10 17:38:07 +02:00
|
|
|
image_tag
|
|
|
|
])
|
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
# Publish Image
|
|
|
|
subprocess.run([
|
|
|
|
"podman", "image", "push",
|
|
|
|
image_tag,
|
|
|
|
f"{REGISTRY_HOST}/{REGISTRY_USER}/{image_tag}",
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
####################
|
|
|
|
# - Actions - Run
|
|
|
|
####################
|
|
|
|
def action_app() -> None:
|
|
|
|
action_build()
|
2023-08-10 17:38:07 +02:00
|
|
|
subprocess.run([
|
2023-08-13 01:22:05 +02:00
|
|
|
"podman", "run", "--rm", "-it",
|
|
|
|
"--publish", "8787:8787",
|
2023-08-14 13:37:46 +02:00
|
|
|
f"{IMAGE_NAME}:dev",
|
2023-08-10 17:38:07 +02:00
|
|
|
])
|
|
|
|
|
|
|
|
|
2023-08-13 04:37:36 +02:00
|
|
|
####################
|
|
|
|
# - Actions - Analyze
|
|
|
|
####################
|
|
|
|
def action_analyze_security() -> None:
|
|
|
|
Path('.cache-trivy').mkdir(parents=True, exist_ok=True)
|
|
|
|
subprocess.run([
|
|
|
|
"podman", "run", "--rm", "-it",
|
|
|
|
"--workdir", "/src",
|
|
|
|
"--volume", ".:/src",
|
|
|
|
"--volume", "./.cache-trivy:/root/.cache",
|
|
|
|
"ghcr.io/aquasecurity/trivy:latest",
|
|
|
|
"fs", "--quiet", "--scanners", "vuln,secret,config,license",
|
|
|
|
"--exit-code", "1", "./"
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2023-08-10 17:38:07 +02:00
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
|
2023-08-10 17:38:07 +02:00
|
|
|
####################
|
|
|
|
# - Script
|
|
|
|
####################
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# Check Available Commands
|
|
|
|
for cmd in CMD_DEPENDENCIES:
|
|
|
|
if not cmd_exists(cmd) :
|
2023-08-13 04:37:36 +02:00
|
|
|
print(f"{cmd} is not installed. Please see --help for instructions.")
|
2023-08-10 17:38:07 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
with cd_script_dir():
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
{
|
|
|
|
"build": action_build,
|
|
|
|
"publish": action_publish,
|
|
|
|
|
2023-08-13 01:22:05 +02:00
|
|
|
"app": action_app,
|
|
|
|
"dev": lambda: print("TBD"),
|
|
|
|
|
2023-08-13 04:37:36 +02:00
|
|
|
"analyze-security": action_analyze_security,
|
|
|
|
|
2023-08-10 17:38:07 +02:00
|
|
|
"help": action_help,
|
|
|
|
"-h": action_help,
|
|
|
|
"--help": action_help,
|
|
|
|
}[sys.argv[1]]()
|
|
|
|
else:
|
|
|
|
action_help()
|