From eeecb2fd626954d1cefd6a9dd5e108bad44a57b2 Mon Sep 17 00:00:00 2001 From: Pablo Chiavarino Date: Fri, 6 Nov 2026 17:05:36 -0300 Subject: [PATCH] Run the node from a prebuilt binary in Docker, configured from .env scripts/build-binary.sh builds rustock-cli and the maintenance tools for amd64 or arm64 through docker/build.Dockerfile, which exports only the binaries. A small runtime image runs the binary, which docker-compose.yml mounts from ./bin, with settings taken from .env and checked by docker/entrypoint.sh. The root Dockerfile is unchanged: it stays the self-contained image, and .dockerignore keeps its rules and adds the paths the build never needs. RUSTOCK_EXTERNAL_IP is optional, because on mainnet and testnet the node looks its address up when it is unset. The entrypoint requires an IPv4 address when one is given, matching the node. --- .dockerignore | 9 +- .env.example | 32 ++++++ .gitignore | 4 + README.md | 3 + docker-compose.yml | 82 ++++++++++++++++ docker/build.Dockerfile | 38 ++++++++ docker/entrypoint.sh | 76 +++++++++++++++ docker/node.toml | 20 ++++ docker/runtime.Dockerfile | 21 ++++ docs/docker.md | 199 ++++++++++++++++++++++++++++++++++++++ scripts/build-binary.sh | 100 +++++++++++++++++++ 11 files changed, 583 insertions(+), 1 deletion(-) create mode 100644 .env.example create mode 100644 docker-compose.yml create mode 100644 docker/build.Dockerfile create mode 100755 docker/entrypoint.sh create mode 100644 docker/node.toml create mode 100644 docker/runtime.Dockerfile create mode 100644 docs/docker.md create mode 100755 scripts/build-binary.sh diff --git a/.dockerignore b/.dockerignore index b480529..48f6519 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,11 @@ +.git/ target/ **/target/ -.git/ *.log +data +dist +bin +docs +tools +*.md +!README.md diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..9401f75 --- /dev/null +++ b/.env.example @@ -0,0 +1,32 @@ +# Copy to .env; unset values use docker-compose.yml defaults. Never set COMPOSE_* or DOCKER_* here. + +# 30 mainnet, 31 testnet, anything else regtest. +RUSTOCK_NETWORK_ID=30 +# Peers, tcp (rskj: 5050 mainnet, 50505 testnet). +RUSTOCK_P2P_PORT=5050 +# Discovery, udp; must be the peer port + 1. +RUSTOCK_DISCOVERY_PORT=5051 +# Public IPv4 address advertised to peers. Unset, networks 30 and 31 look it up; +# set it behind NAT or to skip the lookup. +RUSTOCK_EXTERNAL_IP= + +# A named volume, or a host path (/ or ./) owned by uid 10001. +RUSTOCK_DATA_DIR=rustock-data + +# trace, debug, info, warn, error, or per-module: info,rustock_sync=debug +RUSTOCK_LOG_LEVEL=info +# false writes rustock.log. into the data directory instead. +RUSTOCK_LOG_TO_STDOUT=true +RUSTOCK_LOG_MAX_SIZE=50m +RUSTOCK_LOG_MAX_FILE=10 + +# The RPC has no authentication; do not bind it beyond a trusted network. +RUSTOCK_RPC_BIND=127.0.0.1 +RUSTOCK_RPC_PORT=4444 +RUSTOCK_METRICS_BIND=127.0.0.1 +RUSTOCK_METRICS_PORT=9898 + +# Node binary to mount. +RUSTOCK_BINARY=./bin/rustock-cli +# Extra rustock-cli flags, e.g. "--snap-sync --rpc-admin". +RUSTOCK_EXTRA_ARGS= diff --git a/.gitignore b/.gitignore index 6485594..96ce020 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,7 @@ tmp/ *.env pegout-alerts.toml !pegout-alerts.example.toml + +# Built and installed node binaries (scripts/build-binary.sh) +/dist/ +/bin/ diff --git a/README.md b/README.md index e10051a..d2fc8e7 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,9 @@ docker run --rm rustock rustock-cli --help The image has no entrypoint; the binary is `/usr/local/bin/rustock-cli`. +To run a node under Docker Compose from a prebuilt binary, see +[docs/docker.md](docs/docker.md). + ### Running Start the full node on RSK Mainnet (default): diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..cb1b8b1 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,82 @@ +# Settings come from .env; each has a default here. +services: + rustock: + image: rustock-runtime:local + build: + context: ./docker + dockerfile: runtime.Dockerfile + # Restart after a crash, not after a clean exit such as rsk_shutdown. + restart: on-failure + + # The running node ignores SIGTERM; stop it with rsk_shutdown (docs/docker.md). + stop_grace_period: 30s + + environment: + RUSTOCK_NETWORK_ID: ${RUSTOCK_NETWORK_ID:-30} + RUSTOCK_P2P_PORT: ${RUSTOCK_P2P_PORT:-5050} + RUSTOCK_DISCOVERY_PORT: ${RUSTOCK_DISCOVERY_PORT:-5051} + RUSTOCK_EXTERNAL_IP: ${RUSTOCK_EXTERNAL_IP:-} + RUSTOCK_LOG_LEVEL: ${RUSTOCK_LOG_LEVEL:-info} + RUSTOCK_LOG_TO_STDOUT: ${RUSTOCK_LOG_TO_STDOUT:-true} + RUSTOCK_EXTRA_ARGS: ${RUSTOCK_EXTRA_ARGS:-} + + # json-file never rotates without limits. + logging: + driver: json-file + options: + max-size: ${RUSTOCK_LOG_MAX_SIZE:-50m} + max-file: "${RUSTOCK_LOG_MAX_FILE:-10}" + + # P2P and discovery ports are identical on host and container: the node + # advertises its own port to peers. + ports: + - "${RUSTOCK_P2P_PORT:-5050}:${RUSTOCK_P2P_PORT:-5050}/tcp" + - "${RUSTOCK_DISCOVERY_PORT:-5051}:${RUSTOCK_DISCOVERY_PORT:-5051}/udp" + - "${RUSTOCK_RPC_BIND:-127.0.0.1}:${RUSTOCK_RPC_PORT:-4444}:4444" + - "${RUSTOCK_METRICS_BIND:-127.0.0.1}:${RUSTOCK_METRICS_PORT:-9898}:9898" + + volumes: + # A path starting with / or ./ is a host directory; a bare name is the + # named volume declared below. + - ${RUSTOCK_DATA_DIR:-rustock-data}:/var/lib/rustock + - ./docker/node.toml:/etc/rustock/node.toml:ro + # create_host_path: false fails instead of creating a directory when the + # binary is missing. + - type: bind + source: ${RUSTOCK_BINARY:-./bin/rustock-cli} + target: /usr/local/bin/rustock-cli + read_only: true + bind: + create_host_path: false + + ulimits: + nofile: + soft: 65536 + hard: 65536 + + healthcheck: + test: ["CMD", "bash", "-c", "exec 3<>/dev/tcp/127.0.0.1/9898"] + interval: 30s + timeout: 5s + retries: 3 + start_period: 120s + + # Offline maintenance: docker compose run --rm tools /var/lib/rustock ... + tools: + image: rustock-runtime:local + profiles: [tools] + restart: "no" + entrypoint: [] + environment: + PATH: /opt/rustock-tools:/usr/local/bin:/usr/bin:/bin + volumes: + - ${RUSTOCK_DATA_DIR:-rustock-data}:/var/lib/rustock + - type: bind + source: ./bin/tools + target: /opt/rustock-tools + read_only: true + bind: + create_host_path: false + +volumes: + rustock-data: diff --git a/docker/build.Dockerfile b/docker/build.Dockerfile new file mode 100644 index 0000000..ed01571 --- /dev/null +++ b/docker/build.Dockerfile @@ -0,0 +1,38 @@ +# syntax=docker/dockerfile:1 + +# Keep in step with rust-toolchain.toml. +ARG RUST_VERSION=1.93.1 + +# The binary links this image's glibc and libstdc++; docker/runtime.Dockerfile +# must stay on the same Debian release. +FROM rust:${RUST_VERSION}-bookworm AS build + +# librocksdb-sys compiles C++, and zstd-sys runs bindgen, which needs libclang. +RUN apt-get update \ + && apt-get install -y --no-install-recommends build-essential clang libclang-dev \ + && rm -rf /var/lib/apt/lists/* + +# Stops rustup from fetching the components listed in rust-toolchain.toml. +ARG RUST_VERSION +ENV RUSTUP_TOOLCHAIN=${RUST_VERSION} + +WORKDIR /src +COPY . . + +# Comma-separated cargo features for rustock-cli, e.g. "smtp". +ARG FEATURES="" +# Maintenance tools: crates/cli/examples/.rs, shipped beside the binary. +ARG TOOLS="rewind_to set_exec_head reset_exec_head block_info height_peek" +RUN --mount=type=cache,target=/usr/local/cargo/registry \ + --mount=type=cache,target=/src/target \ + examples=""; for t in $TOOLS; do examples="$examples --example $t"; done; \ + cargo build --release --locked -p rustock-cli ${FEATURES:+--features "$FEATURES"} $examples \ + && install -D target/release/rustock-cli /out/rustock-cli \ + && for t in $TOOLS; do install -D "target/release/examples/$t" "/out/tools/$t"; done + + +FROM scratch AS binary +COPY --from=build /out/rustock-cli /rustock-cli + +FROM scratch AS tools +COPY --from=build /out/tools/ / diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100755 index 0000000..2229e97 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,76 @@ +#!/bin/bash +# Turns the RUSTOCK_* environment into rustock-cli flags, then execs the node +# so it stays PID 1. + +set -euo pipefail + +fail() { + echo "entrypoint: $*" >&2 + exit 1 +} + +is_port() { + [[ "$1" =~ ^[0-9]+$ ]] && [ "$1" -ge 1 ] && [ "$1" -le 65535 ] +} + +is_ipv4() { + [[ "$1" =~ ^(0|[1-9][0-9]{0,2})(\.(0|[1-9][0-9]{0,2})){3}$ ]] || return 1 + local octet + for octet in ${1//./ }; do + [ "$octet" -le 255 ] || return 1 + done +} + +data_dir=/var/lib/rustock +p2p_port="${RUSTOCK_P2P_PORT:-5050}" +discovery_port="${RUSTOCK_DISCOVERY_PORT:-5051}" +network_id="${RUSTOCK_NETWORK_ID:-30}" +external_ip="${RUSTOCK_EXTERNAL_IP:-}" +log_level="${RUSTOCK_LOG_LEVEL:-info}" +log_to_stdout="${RUSTOCK_LOG_TO_STDOUT:-true}" +extra_args="${RUSTOCK_EXTRA_ARGS:-}" + +is_port "$p2p_port" || fail "RUSTOCK_P2P_PORT must be a port number, got '$p2p_port'" +is_port "$discovery_port" || fail "RUSTOCK_DISCOVERY_PORT must be a port number, got '$discovery_port'" +[ -z "$external_ip" ] || is_ipv4 "$external_ip" || fail "RUSTOCK_EXTERNAL_IP must be an IPv4 address, got '$external_ip'" +[[ "$network_id" =~ ^[0-9]+$ ]] || fail "RUSTOCK_NETWORK_ID must be a number, got '$network_id'" +[ -n "$log_level" ] || fail "RUSTOCK_LOG_LEVEL must not be empty" +case "$log_to_stdout" in + true|false) ;; + *) fail "RUSTOCK_LOG_TO_STDOUT must be true or false, got '$log_to_stdout'" ;; +esac + +# The node binds discovery to the P2P port + 1 and cannot be told otherwise. +if [ "$discovery_port" -ne $((p2p_port + 1)) ]; then + fail "RUSTOCK_DISCOVERY_PORT is $discovery_port but must be RUSTOCK_P2P_PORT + 1 = $((p2p_port + 1))" +fi + +# Docker creates a missing bind-mount source owned by root. +if [ ! -w "$data_dir" ]; then + fail "$data_dir is not writable by uid $(id -u); run: chown $(id -u):$(id -g) " +fi + +args=(--config /etc/rustock/node.toml --port "$p2p_port" --network-id "$network_id" --log-level "$log_level") + +if [ "$log_to_stdout" = true ]; then + args+=(--log-to-stdout) +fi + +if [ -n "$external_ip" ]; then + args+=(--external-ip "$external_ip") +else + echo "entrypoint: RUSTOCK_EXTERNAL_IP is not set; networks 30 and 31 look it up, others advertise 127.0.0.1" >&2 +fi + +# The node gives RUST_LOG precedence over --log-level. +if [ -n "${RUST_LOG:-}" ]; then + echo "entrypoint: RUST_LOG is set and overrides RUSTOCK_LOG_LEVEL" >&2 +fi + +if [ -n "$extra_args" ]; then + read -r -a extra <<< "$extra_args" + args+=("${extra[@]}") +fi + +echo "entrypoint: network $network_id, p2p tcp $p2p_port, discovery udp $discovery_port, log level $log_level" >&2 +exec /usr/local/bin/rustock-cli "${args[@]}" "$@" diff --git a/docker/node.toml b/docker/node.toml new file mode 100644 index 0000000..a732d4a --- /dev/null +++ b/docker/node.toml @@ -0,0 +1,20 @@ +# Network, ports, advertised address and logging come from .env through +# entrypoint.sh as flags, which override this file. node.example.toml documents +# every setting. + +[node] +data_dir = "/var/lib/rustock" + +[trie] +# "single" aborts on an empty data directory; "epoch" keeps only recent state, +# so historical queries below its retention window fail. An imported rskj +# database needs "single" (see docs/docker.md). +backend = "epoch" + +# Listen on 0.0.0.0: a published port cannot reach a loopback listener. +[rpc] +host = "0.0.0.0" + +[metrics] +host = "0.0.0.0" +port = 9898 diff --git a/docker/runtime.Dockerfile b/docker/runtime.Dockerfile new file mode 100644 index 0000000..f2a85d7 --- /dev/null +++ b/docker/runtime.Dockerfile @@ -0,0 +1,21 @@ +# Runs the prebuilt rustock-cli that docker-compose.yml mounts at /usr/local/bin. +FROM debian:bookworm-slim + +RUN apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates libstdc++6 \ + && rm -rf /var/lib/apt/lists/* \ + && groupadd --system --gid 10001 rustock \ + && useradd --system --uid 10001 --gid rustock --home-dir /var/lib/rustock --shell /usr/sbin/nologin rustock \ + && install -d -o rustock -g rustock /var/lib/rustock \ + && install -d /etc/rustock + +COPY --chmod=755 entrypoint.sh /usr/local/bin/entrypoint.sh + +USER rustock +WORKDIR /var/lib/rustock +VOLUME /var/lib/rustock + +# P2P tcp, discovery udp (P2P + 1), RPC, metrics. Defaults; see .env.example. +EXPOSE 5050/tcp 5051/udp 4444/tcp 9898/tcp + +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] diff --git a/docs/docker.md b/docs/docker.md new file mode 100644 index 0000000..58e15a2 --- /dev/null +++ b/docs/docker.md @@ -0,0 +1,199 @@ +# Running Rustock in Docker + +The node is built once into a binary. A small runtime image runs it, with the +binary mounted from `./bin`. Servers need Docker, not Rust. + +Rustock is an unaudited prototype: run it on testnet or beside rskj first. + +| File | Role | +|---|---| +| `scripts/build-binary.sh` | builds `rustock-cli` and the maintenance tools for amd64 or arm64 | +| `docker/build.Dockerfile` | the build stage; exports only the binary | +| `docker/runtime.Dockerfile` | the runtime image | +| `docker-compose.yml` | runs it, configured from `.env` | +| `docker/node.toml` | node settings that are the same everywhere | + +The `Dockerfile` in the repository root is a separate, self-contained image; +Compose does not use it. + +## Build + +```bash +scripts/build-binary.sh amd64 # or arm64 +``` + +Writes `dist/rustock-0.1.0--linux-` and +`dist/rustock-tools-0.1.0--linux-.tar.gz`, each with a `.sha256` +file, and `-dirty` appended for uncommitted changes. `FEATURES=smtp` enables +peg-out alert email. `TOOLS="rewind_to block_info"` overrides the tool list. + +Build each architecture on matching hardware; emulated builds are very slow +(RocksDB, LTO). Outside Docker the binary needs the glibc of Debian bookworm or +newer. + +## Install and start + +Copy the binary, its `.sha256` file, `docker-compose.yml`, `.env.example` and +`docker/` to the server. `uname -m` is `x86_64` for amd64, `aarch64` for arm64. + +```bash +sha256sum -c rustock-0.1.0--linux-amd64.sha256 +mkdir -p bin +install -m 755 rustock-0.1.0--linux-amd64 bin/.rustock-cli.new +mv bin/.rustock-cli.new bin/rustock-cli +cp .env.example .env # set RUSTOCK_EXTERNAL_IP behind NAT +docker compose up -d --build +docker compose logs -f rustock +``` + +Without `bin/rustock-cli` Compose refuses to start. To roll back, install the +previous binary and recreate the container. + +A fresh start syncs mainnet from genesis, which takes days. +`RUSTOCK_EXTRA_ARGS=--snap-sync` joins from a recent state instead. + +`docker/node.toml` selects the `epoch` trie backend because the default +`single` backend aborts on an empty data directory ("This database has no trie +in it"). `epoch` keeps only recent state, so historical queries below its +retention window fail. + +## Ports + +| Port | Purpose | Published on | Setting | +|---|---|---|---| +| 5050/tcp | peers | all interfaces | `RUSTOCK_P2P_PORT` | +| 5051/udp | discovery | all interfaces | `RUSTOCK_DISCOVERY_PORT` | +| 4444/tcp | JSON-RPC | 127.0.0.1 | `RUSTOCK_RPC_BIND`, `RUSTOCK_RPC_PORT` | +| 9898/tcp | metrics | 127.0.0.1 | `RUSTOCK_METRICS_BIND`, `RUSTOCK_METRICS_PORT` | + +- Open the peer and discovery ports in the firewall. On mainnet and testnet the + node looks up its public IPv4 address at start (`--ip-check-url`, set through + `RUSTOCK_EXTRA_ARGS`; `off` disables it). Behind NAT the lookup returns the + outbound address, so set `RUSTOCK_EXTERNAL_IP` and forward both ports. With no + address and no lookup the node advertises `127.0.0.1`. +- The node binds discovery to the peer port + 1, so `RUSTOCK_DISCOVERY_PORT` + must equal `RUSTOCK_P2P_PORT + 1`. The container refuses to start otherwise. +- 5050 is rskj's mainnet port. For testnet use `RUSTOCK_NETWORK_ID=31`, + `RUSTOCK_P2P_PORT=50505` and `RUSTOCK_DISCOVERY_PORT=50506`. +- Publishing on `127.0.0.1` keeps other hosts out only on Docker Engine 28.3.3 + or later. +- The node has no RPC authentication. Put an authenticating proxy in front of + any RPC port that leaves a trusted network. + +## Configuration + +`.env` holds per-server settings; [`.env.example`](../.env.example) documents +each. `docker/node.toml` holds the rest; [`node.example.toml`](../node.example.toml) +documents every setting. Flags from `.env` override the file, and an unknown +key in it is an error. + +| Changed | Run | +|---|---| +| `.env` | `docker compose up -d` | +| `bin/rustock-cli`, `docker/node.toml` | `docker compose up -d --force-recreate` | +| `docker/entrypoint.sh`, `docker/runtime.Dockerfile` | `docker compose up -d --build` | + +Recreating the container kills a running node; stop it first (see Stop). + +## Data + +`RUSTOCK_DATA_DIR` holds the database, the node key and file logs. + +| Value | Meaning | +|---|---| +| `rustock-data` (default) | Docker named volume | +| path starting with `/` or `./` | host directory | + +A host directory must be owned by uid 10001, or the container refuses to start: + +```bash +sudo install -d -o 10001 -g 10001 /srv/rustock +``` + +`docker compose down -v` deletes a named volume: the chain and the node key. An +imported mainnet database is about 154 GB; use local SSD. + +## Logs + +By default the node logs to stdout: `docker compose logs -f rustock`. Docker +rotates them at `RUSTOCK_LOG_MAX_SIZE` and `RUSTOCK_LOG_MAX_FILE`, about 500 MB +in total by default. `RUSTOCK_LOG_LEVEL` accepts per-module directives such as +`info,rustock_sync=debug`; a `RUST_LOG` variable in the container overrides it. + +`RUSTOCK_LOG_TO_STDOUT=false` writes `rustock.log.` into the data +directory, one file a day, never pruned. The log directory cannot be set +separately from the data directory. + +## Stop + +The running node ignores SIGTERM, so `docker compose stop` waits 30 seconds and +then kills it. For a clean stop, set `RUSTOCK_EXTRA_ARGS=--rpc-admin` and call +`rsk_shutdown`, which flushes the trie store and exits 0. The container then +stays stopped; `docker compose up -d` starts it again. The admin methods have +no authentication. + +```bash +docker compose exec rustock bash -c ' + body="{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"rsk_shutdown\",\"params\":[]}" + exec 3<>/dev/tcp/127.0.0.1/4444 + printf "POST / HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\nContent-Length: ${#body}\r\nConnection: close\r\n\r\n%s" "$body" >&3 + cat <&3' +``` + +## Import an rskj database + +Stop rskj, then import the directory that contains `blocks/` and `unitrie/`, +mounted read-only. `receipts/`, `blooms/` and `stateRoots/` are not needed. The +importer opens the datasources as RocksDB, so check that `dbKind.properties` +says `keyvalue.datasource=ROCKS_DB`. + +```bash +docker compose run --rm -v /var/lib/rsk/database/mainnet:/import:ro rustock \ + --import-rskj /import --import-skip-metadata +docker compose run --rm rustock --metadata-in-memory +``` + +Without `--import-skip-metadata` the import rebuilds the metadata itself by +walking parent hashes, which takes hours. The import also keeps running after +a Ctrl-C: it skips to compaction instead of stopping. Detach from the terminal +rather than interrupting it, and restart from an empty data directory if it was +interrupted, because re-importing over a partial one needs room for both copies. + +The import writes the state trie into the node's own database, which the +`epoch` backend refuses to start against. Set +`RUSTOCK_EXTRA_ARGS=--trie-backend single` before the next start. That backend +keeps every historical state version, so the database grows without bound. + +## Maintenance tools + +Offline programs from `crates/cli/examples/`, shipped in the tools tarball. The +node must be stopped: they take the database lock. + +| Tool | Does | +|---|---| +| `rewind_to ` | resets the heads to ``; the node re-syncs the tail | +| `set_exec_head [--dry-run]` | moves the executed head to a canonical block | +| `reset_exec_head ` | replays execution from genesis, keeping downloaded blocks | +| `block_info ...` | prints stored block metadata | +| `height_peek [to]` | lists every block stored at a height | + +```bash +mkdir -p bin/tools && tar -xzf rustock-tools--linux-amd64.tar.gz -C bin/tools +docker compose run --rm tools rewind_to /var/lib/rustock +``` + +The tools run in the image built by `docker compose up -d --build`. Check +`set_exec_head` with `--dry-run` first. `rewind_to` needs the state at the +target block in the database. + +## Monitoring + +The health check opens a TCP connection to the metrics port, so it shows that +the process serves, not that it is synced. Sync progress: + +```bash +curl -s localhost:9898/metrics | grep -E 'rustock_(peers|sync_height|sync_target_height) ' +``` + +Metrics are listed in [metrics.md](metrics.md) and alerting is configured in +[alerts.md](alerts.md). diff --git a/scripts/build-binary.sh b/scripts/build-binary.sh new file mode 100755 index 0000000..0864a25 --- /dev/null +++ b/scripts/build-binary.sh @@ -0,0 +1,100 @@ +#!/bin/bash +# Builds rustock-cli and its maintenance tools for one Linux architecture in +# Docker and writes +# dist/rustock--linux- +# dist/rustock-tools--linux-.tar.gz +# each with a .sha256 file. +# +# Usage: scripts/build-binary.sh +# FEATURES=smtp scripts/build-binary.sh amd64 +# TOOLS="rewind_to block_info" scripts/build-binary.sh amd64 +# +# is -, plus -dirty when the working +# tree has uncommitted changes. A non-native architecture builds under +# emulation, which is slow. + +set -euo pipefail + +fail() { + echo "error: $*" >&2 + exit 1 +} + +for tool in docker git awk tar; do + command -v "$tool" >/dev/null 2>&1 || fail "missing required tool: $tool" +done +docker buildx version >/dev/null 2>&1 || fail "docker buildx is required" + +if command -v sha256sum >/dev/null 2>&1; then + sha256_of() { sha256sum "$1" | awk '{print $1}'; } +elif command -v shasum >/dev/null 2>&1; then + sha256_of() { shasum -a 256 "$1" | awk '{print $1}'; } +else + fail "missing required tool: sha256sum or shasum" +fi + +[ "$#" -eq 1 ] || fail "usage: $0 " +arch="$1" +case "$arch" in + amd64|arm64) ;; + *) fail "unsupported architecture '$arch' (expected amd64 or arm64)" ;; +esac + +repo="$(cd "$(dirname "$0")/.." && pwd)" +cd "$repo" + +crate_version="$(awk -F'"' '/^version *=/ {print $2; exit}' crates/cli/Cargo.toml)" +[ -n "$crate_version" ] || fail "cannot read the version from crates/cli/Cargo.toml" +commit="$(git rev-parse --short HEAD)" +version="${crate_version}-${commit}" +if [ -n "$(git status --porcelain)" ]; then + version="${version}-dirty" +fi + +name="rustock-${version}-linux-${arch}" +out="dist/${name}" +mkdir -p dist +staging="$(mktemp -d "$repo/dist/.build.XXXXXX")" +trap 'rm -rf "$staging"' EXIT + +echo "building $name" +docker buildx build \ + --platform "linux/${arch}" \ + --file docker/build.Dockerfile \ + --target binary \ + --build-arg "FEATURES=${FEATURES:-}" \ + ${TOOLS:+--build-arg "TOOLS=${TOOLS}"} \ + --output "type=local,dest=${staging}" \ + . + +[ -f "$staging/rustock-cli" ] || fail "the build produced no rustock-cli" + +tools_name="rustock-tools-${version}-linux-${arch}.tar.gz" +docker buildx build \ + --platform "linux/${arch}" \ + --file docker/build.Dockerfile \ + --target tools \ + --build-arg "FEATURES=${FEATURES:-}" \ + ${TOOLS:+--build-arg "TOOLS=${TOOLS}"} \ + --output "type=local,dest=${staging}/tools" \ + . + +[ -n "$(ls -A "$staging/tools")" ] || fail "the build produced no tools" +tar -C "$staging/tools" -czf "$staging/$tools_name" . + +chmod 755 "$staging/rustock-cli" +mv "$staging/rustock-cli" "$out" +mv "$staging/$tools_name" "dist/$tools_name" + +write_sha() { + local file="$1" name hash + name="$(basename "$file")" + hash="$(sha256_of "$file")" + [ -n "$hash" ] || fail "could not compute the checksum of $name" + printf '%s %s\n' "$hash" "$name" > "${file}.sha256" + echo "wrote $file" + echo "wrote ${file}.sha256" +} + +write_sha "$out" +write_sha "dist/$tools_name"