2019-10-29 20:54:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-11-22 02:02:06 +00:00
|
|
|
"regexp"
|
2019-11-07 00:59:24 +00:00
|
|
|
"strings"
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/utils"
|
2019-11-02 14:32:58 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-10-29 20:54:47 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-11-07 00:59:24 +00:00
|
|
|
var arch string
|
2019-12-08 15:51:12 +00:00
|
|
|
|
2020-04-03 05:13:24 +00:00
|
|
|
var supportedArch = []string{"amd64", "arm32v7", "arm64v8", "darwin"}
|
2019-11-07 00:59:24 +00:00
|
|
|
var defaultArch = "amd64"
|
2019-12-27 11:07:53 +00:00
|
|
|
var buildkiteQEMU = os.Getenv("BUILDKITE_AGENT_META_DATA_QEMU")
|
2020-01-16 20:57:44 +00:00
|
|
|
var ciBranch = os.Getenv("BUILDKITE_BRANCH")
|
|
|
|
var ciPullRequest = os.Getenv("BUILDKITE_PULL_REQUEST")
|
|
|
|
var ciTag = os.Getenv("BUILDKITE_TAG")
|
2019-12-10 08:21:54 +00:00
|
|
|
var dockerTags = regexp.MustCompile(`v(?P<Patch>(?P<Minor>(?P<Major>\d+)\.\d+)\.\d+.*)`)
|
2019-11-28 10:58:17 +00:00
|
|
|
var ignoredSuffixes = regexp.MustCompile("alpha|beta")
|
2020-01-30 07:37:11 +00:00
|
|
|
var publicRepo = regexp.MustCompile(`.*\:.*`)
|
2020-01-16 20:57:44 +00:00
|
|
|
var tags = dockerTags.FindStringSubmatch(ciTag)
|
2019-11-07 00:59:24 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
for _, a := range supportedArch {
|
|
|
|
if arch == a {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Fatal("Architecture is not supported. Please select one of " + strings.Join(supportedArch, ", ") + ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
func dockerBuildOfficialImage(arch string) error {
|
2019-11-03 12:07:02 +00:00
|
|
|
docker := &Docker{}
|
2019-11-07 00:59:24 +00:00
|
|
|
// Set default Architecture Dockerfile to amd64
|
|
|
|
dockerfile := "Dockerfile"
|
2019-12-03 03:19:49 +00:00
|
|
|
// Set version of QEMU
|
2020-03-06 08:40:56 +00:00
|
|
|
qemuversion := "v4.2.0-6"
|
2019-11-07 00:59:24 +00:00
|
|
|
|
|
|
|
// If not the default value
|
|
|
|
if arch != defaultArch {
|
|
|
|
dockerfile = fmt.Sprintf("%s.%s", dockerfile, arch)
|
|
|
|
}
|
|
|
|
|
|
|
|
if arch == "arm32v7" {
|
2019-12-27 11:07:53 +00:00
|
|
|
if buildkiteQEMU != "true" {
|
|
|
|
err := utils.CommandWithStdout("docker", "run", "--rm", "--privileged", "multiarch/qemu-user-static", "--reset", "-p", "yes").Run()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-07 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 11:07:53 +00:00
|
|
|
err := utils.CommandWithStdout("bash", "-c", "wget https://github.com/multiarch/qemu-user-static/releases/download/"+qemuversion+"/qemu-arm-static -O ./qemu-arm-static && chmod +x ./qemu-arm-static").Run()
|
2019-11-07 00:59:24 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else if arch == "arm64v8" {
|
2019-12-27 11:07:53 +00:00
|
|
|
if buildkiteQEMU != "true" {
|
|
|
|
err := utils.CommandWithStdout("docker", "run", "--rm", "--privileged", "multiarch/qemu-user-static", "--reset", "-p", "yes").Run()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-07 00:59:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 11:07:53 +00:00
|
|
|
err := utils.CommandWithStdout("bash", "-c", "wget https://github.com/multiarch/qemu-user-static/releases/download/"+qemuversion+"/qemu-aarch64-static -O ./qemu-aarch64-static && chmod +x ./qemu-aarch64-static").Run()
|
2019-11-07 00:59:24 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 11:07:53 +00:00
|
|
|
gitTag := ciTag
|
2019-12-08 15:51:12 +00:00
|
|
|
if gitTag == "" {
|
2020-01-13 00:30:05 +00:00
|
|
|
// If commit is not tagged, mark the build has having master tag.
|
|
|
|
gitTag = "master"
|
2019-12-08 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-11-03 12:07:02 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 20:54:47 +00:00
|
|
|
// DockerBuildCmd Command for building docker image of Authelia.
|
|
|
|
var DockerBuildCmd = &cobra.Command{
|
|
|
|
Use: "build",
|
|
|
|
Short: "Build the docker image of Authelia",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Infof("Building Docker image %s...", DockerImageName)
|
2019-11-07 00:59:24 +00:00
|
|
|
checkArchIsSupported(arch)
|
|
|
|
err := dockerBuildOfficialImage(arch)
|
2019-11-03 12:07:02 +00:00
|
|
|
|
2019-10-29 20:54:47 +00:00
|
|
|
if err != nil {
|
2019-11-03 12:07:02 +00:00
|
|
|
log.Fatal(err)
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 12:07:02 +00:00
|
|
|
docker := &Docker{}
|
2019-10-29 20:54:47 +00:00
|
|
|
err = docker.Tag(IntermediateDockerImageName, DockerImageName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-01-16 20:57:44 +00:00
|
|
|
// DockerPushCmd Command for pushing Authelia docker image to Docker Hub
|
2019-10-29 20:54:47 +00:00
|
|
|
var DockerPushCmd = &cobra.Command{
|
2019-11-07 00:59:24 +00:00
|
|
|
Use: "push-image",
|
2020-01-16 20:57:44 +00:00
|
|
|
Short: "Publish Authelia docker image to Docker Hub",
|
2019-10-29 20:54:47 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2020-01-16 20:57:44 +00:00
|
|
|
log.Infof("Pushing Docker image %s to Docker Hub...", DockerImageName)
|
2019-11-07 00:59:24 +00:00
|
|
|
checkArchIsSupported(arch)
|
|
|
|
publishDockerImage(arch)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-01-16 20:57:44 +00:00
|
|
|
// DockerManifestCmd Command for pushing Authelia docker manifest to Docker Hub
|
2019-11-07 00:59:24 +00:00
|
|
|
var DockerManifestCmd = &cobra.Command{
|
|
|
|
Use: "push-manifest",
|
2020-01-16 20:57:44 +00:00
|
|
|
Short: "Publish Authelia docker manifest to Docker Hub",
|
2019-11-07 00:59:24 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2020-01-16 20:57:44 +00:00
|
|
|
log.Infof("Pushing Docker manifest of %s to Docker Hub...", DockerImageName)
|
2019-11-07 00:59:24 +00:00
|
|
|
publishDockerManifest()
|
2019-10-29 20:54:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func login(docker *Docker) {
|
|
|
|
username := os.Getenv("DOCKER_USERNAME")
|
|
|
|
password := os.Getenv("DOCKER_PASSWORD")
|
|
|
|
|
|
|
|
if username == "" {
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Fatal(errors.New("DOCKER_USERNAME is empty"))
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if password == "" {
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Fatal(errors.New("DOCKER_PASSWORD is empty"))
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:57:44 +00:00
|
|
|
log.Infof("Login to Docker Hub as %s", username)
|
2019-10-29 20:54:47 +00:00
|
|
|
err := docker.Login(username, password)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-01-16 20:57:44 +00:00
|
|
|
log.Fatal("Login to Docker Hub failed", err)
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deploy(docker *Docker, tag string) {
|
|
|
|
imageWithTag := DockerImageName + ":" + tag
|
2019-11-09 18:42:36 +00:00
|
|
|
|
2020-01-16 20:57:44 +00:00
|
|
|
log.Infof("Docker image %s will be deployed on Docker Hub", imageWithTag)
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
if err := docker.Tag(DockerImageName, imageWithTag); err != nil {
|
|
|
|
log.Fatal(err)
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
if err := docker.Push(imageWithTag); err != nil {
|
|
|
|
log.Fatal(err)
|
2019-11-07 00:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deployManifest(docker *Docker, tag string, amd64tag string, arm32v7tag string, arm64v8tag string) {
|
2019-11-08 01:26:54 +00:00
|
|
|
dockerImagePrefix := DockerImageName + ":"
|
2019-11-09 18:42:36 +00:00
|
|
|
|
2020-01-16 20:57:44 +00:00
|
|
|
log.Infof("Docker manifest %s%s will be deployed on Docker Hub", dockerImagePrefix, tag)
|
2019-11-07 00:59:24 +00:00
|
|
|
|
2019-11-08 01:26:54 +00:00
|
|
|
err := docker.Manifest(dockerImagePrefix+tag, dockerImagePrefix+amd64tag, dockerImagePrefix+arm32v7tag, dockerImagePrefix+arm64v8tag)
|
2019-10-29 20:54:47 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Fatal(err)
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
2019-11-08 23:57:39 +00:00
|
|
|
|
|
|
|
tags := []string{amd64tag, arm32v7tag, arm64v8tag}
|
|
|
|
for _, t := range tags {
|
2020-01-16 20:57:44 +00:00
|
|
|
log.Infof("Docker removing tag for %s%s on Docker Hub", dockerImagePrefix, t)
|
2019-11-08 23:57:39 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
if err := docker.CleanTag(t); err != nil {
|
2019-11-08 23:57:39 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 00:59:24 +00:00
|
|
|
func publishDockerImage(arch string) {
|
|
|
|
docker := &Docker{}
|
|
|
|
|
2020-02-05 12:24:19 +00:00
|
|
|
if ciTag != "" {
|
2019-12-10 08:21:54 +00:00
|
|
|
if len(tags) == 4 {
|
2019-12-27 11:07:53 +00:00
|
|
|
log.Infof("Detected tags: '%s' | '%s' | '%s'", tags[1], tags[2], tags[3])
|
2019-12-10 08:21:54 +00:00
|
|
|
|
2019-11-28 10:58:17 +00:00
|
|
|
login(docker)
|
2019-12-10 08:21:54 +00:00
|
|
|
deploy(docker, tags[1]+"-"+arch)
|
2019-12-27 11:07:53 +00:00
|
|
|
if !ignoredSuffixes.MatchString(ciTag) {
|
2019-12-10 08:21:54 +00:00
|
|
|
deploy(docker, tags[2]+"-"+arch)
|
|
|
|
deploy(docker, tags[3]+"-"+arch)
|
|
|
|
deploy(docker, "latest-"+arch)
|
|
|
|
}
|
2019-11-28 10:58:17 +00:00
|
|
|
} else {
|
|
|
|
log.Fatal("Docker image will not be published, the specified tag does not conform to the standard")
|
|
|
|
}
|
2020-02-05 12:24:19 +00:00
|
|
|
} else if ciBranch != "master" && !publicRepo.MatchString(ciBranch) {
|
|
|
|
login(docker)
|
|
|
|
deploy(docker, ciBranch+"-"+arch)
|
|
|
|
} else if ciBranch != "master" && publicRepo.MatchString(ciBranch) {
|
|
|
|
login(docker)
|
|
|
|
deploy(docker, "PR"+ciPullRequest+"-"+arch)
|
|
|
|
} else if ciBranch == "master" && ciPullRequest == "false" {
|
|
|
|
login(docker)
|
|
|
|
deploy(docker, "master-"+arch)
|
2019-11-07 00:59:24 +00:00
|
|
|
} else {
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Info("Docker image will not be published")
|
2019-11-07 00:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func publishDockerManifest() {
|
2019-10-29 20:54:47 +00:00
|
|
|
docker := &Docker{}
|
|
|
|
|
2020-02-05 12:24:19 +00:00
|
|
|
if ciTag != "" {
|
2019-12-10 08:21:54 +00:00
|
|
|
if len(tags) == 4 {
|
2019-12-27 11:07:53 +00:00
|
|
|
log.Infof("Detected tags: '%s' | '%s' | '%s'", tags[1], tags[2], tags[3])
|
2019-12-10 08:21:54 +00:00
|
|
|
|
2019-11-28 10:58:17 +00:00
|
|
|
login(docker)
|
2019-12-10 08:21:54 +00:00
|
|
|
deployManifest(docker, tags[1], tags[1]+"-amd64", tags[1]+"-arm32v7", tags[1]+"-arm64v8")
|
2020-01-16 20:57:44 +00:00
|
|
|
publishDockerReadme(docker)
|
2019-12-10 08:21:54 +00:00
|
|
|
|
2019-12-27 11:07:53 +00:00
|
|
|
if !ignoredSuffixes.MatchString(ciTag) {
|
2019-12-10 08:21:54 +00:00
|
|
|
deployManifest(docker, tags[2], tags[2]+"-amd64", tags[2]+"-arm32v7", tags[2]+"-arm64v8")
|
|
|
|
deployManifest(docker, tags[3], tags[3]+"-amd64", tags[3]+"-arm32v7", tags[3]+"-arm64v8")
|
|
|
|
deployManifest(docker, "latest", "latest-amd64", "latest-arm32v7", "latest-arm64v8")
|
2020-01-16 20:57:44 +00:00
|
|
|
publishDockerReadme(docker)
|
|
|
|
updateMicroBadger(docker)
|
2019-12-10 08:21:54 +00:00
|
|
|
}
|
2019-11-28 10:58:17 +00:00
|
|
|
} else {
|
|
|
|
log.Fatal("Docker manifest will not be published, the specified tag does not conform to the standard")
|
|
|
|
}
|
2020-02-05 12:24:19 +00:00
|
|
|
} else if ciBranch != "master" && !publicRepo.MatchString(ciBranch) {
|
|
|
|
login(docker)
|
|
|
|
deployManifest(docker, ciBranch, ciBranch+"-amd64", ciBranch+"-arm32v7", ciBranch+"-arm64v8")
|
|
|
|
} else if ciBranch != "master" && publicRepo.MatchString(ciBranch) {
|
|
|
|
login(docker)
|
|
|
|
deployManifest(docker, "PR"+ciPullRequest, "PR"+ciPullRequest+"-amd64", "PR"+ciPullRequest+"-arm32v7", "PR"+ciPullRequest+"-arm64v8")
|
|
|
|
} else if ciBranch == "master" && ciPullRequest == "false" {
|
|
|
|
login(docker)
|
|
|
|
deployManifest(docker, "master", "master-amd64", "master-arm32v7", "master-arm64v8")
|
|
|
|
publishDockerReadme(docker)
|
2019-10-29 20:54:47 +00:00
|
|
|
} else {
|
2019-12-27 11:07:53 +00:00
|
|
|
log.Info("Docker manifest will not be published")
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-16 20:57:44 +00:00
|
|
|
|
|
|
|
func publishDockerReadme(docker *Docker) {
|
|
|
|
log.Info("Docker pushing README.md to Docker Hub")
|
|
|
|
|
|
|
|
if err := docker.PublishReadme(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func updateMicroBadger(docker *Docker) {
|
|
|
|
log.Info("Updating MicroBadger metadata from Docker Hub")
|
|
|
|
|
|
|
|
if err := docker.UpdateMicroBadger(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|