From a31a17b2223cae0c3c56c462cde9e6ebb0bea46a Mon Sep 17 00:00:00 2001 From: Amir Zarrinkafsh Date: Wed, 5 May 2021 17:09:31 +1000 Subject: [PATCH] fix(cmd): retry clean tag logic for dockerhub (#1976) This change will ensure that if the curl command for the cleaning of Docker tags on DockerHub fails it will be reattempted up to 2 more times (total of 3) with a 10 second sleep between each attempt. The clean tag logic itself within curl attempts to execute the http request upto 3 times so this will ensure a maximum of 9 attempts. --- cmd/authelia-scripts/cmd_docker.go | 18 +++++++++++------- internal/utils/exec.go | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/cmd/authelia-scripts/cmd_docker.go b/cmd/authelia-scripts/cmd_docker.go index 63209d1c..f396c4e5 100644 --- a/cmd/authelia-scripts/cmd_docker.go +++ b/cmd/authelia-scripts/cmd_docker.go @@ -6,6 +6,7 @@ import ( "os" "regexp" "strings" + "time" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -57,27 +58,27 @@ func dockerBuildOfficialImage(arch string) error { if buildkiteQEMU != stringTrue { err := utils.CommandWithStdout("docker", "run", "--rm", "--privileged", "multiarch/qemu-user-static", "--reset", "-p", "yes").Run() if err != nil { - panic(err) + log.Fatal(err) } } 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() if err != nil { - panic(err) + log.Fatal(err) } } else if arch == "arm64v8" { if buildkiteQEMU != stringTrue { err := utils.CommandWithStdout("docker", "run", "--rm", "--privileged", "multiarch/qemu-user-static", "--reset", "-p", "yes").Run() if err != nil { - panic(err) + log.Fatal(err) } } 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() if err != nil { - panic(err) + log.Fatal(err) } } @@ -118,7 +119,7 @@ var DockerBuildCmd = &cobra.Command{ err = docker.Tag(IntermediateDockerImageName, DockerImageName) if err != nil { - panic(err) + log.Fatal(err) } }, } @@ -204,8 +205,11 @@ func deployManifest(docker *Docker, tag, amd64tag, arm32v7tag, arm64v8tag, regis for _, t := range tags { log.Infof("Docker removing tag for %s%s on Docker Hub", dockerImagePrefix, t) - if err := docker.CleanTag(t); err != nil { - panic(err) + if err := utils.RunFuncWithRetry(3, 10*time.Second, func() (err error) { + err = docker.CleanTag(t) + return + }); err != nil { + log.Fatal(err) } } } diff --git a/internal/utils/exec.go b/internal/utils/exec.go index ce48c4b4..f2c08b53 100644 --- a/internal/utils/exec.go +++ b/internal/utils/exec.go @@ -147,3 +147,23 @@ func RunCommandWithTimeout(cmd *exec.Cmd, timeout time.Duration) error { return err } } + +// RunFuncWithRetry run a function for n attempts with a sleep of n duration between each attempt. +func RunFuncWithRetry(attempts int, sleep time.Duration, f func() error) (err error) { + for i := 0; ; i++ { + err = f() + if err == nil { + return + } + + if i >= (attempts - 1) { + break + } + + time.Sleep(sleep) + + log.Printf("Retrying after error: %s", err) + } + + return fmt.Errorf("Failed after %d attempts, last error: %s", attempts, err) +}