From 52881dd8c4c4ff3e1d788ee14a2f68b731892f5c Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Tue, 17 Oct 2023 19:53:45 +0100 Subject: [PATCH 01/10] feat: dockerize aztec-cli --- build_manifest.yml | 6 ++ yarn-project/cli/Dockerfile | 32 +++++++++++ yarn-project/cli/aztec-cli | 108 ++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 yarn-project/cli/Dockerfile create mode 100755 yarn-project/cli/aztec-cli diff --git a/build_manifest.yml b/build_manifest.yml index 6bf170c5c7b..5bf584a3f3e 100644 --- a/build_manifest.yml +++ b/build_manifest.yml @@ -124,6 +124,12 @@ aztec-faucet: dependencies: - yarn-project +aztec-cli: + buildDir: yarn-project + projectDir: yarn-project/cli + dependencies: + - yarn-project + pxe: buildDir: yarn-project projectDir: yarn-project/pxe diff --git a/yarn-project/cli/Dockerfile b/yarn-project/cli/Dockerfile new file mode 100644 index 00000000000..4a9579e64f3 --- /dev/null +++ b/yarn-project/cli/Dockerfile @@ -0,0 +1,32 @@ +FROM 278380418400.dkr.ecr.eu-west-2.amazonaws.com/yarn-project AS yarn-project + +# Start a new image as we need arch specific. +FROM node:18-alpine as builder +ARG COMMIT_TAG="" +RUN apk add jq +COPY --from=yarn-project /usr/src/ /usr/src/ + +WORKDIR /usr/src/yarn-project/cli +RUN if [[ -n "${COMMIT_TAG}" ]]; then \ + jq --arg v ${COMMIT_TAG} '.version = $v' package.json > _temp && mv _temp package.json; \ + fi + +# Productionify. See comment in yarn-project-base/Dockerfile. +RUN yarn cache clean && yarn workspaces focus --production + +# Create final, minimal size image. +FROM node:18-alpine + +COPY --from=builder /usr/src/ /usr/src/ + +WORKDIR /usr/src/yarn-project/cli + +ENV XDG_CACHE_HOME /cache +RUN mkdir /cache && chmod 777 /cache +VOLUME [ "/cache" ] + +# run as non-root user +RUN addgroup -S aztec && adduser -S aztec -G aztec +USER aztec + +ENTRYPOINT ["yarn", "start"] diff --git a/yarn-project/cli/aztec-cli b/yarn-project/cli/aztec-cli new file mode 100755 index 00000000000..2b087c1c53b --- /dev/null +++ b/yarn-project/cli/aztec-cli @@ -0,0 +1,108 @@ +#!/usr/bin/env bash + +set -euo pipefail + +SCRIPT="$0" +SCRIPT_LOCATION="$(realpath $(dirname $SCRIPT))" + +# we need to look at which command was run in order to set up mount points +COMMAND="$1" + +PXE_URL=${PXE_URL:-"http://host.docker.internal:8080"} + +CLI_IMAGE="278380418400.dkr.ecr.eu-west-2.amazonaws.com/aztec-cli" +CLI_VERSION=${CLI_VERSION:-"latest"} + +DOCKER_CMD="" + +if command -v podman &> /dev/null; then + DOCKER_CMD="podman" +elif command -v docker &> /dev/null; then + DOCKER_CMD="docker" +else + echo "No docker or podman found" + exit 1 +fi + +DOCKER_RUN_ARGS="--rm --user $(id -u):$(id -g)" + +UNAME=$(uname -s) + +# set up host.docker.internal alias on linux, just like it is on mac +if [[ "$UNAME" == "Linux" ]]; then + DOCKER_RUN_ARGS="$DOCKER_RUN_ARGS --add-host=host.docker.internal:host-gateway" +fi + +# Build a list of mount points +MOUNTS="" +function add_mount() { + DIR="$1" + + echo "mounting $DIR" + + # grab its dirname if its a file + # e.g. passing the file path to a JSON artifact + if [[ -f "$DIR" ]]; then + DIR=$(dirname "$DIR") + fi + + if [[ ! -d "$DIR" ]]; then + return + fi + + REALDIR=$(realpath $DIR) + if [[ "$MOUNTS" =~ "$REALDIR:" ]]; then + return + fi + + MOUNTS="$MOUNTS -v $REALDIR:$REALDIR" +} + +if [[ "$COMMAND" == "compile" || "$COMMAND" == "deploy" || "$COMMAND" == "inspect-contract" ]]; then + CONTRACT=$2 + add_mount "$CONTRACT" +fi + +if [[ "$COMMAND" == "compile" || "$COMMAND" == "call" || "$COMMAND" == "send" ]]; then + for (( i=2; i <= "$#"; i++ )); do + arg_value=${!i} + next_index=$((i+1)) + if [[ "$next_index" -gt "$#" ]]; then + continue + fi + next_arg=${!next_index} + case $arg_value in + -o | --outdir) + add_mount "$next_arg" + ;; + -ts | --typescript) + add_mount "$next_arg" + ;; + -i | --interface) + add_mount "$next_arg" + ;; + -c | --contract-artifact) + add_mount "$next_arg" + ;; + *) + ;; + esac + done +fi + +ENV="-e PXE_URL=$PXE_URL" + +if [[ ! -z "${PRIVATE_KEY:-}" ]]; then + ENV="$ENV -e PRIVATE_KEY=$PRIVATE_KEY" +fi + +echo "-----" + +echo $DOCKER_CMD run $DOCKER_RUN_ARGS \ + $ENV \ + $MOUNTS \ + $CLI_IMAGE:$CLI_VERSION $@ + +echo "-----" + +$DOCKER_CMD run $DOCKER_RUN_ARGS $ENV $MOUNTS $CLI_IMAGE:$CLI_VERSION $@ From ff863b32b7532846d10d56643684bc4026494185 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Wed, 25 Oct 2023 16:24:02 +0100 Subject: [PATCH 02/10] refactor: clean up aztec-cli.sh --- yarn-project/cli/aztec-cli | 106 ++++++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 37 deletions(-) diff --git a/yarn-project/cli/aztec-cli b/yarn-project/cli/aztec-cli index 2b087c1c53b..7047271434e 100755 --- a/yarn-project/cli/aztec-cli +++ b/yarn-project/cli/aztec-cli @@ -2,44 +2,46 @@ set -euo pipefail -SCRIPT="$0" -SCRIPT_LOCATION="$(realpath $(dirname $SCRIPT))" +CLI_IMAGE="278380418400.dkr.ecr.eu-west-2.amazonaws.com/aztec-cli" +CLI_VERSION=${CLI_VERSION:-"latest"} -# we need to look at which command was run in order to set up mount points -COMMAND="$1" +DOCKER_PATH="" -PXE_URL=${PXE_URL:-"http://host.docker.internal:8080"} +# any host bindings we might send to the container +DOCKER_HOST="" -CLI_IMAGE="278380418400.dkr.ecr.eu-west-2.amazonaws.com/aztec-cli" -CLI_VERSION=${CLI_VERSION:-"latest"} +# env vars to pass to the container +DOCKER_ENV="" + +# volumes to pass to the container +DOCKER_VOLUME="" -DOCKER_CMD="" +# assume the sandbox is running on listening on the host interface +# unless something else was passed through +DEFAULT_PXE_URL="http://host.docker.internal:8080" +PXE_URL=${PXE_URL:-$DEFAULT_PXE_URL} + +# need to know if we're running on macOS or Linux +UNAME=$(uname -s) if command -v podman &> /dev/null; then - DOCKER_CMD="podman" + DOCKER_PATH="podman" elif command -v docker &> /dev/null; then - DOCKER_CMD="docker" + DOCKER_PATH="docker" else echo "No docker or podman found" exit 1 fi -DOCKER_RUN_ARGS="--rm --user $(id -u):$(id -g)" - -UNAME=$(uname -s) - -# set up host.docker.internal alias on linux, just like it is on mac -if [[ "$UNAME" == "Linux" ]]; then - DOCKER_RUN_ARGS="$DOCKER_RUN_ARGS --add-host=host.docker.internal:host-gateway" +# set up host.docker.internal alias on Linux, just like it is on mac +if [[ "$UNAME" == "Linux" && "$PXE_URL" == "$DEFAULT_PXE_URL" ]]; then + DOCKER_HOST="$DOCKER_HOST --add-host host.docker.internal:host-gateway" fi # Build a list of mount points -MOUNTS="" function add_mount() { DIR="$1" - echo "mounting $DIR" - # grab its dirname if its a file # e.g. passing the file path to a JSON artifact if [[ -f "$DIR" ]]; then @@ -50,20 +52,43 @@ function add_mount() { return fi + # check if it's already been added REALDIR=$(realpath $DIR) - if [[ "$MOUNTS" =~ "$REALDIR:" ]]; then + if [[ "$DOCKER_VOLUME" =~ "$REALDIR:" ]]; then return fi - MOUNTS="$MOUNTS -v $REALDIR:$REALDIR" + DOCKER_VOLUME="$DOCKER_VOLUME -v $REALDIR:$REALDIR" } -if [[ "$COMMAND" == "compile" || "$COMMAND" == "deploy" || "$COMMAND" == "inspect-contract" ]]; then - CONTRACT=$2 - add_mount "$CONTRACT" +# we need to look at which command was run in order to set up mount points +AZTEC_CLI_COMMAND="$1" + +# first process commands with positional arguments +# assumes positional arguments are the first thing +if [[ "$AZTEC_CLI_COMMAND" == "compile" || "$AZTEC_CLI_COMMAND" == "deploy" || "$AZTEC_CLI_COMMAND" == "inspect-contract" ]]; then + add_mount "$2" +fi + +# aztec-cli unbox Token my_token_contract +# if my_token_contract is missing then pass current folder +if [[ "$AZTEC_CLI_COMMAND" == "unbox" ]]; then + DIR="${3:-$PWD}" + + if [[ ! -d "$DIR" ]]; then + mkdir -p "$DIR" + fi + + add_mount "$DIR" fi -if [[ "$COMMAND" == "compile" || "$COMMAND" == "call" || "$COMMAND" == "send" ]]; then +# process flags +if [[ "$AZTEC_CLI_COMMAND" == "compile" || "$AZTEC_CLI_COMMAND" == "call" || "$AZTEC_CLI_COMMAND" == "send" ]]; then + + # bash's builtin getops only works with single characeter flags + # GNU getopt doesn't exist on macOS + # process the flags manually + # NOTE: this won't work with assignement-style flags, e.g. --outdir=/foo for (( i=2; i <= "$#"; i++ )); do arg_value=${!i} next_index=$((i+1)) @@ -90,19 +115,26 @@ if [[ "$COMMAND" == "compile" || "$COMMAND" == "call" || "$COMMAND" == "send" ]] done fi -ENV="-e PXE_URL=$PXE_URL" +DOCKER_ENV="$DOCKER_ENV -e PXE_URL=$PXE_URL" +# pass along any private keys if [[ ! -z "${PRIVATE_KEY:-}" ]]; then - ENV="$ENV -e PRIVATE_KEY=$PRIVATE_KEY" + DOCKER_ENV="$DOCKER_ENV -e PRIVATE_KEY=$PRIVATE_KEY" fi -echo "-----" - -echo $DOCKER_CMD run $DOCKER_RUN_ARGS \ - $ENV \ - $MOUNTS \ - $CLI_IMAGE:$CLI_VERSION $@ - -echo "-----" +DOCKER_CMD="$DOCKER_PATH run \ + --rm \ + --user $(id -u):$(id -g) \ + --workdir \"$PWD\" \ + $DOCKER_HOST \ + $DOCKER_ENV \ + $DOCKER_VOLUME \ + $CLI_IMAGE:$CLI_VERSION $@" + +if [ ! -z "${DEBUG:-}" ]; then + echo "----" + echo $DOCKER_CMD + echo "----" +fi -$DOCKER_CMD run $DOCKER_RUN_ARGS $ENV $MOUNTS $CLI_IMAGE:$CLI_VERSION $@ +eval "$DOCKER_CMD" From 90fc89eed3e19c2f3963e8729a06edab4ae3b2ff Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Wed, 25 Oct 2023 21:53:53 +0100 Subject: [PATCH 03/10] feat: fallback to node aztec-cli --- yarn-project/cli/aztec-cli | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/yarn-project/cli/aztec-cli b/yarn-project/cli/aztec-cli index 7047271434e..0fcb6bba1be 100755 --- a/yarn-project/cli/aztec-cli +++ b/yarn-project/cli/aztec-cli @@ -2,6 +2,33 @@ set -euo pipefail +# use node.js aztec-cli if available +# check local node_modules (e.g. @aztec/cli is a dependency in current package.json) +# this doesn't check node_modules hierarchy +if [[ -f "$PWD/node_modules/.bin/aztec-cli" ]]; then + "$PWD/node_modules/.bin/aztec-cli" $@ + exit 0 +elif command -v npm &> /dev/null; then + # next look for a global install + # e.g. if user has run `npm install -g @aztec/cli` after playing with the cli in dockerized form + GLOBAL_NPM_BIN_DIR=$(npm --global bin) + if [[ -f "$GLOBAL_NPM_BIN_DIR/aztec-cli" ]]; then + "$GLOBAL_NPM_BIN_DIR/aztec-cli" $@ + exit 0 + fi +elif command -v yarn &> /dev/null; then + YARN_VER=$(yarn --version) + if [[ "$YARN_VER" =~ ^1\..* ]]; then + GLOBAL_YARN_BIN_DIR=$(yarn --global bin) + if [[ -f "$GLOBAL_YARN_BIN_DIR/aztec-cli" ]]; then + "$GLOBAL_YARN_BIN_DIR/aztec-cli" $@ + exit 0 + fi + fi +fi + +# fallback on docker + CLI_IMAGE="278380418400.dkr.ecr.eu-west-2.amazonaws.com/aztec-cli" CLI_VERSION=${CLI_VERSION:-"latest"} From 27bce8e7fbaa5437853ed48e740d0b2034799510 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Wed, 25 Oct 2023 21:54:15 +0100 Subject: [PATCH 04/10] feat: docker entrypoint works with any workdir --- yarn-project/cli/Dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn-project/cli/Dockerfile b/yarn-project/cli/Dockerfile index 4a9579e64f3..1d8608c7861 100644 --- a/yarn-project/cli/Dockerfile +++ b/yarn-project/cli/Dockerfile @@ -19,8 +19,6 @@ FROM node:18-alpine COPY --from=builder /usr/src/ /usr/src/ -WORKDIR /usr/src/yarn-project/cli - ENV XDG_CACHE_HOME /cache RUN mkdir /cache && chmod 777 /cache VOLUME [ "/cache" ] @@ -29,4 +27,6 @@ VOLUME [ "/cache" ] RUN addgroup -S aztec && adduser -S aztec -G aztec USER aztec -ENTRYPOINT ["yarn", "start"] +ENV NODE_OPTIONS "--no-warnings" + +ENTRYPOINT ["node", "/usr/src/yarn-project/cli/dest/bin/index.js"] From e3dcbea22ca59bb6d722f14cb94cb47f1e1df2b9 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Wed, 25 Oct 2023 21:57:32 +0100 Subject: [PATCH 05/10] fix: set up cache volume --- yarn-project/cli/aztec-cli | 2 ++ 1 file changed, 2 insertions(+) diff --git a/yarn-project/cli/aztec-cli b/yarn-project/cli/aztec-cli index 0fcb6bba1be..60cb585e6ea 100755 --- a/yarn-project/cli/aztec-cli +++ b/yarn-project/cli/aztec-cli @@ -149,6 +149,8 @@ if [[ ! -z "${PRIVATE_KEY:-}" ]]; then DOCKER_ENV="$DOCKER_ENV -e PRIVATE_KEY=$PRIVATE_KEY" fi +DOCKER_VOLUME="$DOCKER_VOLUME -v cache:/cache" + DOCKER_CMD="$DOCKER_PATH run \ --rm \ --user $(id -u):$(id -g) \ From 6aaceb3c3e769f7856d967cc8b7a673ae656ed92 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Thu, 26 Oct 2023 16:38:00 +0100 Subject: [PATCH 06/10] feat: use --compiler wasm --- yarn-project/cli/aztec-cli | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/yarn-project/cli/aztec-cli b/yarn-project/cli/aztec-cli index 60cb585e6ea..64e4d001fbc 100755 --- a/yarn-project/cli/aztec-cli +++ b/yarn-project/cli/aztec-cli @@ -90,6 +90,7 @@ function add_mount() { # we need to look at which command was run in order to set up mount points AZTEC_CLI_COMMAND="$1" +AZTEC_CLI_EXTRA_ARGS="" # first process commands with positional arguments # assumes positional arguments are the first thing @@ -142,6 +143,10 @@ if [[ "$AZTEC_CLI_COMMAND" == "compile" || "$AZTEC_CLI_COMMAND" == "call" || "$A done fi +if [[ "$AZTEC_CLI_COMMAND" == "compile" ]]; then + AZTEC_CLI_EXTRA_ARGS="$AZTEC_CLI_EXTRA_ARGS --compiler wasm" +fi + DOCKER_ENV="$DOCKER_ENV -e PXE_URL=$PXE_URL" # pass along any private keys @@ -158,7 +163,7 @@ DOCKER_CMD="$DOCKER_PATH run \ $DOCKER_HOST \ $DOCKER_ENV \ $DOCKER_VOLUME \ - $CLI_IMAGE:$CLI_VERSION $@" + $CLI_IMAGE:$CLI_VERSION $@ $AZTEC_CLI_EXTRA_ARGS" if [ ! -z "${DEBUG:-}" ]; then echo "----" From 24c2a27f7ecf1b30cb6316887c6ad292bc405064 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Mon, 30 Oct 2023 09:04:37 +0000 Subject: [PATCH 07/10] fix: check npm prefix folder --- yarn-project/cli/aztec-cli | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/cli/aztec-cli b/yarn-project/cli/aztec-cli index 64e4d001fbc..0779761553e 100755 --- a/yarn-project/cli/aztec-cli +++ b/yarn-project/cli/aztec-cli @@ -11,7 +11,7 @@ if [[ -f "$PWD/node_modules/.bin/aztec-cli" ]]; then elif command -v npm &> /dev/null; then # next look for a global install # e.g. if user has run `npm install -g @aztec/cli` after playing with the cli in dockerized form - GLOBAL_NPM_BIN_DIR=$(npm --global bin) + GLOBAL_NPM_BIN_DIR=$(npm --global prefix)/bin if [[ -f "$GLOBAL_NPM_BIN_DIR/aztec-cli" ]]; then "$GLOBAL_NPM_BIN_DIR/aztec-cli" $@ exit 0 From 2766b38e41b0fdf196e349dab3ff5075e281c31a Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Mon, 30 Oct 2023 09:26:20 +0000 Subject: [PATCH 08/10] fix: use public image --- yarn-project/cli/aztec-cli | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/cli/aztec-cli b/yarn-project/cli/aztec-cli index 0779761553e..4d7dc85c5a6 100755 --- a/yarn-project/cli/aztec-cli +++ b/yarn-project/cli/aztec-cli @@ -29,7 +29,7 @@ fi # fallback on docker -CLI_IMAGE="278380418400.dkr.ecr.eu-west-2.amazonaws.com/aztec-cli" +CLI_IMAGE=${CLI_IMAGE:-"aztecprotocol/aztec-cli"} CLI_VERSION=${CLI_VERSION:-"latest"} DOCKER_PATH="" From ffeb8ef34cac35c7933633fb684c64c2e7de4684 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Mon, 30 Oct 2023 09:28:27 +0000 Subject: [PATCH 09/10] chore: remove debug --- yarn-project/cli/aztec-cli | 6 ------ 1 file changed, 6 deletions(-) diff --git a/yarn-project/cli/aztec-cli b/yarn-project/cli/aztec-cli index 4d7dc85c5a6..a9282433915 100755 --- a/yarn-project/cli/aztec-cli +++ b/yarn-project/cli/aztec-cli @@ -165,10 +165,4 @@ DOCKER_CMD="$DOCKER_PATH run \ $DOCKER_VOLUME \ $CLI_IMAGE:$CLI_VERSION $@ $AZTEC_CLI_EXTRA_ARGS" -if [ ! -z "${DEBUG:-}" ]; then - echo "----" - echo $DOCKER_CMD - echo "----" -fi - eval "$DOCKER_CMD" From b03c5ecba58c4252ad638a47f42b346cc869de6c Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Mon, 30 Oct 2023 10:00:00 +0000 Subject: [PATCH 10/10] docs: add comment header to aztec-cli script --- yarn-project/cli/aztec-cli | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/yarn-project/cli/aztec-cli b/yarn-project/cli/aztec-cli index a9282433915..1ca8a521c55 100755 --- a/yarn-project/cli/aztec-cli +++ b/yarn-project/cli/aztec-cli @@ -1,5 +1,10 @@ #!/usr/bin/env bash +# Wrapper script around @aztec/cli using Docker. This is intended to be used by devs that don't have +# a NodeJS environment setup locally. The script starts a Docker container passing any commands and +# arguments to the CLI running inside the container. +# If this wrapper script detecs a global install of @aztec/cli it falls back on that instead of Docker. + set -euo pipefail # use node.js aztec-cli if available @@ -120,6 +125,7 @@ if [[ "$AZTEC_CLI_COMMAND" == "compile" || "$AZTEC_CLI_COMMAND" == "call" || "$A for (( i=2; i <= "$#"; i++ )); do arg_value=${!i} next_index=$((i+1)) + # edge case: odd number of flags if [[ "$next_index" -gt "$#" ]]; then continue fi @@ -144,6 +150,7 @@ if [[ "$AZTEC_CLI_COMMAND" == "compile" || "$AZTEC_CLI_COMMAND" == "call" || "$A fi if [[ "$AZTEC_CLI_COMMAND" == "compile" ]]; then + # can't use Nargo inside the container AZTEC_CLI_EXTRA_ARGS="$AZTEC_CLI_EXTRA_ARGS --compiler wasm" fi