2019-10-30 03:54:47 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-11-07 07:59:24 +07:00
|
|
|
"strings"
|
2019-10-30 03:54:47 +07:00
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
"github.com/clems4ever/authelia/utils"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2019-10-30 03:54:47 +07:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-11-07 07:59:24 +07:00
|
|
|
var arch string
|
|
|
|
var supportedArch = []string{"amd64", "arm32v7", "arm64v8"}
|
|
|
|
var defaultArch = "amd64"
|
|
|
|
|
|
|
|
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 19:07:02 +07:00
|
|
|
docker := &Docker{}
|
2019-11-07 07:59:24 +07:00
|
|
|
// Set default Architecture Dockerfile to amd64
|
|
|
|
dockerfile := "Dockerfile"
|
|
|
|
|
|
|
|
// If not the default value
|
|
|
|
if arch != defaultArch {
|
|
|
|
dockerfile = fmt.Sprintf("%s.%s", dockerfile, arch)
|
|
|
|
}
|
|
|
|
|
|
|
|
if arch == "arm32v7" {
|
2019-11-02 21:32:58 +07:00
|
|
|
err := utils.CommandWithStdout("docker", "run", "--rm", "--privileged", "multiarch/qemu-user-static", "--reset", "-p", "yes").Run()
|
2019-11-07 07:59:24 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
err = utils.CommandWithStdout("bash", "-c", "wget https://github.com/multiarch/qemu-user-static/releases/download/v4.1.0-1/qemu-arm-static -O ./qemu-arm-static && chmod +x ./qemu-arm-static").Run()
|
2019-11-07 07:59:24 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else if arch == "arm64v8" {
|
2019-11-02 21:32:58 +07:00
|
|
|
err := utils.CommandWithStdout("docker", "run", "--rm", "--privileged", "multiarch/qemu-user-static", "--reset", "-p", "yes").Run()
|
2019-11-07 07:59:24 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
err = utils.CommandWithStdout("bash", "-c", "wget https://github.com/multiarch/qemu-user-static/releases/download/v4.1.0-1/qemu-aarch64-static -O ./qemu-aarch64-static && chmod +x ./qemu-aarch64-static").Run()
|
2019-11-07 07:59:24 +07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return docker.Build(IntermediateDockerImageName, dockerfile, ".")
|
2019-11-03 19:07:02 +07:00
|
|
|
}
|
|
|
|
|
2019-10-30 03:54:47 +07: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 21:32:58 +07:00
|
|
|
log.Infof("Building Docker image %s...", DockerImageName)
|
2019-11-07 07:59:24 +07:00
|
|
|
checkArchIsSupported(arch)
|
|
|
|
err := dockerBuildOfficialImage(arch)
|
2019-11-03 19:07:02 +07:00
|
|
|
|
2019-10-30 03:54:47 +07:00
|
|
|
if err != nil {
|
2019-11-03 19:07:02 +07:00
|
|
|
log.Fatal(err)
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:07:02 +07:00
|
|
|
docker := &Docker{}
|
2019-10-30 03:54:47 +07:00
|
|
|
err = docker.Tag(IntermediateDockerImageName, DockerImageName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// DockerPushCmd Command for pushing Authelia docker image to Dockerhub
|
|
|
|
var DockerPushCmd = &cobra.Command{
|
2019-11-07 07:59:24 +07:00
|
|
|
Use: "push-image",
|
2019-10-30 03:54:47 +07:00
|
|
|
Short: "Publish Authelia docker image to Dockerhub",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Infof("Pushing Docker image %s to dockerhub...", DockerImageName)
|
2019-11-07 07:59:24 +07:00
|
|
|
checkArchIsSupported(arch)
|
|
|
|
publishDockerImage(arch)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// DockerManifestCmd Command for pushing Authelia docker manifest to Dockerhub
|
|
|
|
var DockerManifestCmd = &cobra.Command{
|
|
|
|
Use: "push-manifest",
|
|
|
|
Short: "Publish Authelia docker manifest to Dockerhub",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Infof("Pushing Docker manifest of %s to dockerhub...", DockerImageName)
|
2019-11-07 07:59:24 +07:00
|
|
|
publishDockerManifest()
|
2019-10-30 03:54:47 +07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func login(docker *Docker) {
|
|
|
|
username := os.Getenv("DOCKER_USERNAME")
|
|
|
|
password := os.Getenv("DOCKER_PASSWORD")
|
|
|
|
|
|
|
|
if username == "" {
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Fatal(errors.New("DOCKER_USERNAME is empty"))
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if password == "" {
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Fatal(errors.New("DOCKER_PASSWORD is empty"))
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Debug("Login to dockerhub as " + username)
|
2019-10-30 03:54:47 +07:00
|
|
|
err := docker.Login(username, password)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Fatal("Login to dockerhub failed", err)
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deploy(docker *Docker, tag string) {
|
|
|
|
imageWithTag := DockerImageName + ":" + tag
|
2019-11-10 01:42:36 +07:00
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Debug("Docker image " + imageWithTag + " will be deployed on Dockerhub.")
|
2019-10-30 03:54:47 +07:00
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
if err := docker.Tag(DockerImageName, imageWithTag); err != nil {
|
|
|
|
log.Fatal(err)
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
if err := docker.Push(imageWithTag); err != nil {
|
|
|
|
log.Fatal(err)
|
2019-11-07 07:59:24 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deployManifest(docker *Docker, tag string, amd64tag string, arm32v7tag string, arm64v8tag string) {
|
2019-11-08 08:26:54 +07:00
|
|
|
dockerImagePrefix := DockerImageName + ":"
|
2019-11-10 01:42:36 +07:00
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Debug("Docker manifest " + dockerImagePrefix + tag + " will be deployed on Dockerhub.")
|
2019-11-07 07:59:24 +07:00
|
|
|
|
2019-11-08 08:26:54 +07:00
|
|
|
err := docker.Manifest(dockerImagePrefix+tag, dockerImagePrefix+amd64tag, dockerImagePrefix+arm32v7tag, dockerImagePrefix+arm64v8tag)
|
2019-10-30 03:54:47 +07:00
|
|
|
|
|
|
|
if err != nil {
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Fatal(err)
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
2019-11-09 06:57:39 +07:00
|
|
|
|
|
|
|
tags := []string{amd64tag, arm32v7tag, arm64v8tag}
|
|
|
|
for _, t := range tags {
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Debug("Docker removing tag for " + dockerImagePrefix + t + " on Dockerhub.")
|
2019-11-09 06:57:39 +07:00
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
if err := docker.CleanTag(t); err != nil {
|
2019-11-09 06:57:39 +07:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-10 01:42:36 +07:00
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Debug("Docker pushing README.md to Dockerhub.")
|
2019-11-10 01:42:36 +07:00
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
if err := docker.PublishReadme(); err != nil {
|
|
|
|
log.Fatal(err)
|
2019-11-10 01:42:36 +07:00
|
|
|
}
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
|
2019-11-07 07:59:24 +07:00
|
|
|
func publishDockerImage(arch string) {
|
|
|
|
docker := &Docker{}
|
|
|
|
|
|
|
|
travisBranch := os.Getenv("TRAVIS_BRANCH")
|
|
|
|
travisPullRequest := os.Getenv("TRAVIS_PULL_REQUEST")
|
|
|
|
travisTag := os.Getenv("TRAVIS_TAG")
|
|
|
|
|
|
|
|
if travisBranch == "master" && travisPullRequest == "false" {
|
|
|
|
login(docker)
|
|
|
|
deploy(docker, "master-"+arch)
|
|
|
|
} else if travisTag != "" {
|
|
|
|
login(docker)
|
|
|
|
deploy(docker, travisTag+"-"+arch)
|
|
|
|
deploy(docker, "latest-"+arch)
|
|
|
|
} else {
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Info("Docker image will not be published")
|
2019-11-07 07:59:24 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func publishDockerManifest() {
|
2019-10-30 03:54:47 +07:00
|
|
|
docker := &Docker{}
|
|
|
|
|
|
|
|
travisBranch := os.Getenv("TRAVIS_BRANCH")
|
|
|
|
travisPullRequest := os.Getenv("TRAVIS_PULL_REQUEST")
|
|
|
|
travisTag := os.Getenv("TRAVIS_TAG")
|
|
|
|
|
|
|
|
if travisBranch == "master" && travisPullRequest == "false" {
|
|
|
|
login(docker)
|
2019-11-07 07:59:24 +07:00
|
|
|
deployManifest(docker, "master", "master-amd64", "master-arm32v7", "master-arm64v8")
|
2019-10-30 03:54:47 +07:00
|
|
|
} else if travisTag != "" {
|
|
|
|
login(docker)
|
2019-11-07 07:59:24 +07:00
|
|
|
deployManifest(docker, travisTag, travisTag+"-amd64", travisTag+"-arm32v7", travisTag+"-arm64v8")
|
|
|
|
deployManifest(docker, "latest", "latest-amd64", "latest-arm32v7", "latest-arm64v8")
|
2019-10-30 03:54:47 +07:00
|
|
|
} else {
|
2019-11-07 07:59:24 +07:00
|
|
|
fmt.Println("Docker manifest will not be published")
|
2019-10-30 03:54:47 +07:00
|
|
|
}
|
|
|
|
}
|