Skip to content

Commit

Permalink
feat: docker (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmorency authored Mar 27, 2024
1 parent a553bf6 commit d28d58a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
46 changes: 46 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Start from the official Golang image to build our application.
FROM golang:1.22 AS builder

# Set the current working directory inside the container.
WORKDIR /app

# Copy go.mod and go.sum to download the dependencies.
# This is done before copying the source code to cache the dependencies layer.
COPY go.mod ./
COPY go.sum ./

# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed.
RUN go mod download

# Copy the source code into the container.
COPY . .

# Build the Go app as a static binary.
# -o specifies the output file, in this case, the executable name.
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o mfx-migrator .

# Start from a Debian Slim image to keep the final image size down.
FROM debian:buster-slim

# Install cron to schedule the job.
RUN apt-get update && apt-get install -y cron && rm -rf /var/lib/apt/lists/*

# The application configuration file should be stored in /mfx-migrator
VOLUME /mfx-migrator

# The job files should be stored in /jobs
VOLUME /jobs

# Copy the pre-built binary file and script from the previous stage.
COPY --from=builder /app/mfx-migrator /usr/local/bin/mfx-migrator
COPY --from=builder /app/scripts/claim_and_migrate.sh /etc/cron.d/claim_and_migrate.sh
RUN chmod +x /etc/cron.d/claim_and_migrate.sh

# Add a cron job to run the script every minute.
RUN echo "* * * * * root /etc/cron.d/claim_and_migrate.sh >> /var/log/cron.log 2>&1" >> /etc/crontab

# Create a log file to store the cron job output.
RUN touch /var/log/cron.log

# Command to run the executable.
CMD cron && tail -f /var/log/cron.log
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func init() {
SetupRootCmdFlags(rootCmd)

viper.AddConfigPath("./")
viper.AddConfigPath("/mfx-migrator")
viper.AddConfigPath("/config")
viper.SetConfigName("migrator-config")

viper.AutomaticEnv()
Expand Down
11 changes: 11 additions & 0 deletions scripts/claim_and_migrate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

WORKDIR=/jobs

cd "$WORKDIR" || exit 1

# Claim some work from queue
mfx-migrator claim

# Run the migration for each JSON file in the workdir
find . -name '*.json' ! -name "config.json" -print0 | xargs -0 -I{} -P 1 bash -c 'mfx-migrator migrate --uuid $(basename "{}" .json)'

0 comments on commit d28d58a

Please sign in to comment.