Skip to content

Commit

Permalink
main: refactor sidecar usage
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent 59614e9 commit 2f9d783
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 37 deletions.
61 changes: 61 additions & 0 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
package main

import (
"context"
"fmt"
"io"
"os"
"time"

"github.com/tsuru/deploy-agent/internal/docker"
"github.com/tsuru/deploy-agent/internal/tsuru"
"github.com/tsuru/tsuru/exec"
)
Expand Down Expand Up @@ -47,3 +52,59 @@ func deploy(c tsuru.Client, appName string, fs Filesystem, executor exec.Executo
_, err = c.RegisterUnit(appName, yamlData)
return err
}

// setupSidecar setups up a sidecar instance and uploads the input file to the primary container
func setupSidecar(dockerClient *docker.Client, config Config) (*docker.Sidecar, error) {
sideCar, err := docker.NewSidecar(dockerClient, config.RunAsUser)
if err != nil {
return nil, fmt.Errorf("failed to create sidecar: %v", err)
}
if config.InputFile == "" {
return sideCar, nil
}
err = sideCar.UploadToPrimaryContainer(context.Background(), config.InputFile)
if err != nil {
fatal("failed to upload input file: %v", err)
}
return sideCar, nil
}

// pushSidecar commits the sidecar primary container, tags and pushes its image
func pushSidecar(dockerClient *docker.Client, sideCar *docker.Sidecar, config Config, w io.Writer) error {
fmt.Fprintln(w, "---- Building application image ----")
img, err := sideCar.CommitPrimaryContainer(context.Background(), config.DestinationImage)
if err != nil {
return fmt.Errorf("failed to commit main container: %v", err)
}
authConfig := docker.AuthConfig{
Username: config.RegistryAuthUser,
Password: config.RegistryAuthPass,
Email: config.RegistryAuthEmail,
ServerAddress: config.RegistryAddress,
}
if err := tagAndPush(dockerClient, img, authConfig, config.RegistryPushRetries, w); err != nil {
return fmt.Errorf("Error pushing image: %v", err)
}
return nil
}

func tagAndPush(dockerClient *docker.Client, img docker.Image, auth docker.AuthConfig, retries int, w io.Writer) error {
err := dockerClient.Tag(context.Background(), img)
if err != nil {
return fmt.Errorf("error tagging image %v: %v", img, err)
}
fmt.Fprintf(w, " ---> Sending image to repository (%s)\n", img)
for i := 0; i < retries; i++ {
err = dockerClient.Push(context.Background(), auth, img)
if err != nil {
fmt.Fprintf(w, "Could not send image, trying again. Original error: %v\n", err)
time.Sleep(time.Second)
continue
}
break
}
if err != nil {
return fmt.Errorf("Error pushing image: %v", err)
}
return nil
}
42 changes: 5 additions & 37 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"time"

"github.com/kelseyhightower/envconfig"
"github.com/tsuru/deploy-agent/internal/docker"
Expand Down Expand Up @@ -67,46 +65,16 @@ func main() {
if err != nil {
fatal("failed to create docker client: %v", err)
}
sideCar, err := docker.NewSidecar(dockerClient, config.RunAsUser)
sideCar, err := setupSidecar(dockerClient, config)
if err != nil {
fatal("failed to create sidecar: %v", err)
}
executor = sideCar
filesystem = &executorFS{executor: sideCar}
err = sideCar.UploadToPrimaryContainer(context.Background(), config.InputFile)
if err != nil {
fatal("failed to upload input file: %v", err)
}
defer func() {
fmt.Println("---- Building application image ----")
img, err := sideCar.CommitPrimaryContainer(context.Background(), config.DestinationImage)
if err != nil {
fatal("failed to commit main container: %v", 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)
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", err)
}
}()
// we defer the call to pushSidecar so the normal build/deploy steps are executed
// by the sidecar executor. This will only be executed if those steps finish without
// any error the call to fatal() exits.
defer pushSidecar(dockerClient, sideCar, config, os.Stdout)
}

switch command[len(command)-1] {
Expand Down

0 comments on commit 2f9d783

Please sign in to comment.