feat: Basic working image.
parent
c976e25148
commit
c02c7b2210
|
@ -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/
|
||||||
|
|
|
@ -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
|
||||||
|
|
160
run.py
160
run.py
|
@ -1,19 +1,20 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
#Copyright (C) 2023 Sofus Albert Høgsbro Rose
|
# Copyright (C) 2023 Sofus Albert Høgsbro Rose
|
||||||
#
|
#
|
||||||
#This program is free software: you can redistribute it and/or modify
|
# 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
|
# it under the terms of the GNU General Public License as published by
|
||||||
#the Free Software Foundation, either version 3 of the License, or
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
#(at your option) any later version.
|
# (at your option) any later version.
|
||||||
#
|
#
|
||||||
#This program is distributed in the hope that it will be useful,
|
# This program is distributed in the hope that it will be useful,
|
||||||
#but WITHOUT ANY WARRANTY; without even the implied warranty of
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
#GNU General Public License for more details.
|
# GNU General Public License for more details.
|
||||||
#
|
#
|
||||||
#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,35 +168,85 @@ 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}",
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
####################
|
####################
|
||||||
# - 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)
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
with cd_script_dir():
|
||||||
{
|
if len(sys.argv) > 1:
|
||||||
"build": action_build,
|
{
|
||||||
"publish": action_publish,
|
"run": action_run,
|
||||||
|
|
||||||
"help": action_help,
|
"build": action_build,
|
||||||
"-h": action_help,
|
"publish": action_publish,
|
||||||
"--help": action_help,
|
|
||||||
}[sys.argv[1]]()
|
"help": action_help,
|
||||||
else:
|
"-h": action_help,
|
||||||
action_help()
|
"--help": action_help,
|
||||||
|
}[sys.argv[1]]()
|
||||||
|
else:
|
||||||
|
action_help()
|
||||||
|
|
Loading…
Reference in New Issue