From d274d25c2da31fd6cf5b8a86f81d9d79c4545e8c Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 3 Feb 2021 12:12:20 -0800 Subject: Replace dev.sh with Docker Compose Managing development containers through Docker Compose is convenient. However, it isn't quite flexible enough to facilitate both development and normal use. It's not really worth accommodating the latter since the container gets pushed to a registry and that's the intended way to run the service. Anyone that is checking out the repository and therefore has access to the compose file is likely a developer, not a user. --- scripts/dev.sh | 65 ---------------------------------------------------------- 1 file changed, 65 deletions(-) delete mode 100755 scripts/dev.sh (limited to 'scripts') diff --git a/scripts/dev.sh b/scripts/dev.sh deleted file mode 100755 index efbd93a..0000000 --- a/scripts/dev.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env sh - -# Sets up a development environment and runs a shell in a docker container. -# Usage: dev.sh [--build [--clean]] [bash_args ...] - -if [ "$1" = "--build" ]; then - shift - printf "Building ghcr.io/python-discord/snekbox-venv:dev..." - - docker build \ - -t ghcr.io/python-discord/snekbox-venv:dev \ - -f Dockerfile \ - --build-arg DEV=1 \ - --target venv \ - -q \ - . \ - >/dev/null \ - && printf " done!\n" || exit "$?" - - if [ "$1" = "--clean" ]; then - shift - dangling_imgs=$(docker images -f "dangling=true" -q) - - if [ -n "${dangling_imgs}" ]; then - printf "Removing dangling images..." - - # shellcheck disable=SC2086 - docker rmi $dangling_imgs >/dev/null \ - && printf " done!\n" || exit "$?" - fi - fi -fi - -# Keep the container up in the background so it doesn't have to be restarted -# for the ownership fix. -# The volume is mounted to same the path in the container as the source -# directory on the host to ensure coverage can find the source files. -docker run \ - --tty \ - --detach \ - --name snekbox_test \ - --privileged \ - --hostname pdsnk-dev \ - --ipc="none" \ - -e PYTHONDONTWRITEBYTECODE=1 \ - -e PIPENV_PIPFILE="/snekbox/Pipfile" \ - --volume "${PWD}":"${PWD}" \ - --workdir "${PWD}"\ - --entrypoint /bin/bash \ - ghcr.io/python-discord/snekbox-venv:dev \ - >/dev/null \ - -# Execute the given command(s) -docker exec -it snekbox_test /bin/bash "$@" - -# Fix ownership of coverage file -# BusyBox doesn't support --reference for chown -docker exec \ - -it \ - -e CWD="${PWD}" \ - snekbox_test \ - /bin/bash \ - -c 'chown "$(stat -c "%u:%g" "${CWD}")" "${CWD}/.coverage"' - -docker rm -f snekbox_test >/dev/null # Stop and remove the container -- cgit v1.2.3 From 12548fb233700792a5b109950db41e49284ad67f Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Wed, 3 Feb 2021 13:44:53 -0800 Subject: Replace protoc shell script with a Python one --- Pipfile | 2 +- scripts/protoc.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/protoc.sh | 14 ------------- 3 files changed, 63 insertions(+), 15 deletions(-) create mode 100755 scripts/protoc.py delete mode 100755 scripts/protoc.sh (limited to 'scripts') diff --git a/Pipfile b/Pipfile index c7a57fd..7b087ba 100644 --- a/Pipfile +++ b/Pipfile @@ -49,4 +49,4 @@ build = "docker build -t ghcr.io/python-discord/snekbox:latest ." devsh = "docker-compose run --entrypoint /bin/bash --rm snekbox" # Other -protoc = "sh scripts/protoc.sh" +protoc = "python -m scripts.protoc" diff --git a/scripts/protoc.py b/scripts/protoc.py new file mode 100755 index 0000000..09429d3 --- /dev/null +++ b/scripts/protoc.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 +import shutil +import subprocess +import sys +from argparse import ArgumentParser +from pathlib import Path +from tempfile import TemporaryDirectory +from urllib.request import urlopen + +SRC_DIR = Path("snekbox").resolve(strict=True) +FILE_NAME = "config" + + +def compile_proto(path: Path) -> None: + """Compile a protobuf file at `path` into Python code.""" + protoc_bin = shutil.which("protoc") + if not protoc_bin: + print("protoc binary could not be found on PATH", file=sys.stderr) + sys.exit(1) + + args = [protoc_bin, f"--proto_path={path.parent}", f"--python_out={SRC_DIR}", path] + result = subprocess.run(args) + + if result.returncode != 0: + sys.exit(result.returncode) + + +def get_version() -> str: + """Get the NsJail version from the command line arguments.""" + parser = ArgumentParser(description="Compile an NsJail config protobuf into Python.") + parser.add_argument("version", help="the NsJail version from which to get the protobuf file") + args = parser.parse_args() + + return args.version + + +def main() -> None: + """Get a config.proto for NsJail and compile it into Python.""" + version = get_version() + url = f"https://raw.githubusercontent.com/google/nsjail/{version}/config.proto" + + with urlopen(url) as response: + if response.status >= 400: + print(f"Failed to retrieve config.proto: status {response.status}", file=sys.stderr) + sys.exit(1) + + with TemporaryDirectory() as dir_name: + file_path = Path(dir_name) / f"{FILE_NAME}.proto" + with open(file_path, "wb") as file: + file.write(response.read()) + compile_proto(file_path) + + # Remove the _pb suffix from the generated Python file. + if generated_py := next(SRC_DIR.glob(f"{FILE_NAME}_pb*.py"), None): + generated_py.rename(generated_py.with_stem(FILE_NAME)) + else: + print(f"Could not find the generated Python file in {SRC_DIR}.", file=sys.stderr) + sys.exit(1) + + +if __name__ == "__main__": + main() diff --git a/scripts/protoc.sh b/scripts/protoc.sh deleted file mode 100755 index 5771b95..0000000 --- a/scripts/protoc.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env sh - -set -eu - -URL='https://raw.githubusercontent.com/google/nsjail/2.9/config.proto' -SRC_DIR='snekbox' -FILE_NAME='config' -PROTO_PATH="${SRC_DIR}/${FILE_NAME}.proto" - -curl -SsL "${URL}" -o "${PROTO_PATH}" -protoc --proto_path="${SRC_DIR}" --python_out="${SRC_DIR}" "${PROTO_PATH}" - -rm -f "${PROTO_PATH}" -mv -f "${SRC_DIR}/${FILE_NAME}_pb"*.py "${SRC_DIR}/${FILE_NAME}.py" -- cgit v1.2.3