feat: Basic working image.

main
Sofus Albert Høgsbro Rose 2023-08-13 04:42:53 +02:00
parent c976e25148
commit c02c7b2210
Signed by: so-rose
GPG Key ID: AD901CB0F3701434
3 changed files with 134 additions and 36 deletions

View File

@ -1,7 +1,7 @@
#################### ####################
# - Stage: Dependencies # - 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_INSTALL_ROOT /usr/local/
ENV CARGO_TARGET_DIR /tmp/target/ ENV CARGO_TARGET_DIR /tmp/target/

View File

@ -1,4 +1,4 @@
## Plugins # MDBook Plugins
The following plugins for `mdbook` are in use: The following plugins for `mdbook` are in use:
- `linkcheck`: Checks if links in the book are alive. - `linkcheck`: Checks if links in the book are alive.
- **crates.io**: https://crates.io/crates/mdbook-linkcheck - **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 - **crates.io**: https://crates.io/crates/mdbook-admonish
- `quiz`: Provides interactive quizzes while reading. - `quiz`: Provides interactive quizzes while reading.
- **crates.io**: https://crates.io/crates/mdbook-quiz - **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

108
run.py
View File

@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import sys import sys
if not all([ if not all([
sys.version_info.major == 3, sys.version_info.major == 3,
@ -25,15 +26,12 @@ from pathlib import Path
import platform import platform
import shutil import shutil
import subprocess import subprocess
import contextlib
#################### ####################
# - Constants # - Constants
#################### ####################
IMAGE_NAME = "docker-mdbook" SCRIPT_PATH = Path(__file__).resolve().parent
IMAGE_VERSION = "0.4.34"
RUST_IMAGE = "docker.io/rust"
RUST_TAG = "1-slim-bullseye"
CMD_DEPENDENCIES = [ CMD_DEPENDENCIES = [
'podman', 'podman',
@ -41,6 +39,16 @@ CMD_DEPENDENCIES = [
'pre-commit' '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 # - Help Text
#################### ####################
@ -112,12 +120,46 @@ def action_help() -> None:
#################### ####################
# - Utilities # - 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: def cmd_exists(cmd: str) -> bool:
"""Checks if a command exists. Supports Linux, Mac, Windows. """Checks if a command exists. Supports Linux, Mac, Windows.
""" """
return shutil.which(cmd) is not None 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 # - Actions
#################### ####################
@ -126,14 +168,58 @@ def action_build() -> None:
"podman", "build", "podman", "build",
".", ".",
"--target", "base", "--target", "base",
"--tag", f"{IMAGE_NAME}:{IMAGE_VERSION}",
# :<commit_id> - 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: def action_publish() -> None:
action_build() action_build()
subprocess.run([ subprocess.run([
"podman", "image", "push", "--all-tags", "podman", "login", REGISTRY_HOST,
f"git.sofus.io/so-rose/{IMAGE_NAME}", "--username", REGISTRY_USER,
"--password", REGISTRY_PASSWORD,
])
# Publish Image @ :<commit_id>
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}",
]) ])
@ -141,14 +227,20 @@ def action_publish() -> None:
# - Script # - Script
#################### ####################
if __name__ == "__main__": if __name__ == "__main__":
# Check version match w/MDBOOK in Dockerfile
check_mdbook_version_matches()
# Check Available Commands # Check Available Commands
for cmd in CMD_DEPENDENCIES: for cmd in CMD_DEPENDENCIES:
if not cmd_exists(cmd) : if not cmd_exists(cmd) :
print("One or more dependencies are not installed. Please see --help.") print("One or more dependencies are not installed. Please see --help.")
sys.exit(1) sys.exit(1)
with cd_script_dir():
if len(sys.argv) > 1: if len(sys.argv) > 1:
{ {
"run": action_run,
"build": action_build, "build": action_build,
"publish": action_publish, "publish": action_publish,