Skip to content

Dockerfile, additional scripts and minor improvements #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Dockerfile

CHANGELOG.md
README.md
LICENSE
6 changes: 5 additions & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ on:
jobs:

build:
strategy:
matrix:
go-version: [ '1.21', '1.24' ]

runs-on: ubuntu-latest
steps:
- name: Install RabbitMQ
Expand All @@ -19,7 +23,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: ${{ matrix.go-version }}

- name: Build
run: go build -v ./...
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/build_container.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: build-container

on:
push:
ignore_branches: [ no_test ]
pull_request:
ignore_branches: [ no_test ]

jobs:

build_container_scratch:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Run build_container to generate scratch image
run: scripts/build_container -g scratch -e BASE_IMAGE=scratch

build_container_params:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Run build_container to generate and image using kaniko
run: scripts/build_container --tag kaniko --builder kaniko -t debug -e UID=1234 -e GID=5678

- name: Load rabbitmq-dump-queue:kaniko image
run: docker load -i image.tar

- name: Test container ids
run: docker run --rm --entrypoint= rabbitmq-dump-queue:kaniko /bin/id | grep --color=always '1234' | grep --color=always '5678'

build_container_test:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Run scripts/test
run: scripts/test
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/bin
/release
/rabbitmq-dump-queue

# exclude data dir
/data
# kaniko build tar
image.tar
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## Upcoming

## v0.7.2 (2025-03-11)

* Update the RabbitMQ library to v1.10.0.
* Update Go to 1.24.
* Modify main.go to return help when no queue is provided
* Add Dockerfile and .dockerignore [@drodbar](https://github.com/drodbar).
* Add build_container and test scripts [@drodbar](https://github.com/drodbar).


## v0.7.1 (2024-01-26)

* Update the RabbitMQ library to v1.9.0.
Expand Down
48 changes: 48 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
ARG BASE_IMAGE=gcr.io/distroless/static

# build stage
FROM golang:alpine AS build

COPY go* main* .

RUN apk add --no-cache git

RUN CGO_ENABLED=0 go build -o rabbitmq-dump-queue .


# test stage
FROM build AS test

ENV GOPATH=''
ENTRYPOINT [ "go", "test" ]


# production stage
FROM ${BASE_IMAGE} AS production
ARG UID=65532
ARG GID=65532

# make latest alpine certs available
COPY --from=alpine:latest /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

USER ${UID}:${GID}

# copy app binary
COPY --from=build /go/rabbitmq-dump-queue /usr/local/bin/

ENTRYPOINT [ "rabbitmq-dump-queue" ]

# volume dir to output data
VOLUME /data
WORKDIR /data


# degug stage
FROM busybox:stable-uclibc AS busybox
FROM production AS debug

COPY --from=busybox /bin/id /bin/sh /bin/busybox /bin/


# default stage: production
FROM production
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# rabbitmq-dump-queue

Dump messages from a RabbitMQ queue to files, without affecting the queue.

[![build-and-test](https://github.com/dubek/rabbitmq-dump-queue/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/dubek/rabbitmq-dump-queue/actions/workflows/build-and-test.yml)
Expand All @@ -17,12 +16,34 @@ If you have [Go](https://golang.org/doc/install) installed, you can install
rabbitmq-dump-queue from source by running:

```
go get github.com/dubek/rabbitmq-dump-queue
go install github.com/dubek/rabbitmq-dump-queue
```

The `rabbitmq-dump-queue` executable will be created in the `$GOPATH/bin`
directory.

### Compile in docker

If you want to use the application within a root-less container use `scripts/build_container`

``` bash
$ scripts/build_container -h
Usage: build_container [OPTIONS]

Options:
-b, --builder <builder> Specify the builder (docker or kaniko).
-t, --target <target> Specify the target (build, test, debug or production).
Default: production.
-g, --tag <tag> Specify the tag.
Default: latest for production target or <target>.
-p, --dry-run Print only mode.
-h, --help Display this help message.

Examples:
build_container -b docker -t my-image --tag latest
build_container --builder kaniko --target debug

```

## Usage

Expand Down Expand Up @@ -123,6 +144,9 @@ To stop the RabbitMQ server container, run:
docker stop test-rabbitmq
docker rm test-rabbitmq

Testing with `docker` can simpify the task, simply run:

bash scripts/test

## Contributing

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/dubek/rabbitmq-dump-queue

go 1.21
go 1.24

require github.com/rabbitmq/amqp091-go v1.9.0
require github.com/rabbitmq/amqp091-go v1.10.0
29 changes: 4 additions & 25 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rabbitmq/amqp091-go v1.9.0 h1:qrQtyzB4H8BQgEuJwhmVQqVHB9O4+MNDJCCAcpc3Aoo=
github.com/rabbitmq/amqp091-go v1.9.0/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv/f+6luSD3pc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ func dial(amqpURI string) (*amqp091.Connection, error) {

func dumpMessagesFromQueue(amqpURI string, queueName string, maxMessages uint, outputDir string) error {
if queueName == "" {
return fmt.Errorf("Must supply queue name")
error := fmt.Errorf("Must supply queue name")
flag.Usage()
return error
}

conn, err := dial(amqpURI)
Expand Down
90 changes: 90 additions & 0 deletions scripts/build_container
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash

set -e -o pipefail

usage() {
cat << USAGE >&2
Usage: $0 [OPTIONS]

Options:
-b, --builder <builder> Specify the builder (docker or kaniko).
-e, --extra-args <arg=value> Specify additional build-args.
-t, --target <target> Specify the target (build, test, debug or production).
Default: production.
-g, --tag <tag> Specify the tag.
Default: latest for production target or <target>.
-p, --dry-run Print only mode.
-h, --help Display this help message.

Examples:
$0 -b docker -t test --tag latest
$0 --builder kaniko --target debug
$0 -g local -e UID=\$UID -e GID=\$GID -e BASE_IMAGE=scratch

USAGE
exit 1
}

main() {
parse_args "$@"
set_default
build_image
}

build_with_docker() {
${DRY_RUN} docker buildx build \
-t "${IMAGE_NAME}:${TAG}" \
"${BUILD_ARGS[@]}" \
--target="${TARGET}" .
}

build_with_kaniko() {
KANIKO_IMG="gcr.io/kaniko-project/executor:latest"
${DRY_RUN} docker run -w /build \
-v "$PWD":/build \
--rm \
"${KANIKO_IMG}" \
"${BUILD_ARGS[@]}" \
-d "${IMAGE_NAME}:${TAG}" \
--target "${TARGET}" \
--context . \
--no-push \
--tar-path=/build/image.tar
}

parse_args() {
set -- "$@" "${EOL:=$(printf '\1\3\3\7')}" # end-of-list marker
while [ "$1" != "$EOL" ]; do
opt="$1"; shift
case $opt in
--builder|-b) BUILDER="$1"; shift; ;;
--extra-args|-e) BUILD_ARGS+=( --build-arg "$1" ); shift; ;;
--target|-t) TARGET="$1"; shift; ;;
--tag|-g) TAG="$1"; shift; ;;
--help|-h) usage; ;;
--dry-run|-p) DRY_RUN='echo'; ;;
*) printf "[ERROR] Unknwon arg [%s]\n" "$opt" >&2; usage; ;;
esac
done; shift
}

