From deb2674b17a721a49236519d48c50fce1dd55046 Mon Sep 17 00:00:00 2001 From: fsociety Date: Fri, 28 Feb 2025 15:34:48 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20feat(docker):=20add=20HAVEN=5FIM?= =?UTF-8?q?PORT=5FFLAG=20to=20control=20import=20behavior=20and=20backup?= =?UTF-8?q?=20.env=20file=20during=20relay=20start=20scripts.=20fixes=20#2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + Dockerfile | 33 ++++++++++++++++++++++++++------- README.md | 9 +++++++++ entrypoint.sh | 7 +++++++ scripts/start-relay-tor.sh | 25 ++++++++++++++++++++++++- scripts/start-relay.sh | 23 +++++++++++++++++++++++ 6 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 entrypoint.sh diff --git a/.gitignore b/.gitignore index 02d89fe..f983f89 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea .env +.env.bak relays_import.json relays_blastr.json db.zip diff --git a/Dockerfile b/Dockerfile index 9e7deb9..4fab3cf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,19 +15,38 @@ ARG REPO_URL=https://github.com/bitvora/haven.git ARG VERSION RUN git clone --branch ${VERSION} --single-branch ${REPO_URL} . RUN --mount=type=cache,target=/gomod-cache --mount=type=cache,target=/go-cache \ - go build -ldflags="-w -s" -o main . + go build -a -tags netgo -ldflags '-w -s -extldflags "-static"' -o haven . -# Final Distroless image -FROM gcr.io/distroless/base +# Final Alpine image +FROM alpine:latest + +ENV HAVEN_IMPORT_FLAG=false + +# Install libc6-compat for Go binary compatibility +RUN apk add --no-cache libc6-compat # Add non-root user specification -USER nonroot +RUN adduser -D -g '' nonroot WORKDIR /app # Copy Go application -COPY --from=builder /app/main . +COPY --from=builder /app/haven . + +# Ensure the main executable has the correct permissions +RUN chmod +x /app/haven + +# Copy the entrypoint script +COPY entrypoint.sh /entrypoint.sh + +# Ensure the entrypoint script has the correct permissions +RUN chmod +x /entrypoint.sh + +# Set the entrypoint +ENTRYPOINT ["/entrypoint.sh"] + +# Switch to non-root user +USER nonroot -# Expose port and set command +# Expose port EXPOSE 3355 -CMD ["./main"] diff --git a/README.md b/README.md index 2fa95af..d5bef2c 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,15 @@ cp -r templates-example/* templates/ Open the `.env` file and set the necessary environment variables. +Setting the HAVEN_IMPORT_FLAG Environment Variable +You can control whether the --import flag is passed to the application by setting the HAVEN_IMPORT_FLAG=true environment +variable. + +This can be done either through a user prompt in the provided scripts or manually before running the docker +compose command. + +`HAVEN_IMPORT_FLAG=true` + ### 3. Create the relays JSON files Copy the example relay JSON files for your seed and blastr relays: diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..0e29b1f --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +if [ "$HAVEN_IMPORT_FLAG" = "true" ]; then + exec /app/haven --import +else + exec /app/haven +fi \ No newline at end of file diff --git a/scripts/start-relay-tor.sh b/scripts/start-relay-tor.sh index 9a30659..fdc8fda 100755 --- a/scripts/start-relay-tor.sh +++ b/scripts/start-relay-tor.sh @@ -9,7 +9,30 @@ echo "Stopping and removing old container 'haven-tor'..." docker stop haven-tor docker rm haven-tor -# Pull the latest images and start the services +# Ask the user if HAVEN_IMPORT_FLAG should be set +read -p "Do you want to import your old notes? (y/n): " IMPORT_FLAG_INPUT + +# Determine the value for HAVEN_IMPORT_FLAG +if [ "$IMPORT_FLAG_INPUT" = "y" ]; then + IMPORT_FLAG_VALUE="true" +else + IMPORT_FLAG_VALUE="false" +fi + +# Backup the .env file +cp .env .env.bak + +# Update or add HAVEN_IMPORT_FLAG to the .env file +if grep -q "^HAVEN_IMPORT_FLAG=" .env; then + # If HAVEN_IMPORT_FLAG exists, update it + sed -i.bak "/^HAVEN_IMPORT_FLAG=/c\HAVEN_IMPORT_FLAG=$IMPORT_FLAG_VALUE" .env +else + # If HAVEN_IMPORT_FLAG does not exist, add it + echo "" >> .env + echo "HAVEN_IMPORT_FLAG=$IMPORT_FLAG_VALUE" >> .env +fi + +# Pull the latest images and start the services with the IMPORT_FLAG environment variable echo "Starting Docker Compose services..." $DOCKER_COMPOSE_COMMAND -f docker-compose.tor.yml up --build -d diff --git a/scripts/start-relay.sh b/scripts/start-relay.sh index ae3d39a..31da0ae 100755 --- a/scripts/start-relay.sh +++ b/scripts/start-relay.sh @@ -6,6 +6,29 @@ echo "Stopping and removing old container 'haven-relay'..." docker stop haven-relay docker rm haven-relay +# Ask the user if HAVEN_IMPORT_FLAG should be set +read -p "Do you want to import your old notes? (y/n): " IMPORT_FLAG_INPUT + +# Determine the value for HAVEN_IMPORT_FLAG +if [ "$IMPORT_FLAG_INPUT" = "y" ]; then + IMPORT_FLAG_VALUE="true" +else + IMPORT_FLAG_VALUE="false" +fi + +# Backup the .env file +cp .env .env.bak + +# Update or add HAVEN_IMPORT_FLAG to the .env file +if grep -q "^HAVEN_IMPORT_FLAG=" .env; then + # If HAVEN_IMPORT_FLAG exists, update it + sed -i.bak "/^HAVEN_IMPORT_FLAG=/c\HAVEN_IMPORT_FLAG=$IMPORT_FLAG_VALUE" .env +else + # If HAVEN_IMPORT_FLAG does not exist, add it + echo "" >> .env + echo "HAVEN_IMPORT_FLAG=$IMPORT_FLAG_VALUE" >> .env +fi + # Pull the latest images and start the services echo "Starting Docker Compose services..." $DOCKER_COMPOSE_COMMAND up --build -d