#!/usr/bin/env bash SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) CONTAINER_NAME="piston_api" DEV_VOLUME_NAME="piston_nix" IMAGE_TAG="base-latest" IMAGE_NAME="ghcr.io/engineer-man/piston" IMAGE_NAME_DEV="piston" SUBCOMMAND="$1" shift build_base() { container_flake_key=$([[ $1 == "dev" ]] && echo "containers.dev" || echo "containers.prod") CONTAINER_PATH="$(nix build ".#$container_flake_key" --no-link --json | jq '.[0].outputs.out' -r)" echo "The image archive was created at: $CONTAINER_PATH" docker load -i $CONTAINER_PATH || exit 1 } case "$SUBCOMMAND" in logs) docker logs -f $CONTAINER_NAME;; restart) docker restart $CONTAINER_NAME ;; start) docker run \ -p 2000:2000 \ --rm \ --name $CONTAINER_NAME \ -d "$IMAGE_NAME:$IMAGE_TAG" ;; stop) docker stop $CONTAINER_NAME ;; bash|shell) docker exec -it $CONTAINER_NAME bash ;; update) git pull docker pull "$IMAGE_NAME:$IMAGE_TAG" ;; build) build_base if [[ ! -z "$1" ]]; then # $1 contains a variant to build runtime_set=$1 docker build \ --build-arg RUNTIMESET=$runtime_set \ -f "$SCRIPT_DIR"/Dockerfile.withset \ -t "$IMAGE_NAME_DEV:$runtime_set-latest" \ . fi ;; # dev commands scaffold) pushd "$SCRIPT_DIR/runtimes" > /dev/null ./scaffold.sh $1 $2 popd > /dev/null ;; build-dev) echo "Removing the Nix volume if it exists" docker volume rm -f $DEV_VOLUME_NAME || exit 1 echo "Building the base docker image" build_base dev echo "Installing the required node modules" docker run \ --rm \ -p 2000:2000 \ -it \ --name $CONTAINER_NAME \ -e PISTON_LOG_LEVEL=DEBUG \ -e PISTON_FLAKE_PATH=/piston/src \ -e PISTON_RUNTIME_SET=none \ -v "$SCRIPT_DIR":/piston/src \ "$IMAGE_NAME_DEV:$IMAGE_TAG" \ bash -c "cd /piston/src/api && yarn install" echo "Done building" ;; start-dev) runtime_set=all if [[ ! -z "$1" ]]; then runtime_set=$1 fi docker run \ --rm \ -p 2000:2000 \ -it \ --name $CONTAINER_NAME \ -e PISTON_LOG_LEVEL=DEBUG \ -e PISTON_FLAKE_PATH=/piston/src \ -e PISTON_RUNTIME_SET=$runtime_set \ -v "$SCRIPT_DIR":/piston/src \ -v $DEV_VOLUME_NAME:/nix \ -d "$IMAGE_NAME_DEV:$IMAGE_TAG" \ bash -c "cd /piston/src/api && yarn run dev" ;; test) docker run \ --rm \ -it \ -e PISTON_FLAKE_PATH=/piston/src \ -v "$SCRIPT_DIR":/piston/src \ --name piston_test_runner \ "$IMAGE_NAME:$IMAGE_TAG" \ piston-test $1 ;; test-dev) docker run \ --rm \ -it \ -e PISTON_FLAKE_PATH=/piston/src \ -v "$SCRIPT_DIR":/piston/src \ -v $DEV_VOLUME_NAME:/nix \ --name piston_test_runner \ "$IMAGE_NAME_DEV:$IMAGE_TAG" \ /piston/src/api/src/bin/test.js $1 ;; *) echo "=== Piston Management ===" echo echo "======================" echo "General Commands" echo "======================" echo " logs Show Piston logs" echo " start Start Piston production container" echo " stop Stop Piston" echo " restart Restart Piston" echo " shell Open a bash shell for the container" echo " update Fetch latest updates" echo " exec Execute the files on Piston with language" echo echo "======================" echo "Development Commands" echo "Running some of these commands require a nix environment setup and on the path" echo "See https://nixos.wiki/wiki/Nix_Installation_Guide#Stable_Nix" echo "======================" echo " build-dev Build the development image" echo " build [runtime-set] Build the production image, and optionally" echo " include a specified runtime set" echo " start-dev Start Piston development container" echo " scaffold [runtime] Initialize a template for a new runtime" echo " test Run unit tests on the given runtime" echo " Optionally set runtime to --all to test all" echo " NOTE: This is only for the runtimes contained" echo " within this repo" echo " test-dev test using the development container and volume" echo ;; esac