From d0903a2780bad6d9187edfc07d2c4330967e838f Mon Sep 17 00:00:00 2001 From: David Boreham Date: Wed, 7 Dec 2022 11:38:32 -0700 Subject: [PATCH] Build js builder container --- app/build_containers.py | 28 ++++++++++++----- app/data/container-image-list.txt | 1 + container-build/cerc-builder-js/Dockerfile | 36 ++++++++++++++++++++++ container-build/default-build.sh | 11 +++++++ 4 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 container-build/cerc-builder-js/Dockerfile create mode 100755 container-build/default-build.sh diff --git a/app/build_containers.py b/app/build_containers.py index 3621e5da..f8edb0b3 100644 --- a/app/build_containers.py +++ b/app/build_containers.py @@ -21,7 +21,6 @@ # TODO: display the available list of containers; allow re-build of either all or specific containers import os -import sys from decouple import config import subprocess import click @@ -44,6 +43,9 @@ def command(ctx, include, exclude): dry_run = ctx.obj.dry_run local_stack = ctx.obj.local_stack + # TODO: check this still works in the shiv package scenario + container_build_dir = os.path.join(os.getcwd(), "container-build") + if local_stack: dev_root_path = os.getcwd()[0:os.getcwd().rindex("stack-orchestrator")] print(f'Local stack dev_root_path (CERC_REPO_BASE_DIR) overridden to: {dev_root_path}') @@ -65,15 +67,25 @@ def command(ctx, include, exclude): def process_container(container): if not quiet: print(f"Building: {container}") - build_script_filename = os.path.join("container-build", container.replace("/", "-"), "build.sh") + build_dir = os.path.join(container_build_dir, container.replace("/", "-")) + build_script_filename = os.path.join(build_dir, "build.sh") if verbose: - print(f"Script: {build_script_filename}") - if not os.path.exists(build_script_filename): - print(f"Error, script: {build_script_filename} doesn't exist") - sys.exit(1) + print(f"Build script filename: {build_script_filename}") + if os.path.exists(build_script_filename): + build_command = build_script_filename + else: + if verbose: + print(f"No script file found: {build_script_filename}, using default build script") + repo_dir = container.split('/')[1] + # TODO: make this less of a hack -- should be specified in some metadata somewhere + # Check if we have a repo for this container. If not, set the context dir to the container-build subdir + repo_full_path = os.path.join(dev_root_path, repo_dir) + repo_dir_or_build_dir = repo_dir if os.path.exists(repo_full_path) else build_dir + build_command = os.path.join("container-build", "default-build.sh") + f" {container} {repo_dir_or_build_dir}" if not dry_run: - # We need to export CERC_REPO_BASE_DIR - build_result = subprocess.run(build_script_filename, shell=True, env={'CERC_REPO_BASE_DIR': dev_root_path}) + if verbose: + print(f"Executing: {build_command}") + build_result = subprocess.run(build_command, shell=True, env={'CERC_REPO_BASE_DIR': dev_root_path}) # TODO: check result in build_result.returncode print(f"Result is: {build_result}") else: diff --git a/app/data/container-image-list.txt b/app/data/container-image-list.txt index 47c488dc..2f7675a8 100644 --- a/app/data/container-image-list.txt +++ b/app/data/container-image-list.txt @@ -15,3 +15,4 @@ cerc/fixturenet-eth-lighthouse cerc/watcher-mobymask cerc/test-container cerc/eth-probe +cerc/builder-js diff --git a/container-build/cerc-builder-js/Dockerfile b/container-build/cerc-builder-js/Dockerfile new file mode 100644 index 00000000..1f020839 --- /dev/null +++ b/container-build/cerc-builder-js/Dockerfile @@ -0,0 +1,36 @@ +# Originally from: https://github.com/devcontainers/images/blob/main/src/javascript-node/.devcontainer/Dockerfile +# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 18, 16, 14, 18-bullseye, 16-bullseye, 14-bullseye, 18-buster, 16-buster, 14-buster +ARG VARIANT=16-bullseye +FROM node:${VARIANT} + +ARG USERNAME=node +ARG NPM_GLOBAL=/usr/local/share/npm-global + +# Add NPM global to PATH. +ENV PATH=${NPM_GLOBAL}/bin:${PATH} + +RUN \ + # Configure global npm install location, use group to adapt to UID/GID changes + if ! cat /etc/group | grep -e "^npm:" > /dev/null 2>&1; then groupadd -r npm; fi \ + && usermod -a -G npm ${USERNAME} \ + && umask 0002 \ + && mkdir -p ${NPM_GLOBAL} \ + && touch /usr/local/etc/npmrc \ + && chown ${USERNAME}:npm ${NPM_GLOBAL} /usr/local/etc/npmrc \ + && chmod g+s ${NPM_GLOBAL} \ + && npm config -g set prefix ${NPM_GLOBAL} \ + && su ${USERNAME} -c "npm config -g set prefix ${NPM_GLOBAL}" \ + # Install eslint + && su ${USERNAME} -c "umask 0002 && npm install -g eslint" \ + && npm cache clean --force > /dev/null 2>&1 + +# [Optional] Uncomment this section to install additional OS packages. +# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ +# && apt-get -y install --no-install-recommends + +# [Optional] Uncomment if you want to install an additional version of node using nvm +# ARG EXTRA_NODE_VERSION=10 +# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}" + +# [Optional] Uncomment if you want to install more global node modules +# RUN su node -c "npm install -g " \ No newline at end of file diff --git a/container-build/default-build.sh b/container-build/default-build.sh new file mode 100755 index 00000000..b369e2c9 --- /dev/null +++ b/container-build/default-build.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# Usage: default-build.sh [] +# if is not supplied, the context is the directory where the Dockerfile lives +if [[ $# -ne 2 ]]; then + echo "Illegal number of parameters" >&2 + exit 1 +fi +image_tag=$1 +build_dir=$2 +echo "Building ${image_tag} in ${build_dir}" +docker build -t ${image_tag} ${build_dir}