Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #15 from barcus/dsn-using-var
Browse files Browse the repository at this point in the history
Ease usage with docker entrypoint and env vars
  • Loading branch information
vierbergenlars authored Apr 4, 2022
2 parents 1803d6c + 1132fd4 commit b3ad52e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
31 changes: 25 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
FROM golang as builder
COPY . /go/src/github.com/vierbergenlars/bareos_exporter
FROM golang:1.18.0 as builder

RUN apt-get update \
&& apt-get install -y upx

WORKDIR /go/src/github.com/vierbergenlars/bareos_exporter
COPY . .

RUN go get -v .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o bareos_exporter .
RUN upx bareos_exporter

FROM busybox:latest

FROM busybox:1.34.1
WORKDIR /bareos_exporter
COPY --from=builder /go/src/github.com/vierbergenlars/bareos_exporter/bareos_exporter bareos_exporter
COPY entrypoint.sh /entrypoint.sh
RUN chmod u+x /entrypoint.sh

ENV PORT 9625
ENV DB_TYPE postgres
ENV DB_HOST localhost
ENV DB_PORT 5432
ENV DB_NAME bareos
ENV DB_USER bareos
ENV SSL_MODE disable
ENV ENDPOINT /metrics
ENV JOB_DAYS 7
ENV WAIT_FOR_DB 0

ENTRYPOINT ["./bareos_exporter"]
EXPOSE $port
EXPOSE $PORT
ENTRYPOINT ["/entrypoint.sh"]
CMD ["./bareos_exporter"]
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,41 @@
## [`Dockerfile`](./Dockerfile)

## Usage with [docker](https://hub.docker.com/r/vierbergenlnars/bareos_exporter)
1. Create a file containing your mysql password and mount it inside `/bareos_exporter/pw/auth`
2. **(optional)** [Overwrite](https://docs.docker.com/engine/reference/run/#env-environment-variables) default args using ENV variables
3. Run docker image as follows

1. Create a .env file containing at least your database password using `DB_PASSWORD` variable.
2. Run docker image as follows

### Using environment variables

```bash
docker run --name bareos_exporter -p 9625:9625 -e DB_TYPE=mysql -e DB_NAME=bareos-dir -d vierbergenlars/bareos_exporter:latest
```

Default variables:

* `PORT` 9625 (exporter)
* `DB_TYPE` postgres
* `DB_HOST` localhost
* `DB_PORT` 5432 (postgres)
* `DB_USER` bareos
* `DB_NAME` bareos
* `SSL_MODE` disable (postgres only)
* `ENDPOINT` /metrics
* `JOB_DAYS` 7
* `WAIT_FOR_DB` 0

### Using custom Docker CMD

```bash
docker run --name bareos_exporter -p 9625:9625 -d vierbergenlars/bareos_exporter:latest -dsn mysql://user:password@host/dbname
docker run --name bareos_exporter -p 9625:9625 -d vierbergenlars/bareos_exporter:latest -dsn 'mysql://login:password@tcp(host:port)/dbname?parseTime=true'
```

### Troubleshooting

```bash
docker run --name bareos_exporter -t -i --rm --entrypoint=/bin/sh vierbergenlars/bareos_exporter:latest
```

## Metrics

### Aggregated metrics for all jobs
Expand Down
24 changes: 24 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/env sh
#set -x

# Wait for DB (default 0)
sleep ${WAIT_FOR_DB}

# Run Dockerfile CMD
if [ "$1" == './bareos_exporter' ]; then
if [ -z "${DB_TYPE}" ]; then
echo 'Error: DB_TYPE is empty'
exit 1
fi
if [ -z "${DB_PASSWORD}" ]; then
echo 'Warning: DB_PASSWORD is empty'
fi
if [ "${DB_TYPE}" == 'postgres' ]; then
exec ./bareos_exporter -endpoint ${ENDPOINT} -job-discovery-days ${JOB_DAYS} -port ${PORT} -dsn "${DB_TYPE}://host=${DB_HOST} dbname=${DB_NAME} sslmode=${SSL_MODE} user=${DB_USER} password=${DB_PASSWORD} port=${DB_PORT}"
fi
if [ "${DB_TYPE}" == 'mysql' ]; then
exec ./bareos_exporter -endpoint ${ENDPOINT} -job-discovery-days ${JOB_DAYS} -port ${PORT} -dsn "${DB_TYPE}://${DB_USER}:${DB_PASSWORD}@tcp(${DB_HOST}:${DB_PORT})/${DB_NAME}?parseTime=true"
fi
else
exec ./bareos_exporter "$@"
fi

0 comments on commit b3ad52e

Please sign in to comment.