From c51c6800d62d680db547d973a6cfa89822245bec Mon Sep 17 00:00:00 2001 From: smainz Date: Sat, 17 Feb 2024 20:06:24 +0100 Subject: [PATCH 1/5] Read datasource config from file when run in docker The location of the sqlite3 database file is defined in a config file. The location of the config file is provided to the woodpecker-server in an ENV var. This is done instead of providing the location directly inside an ENV var, as the config file has the least precedence, so it can be overriden in a docker-compose.yml file. Remember: The config file must not have a line break at the end! --- docker/Dockerfile.server.alpine.multiarch | 4 +++- docker/Dockerfile.server.multiarch | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docker/Dockerfile.server.alpine.multiarch b/docker/Dockerfile.server.alpine.multiarch index c2513ab356..af0a02c86e 100644 --- a/docker/Dockerfile.server.alpine.multiarch +++ b/docker/Dockerfile.server.alpine.multiarch @@ -6,7 +6,9 @@ ENV CA_CERTIFICATES_VERSION="20230506-r0" ARG TARGETOS TARGETARCH RUN apk add -U --no-cache ca-certificates=${CA_CERTIFICATES_VERSION} ENV GODEBUG=netdns=go -ENV WOODPECKER_DATABASE_DATASOURCE=/var/lib/woodpecker/woodpecker.sqlite +# create datasource config file +RUN echo -n "/var/lib/woodpecker/woodpecker.sqlite" > /etc/woodpecker-datasource.conf +ENV WOODPECKER_DATABASE_DATASOURCE_FILE=/etc/woodpecker-datasource.conf ENV WOODPECKER_DATABASE_DRIVER=sqlite3 ENV XDG_CACHE_HOME=/var/lib/woodpecker ENV XDG_DATA_HOME=/var/lib/woodpecker diff --git a/docker/Dockerfile.server.multiarch b/docker/Dockerfile.server.multiarch index 28afba5199..00621d6f92 100644 --- a/docker/Dockerfile.server.multiarch +++ b/docker/Dockerfile.server.multiarch @@ -1,14 +1,20 @@ FROM --platform=$BUILDPLATFORM docker.io/golang:1.22 AS certs +FROM docker.io/alpine:3.19 AS conf +# create datasource config file +RUN echo -n "/var/lib/woodpecker/woodpecker.sqlite" > /etc/woodpecker-datasource.conf + FROM scratch ARG TARGETOS TARGETARCH ENV GODEBUG=netdns=go -ENV WOODPECKER_DATABASE_DATASOURCE=/var/lib/woodpecker/woodpecker.sqlite +ENV WOODPECKER_DATABASE_DATASOURCE_FILE=/etc/woodpecker-datasource.conf ENV WOODPECKER_DATABASE_DRIVER=sqlite3 ENV XDG_CACHE_HOME=/var/lib/woodpecker ENV XDG_DATA_HOME=/var/lib/woodpecker EXPOSE 8000 9000 80 443 +# copy datasource config from conf image +COPY --from=conf /etc/woodpecker-datasource.conf /etc/woodpecker-datasource.conf # copy certs from certs image COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt # copy server binary From 4020b949c60bab233c4e7d32f03991fe6dd2b4c8 Mon Sep 17 00:00:00 2001 From: smainz Date: Thu, 22 Feb 2024 11:51:54 +0100 Subject: [PATCH 2/5] Change config file location to be consistent with agent. --- docker/Dockerfile.server.alpine.multiarch | 5 +++-- docker/Dockerfile.server.multiarch | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/docker/Dockerfile.server.alpine.multiarch b/docker/Dockerfile.server.alpine.multiarch index af0a02c86e..26cbd54861 100644 --- a/docker/Dockerfile.server.alpine.multiarch +++ b/docker/Dockerfile.server.alpine.multiarch @@ -7,8 +7,9 @@ ARG TARGETOS TARGETARCH RUN apk add -U --no-cache ca-certificates=${CA_CERTIFICATES_VERSION} ENV GODEBUG=netdns=go # create datasource config file -RUN echo -n "/var/lib/woodpecker/woodpecker.sqlite" > /etc/woodpecker-datasource.conf -ENV WOODPECKER_DATABASE_DATASOURCE_FILE=/etc/woodpecker-datasource.conf +RUN mkdir -p /etc/woodpecker && \ + echo -n "/var/lib/woodpecker/woodpecker.sqlite" > /etc/woodpecker/datasource.conf +ENV WOODPECKER_DATABASE_DATASOURCE_FILE=/etc/woodpecker/datasource.conf ENV WOODPECKER_DATABASE_DRIVER=sqlite3 ENV XDG_CACHE_HOME=/var/lib/woodpecker ENV XDG_DATA_HOME=/var/lib/woodpecker diff --git a/docker/Dockerfile.server.multiarch b/docker/Dockerfile.server.multiarch index 00621d6f92..0cade43703 100644 --- a/docker/Dockerfile.server.multiarch +++ b/docker/Dockerfile.server.multiarch @@ -2,19 +2,20 @@ FROM --platform=$BUILDPLATFORM docker.io/golang:1.22 AS certs FROM docker.io/alpine:3.19 AS conf # create datasource config file -RUN echo -n "/var/lib/woodpecker/woodpecker.sqlite" > /etc/woodpecker-datasource.conf +RUN mkdir -p /etc/woodpecker && \ + echo -n "/var/lib/woodpecker/woodpecker.sqlite" > /etc/woodpecker/datasource.conf FROM scratch ARG TARGETOS TARGETARCH ENV GODEBUG=netdns=go -ENV WOODPECKER_DATABASE_DATASOURCE_FILE=/etc/woodpecker-datasource.conf +ENV WOODPECKER_DATABASE_DATASOURCE_FILE=/etc/woodpecker/datasource.conf ENV WOODPECKER_DATABASE_DRIVER=sqlite3 ENV XDG_CACHE_HOME=/var/lib/woodpecker ENV XDG_DATA_HOME=/var/lib/woodpecker EXPOSE 8000 9000 80 443 # copy datasource config from conf image -COPY --from=conf /etc/woodpecker-datasource.conf /etc/woodpecker-datasource.conf +COPY --from=conf /etc/woodpecker/datasource.conf /etc/woodpecker/datasource.conf # copy certs from certs image COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt # copy server binary From ed6a7a17345b65d947de330f66ba311338014b78 Mon Sep 17 00:00:00 2001 From: smainz Date: Thu, 22 Feb 2024 17:06:02 +0100 Subject: [PATCH 3/5] Set default datasource to /var/lib/woodpecker/woodpecker.sql if running inside container --- cmd/server/flags.go | 13 ++++++++++++- docker/Dockerfile.server.alpine.multiarch | 7 ++----- docker/Dockerfile.server.multiarch | 11 ++--------- docs/docs/30-administration/10-server-config.md | 2 +- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/cmd/server/flags.go b/cmd/server/flags.go index 8d79986cf2..d501b01ddc 100644 --- a/cmd/server/flags.go +++ b/cmd/server/flags.go @@ -219,7 +219,7 @@ var flags = append([]cli.Flag{ EnvVars: []string{"WOODPECKER_DATABASE_DATASOURCE"}, Name: "datasource", Usage: "database driver configuration string", - Value: "woodpecker.sqlite", + Value: datasourceDefaultValue(), FilePath: os.Getenv("WOODPECKER_DATABASE_DATASOURCE_FILE"), }, &cli.StringFlag{ @@ -473,3 +473,14 @@ var flags = append([]cli.Flag{ Usage: "Flag to decrypt all encrypted data and disable encryption on server", }, }, logger.GlobalLoggerFlags...) + +// If woodpecker is running inside a container the default value for +// the datasource is different from running outside a container. +func datasourceDefaultValue() string { + _, found := os.LookupEnv("WOODPECKER_IN_CONTAINER") + if found { + return "/var/lib/woodpecker/woodpecker.sqlite" + } else { + return "woodpecker.sqlite" + } +} diff --git a/docker/Dockerfile.server.alpine.multiarch b/docker/Dockerfile.server.alpine.multiarch index 26cbd54861..2f9cd674a4 100644 --- a/docker/Dockerfile.server.alpine.multiarch +++ b/docker/Dockerfile.server.alpine.multiarch @@ -6,11 +6,8 @@ ENV CA_CERTIFICATES_VERSION="20230506-r0" ARG TARGETOS TARGETARCH RUN apk add -U --no-cache ca-certificates=${CA_CERTIFICATES_VERSION} ENV GODEBUG=netdns=go -# create datasource config file -RUN mkdir -p /etc/woodpecker && \ - echo -n "/var/lib/woodpecker/woodpecker.sqlite" > /etc/woodpecker/datasource.conf -ENV WOODPECKER_DATABASE_DATASOURCE_FILE=/etc/woodpecker/datasource.conf -ENV WOODPECKER_DATABASE_DRIVER=sqlite3 +# Signal that woodpecker is running inside a container +ENV WOODPECKER_IN_CONTAINER=true ENV XDG_CACHE_HOME=/var/lib/woodpecker ENV XDG_DATA_HOME=/var/lib/woodpecker EXPOSE 8000 9000 80 443 diff --git a/docker/Dockerfile.server.multiarch b/docker/Dockerfile.server.multiarch index 0cade43703..4ee5f0b149 100644 --- a/docker/Dockerfile.server.multiarch +++ b/docker/Dockerfile.server.multiarch @@ -1,21 +1,14 @@ FROM --platform=$BUILDPLATFORM docker.io/golang:1.22 AS certs -FROM docker.io/alpine:3.19 AS conf -# create datasource config file -RUN mkdir -p /etc/woodpecker && \ - echo -n "/var/lib/woodpecker/woodpecker.sqlite" > /etc/woodpecker/datasource.conf - FROM scratch ARG TARGETOS TARGETARCH ENV GODEBUG=netdns=go -ENV WOODPECKER_DATABASE_DATASOURCE_FILE=/etc/woodpecker/datasource.conf -ENV WOODPECKER_DATABASE_DRIVER=sqlite3 +# Signal that woodpecker is running inside a container +ENV WOODPECKER_IN_CONTAINER=true ENV XDG_CACHE_HOME=/var/lib/woodpecker ENV XDG_DATA_HOME=/var/lib/woodpecker EXPOSE 8000 9000 80 443 -# copy datasource config from conf image -COPY --from=conf /etc/woodpecker/datasource.conf /etc/woodpecker/datasource.conf # copy certs from certs image COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt # copy server binary diff --git a/docs/docs/30-administration/10-server-config.md b/docs/docs/30-administration/10-server-config.md index 7d38c75a2d..f9cf1fe946 100644 --- a/docs/docs/30-administration/10-server-config.md +++ b/docs/docs/30-administration/10-server-config.md @@ -419,7 +419,7 @@ The database driver name. Possible values are `sqlite3`, `mysql` or `postgres`. ### `WOODPECKER_DATABASE_DATASOURCE` -> Default: `woodpecker.sqlite` +> Default: `woodpecker.sqlite` if not running inside a container, `/var/lib/woodpecker/woodpecker.sqlite` if running inside a container The database connection string. The default value is the path of the embedded SQLite database file. From f41345b79931c0638f8fd2b27d9fd47a609cd2ac Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 26 Feb 2024 19:35:53 +0100 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: Anbraten <6918444+anbraten@users.noreply.github.com> --- docker/Dockerfile.server.alpine.multiarch | 2 +- docker/Dockerfile.server.multiarch | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/Dockerfile.server.alpine.multiarch b/docker/Dockerfile.server.alpine.multiarch index 2f9cd674a4..2454b557d0 100644 --- a/docker/Dockerfile.server.alpine.multiarch +++ b/docker/Dockerfile.server.alpine.multiarch @@ -6,7 +6,7 @@ ENV CA_CERTIFICATES_VERSION="20230506-r0" ARG TARGETOS TARGETARCH RUN apk add -U --no-cache ca-certificates=${CA_CERTIFICATES_VERSION} ENV GODEBUG=netdns=go -# Signal that woodpecker is running inside a container +# Internal setting do NOT change! Signals that woodpecker is running inside a container ENV WOODPECKER_IN_CONTAINER=true ENV XDG_CACHE_HOME=/var/lib/woodpecker ENV XDG_DATA_HOME=/var/lib/woodpecker diff --git a/docker/Dockerfile.server.multiarch b/docker/Dockerfile.server.multiarch index 4ee5f0b149..3bf86a3bea 100644 --- a/docker/Dockerfile.server.multiarch +++ b/docker/Dockerfile.server.multiarch @@ -3,7 +3,7 @@ FROM --platform=$BUILDPLATFORM docker.io/golang:1.22 AS certs FROM scratch ARG TARGETOS TARGETARCH ENV GODEBUG=netdns=go -# Signal that woodpecker is running inside a container +# Internal setting do NOT change! Signals that woodpecker is running inside a container ENV WOODPECKER_IN_CONTAINER=true ENV XDG_CACHE_HOME=/var/lib/woodpecker ENV XDG_DATA_HOME=/var/lib/woodpecker From 893c39aa8188107e90c61132694172bc9e96cc2c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 26 Feb 2024 19:37:23 +0100 Subject: [PATCH 5/5] address lint --- cmd/server/flags.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/server/flags.go b/cmd/server/flags.go index cd8c786144..d3356ac73f 100644 --- a/cmd/server/flags.go +++ b/cmd/server/flags.go @@ -517,7 +517,6 @@ func datasourceDefaultValue() string { _, found := os.LookupEnv("WOODPECKER_IN_CONTAINER") if found { return "/var/lib/woodpecker/woodpecker.sqlite" - } else { - return "woodpecker.sqlite" } + return "woodpecker.sqlite" }