diff --git a/Earthfile b/Earthfile new file mode 100644 index 0000000000..54111591aa --- /dev/null +++ b/Earthfile @@ -0,0 +1,93 @@ +# Set the Earthly version to 0.7 +VERSION 0.7 + +rust-toolchain: + FROM rust:1.71-slim-bullseye + RUN rustup component add rustfmt + +# Installs Cargo chef +install-chef: + FROM +rust-toolchain + RUN cargo install --debug cargo-chef + +# Prepares the local cache +prepare-cache: + FROM +install-chef + COPY --dir jormungandr jcli jormungandr-lib explorer modules testing . + COPY Cargo.lock Cargo.toml . + RUN cargo chef prepare + SAVE ARTIFACT recipe.json + SAVE IMAGE --cache-hint + +# Builds the local cache +build-cache: + FROM +install-chef + COPY +prepare-cache/recipe.json ./ + + # Install build dependencies + RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + build-essential \ + libssl-dev \ + libpq-dev \ + libsqlite3-dev \ + pkg-config \ + protobuf-compiler + + RUN cargo chef cook --release + SAVE ARTIFACT target + SAVE ARTIFACT $CARGO_HOME cargo_home + SAVE IMAGE --cache-hint + +# This is the default builder that all other builders should inherit from +builder: + FROM +rust-toolchain + + WORKDIR /src + + # Install build dependencies + RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + build-essential \ + libssl-dev \ + libpq-dev \ + libsqlite3-dev \ + pkg-config \ + protobuf-compiler + COPY --dir jormungandr jcli . + COPY Cargo.lock Cargo.toml . + COPY +build-cache/cargo_home $CARGO_HOME + COPY +build-cache/target target + SAVE ARTIFACT /src + +build: + FROM +builder + + COPY --dir jormungandr jcli jormungandr-lib explorer modules testing . + COPY Cargo.lock Cargo.toml . + + RUN cargo build --locked --release -p jormungandr -p jcli + + SAVE ARTIFACT /src/target/release/jormungandr jormungandr + SAVE ARTIFACT /src/target/release/jcli jcli + +publish: + FROM debian:stable-slim + WORKDIR /app + + ARG tag=latest + + # Install build dependencies + RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + libssl-dev \ + libpq-dev \ + libsqlite3-dev + + COPY +build/jormungandr . + COPY jormungandr/entrypoint.sh . + RUN chmod +x entrypoint.sh + + ENTRYPOINT ["/app/entrypoint.sh"] + + SAVE IMAGE jormungandr:${tag} \ No newline at end of file diff --git a/jormungandr/entrypoint.sh b/jormungandr/entrypoint.sh new file mode 100644 index 0000000000..4322fab1f2 --- /dev/null +++ b/jormungandr/entrypoint.sh @@ -0,0 +1,91 @@ +#!/usr/bin/bash + +echo ">>> Entering entrypoint script..." + +# Verify the storage path exists +if [[ ! -d "$STORAGE_PATH" ]]; then + echo "ERROR: storage path does not exist at: $STORAGE_PATH" + echo ">>> Aborting..." + exit 1 +fi + +# Verify config is present +if [[ ! -f "$NODE_CONFIG_PATH" ]]; then + echo "ERROR: node configuration is absent at: $NODE_CONFIG_PATH" + echo ">>> Aborting..." + exit 1 +fi + +# Verify genesis block is present +if [[ ! -f "$GENESIS_PATH" ]]; then + echo "ERROR: genesis block is absent at: $GENESIS_PATH" + echo ">>> Aborting..." + exit 1 +fi + +# Allow overriding jormungandr binary +BIN_PATH=''${BIN_PATH:=/app/jormungandr} + +echo ">>> Using the following parameters:" +echo "Storage path: $STORAGE_PATH" +echo "Node config: $NODE_CONFIG_PATH" +echo "Genesis block: $GENESIS_PATH" +echo "Binary path: $BIN_PATH" + +args+=() +args+=("--storage" "$STORAGE_PATH") +args+=("--config" "$NODE_CONFIG_PATH") +args+=("--genesis-block" "$GENESIS_PATH") + +if [[ -n "${LEADER:=}" ]]; then + echo ">>> Configuring node as leader..." + + # shellcheck disable=SC2153 + if [[ ! -f "$BFT_PATH" ]]; then + echo "ERROR: BFT is absent at: $BFT_PATH" + echo ">>> Aborting..." + exit 1 + fi + + echo ">>> Using BFT at: $BFT_PATH" + args+=("--secret" "$BFT_PATH") +fi + +# Nodes will fail to start if they cannot resolve the domain names of +# their respective peers. If domains are used for peers, it's necessary +# to wait for them to resolve first before starting the node. +if [[ -n "${DNS_PEERS:=}" ]]; then + for PEER in $DNS_PEERS; do + while ! nslookup "$PEER"; do + echo ">>> Waiting for $PEER to be resolvable..." + sleep 1 + done + echo "Successfully resolved $PEER" + done +fi + +# Allows resetting our footprint in persistent storage +if [[ -f "$STORAGE_PATH/reset" ]]; then + echo ">>> Reset file detected at $STORAGE_PATH/reset" + rm -rf "$STORAGE_PATH/reset" + + if [[ -d "$STORAGE_PATH/fragments" ]]; then + echo ">>> Deleting $STORAGE_PATH/fragments" + rm -rf "$STORAGE_PATH/fragments" + fi + + if [[ -d "$STORAGE_PATH/permanent" ]]; then + echo ">>> Deleting $STORAGE_PATH/permanent" + rm -rf "$STORAGE_PATH/permanent" + fi + + if [[ -d "$STORAGE_PATH/volatile" ]]; then + echo ">>> Deleting $STORAGE_PATH/volatile" + rm -rf "$STORAGE_PATH/volatile" + fi + + echo ">>> Reset complete" +fi + +echo "Starting node..." +exec "$BIN_PATH" "${args[@]}" \ No newline at end of file