diff --git a/Dockerfile b/Dockerfile index 6968f9c..145181f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ #################### # - Stage: Dependencies #################### -FROM docker.io/rust:1.57-slim-bookworm AS base +FROM docker.io/rust:1-slim-bookworm AS base ENV CARGO_INSTALL_ROOT /usr/local/ ENV CARGO_TARGET_DIR /tmp/target/ diff --git a/README.md b/README.md index 736a5bc..7d0e3ad 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## Plugins +# MDBook Plugins The following plugins for `mdbook` are in use: - `linkcheck`: Checks if links in the book are alive. - **crates.io**: https://crates.io/crates/mdbook-linkcheck @@ -13,3 +13,9 @@ The following plugins for `mdbook` are in use: - **crates.io**: https://crates.io/crates/mdbook-admonish - `quiz`: Provides interactive quizzes while reading. - **crates.io**: https://crates.io/crates/mdbook-quiz + +# TODO +- [ ] Do an `all()` thing for the checks + +# Pre-Commit Hooks +https://github.com/compilerla/conventional-pre-commit diff --git a/run.py b/run.py index ab6c495..f56f7c3 100755 --- a/run.py +++ b/run.py @@ -1,19 +1,20 @@ #!/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 . +# 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 . +import os import sys if not all([ sys.version_info.major == 3, @@ -25,15 +26,12 @@ from pathlib import Path import platform import shutil import subprocess +import contextlib #################### # - Constants #################### -IMAGE_NAME = "docker-mdbook" -IMAGE_VERSION = "0.4.34" - -RUST_IMAGE = "docker.io/rust" -RUST_TAG = "1-slim-bullseye" +SCRIPT_PATH = Path(__file__).resolve().parent CMD_DEPENDENCIES = [ 'podman', @@ -41,6 +39,16 @@ CMD_DEPENDENCIES = [ 'pre-commit' ] +IMAGE_NAME = "docker-mdbook" +IMAGE_VERSION = ("0", "4", "34") + +REGISTRY_HOST = "git.sofus.io" +REGISTRY_USER = "so-rose" +REGISTRY_PASSWORD = subprocess.check_output( + ['pass', 'services/home/git.sofus.io/container-registry-token'] +).decode('ascii').strip() + + #################### # - Help Text #################### @@ -112,12 +120,46 @@ def action_help() -> None: #################### # - Utilities #################### +@contextlib.contextmanager +def cd_script_dir() -> None: + cwd_orig = Path.cwd() + + os.chdir(SCRIPT_PATH) + try: + yield + finally: + os.chdir(cwd_orig) + +def get_git_revision_hash(short = True) -> str: + commit_id = subprocess.check_output( + ['git', 'rev-parse', 'HEAD'] + ).decode('ascii').strip() + + return commit_id[:16] if short else commit_id + def cmd_exists(cmd: str) -> bool: """Checks if a command exists. Supports Linux, Mac, Windows. """ return shutil.which(cmd) is not None + +#################### +# - Checks +#################### +def check_mdbook_version_matches() -> None: + with cd_script_dir(): + with open('Dockerfile', 'r') as f: + mdbook_version_lines = [ + line for line in f.readlines() + if line.strip() == f'ARG MDBOOK_VERSION="{".".join(IMAGE_VERSION)}"' + ] + + if not len(mdbook_version_lines) == 1: + print("IMAGE_VERSION doesn't match MDBOOK_VERSION") + sys.exit(1) + + #################### # - Actions #################### @@ -126,35 +168,85 @@ def action_build() -> None: "podman", "build", ".", "--target", "base", - "--tag", f"{IMAGE_NAME}:{IMAGE_VERSION}", + + # : - Tag Commit ID + "--tag", + f"{IMAGE_NAME}:{get_git_revision_hash()}", + ]) - + + +def action_run() -> None: + print(" ".join([ + "podman", "run", "--rm", "-it", + "--volume", "./test:/src", + "--workdir", "/src", + "--publish", "3000:3000", + f"{IMAGE_NAME}:{get_git_revision_hash()}", + ] + sys.argv[2:], + )) + + def action_publish() -> None: action_build() + subprocess.run([ - "podman", "image", "push", "--all-tags", - f"git.sofus.io/so-rose/{IMAGE_NAME}", + "podman", "login", REGISTRY_HOST, + "--username", REGISTRY_USER, + "--password", REGISTRY_PASSWORD, ]) + # Publish Image @ : + subprocess.run([ + "podman", "image", "push", + f"{IMAGE_NAME}:{get_git_revision_hash()}", + f"{REGISTRY_HOST}/{REGISTRY_USER}/{IMAGE_NAME}:{get_git_revision_hash()}", + ]) + + # Publish Image @ :M, :M.m, :M.m.p + for image_tag in [ + f"{IMAGE_NAME}:{'.'.join(IMAGE_VERSION)}", + f"{IMAGE_NAME}:{'.'.join(IMAGE_VERSION[:2])}", + f"{IMAGE_NAME}:{IMAGE_VERSION[0]}", + ]: + subprocess.run([ + "podman", "tag", + f"{IMAGE_NAME}:{get_git_revision_hash()}", + image_tag + ]) + + # Publish Image + subprocess.run([ + "podman", "image", "push", + image_tag, + f"{REGISTRY_HOST}/{REGISTRY_USER}/{image_tag}", + ]) + #################### # - Script #################### if __name__ == "__main__": + # Check version match w/MDBOOK in Dockerfile + check_mdbook_version_matches() + # Check Available Commands for cmd in CMD_DEPENDENCIES: if not cmd_exists(cmd) : print("One or more dependencies are not installed. Please see --help.") sys.exit(1) - if len(sys.argv) > 1: - { - "build": action_build, - "publish": action_publish, - - "help": action_help, - "-h": action_help, - "--help": action_help, - }[sys.argv[1]]() - else: - action_help() + with cd_script_dir(): + if len(sys.argv) > 1: + { + "run": action_run, + + "build": action_build, + "publish": action_publish, + + "help": action_help, + "-h": action_help, + "--help": action_help, + }[sys.argv[1]]() + else: + action_help()