From b4a8c4f0ec69773e4667dbdabecb07d71f6dc6fe Mon Sep 17 00:00:00 2001 From: Clement Michaud Date: Sun, 8 Dec 2019 16:51:12 +0100 Subject: [PATCH] Introduce version command to Authelia to check the version The version command displays the tag and the commit hash of the built commit along with the time when the build was done. --- Dockerfile | 12 +++++- Dockerfile.arm32v7 | 12 +++++- Dockerfile.arm64v8 | 12 +++++- cmd/authelia-scripts/cmd_docker.go | 19 ++++++++- cmd/authelia-scripts/cmd_serve.go | 2 +- cmd/authelia-scripts/docker.go | 6 ++- cmd/authelia/constants.go | 5 +++ cmd/authelia/main.go | 48 +++++++++++++++-------- docs/configuration.md | 2 +- docs/deployment-dev.md | 2 +- docs/deployment-production.md | 4 +- example/compose/authelia/resources/run.sh | 4 +- 12 files changed, 97 insertions(+), 31 deletions(-) create mode 100644 cmd/authelia/constants.go diff --git a/Dockerfile b/Dockerfile index 02a7554e2..c5f156597 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,9 @@ # ======================================= FROM golang:1.13-alpine AS builder-backend +ARG BUILD_TAG +ARG BUILD_COMMIT + # gcc and musl-dev are required for building go-sqlite3 RUN apk --no-cache add gcc musl-dev @@ -16,6 +19,13 @@ RUN go mod download COPY cmd cmd COPY internal internal +# Set the build version and time +RUN echo "Write tag ${BUILD_TAG} and commit ${BUILD_COMMIT} in binary." && \ + BUILD_TIME=`date +"%Y-%m-%d %T"` && \ + sed -i "s/__BUILD_TAG__/${BUILD_TAG}/" cmd/authelia/constants.go && \ + sed -i "s/__BUILD_COMMIT__/${BUILD_COMMIT}/" cmd/authelia/constants.go && \ + sed -i "s/__BUILD_TIME__/${BUILD_TIME}/" cmd/authelia/constants.go + # CGO_ENABLED=1 is mandatory for building go-sqlite3 RUN cd cmd/authelia && GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -tags netgo -ldflags '-w' -o authelia @@ -48,4 +58,4 @@ EXPOSE 9091 VOLUME /etc/authelia VOLUME /var/lib/authelia -CMD ["./authelia", "-config", "/etc/authelia/configuration.yml"] +CMD ["./authelia", "--config", "/etc/authelia/configuration.yml"] diff --git a/Dockerfile.arm32v7 b/Dockerfile.arm32v7 index 689c06fea..3e78508b4 100644 --- a/Dockerfile.arm32v7 +++ b/Dockerfile.arm32v7 @@ -3,6 +3,9 @@ # ======================================= FROM arm32v7/golang:1.13-alpine AS builder-backend +ARG BUILD_TAG +ARG BUILD_COMMIT + # qemu binary, gcc and musl-dev are required for building go-sqlite3 COPY ./qemu-arm-static /usr/bin/qemu-arm-static RUN apk --no-cache add gcc musl-dev @@ -17,6 +20,13 @@ RUN go mod download COPY cmd cmd COPY internal internal +# Set the build version and time +RUN echo "Write tag ${BUILD_TAG} and commit ${BUILD_COMMIT} in binary." && \ + BUILD_TIME=`date +"%Y-%m-%d %T"` && \ + sed -i "s/__BUILD_TAG__/${BUILD_TAG}/" cmd/authelia/constants.go && \ + sed -i "s/__BUILD_COMMIT__/${BUILD_COMMIT}/" cmd/authelia/constants.go && \ + sed -i "s/__BUILD_TIME__/${BUILD_TIME}/" cmd/authelia/constants.go + # CGO_ENABLED=1 is mandatory for building go-sqlite3 RUN cd cmd/authelia && GOOS=linux GOARCH=arm CGO_ENABLED=1 go build -tags netgo -ldflags '-w' -o authelia @@ -52,4 +62,4 @@ EXPOSE 9091 VOLUME /etc/authelia VOLUME /var/lib/authelia -CMD ["./authelia", "-config", "/etc/authelia/configuration.yml"] +CMD ["./authelia", "--config", "/etc/authelia/configuration.yml"] diff --git a/Dockerfile.arm64v8 b/Dockerfile.arm64v8 index 871a334a4..da11d2c2f 100644 --- a/Dockerfile.arm64v8 +++ b/Dockerfile.arm64v8 @@ -3,6 +3,9 @@ # ======================================= FROM arm64v8/golang:1.13-alpine AS builder-backend +ARG BUILD_TAG +ARG BUILD_COMMIT + # qemu binary, gcc and musl-dev are required for building go-sqlite3 COPY ./qemu-aarch64-static /usr/bin/qemu-aarch64-static RUN apk --no-cache add gcc musl-dev @@ -17,6 +20,13 @@ RUN go mod download COPY cmd cmd COPY internal internal +# Set the build version and time +RUN echo "Write tag ${BUILD_TAG} and commit ${BUILD_COMMIT} in binary." && \ + BUILD_TIME=`date +"%Y-%m-%d %T"` && \ + sed -i "s/__BUILD_TAG__/${BUILD_TAG}/" cmd/authelia/constants.go && \ + sed -i "s/__BUILD_COMMIT__/${BUILD_COMMIT}/" cmd/authelia/constants.go && \ + sed -i "s/__BUILD_TIME__/${BUILD_TIME}/" cmd/authelia/constants.go + # CGO_ENABLED=1 is mandatory for building go-sqlite3 RUN cd cmd/authelia && GOOS=linux GOARCH=arm64 CGO_ENABLED=1 go build -tags netgo -ldflags '-w' -o authelia @@ -52,4 +62,4 @@ EXPOSE 9091 VOLUME /etc/authelia VOLUME /var/lib/authelia -CMD ["./authelia", "-config", "/etc/authelia/configuration.yml"] +CMD ["./authelia", "--config", "/etc/authelia/configuration.yml"] diff --git a/cmd/authelia-scripts/cmd_docker.go b/cmd/authelia-scripts/cmd_docker.go index 82c9e513c..f7ab41faa 100644 --- a/cmd/authelia-scripts/cmd_docker.go +++ b/cmd/authelia-scripts/cmd_docker.go @@ -13,6 +13,7 @@ import ( ) var arch string + var supportedArch = []string{"amd64", "arm32v7", "arm64v8"} var defaultArch = "amd64" var travisBranch = os.Getenv("TRAVIS_BRANCH") @@ -25,7 +26,6 @@ var tags = dockerTags.FindStringSubmatch(travisTag) func init() { DockerBuildCmd.PersistentFlags().StringVar(&arch, "arch", defaultArch, "target architecture among: "+strings.Join(supportedArch, ", ")) DockerPushCmd.PersistentFlags().StringVar(&arch, "arch", defaultArch, "target architecture among: "+strings.Join(supportedArch, ", ")) - } func checkArchIsSupported(arch string) { @@ -75,7 +75,22 @@ func dockerBuildOfficialImage(arch string) error { } } - return docker.Build(IntermediateDockerImageName, dockerfile, ".") + gitTag := travisTag + if gitTag == "" { + // If commit is not tagged, mark the build has having unknown tag. + gitTag = "unknown" + } + + cmd := utils.Shell("git rev-parse HEAD") + cmd.Stdout = nil + cmd.Stderr = nil + commitBytes, err := cmd.Output() + if err != nil { + log.Fatal(err) + } + commitHash := strings.Trim(string(commitBytes), "\n") + + return docker.Build(IntermediateDockerImageName, dockerfile, ".", gitTag, commitHash) } // DockerBuildCmd Command for building docker image of Authelia. diff --git a/cmd/authelia-scripts/cmd_serve.go b/cmd/authelia-scripts/cmd_serve.go index ed7917a43..6c4ae8fa4 100644 --- a/cmd/authelia-scripts/cmd_serve.go +++ b/cmd/authelia-scripts/cmd_serve.go @@ -11,7 +11,7 @@ import ( // ServeCmd serve authelia with the provided configuration func ServeCmd(cobraCmd *cobra.Command, args []string) { log.Infof("Running Authelia with config %s...", args[0]) - cmd := utils.CommandWithStdout(OutputDir+"/authelia", "-config", args[0]) + cmd := utils.CommandWithStdout(OutputDir+"/authelia", "--config", args[0]) cmd.Env = append(os.Environ(), "PUBLIC_DIR=dist/public_html") utils.RunCommandUntilCtrlC(cmd) } diff --git a/cmd/authelia-scripts/docker.go b/cmd/authelia-scripts/docker.go index cd4b9e2f3..ccc4e204c 100644 --- a/cmd/authelia-scripts/docker.go +++ b/cmd/authelia-scripts/docker.go @@ -8,8 +8,10 @@ import ( type Docker struct{} // Build build a docker image -func (d *Docker) Build(tag, dockerfile, target string) error { - return utils.CommandWithStdout("docker", "build", "-t", tag, "-f", dockerfile, target).Run() +func (d *Docker) Build(tag, dockerfile, target, gitTag, gitCommit string) error { + return utils.CommandWithStdout( + "docker", "build", "-t", tag, "-f", dockerfile, "--build-arg", + "BUILD_TAG="+gitTag, "--build-arg", "BUILD_COMMIT="+gitCommit, target).Run() } // Tag tag a docker image. diff --git a/cmd/authelia/constants.go b/cmd/authelia/constants.go new file mode 100644 index 000000000..2317e6c38 --- /dev/null +++ b/cmd/authelia/constants.go @@ -0,0 +1,5 @@ +package main + +var BuildTag = "__BUILD_TAG__" +var BuildCommit = "__BUILD_COMMIT__" +var BuildTime = "__BUILD_TIME__" diff --git a/cmd/authelia/main.go b/cmd/authelia/main.go index a5ceb3b96..629e168c7 100644 --- a/cmd/authelia/main.go +++ b/cmd/authelia/main.go @@ -2,7 +2,7 @@ package main import ( "errors" - "flag" + "fmt" "log" "os" @@ -18,33 +18,24 @@ import ( "github.com/clems4ever/authelia/internal/storage" "github.com/clems4ever/authelia/internal/utils" "github.com/sirupsen/logrus" + "github.com/spf13/cobra" ) -func tryExtractConfigPath() (string, error) { - configPtr := flag.String("config", "", "The path to a configuration file.") - flag.Parse() +var configPathFlag string - if *configPtr == "" { - return "", errors.New("No config file path provided") +func startServer() { + if configPathFlag == "" { + log.Fatal(errors.New("No config file path provided")) } - return *configPtr, nil -} - -func main() { if os.Getenv("ENVIRONMENT") == "dev" { logging.Logger().Info("===> Authelia is running in development mode. <===") } - configPath, err := tryExtractConfigPath() - if err != nil { - logging.Logger().Error(err) - } - - config, errs := configuration.Read(configPath) + config, errs := configuration.Read(configPathFlag) if len(errs) > 0 { - for _, err = range errs { + for _, err := range errs { logging.Logger().Error(err) } panic(errors.New("Some errors have been reported")) @@ -109,3 +100,26 @@ func main() { } server.StartServer(*config, providers) } + +func main() { + rootCmd := &cobra.Command{ + Use: "authelia", + Run: func(cmd *cobra.Command, args []string) { + startServer() + }, + } + + rootCmd.Flags().StringVar(&configPathFlag, "config", "", "Configuration file") + + versionCmd := &cobra.Command{ + Use: "version", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("build git tag: %s\n", BuildTag) + fmt.Printf("build git commit: %s\n", BuildCommit) + fmt.Printf("build time: %s\n", BuildTime) + }, + } + + rootCmd.AddCommand(versionCmd) + rootCmd.Execute() +} diff --git a/docs/configuration.md b/docs/configuration.md index 45aca28cb..b86f6c7a3 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -8,5 +8,5 @@ repository. All the details are documented there. When running **Authelia**, you can specify your configuration file by passing the file path as the first argument of **Authelia**. - $ authelia -config config.custom.yml + $ authelia --config config.custom.yml diff --git a/docs/deployment-dev.md b/docs/deployment-dev.md index 80343ad34..dd68623a5 100644 --- a/docs/deployment-dev.md +++ b/docs/deployment-dev.md @@ -27,7 +27,7 @@ either by pulling the Docker image or building distributable version. ## Build and deploy the distributable version $ authelia-scripts build - $ PUBLIC_DIR=./dist/public_html ./dist/authelia -config /path/to/your/config.yml + $ PUBLIC_DIR=./dist/public_html ./dist/authelia --config /path/to/your/config.yml ## Deploy with Docker diff --git a/docs/deployment-production.md b/docs/deployment-production.md index a86db6e8e..b0c7b568b 100644 --- a/docs/deployment-production.md +++ b/docs/deployment-production.md @@ -32,11 +32,11 @@ the root of the repo. # Build it if not done already $ authelia-scripts build - $ PUBLIC_DIR=./dist/public_html authelia -config /path/to/your/config.yml + $ PUBLIC_DIR=./dist/public_html authelia --config /path/to/your/config.yml ### Deploy With Docker - $ docker run -v /path/to/your/config.yml:/etc/authelia/config.yml -e TZ=Europe/Paris clems4ever/$ $ authelia -config /etc/authelia/config.yml + $ docker run -v /path/to/your/config.yml:/etc/authelia/config.yml -e TZ=Europe/Paris clems4ever/$ $ authelia --config /etc/authelia/config.yml ## On top of Kubernetes diff --git a/example/compose/authelia/resources/run.sh b/example/compose/authelia/resources/run.sh index 8a0c1a007..b828b5792 100755 --- a/example/compose/authelia/resources/run.sh +++ b/example/compose/authelia/resources/run.sh @@ -3,10 +3,10 @@ set -e # Build the binary -go build -o /tmp/authelia/authelia-tmp cmd/authelia/main.go +go build -o /tmp/authelia/authelia-tmp cmd/authelia/*.go while true; do - /tmp/authelia/authelia-tmp -config /etc/authelia/configuration.yml + /tmp/authelia/authelia-tmp --config /etc/authelia/configuration.yml sleep 10 done \ No newline at end of file