From 675024a989c80a6037c92f5e547768cdd58e5963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C8=98tefan=20Talpalaru?= Date: Fri, 26 Jun 2020 04:44:24 +0200 Subject: [PATCH] new Docker image for shared testnets --- docker/Dockerfile | 5 +- docker/shared_testnet/Dockerfile | 15 +++++ docker/shared_testnet/Makefile | 28 +++++++++ docker/shared_testnet/README.md | 15 +++++ docker/shared_testnet/entry_point.sh | 93 ++++++++++++++++++++++++++++ scripts/connect_to_testnet.nims | 16 +++-- 6 files changed, 165 insertions(+), 7 deletions(-) create mode 100644 docker/shared_testnet/Dockerfile create mode 100644 docker/shared_testnet/Makefile create mode 100644 docker/shared_testnet/README.md create mode 100755 docker/shared_testnet/entry_point.sh diff --git a/docker/Dockerfile b/docker/Dockerfile index f3fd496a4..d955eecda 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -3,7 +3,7 @@ FROM debian:bullseye-slim AS build SHELL ["/bin/bash", "-c"] RUN apt-get -qq update \ - && apt-get -qq -y install build-essential make wget libpcre3-dev golang-go git &>/dev/null \ + && apt-get -qq -y install build-essential libpcre3-dev git &>/dev/null \ && apt-get -qq clean \ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* @@ -11,8 +11,7 @@ RUN apt-get -qq update \ RUN cd /root \ && git clone https://github.com/status-im/nim-beacon-chain.git \ && cd nim-beacon-chain \ - && make -j$(nproc) update \ - && make deps + && make -j$(nproc) update # Please note that the commands above have the goal of caching the # compilation of Nim, but don't depend on the current git revision. diff --git a/docker/shared_testnet/Dockerfile b/docker/shared_testnet/Dockerfile new file mode 100644 index 000000000..134d0a3b1 --- /dev/null +++ b/docker/shared_testnet/Dockerfile @@ -0,0 +1,15 @@ +FROM debian:bullseye-slim + +SHELL ["/bin/bash", "-c"] + +RUN apt-get -qq update \ + && apt-get -qq -y install build-essential libpcre3-dev git &>/dev/null \ + && apt-get -qq clean \ + && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* + +ARG NETWORK +ENV NETWORK=${NETWORK} + +STOPSIGNAL SIGINT +ENTRYPOINT ["/root/nim-beacon-chain/docker/shared_testnet/entry_point.sh"] + diff --git a/docker/shared_testnet/Makefile b/docker/shared_testnet/Makefile new file mode 100644 index 000000000..f4d5bfe1c --- /dev/null +++ b/docker/shared_testnet/Makefile @@ -0,0 +1,28 @@ +SHELL := bash # the shell used internally by "make" + +# These default settings can be overriden by exporting env variables + +NETWORK ?= witti +IMAGE_TAG ?= testnet2 +IMAGE_NAME ?= statusteam/nimbus_beacon_node:$(IMAGE_TAG) + +CURRENT_BRANCH = $(shell git rev-parse --abbrev-ref HEAD) +COMPUTER_SAYS_NO = { echo "I'm sorry, Dave. I'm afraid I can't do that."; exit 1; } + +.PHONY: build push push-last + +build: + @ DOCKER_BUILDKIT=1 \ + docker build \ + --build-arg="NETWORK=$(NETWORK)" \ + -t $(IMAGE_NAME) \ + --progress=plain \ + . + +push: build + +@ $(MAKE) push-last + +push-last: + @ [[ "$(CURRENT_BRANCH)" != "devel" ]] && $(COMPUTER_SAYS_NO) || true + docker push $(IMAGE_NAME) + diff --git a/docker/shared_testnet/README.md b/docker/shared_testnet/README.md new file mode 100644 index 000000000..3373dd10e --- /dev/null +++ b/docker/shared_testnet/README.md @@ -0,0 +1,15 @@ +## local testing + +From the "nim-beacon-chain" dir: + +```text +make -C docker/shared_testnet build +mkdir tmp +docker run --rm --mount type=bind,source="$(pwd)"/tmp,target=/root/.cache/nimbus --name testnet2 statusteam/nimbus_beacon_node:testnet2 --build +ls -l tmp/nim-beacon-chain/build +docker run --rm --mount type=bind,source="$(pwd)"/tmp,target=/root/.cache/nimbus --name testnet2 -p 127.0.0.1:8008:8008 -p 9000:9000 statusteam/nimbus_beacon_node:testnet2 --run -- --metrics-address=0.0.0.0 +# from another terminal +docker ps +docker stop testnet2 +``` + diff --git a/docker/shared_testnet/entry_point.sh b/docker/shared_testnet/entry_point.sh new file mode 100755 index 000000000..9b263f93a --- /dev/null +++ b/docker/shared_testnet/entry_point.sh @@ -0,0 +1,93 @@ +#!/bin/bash + +set -e + +cd "$(dirname "${BASH_SOURCE[0]}")" + +#################### +# argument parsing # +#################### +! getopt --test > /dev/null +if [ ${PIPESTATUS[0]} != 4 ]; then + echo '`getopt --test` failed in this environment.' + exit 1 +fi + +OPTS="h" +LONGOPTS="help,build,run" + +# default values +BUILD=0 +RUN=0 + +print_help() { + cat < -- + + -h, --help this help message + --build build the beacon_node + --run run the beacon_node +EOF +} + +! PARSED=$(getopt --options=${OPTS} --longoptions=${LONGOPTS} --name "$0" -- "$@") +if [ ${PIPESTATUS[0]} != 0 ]; then + # getopt has complained about wrong arguments to stdout + exit 1 +fi + +# read getopt's output this way to handle the quoting right +eval set -- "$PARSED" +while true; do + case "$1" in + -h|--help) + print_help + exit + ;; + --build) + BUILD=1 + shift + ;; + --run) + RUN=1 + shift + ;; + --) + shift + break + ;; + *) + echo "argument parsing error" + print_help + exit 1 + esac +done + +EXTRA_ARGS="$@" + +######### +# build # +######### + +if [[ "$BUILD" == "1" ]]; then + # "/root/.cache/nimbus" is the external bind-mounted dir, preserved between runs + cd /root/.cache/nimbus + [[ -d nim-beacon-chain ]] || git clone https://github.com/status-im/nim-beacon-chain.git + cd nim-beacon-chain + git config pull.rebase false + git checkout devel + git pull + make -j$(nproc) update + make LOG_LEVEL="TRACE" NIMFLAGS="-d:insecure -d:testnet_servers_image" SCRIPT_PARAMS="--skipGoerliKey --writeLogFile=false --buildOnly" ${NETWORK} +fi + +####### +# run # +####### + +if [[ "$RUN" == "1" ]]; then + cd /root/.cache/nimbus/nim-beacon-chain + # make sure Docker's SIGINT reaches the beacon_node binary + eval $(make SCRIPT_PARAMS="--skipGoerliKey --writeLogFile=false --runOnly --printCmdOnly" ${NETWORK} | tail -n 1) ${EXTRA_ARGS} +fi + diff --git a/scripts/connect_to_testnet.nims b/scripts/connect_to_testnet.nims index a28a6e8e7..e4b30deec 100644 --- a/scripts/connect_to_testnet.nims +++ b/scripts/connect_to_testnet.nims @@ -68,15 +68,14 @@ proc becomeValidator(validatorsDir, beaconNodeBinary, secretsDir, depositContrac discard readLineFromStdin() proc runNode(dataDir, beaconNodeBinary, bootstrapFileOpt, depositContractOpt, genesisFileOpt: string, - basePort, nodeID, baseMetricsPort, baseRpcPort: int) = + basePort, nodeID, baseMetricsPort, baseRpcPort: int, printCmdOnly: bool) = let logLevel = getEnv("LOG_LEVEL") var logLevelOpt = "" if logLevel.len > 0: logLevelOpt = &"""--log-level="{logLevel}" """ mode = Verbose - cd dataDir - execIgnoringExitCode replace(&"""{beaconNodeBinary} + let cmd = replace(&"""{beaconNodeBinary} --data-dir="{dataDir}" --dump --web3-url={web3Url} @@ -91,6 +90,12 @@ proc runNode(dataDir, beaconNodeBinary, bootstrapFileOpt, depositContractOpt, ge {depositContractOpt} {genesisFileOpt} """, "\n", " ") + if printCmdOnly: + echo &"cd {dataDir}; exec {cmd}" + else: + cd dataDir + execIgnoringExitCode cmd + cli do (skipGoerliKey {. desc: "Don't prompt for an Eth1 Goerli key to become a validator" .}: bool, @@ -123,6 +128,9 @@ cli do (skipGoerliKey {. runOnly {. desc: "Just run it." .} = false, + printCmdOnly {. + desc: "Just print the commands (suitable for passing to 'eval'; might replace current shell)." .} = false, + testnetName {.argument .}: string): let nameParts = testnetName.split "/" @@ -222,5 +230,5 @@ cli do (skipGoerliKey {. if not buildOnly: runNode(dataDir, beaconNodeBinary, bootstrapFileOpt, depositContractOpt, genesisFileOpt, - basePort, nodeID, baseMetricsPort, baseRpcPort) + basePort, nodeID, baseMetricsPort, baseRpcPort, printCmdOnly)