-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_ecr.go
107 lines (90 loc) · 2.73 KB
/
cmd_ecr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/altipla-consulting/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/altipla-consulting/wave/internal/query"
)
var cmdECR = &cobra.Command{
Use: "ecr",
Short: "Build a container from a predefined folder structure deploying to AWS ECR.",
Example: "wave ecr foo",
Args: cobra.ExactArgs(1),
}
func init() {
var flagRepo, flagSource, flagRegion string
cmdECR.Flags().StringVar(&flagRepo, "repo", "", "ECR repository name where the container will be stored.")
cmdECR.Flags().StringVar(&flagSource, "source", "", "Source folder. Defaults to a folder with the name of the app.")
cmdECR.Flags().StringVar(&flagRegion, "region", "eu-west-1", "AWS region where the container will be stored.")
cmdECR.MarkFlagRequired("repo")
cmdECR.RunE = func(command *cobra.Command, args []string) error {
app := args[0]
version := query.Version(command.Context())
logger := log.WithFields(log.Fields{
"name": app,
"version": version,
})
logger.Info("Build app")
source := app
if flagSource != "" {
source = flagSource
}
image := fmt.Sprintf("%s/%s", flagRepo, app)
docker := []string{
"build",
"--cache-from", image + ":latest",
"-f", source + "/Dockerfile",
"-t", image + ":latest",
"-t", image + ":" + version,
}
home, err := os.UserHomeDir()
if err != nil {
return errors.Trace(err)
}
if _, err := os.Stat(filepath.Join(home, ".npmrc")); err != nil && !os.IsNotExist(err) {
} else if err == nil {
docker = append(docker, "--secret", "id=npmrc,src="+filepath.Join(home, ".npmrc"))
}
docker = append(docker, ".") // build context
build := exec.Command("docker", docker...)
build.Stdout = os.Stdout
build.Stderr = os.Stderr
if err := build.Run(); err != nil {
return errors.Trace(err)
}
logger.Info("Log in to ECR")
login := exec.Command("aws", "ecr", "get-login-password", "--region", flagRegion)
var buf bytes.Buffer
login.Stdout = &buf
login.Stderr = os.Stderr
if err := login.Run(); err != nil {
return errors.Trace(err)
}
login = exec.Command("docker", "login", "--username", "AWS", "--password-stdin", flagRepo)
login.Stdin = &buf
login.Stdout = os.Stdout
login.Stderr = os.Stderr
if err := login.Run(); err != nil {
return errors.Trace(err)
}
logger.Info("Push to ECR")
push := exec.Command("docker", "push", image+":latest")
push.Stdout = os.Stdout
push.Stderr = os.Stderr
if err := push.Run(); err != nil {
return errors.Trace(err)
}
push = exec.Command("docker", "push", image+":"+version)
push.Stdout = os.Stdout
push.Stderr = os.Stderr
if err := push.Run(); err != nil {
return errors.Trace(err)
}
return nil
}
}