Skip to content

Commit

Permalink
adds retries for image push
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent 10eb7e5 commit 3110e0c
Show file tree
Hide file tree
Showing 12 changed files with 764 additions and 14 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
streamInactivityTimeout = time.Minute
)

type AuthConfig docker.AuthConfiguration

type Container struct {
ID string
}
Expand Down Expand Up @@ -100,15 +102,15 @@ func (c *Client) Tag(ctx context.Context, img Image) error {
})
}

func (c *Client) Push(ctx context.Context, img Image) error {
func (c *Client) Push(ctx context.Context, authConfig AuthConfig, img Image) error {
opts := docker.PushImageOptions{
Name: img.Name(),
Tag: img.tag,
OutputStream: os.Stdout,
Context: ctx,
InactivityTimeout: streamInactivityTimeout,
}
return c.api.PushImage(opts, docker.AuthConfiguration{})
return c.api.PushImage(opts, docker.AuthConfiguration(authConfig))
}

func splitImageName(imageName string) (registry, repo, tag string) {
Expand Down
49 changes: 38 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"time"

"github.com/kelseyhightower/envconfig"
"github.com/tsuru/deploy-agent/internal/docker"
"github.com/tsuru/deploy-agent/internal/tsuru"
"github.com/tsuru/tsuru/exec"
Expand All @@ -20,6 +21,17 @@ import (

const version = "0.2.8"

type Config struct {
DockerHost string `envconfig:"DOCKER_HOST"`
RunAsSidecar bool
DestinationImage string
RegistryPushRetries int `default:"3"`
RegistryAuthEmail string
RegistryAuthPass string
RegistryAuthUser string
RegistryAddress string
}

func main() {
var (
printVersion bool
Expand All @@ -32,6 +44,12 @@ func main() {
return
}

var config Config
err := envconfig.Process("deployagent", &config)
if err != nil {
fatal("error processing environment variables: %v", err)
}

c := tsuru.Client{
URL: os.Args[1],
Token: os.Args[2],
Expand All @@ -43,13 +61,10 @@ func main() {
var filesystem Filesystem = &localFS{Fs: fs.OsFs{}}
var executor exec.Executor = &exec.OsExecutor{}

sideCar := os.Getenv("DEPLOYAGENT_RUN_AS_SIDECAR") == "true"

if sideCar {
dockerClient, err := docker.NewClient(os.Getenv("DOCKER_HOST"))
if config.RunAsSidecar {
dockerClient, err := docker.NewClient(config.DockerHost)
if err != nil {
fatal("failed to create docker client: %v", err)

}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
mainContainer, err := getMainContainer(ctx, dockerClient)
Expand All @@ -61,24 +76,36 @@ func main() {
filesystem = &executorFS{executor: executor}
defer func() {
fmt.Println("---- Building application image ----")
imgName := os.Getenv("DEPLOYAGENT_DST_IMAGE")
img, err := dockerClient.Commit(context.Background(), mainContainer.ID, imgName)
img, err := dockerClient.Commit(context.Background(), mainContainer.ID, config.DestinationImage)
if err != nil {
fatal("error commiting image %v: %v", imgName, err)
fatal("error commiting image %v: %v", config.DestinationImage, err)
}
err = dockerClient.Tag(context.Background(), img)
if err != nil {
fatal("error tagging image %v: %v", img, err)
}
fmt.Printf(" ---> Sending image to repository (%s)\n", img)
err = dockerClient.Push(context.Background(), img)
authConfig := docker.AuthConfig{
Username: config.RegistryAuthUser,
Password: config.RegistryAuthPass,
Email: config.RegistryAuthEmail,
ServerAddress: config.RegistryAddress,
}
for i := 0; i < config.RegistryPushRetries; i++ {
err = dockerClient.Push(context.Background(), authConfig, img)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not send image, trying again. Original error: %v\n", err)
time.Sleep(time.Second)
continue
}
break
}
if err != nil {
fatal("error pushing image %v: %v", img, err)
fatal("Error pushing image: %v", err)
}
}()
}

var err error
switch command[len(command)-1] {
case "build":
err = build(c, appName, command[:len(command)-1], filesystem, executor)
Expand Down
7 changes: 7 additions & 0 deletions vendor/github.com/kelseyhightower/envconfig/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/kelseyhightower/envconfig/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/kelseyhightower/envconfig/MAINTAINERS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

188 changes: 188 additions & 0 deletions vendor/github.com/kelseyhightower/envconfig/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/kelseyhightower/envconfig/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/kelseyhightower/envconfig/env_os.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/kelseyhightower/envconfig/env_syscall.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3110e0c

Please sign in to comment.