diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..394690e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM golang:1.20-alpine as builder + +RUN apk --update --no-cache add make git g++ linux-headers +# DEBUG +RUN apk add busybox-extras + +# Get and build ipfs-blockchain-watcher +ADD . /go/src/github.com/cerc-io/ipld-eth-state-snapshot +#RUN git clone https://github.com/cerc-io/ipld-eth-state-snapshot.git /go/src/github.com/vulcanize/ipld-eth-state-snapshot + +WORKDIR /go/src/github.com/cerc-io/ipld-eth-state-snapshot +RUN GO111MODULE=on GCO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o ipld-eth-state-snapshot . + +# app container +FROM alpine + +RUN apk --no-cache add su-exec bash + +WORKDIR /app + +COPY --from=builder /go/src/github.com/cerc-io/ipld-eth-state-snapshot/startup_script.sh . +COPY --from=builder /go/src/github.com/cerc-io/ipld-eth-state-snapshot/environments environments + +# keep binaries immutable +COPY --from=builder /go/src/github.com/cerc-io/ipld-eth-state-snapshot/ipld-eth-state-snapshot ipld-eth-state-snapshot + +ENTRYPOINT ["/app/startup_script.sh"] diff --git a/pkg/snapshot/config.go b/pkg/snapshot/config.go index e2f6c22..84e8a38 100644 --- a/pkg/snapshot/config.go +++ b/pkg/snapshot/config.go @@ -91,6 +91,7 @@ func NewInPlaceSnapshotConfig() *Config { // Init Initialises config func (c *Config) Init(mode SnapshotMode) error { + viper.BindEnv(LOGRUS_FILE_TOML, LOGRUS_FILE) viper.BindEnv(ETH_NODE_ID_TOML, ETH_NODE_ID) viper.BindEnv(ETH_CLIENT_NAME_TOML, ETH_CLIENT_NAME) viper.BindEnv(ETH_GENESIS_BLOCK_TOML, ETH_GENESIS_BLOCK) @@ -159,6 +160,12 @@ func (c *FileConfig) Init() error { } func (c *ServiceConfig) Init() error { + viper.BindEnv(SNAPSHOT_BLOCK_HEIGHT_TOML, SNAPSHOT_BLOCK_HEIGHT) + viper.BindEnv(SNAPSHOT_START_HEIGHT_TOML, SNAPSHOT_START_HEIGHT) + viper.BindEnv(SNAPSHOT_END_HEIGHT_TOML, SNAPSHOT_END_HEIGHT) + viper.BindEnv(SNAPSHOT_MODE_TOML, SNAPSHOT_MODE) + viper.BindEnv(SNAPSHOT_WORKERS_TOML, SNAPSHOT_WORKERS) + viper.BindEnv(SNAPSHOT_ACCOUNTS_TOML, SNAPSHOT_ACCOUNTS) var allowedAccounts []string viper.UnmarshalKey(SNAPSHOT_ACCOUNTS_TOML, &allowedAccounts) diff --git a/startup_script.sh b/startup_script.sh new file mode 100755 index 0000000..290bf9e --- /dev/null +++ b/startup_script.sh @@ -0,0 +1,63 @@ +#!/bin/bash +# Exit if the variable tests fail +set -e +set -o pipefail + +if [[ -n "$CERC_SCRIPT_DEBUG" ]]; then + env + set -x +fi + +# Check the database variables are set +test "$VDB_COMMAND" + +# docker must be run in privilaged mode for mounts to work +echo "Setting up /app/geth-rw overlayed /app/geth-ro" +mkdir -p /tmp/overlay +mount -t tmpfs tmpfs /tmp/overlay +mkdir -p /tmp/overlay/upper +mkdir -p /tmp/overlay/work +mkdir -p /app/geth-rw + +mount -t overlay overlay -o lowerdir=/app/geth-ro,upperdir=/tmp/overlay/upper,workdir=/tmp/overlay/work /app/geth-rw + +mkdir /var/run/statediff +cd /var/run/statediff + +SETUID="" +if [[ -n "$TARGET_UID" ]] && [[ -n "$TARGET_GID" ]]; then + SETUID="su-exec $TARGET_UID:$TARGET_GID" + chown -R $TARGET_UID:$TARGET_GID /var/run/statediff +fi + +START_TIME=`date -u +"%Y-%m-%dT%H:%M:%SZ"` +echo "Running the snapshot service" && \ +if [[ ! -z "$LOGRUS_FILE" ]]; then + $SETUID /app/ipld-eth-state-snapshot "$VDB_COMMAND" $* |& $SETUID tee ${LOGRUS_FILE}.console + rc=$? +else + $SETUID /app/ipld-eth-state-snapshot "$VDB_COMMAND" $* + rc=$? +fi +STOP_TIME=`date -u +"%Y-%m-%dT%H:%M:%SZ"` + +if [ $rc -eq 0 ] && [ "$VDB_COMMAND" == "stateSnapshot" ] && [ -n "$SNAPSHOT_BLOCK_HEIGHT" ]; then + cat >metadata.json <