Skip to content

Commit

Permalink
add caddy to serve snapshot from node
Browse files Browse the repository at this point in the history
  • Loading branch information
hard-nett committed Dec 28, 2024
1 parent 4216714 commit 7034d44
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
node-data

*.tar.gz

node_modules
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,13 @@ FROM ${BUILD_IMAGE} AS omnibus
LABEL org.opencontainers.image.source https://github.com/terpnetwork/o-line

RUN apt-get update && \
apt-get install --no-install-recommends --assume-yes ca-certificates curl wget file unzip liblz4-tool gnupg2 jq pv && \
apt-get install --no-install-recommends --assume-yes ca-certificates apt-transport-https curl wget file unzip liblz4-tool gnupg2 jq pv && \
apt-get clean

# install caddy
RUN echo "deb [trusted=yes] https://apt.fury.io/caddy/ /" | tee -a /etc/apt/sources.list.d/caddy-fury.list
RUN apt-get update && apt-get install -y caddy

COPY --from=zstd_build /usr/local/bin/zstd /bin/

ARG PROJECT
Expand Down
20 changes: 19 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,22 @@ if [ -n "$SNAPSHOT_PATH" ]; then
else
echo "Running '$START_CMD'..."
exec $START_CMD
fi
fi

# create systemctl for caddy
cat > /etc/systemd/system/caddy.service <<EOF
[Unit]
Description=Caddy Web Server
After=network.target
[Service]
User=root
ExecStart=/usr/bin/caddy start --config /etc/caddy/Caddyfile
Restart=always
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable caddy.service
83 changes: 31 additions & 52 deletions snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ fi
TIME=$(date -u +%T)
DOW=$(date +%u)

# create new caddy file
echo "Creating new Caddy file"
cat > /etc/caddy/Caddyfile <<EOF
:80 {
root * ${SNAPSHOT_PATH}
file_server {
browse
}
rewrite /terp_latest.tar.gz {
to /${SNAPSHOT_PREFIX}_latest.tar.gz
}
}
EOF

echo "$TIME: Starting server"
echo "$TIME: Snapshot will run at $SNAPSHOT_TIME on day $SNAPSHOT_DAY"
exec $SNAPSHOT_CMD &
Expand All @@ -48,66 +62,29 @@ while true; do
TIME=$(date -u +%T)
DOW=$(date +%u)
if [[ ($SNAPSHOT_DAY == "*") || ($SNAPSHOT_DAY == $DOW) ]] && [[ $SNAPSHOT_TIME == $TIME ]]; then
echo "$TIME: Stopping server"
get block height
echo "$TIME: Stopping terp node"
kill -15 $PID
wait

echo "$TIME: Running snapshot"
timestamp=$(date +"%Y-%m-%dT%H:%M:%S")
echo "$TIME: Stopping caddy"
systemctl stop caddy.service

echo "$TIME: Creating snapshot"
timestamp=$(date +"%Y-%m-%dT%H:%M:%S")
SNAPSHOT_SIZE=$(du -sb $SNAPSHOT_DIR | cut -f1)

mkdir -p "${SNAPSHOT_PATH}"
tar -czf "${SNAPSHOT_PATH}/${SNAPSHOT_PREFIX}_${timestamp}.tar.gz" -C "${SNAPSHOT_DIR}" .
ln -sf "${SNAPSHOT_PATH}/${SNAPSHOT_PREFIX}_${timestamp}.tar.gz" "${SNAPSHOT_PATH}/terp_latest.tar.gz"


# compress snapshot_path to tarball
log_this "Creating new snapshot"
time tar cf ${HOME}/${SNAP_NAME} -C ${SNAPSHOT_DIR} . &>>${LOG_PATH}

# move compress image to snapshot folder
log_this "Moving new snapshot to ${SNAP_PATH}"
mv ${HOME}/${CHAIN_ID}*tar ${SNAP_PATH} &>>${LOG_PATH}


# Serve snapshot via caddy
log_this "Serving new snapshot"
systemctl start caddy.service &
caddy_pid=$!
echo "$TIME: Snapshot available at http://localhost:80/terp_latest.tar.gz"
fi
if [[ $SNAPSHOT_RETAIN != "0" || $SNAPSHOT_METADATA != "0" ]]; then
fi
snapshots=()
for line in "${s3Files[@]}"; do
createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
fileName=`echo $line|awk '{$1=$2=$3=""; print $0}' | sed 's/^[ \t]*//'`
if [[ -n $SNAPSHOT_METADATA_URL && $SNAPSHOT_METADATA_URL != */ ]]; then
fileUrl="${SNAPSHOT_METADATA_URL}/${fileName}"
else
fileUrl="${SNAPSHOT_METADATA_URL}${fileName}"
fi
## prune any snapshots if configured
if [ "$SNAPSHOT_RETAIN" != "0" ]; then
olderThan=`date -d"-$SNAPSHOT_RETAIN" +%s`
if [[ $createDate -lt $olderThan ]]; then
if [[ $fileName != "" ]]; then
echo "$TIME: Deleting snapshot $fileName"
fi
else
snapshots+=("$fileUrl")
fi
else
snapshots+=("$fileUrl")
fi
done;

if [ "$SNAPSHOT_METADATA" != "0" ]; then
echo "$TIME: Uploading metadata"
snapshotJson="[]"
for url in ${snapshots[@]}; do
snapshotJson="$(echo $snapshotJson | jq ".+[\"$url\"]")"
done
else
fi
fi
fi

echo "$TIME: Restarting server"
echo "$TIME: Restarting terpd"
exec $SNAPSHOT_CMD &
PID=$!
sleep 1s
Expand All @@ -118,3 +95,5 @@ while true; do
fi
fi
done

# todo: after n snapshots created prune k # of snapshots by saving to jackal storage provider

0 comments on commit 7034d44

Please sign in to comment.