set_default() {
: "${BUILDER:="docker"}"
: "${TARGET:="production"}"
: "${IMAGE_NAME:="rabbitmq-dump-queue"}"
case "$TARGET" in
production) DEFAULT_TAG="latest"; ;;
*) DEFAULT_TAG="${TARGET}"
esac
: "${TAG:="$DEFAULT_TAG"}"
}

build_image() {
case $BUILDER in
kaniko) build_with_kaniko; ;;
docker) build_with_docker; ;;
*) echo "[ERROR] Unsupported builder [${BUILDER}]"
esac
}

main "$@"
44 changes: 44 additions & 0 deletions scripts/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

set -e

DIRNAME="$(dirname "$(readlink -f "$0")")"

: "${RABBITMQ_START_TIMEOUT:=30}"

trap rm_containters EXIT
trap errors_found ERR

info_msg() { printf "[\033[34mINFO\033[0m] %b\n" "$*"; }
error_msg() { printf "[\033[31mERROR\033[0m] %b\n" "$*"; }

rm_containters() {
docker kill rdq-test >/dev/null 2>&1 || true
docker kill rdq-busybox >/dev/null 2>&1 || true
docker kill rdq-rabbitmq >/dev/null 2>&1 || true
}

errors_found() {
error_msg "$0 didn't finish correctly: rc [\033[31m$?\033[0m]"
}

info_msg "Build rabbitmq-dump-queue test image"
"${DIRNAME}/build_container" --target test >/dev/null 2>&1

info_msg "Start rabbitmq container"
docker run --network=host --rm -q \
--detach \
--name rdq-rabbitmq \
rabbitmq:4-management >/dev/null

info_msg "Wait for rabbitmq listener up to [\033[32m${RABBITMQ_START_TIMEOUT}s\033[0m]"
docker run --network=host --rm -q \
--name rdq-busybox \
-e N="$RABBITMQ_START_TIMEOUT" \
busybox \
sh -c 'while [ $N -ge 0 ]; do nc -z localhost 5672 >/dev/null && exit 0; sleep 1; N=$((N - 1)); done; exit 1'

info_msg "Run tests"
docker run --network=host --rm \
--name rdq-test\
rabbitmq-dump-queue:test