From e70ae4e7cee36ab36f77693ca3a1225e454dd843 Mon Sep 17 00:00:00 2001 From: Oren Laadan Date: Tue, 17 Jan 2023 21:45:55 +0800 Subject: [PATCH] CNS-140: dockerize lavad setup Update the Dockerfile to generate docker image that can run lava node. (Implemented using the artifacts in docker/ directory.) To start a lava node in a container, run the command: docker-compose --profile node -f docker/docker-compose.yml up It will execute the script `start_node.sh` (from docker/), which follows the instructions for starting a node (with cosmovisor). --- Dockerfile | 35 +++++++++++++- docker/docker-compose.yml | 27 +++++++++++ docker/entrypoint.sh | 47 +++++++++++++++++++ docker/start_node.sh | 99 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 docker/docker-compose.yml create mode 100755 docker/entrypoint.sh create mode 100755 docker/start_node.sh diff --git a/Dockerfile b/Dockerfile index 8b1c418511..d77bc7b5cc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -98,11 +98,42 @@ RUN --mount=type=cache,target=/root/.cache/go-build \ # Download debian packages for runner -FROM ${RUNNER_IMAGE} +FROM ${RUNNER_IMAGE} as runner-base +RUN apt-get update \ + && apt-get install -yqq --no-install-recommends \ + git curl unzip ca-certificates \ + && apt-get -y purge \ + && apt-get -y clean \ + && apt-get -y autoremove \ + && rm -rf /var/lib/apt/lists/* + +# -------------------------------------------------------- +# Runner +# -------------------------------------------------------- + +FROM runner-base + +COPY --from=cosmovisor /go/bin/cosmovisor /bin/cosmovisor COPY --from=builder /lava/build/lavad /bin/lavad ENV HOME /lava WORKDIR $HOME -ENTRYPOINT ["/bin/lavad"] +COPY docker/entrypoint.sh / +COPY docker/start_node.sh start_node.sh + +# lava api +EXPOSE 1317 +# rosetta +EXPOSE 8080 +# grpc +EXPOSE 9090 +# grpc-web +EXPOSE 9090 +# tendermint p2p +EXPOSE 26656 +# tendermint rpc +EXPOSE 26657 + +ENTRYPOINT ["/entrypoint.sh"] diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 0000000000..b85e01842c --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,27 @@ +version: '3.9' + +x-env: &env-common + environment: + - LAVA_EXTERNAL_IP=${LAVA_EXTERNAL_IP:-0.0.0.0} + +services: + + lava-node: + build: .. + image: lava:latest + command: "node" + <<: *env-common + ports: + - '${LAVA_EXTERNAL_IP:-0.0.0.0}:1317:1317' + - '${LAVA_EXTERNAL_IP:-0.0.0.0}:9090:9090' + - '${LAVA_EXTERNAL_IP:-0.0.0.0}:9091:9091' + - '${LAVA_EXTERNAL_IP:-0.0.0.0}:26656:26656' + - '${LAVA_EXTERNAL_IP:-0.0.0.0}:26657:26657' + deploy: + resources: + limits: + cpus: '4' + memory: '8gb' + restart: unless-stopped + profiles: ["node"] + diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100755 index 0000000000..82963dbc82 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,47 @@ +#!/bin/sh +# vim:sw=4:ts=4:et + +set -e + +if [ -z "${LAVA_QUIET_LOGS:-}" ]; then + exec 3>&1 +else + exec 3>/dev/null +fi + +if /usr/bin/find "/entrypoint.d/" -mindepth 1 -maxdepth 1 -type f -print -quit 2>/dev/null | read v; then + echo >&3 "$0: Looking for shell scripts in /entrypoint.d/" + find "/entrypoint.d/" -follow -type f -print | sort -n | while read -r f; do + case "$f" in + *.sh) + if [ -x "$f" ]; then + echo >&3 "$0: Launching $f"; + "$f" + else + # warn on shell scripts without exec bit + echo >&3 "$0: Ignoring $f, not executable"; + fi + ;; + *) echo >&3 "$0: Ignoring $f";; + esac + done + echo >&3 "$0: Configuration complete; ready for start up" +else + echo >&3 "$0: No files found in /entrypoint.d/, skipping configuration" +fi + +if [ $# -lt 1 ]; then + echo >&3 "$0: No command given" + exit 1 +fi + +case _"$1" in + _node) cmd="/lava/start_node.sh" ;; + *) cmd="$1" ;; +esac + +shift + +echo >&3 "$0: Launching $cmd $@" + +exec "$cmd" "$@" diff --git a/docker/start_node.sh b/docker/start_node.sh new file mode 100755 index 0000000000..9205085178 --- /dev/null +++ b/docker/start_node.sh @@ -0,0 +1,99 @@ +#!/bin/sh +# vim:sw=4:ts=4:et + +set -e + +SETUP_CONFIG_GIT_URL='https://github.com/K433QLtr6RA9ExEq/GHFkqmTzpdNLDd6T.git' +COSMOVISOR_ZIP_URL='https://lava-binary-upgrades.s3.amazonaws.com/testnet/cosmovisor-upgrades/cosmovisor-upgrades.zip' + +debug() { + echo "DBG: $@" +} + +error() { + echo "ERR: $@" + exit 1 +} + +setup_node() { + setup_config_dir=$(basename ${SETUP_CONFIG_GIT_URL}) + + # remove old data (if any) + rm -rf ${setup_config_dir} + + # download setup configuration + git clone --depth 1 ${SETUP_CONFIG_GIT_URL} ${setup_config_dir} || \ + error "setup: failed to clone setup configuration" + + cd ${setup_config_dir}/testnet-1 + . setup_config/setup_config.sh + + # keep a copy handy for when we restart + cp setup_config/setup_config.sh ${HOME} + + # remove old config (if any) + rm -rf ${lavad_home_folder} + + # copy initial configuration and genesis data + mkdir -p ${lavad_home_folder} + mkdir -p ${lava_config_folder} + cp default_lavad_config_files/* ${lava_config_folder} + cp genesis_json/genesis.json ${lava_config_folder}/genesis.json +} + +setup_env() { + # environment variables for cosmovisor + export DAEMON_NAME=lavad + export CHAIN_ID=lava-testnet-1 + export DAEMON_HOME=$HOME/.lava + export DAEMON_ALLOW_DOWNLOAD_BINARIES=true + export DAEMON_LOG_BUFFER_SIZE=512 + export DAEMON_RESTART_AFTER_UPGRADE=true + export UNSAFE_SKIP_BACKUP=true +} + +# note: expected to run in the setup config directory - see setup_node()) +setup_cosmovisor() { + # download latest cosmovisor-upgrades + curl -L --progress-bar -o cosmovisor-upgrades.zip "${COSMOVISOR_ZIP_URL}" || \ + error "setup: failed to download cosmovisor upgrades" + unzip cosmovisor-upgrades.zip || \ + error "setup: failed to unzip cosmovisor upgrades" + + # copy cosmovisor configuration + mkdir -p ${lavad_home_folder}/cosmovisor + cp -r cosmovisor-upgrades/* ${lavad_home_folder}/cosmovisor + + # initialize the chain + output=$( \ + ${lavad_home_folder}/cosmovisor/genesis/bin/lavad init \ + my-node \ + --chain-id lava-testnet-1 \ + --home ${lavad_home_folder} \ + --overwrite \ + ) + + # an error message about missing upgrade-info.json is expected; + # anything else is unexpected and should abort. + if [ $? -ne 0 ]; then + case "$output" in + "*upgrade-info.json: no such file or directory*") ;; + "*") error "setup: failed to initialize the chain" ;; + esac + fi + + # copy genesis data again + cp genesis_json/genesis.json ${lava_config_folder}/genesis.json +} + +setup_env + +if [ ! -e ${HOME}/.lava/cosmovisor/current ]; then + setup_node + setup_cosmovisor +else + . ${HOME}/setup_config.sh +fi + +exec /bin/cosmovisor start --home=${lavad_home_folder} --p2p.seeds ${seed_node} +