-
Notifications
You must be signed in to change notification settings - Fork 1
/
runner.go
116 lines (98 loc) · 2.02 KB
/
runner.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
108
109
110
111
112
113
114
115
116
package beluga
import (
"context"
"os"
"os/exec"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type Runner struct {
Env Environment
Logger logrus.StdLogger
DryRun bool
}
func New() *Runner {
logger := logrus.New()
return &Runner{
Env: Env(logger),
Logger: logger,
}
}
// Exec runs a command in the Beluga context with stderr and stdout bound to the parent process
func (r *Runner) Exec(c *exec.Cmd) error {
r.Logger.Println(c.String())
if r.DryRun {
return nil
}
var err error
if c.Stdin == nil {
c.Stdin = os.Stdin
}
if c.Stdout == nil {
c.Stdout = os.Stdout
}
if c.Stderr == nil {
c.Stderr = os.Stderr
}
c.Env, err = r.Env.Format(GoEnvFormat, r.Env.Keys())
if err != nil {
return errors.Wrap(err, "generate environment list")
}
return c.Run()
}
func (r *Runner) ComposeFile(ctx context.Context) (string, error) {
d := r.docker()
out, err := d.ComposeConfig(ctx)
if err != nil {
return "", err
}
return composeTemplate(string(out), r.Env.ComposeTemplate(), r.Env)
}
func (r *Runner) StackName() string {
return r.Env[varStackName]
}
func (r *Runner) Deploy(ctx context.Context) error {
return r.deployer().Deploy(ctx, r)
}
func (r *Runner) Teardown(ctx context.Context) error {
return r.deployer().Teardown(ctx, r)
}
type BuildOpts struct {
Push bool
}
func (r *Runner) Build(ctx context.Context, opts BuildOpts) error {
e := r.Env
d := r.docker()
if e.RegistryUsername() != "" {
err := d.Login(ctx, e.Registry(), e.RegistryUsername(), e.RegistryPassword())
if err != nil {
return err
}
}
builtImage := ""
images := strings.Fields(e.Images())
for _, image := range images {
if builtImage == "" {
err := d.Build(ctx, e.Context(), e.Dockerfile(), image)
if err != nil {
return err
}
builtImage = image
} else {
err := d.Tag(ctx, builtImage, image)
if err != nil {
return err
}
}
}
if opts.Push {
for _, image := range images {
err := d.Push(ctx, image)
if err != nil {
return err
}
}
}
return nil
}