forked from micahyoung/buildpacks-pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
181 lines (163 loc) · 4.45 KB
/
run.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// TODO: We should rename this file (and its test and test file) to avoid confusion with the one in the `commands` package
package pack
import (
"context"
"fmt"
"io"
"strconv"
"strings"
"github.com/buildpack/lifecycle/image"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/pkg/errors"
"github.com/buildpack/pack/cache"
"github.com/buildpack/pack/docker"
"github.com/buildpack/pack/logging"
"github.com/buildpack/pack/style"
)
// This interface same as BuildConfig
//go:generate mockgen -package mocks -destination mocks/build_runner.go github.com/buildpack/pack BuildRunner
type BuildRunner interface {
Run(context.Context) error
}
type RunFlags struct {
BuildFlags BuildFlags
Ports []string
}
type RunConfig struct {
Ports []string
Build BuildRunner
// All below are from BuildConfig
RepoName string
Cli Docker
Logger *logging.Logger
}
func (bf *BuildFactory) RunConfigFromFlags(ctx context.Context, f *RunFlags) (*RunConfig, error) {
bc, err := bf.BuildConfigFromFlags(ctx, &f.BuildFlags)
if err != nil {
return nil, err
}
rc := &RunConfig{
Build: bc,
Ports: f.Ports,
// All below are from BuildConfig
RepoName: bc.RepoName,
Cli: bc.Cli,
Logger: bc.Logger,
}
return rc, nil
}
func Run(ctx context.Context, outWriter, errWriter io.Writer, appDir, buildImage, runImage string, ports []string) error {
// TODO: Receive Cache and docker client as an argument of this function
dockerClient, err := docker.New()
if err != nil {
return err
}
c, err := cache.New(runImage, dockerClient)
if err != nil {
return err
}
imageFactory, err := image.NewFactory(image.WithOutWriter(outWriter))
if err != nil {
return err
}
imageFetcher := &ImageFetcher{
Factory: imageFactory,
Docker: dockerClient,
}
logger := logging.NewLogger(outWriter, errWriter, true, false)
bf, err := DefaultBuildFactory(logger, c, dockerClient, imageFetcher)
if err != nil {
return err
}
r, err := bf.RunConfigFromFlags(ctx,
&RunFlags{
BuildFlags: BuildFlags{
AppDir: appDir,
Builder: buildImage,
RunImage: runImage,
},
Ports: ports,
})
if err != nil {
return err
}
return r.Run(ctx)
}
func (r *RunConfig) Run(ctx context.Context) error {
err := r.Build.Run(ctx)
if err != nil {
return err
}
r.Logger.Verbose(style.Step("RUNNING"))
if r.Ports == nil {
r.Ports, err = r.exposedPorts(ctx, r.RepoName)
if err != nil {
return err
}
}
exposedPorts, portBindings, err := parsePorts(r.Ports)
if err != nil {
return err
}
ctr, err := r.Cli.ContainerCreate(ctx, &container.Config{
Image: r.RepoName,
AttachStdout: true,
AttachStderr: true,
ExposedPorts: exposedPorts,
Labels: map[string]string{"author": "pack"},
}, &container.HostConfig{
AutoRemove: true,
PortBindings: portBindings,
}, nil, "")
if err != nil {
return err
}
defer r.Cli.ContainerRemove(context.Background(), ctr.ID, dockertypes.ContainerRemoveOptions{Force: true})
logContainerListening(r.Logger, portBindings)
if err = r.Cli.RunContainer(ctx, ctr.ID, r.Logger.VerboseWriter(), r.Logger.VerboseErrorWriter()); err != nil {
return errors.Wrap(err, "run container")
}
return nil
}
func (r *RunConfig) exposedPorts(ctx context.Context, imageID string) ([]string, error) {
i, _, err := r.Cli.ImageInspectWithRaw(ctx, imageID)
if err != nil {
return nil, err
}
var ports []string
for port := range i.Config.ExposedPorts {
ports = append(ports, port.Port())
}
return ports, nil
}
func parsePorts(ports []string) (nat.PortSet, nat.PortMap, error) {
for i, p := range ports {
p = strings.TrimSpace(p)
if _, err := strconv.Atoi(p); err == nil {
// default simple port to localhost and inside the container
p = fmt.Sprintf("127.0.0.1:%s:%s/tcp", p, p)
}
ports[i] = p
}
return nat.ParsePortSpecs(ports)
}
func logContainerListening(logger *logging.Logger, portBindings nat.PortMap) {
// TODO handle case with multiple ports, for now when there is more than
// one port we assume you know what you're doing and don't need guidance
if len(portBindings) == 1 {
for _, bindings := range portBindings {
if len(bindings) == 1 {
binding := bindings[0]
host := binding.HostIP
port := binding.HostPort
if host == "127.0.0.1" {
host = "localhost"
}
// TODO the service may not be http based
logger.Info("Starting container listening at http://%s:%s/\n", host, port)
}
}
}
}