Skip to content

Commit

Permalink
🚀 feat(docker): add HAVEN_IMPORT_FLAG to control import behavior and …
Browse files Browse the repository at this point in the history
…backup .env file during relay start scripts. fixes #2
  • Loading branch information
fsociety committed Feb 28, 2025
1 parent 551734c commit deb2674
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
.env
.env.bak
relays_import.json
relays_blastr.json
db.zip
Expand Down
33 changes: 26 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

if [ "$HAVEN_IMPORT_FLAG" = "true" ]; then
exec /app/haven --import
else
exec /app/haven
fi
25 changes: 24 additions & 1 deletion scripts/start-relay-tor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions scripts/start-relay.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit deb2674

Please sign in to comment.