forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
160 lines (144 loc) · 3.47 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
package pack
import (
"context"
"fmt"
"io"
"log"
"strconv"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/pkg/errors"
)
type RunFlags struct {
BuildFlags BuildFlags
Port string
}
type RunConfig struct {
Port string
Build Task
// All below are from BuildConfig
RepoName string
Cli Docker
Stdout io.Writer
Stderr io.Writer
Log *log.Logger
}
func (bf *BuildFactory) RunConfigFromFlags( f *RunFlags) (*RunConfig, error) {
bc, err := bf.BuildConfigFromFlags(&f.BuildFlags)
if err != nil {
return nil, err
}
rc := &RunConfig{
Build: bc,
Port: f.Port,
// All below are from BuildConfig
RepoName: bc.RepoName,
Cli: bc.Cli,
Stdout: bc.Stdout,
Stderr: bc.Stderr,
Log: bc.Log,
}
return rc, nil
}
func Run(appDir, buildImage, runImage, port string, makeStopCh func() <-chan struct{}) error {
bf, err := DefaultBuildFactory()
if err != nil {
return err
}
r, err := bf.RunConfigFromFlags(&RunFlags{
BuildFlags: BuildFlags{
AppDir: appDir,
Builder: buildImage,
RunImage: runImage,
},
Port: port,
})
if err != nil {
return err
}
return r.Run(makeStopCh)
}
func (r *RunConfig) Run(makeStopCh func() <-chan struct{}) error {
ctx := context.Background()
err := r.Build.Run()
if err != nil {
return err
}
fmt.Println("*** RUNNING:")
if r.Port == "" {
r.Port, err = r.exposedPorts(ctx, r.RepoName)
if err != nil {
return err
}
}
exposedPorts, portBindings, err := parsePorts(r.Port)
if err != nil {
return err
}
ctr, err := r.Cli.ContainerCreate(ctx, &container.Config{
Image: r.RepoName,
AttachStdout: true,
AttachStderr: true,
ExposedPorts: exposedPorts,
}, &container.HostConfig{
AutoRemove: true,
PortBindings: portBindings,
}, nil, "")
logContainerListening(r.Log, portBindings)
running := true
stopCh := makeStopCh()
go func() {
<-stopCh
running = false
r.Cli.ContainerRemove(ctx, ctr.ID, types.ContainerRemoveOptions{
Force: true,
})
}()
if err = r.Cli.RunContainer(ctx, ctr.ID, r.Stdout, r.Stderr); err != nil && running {
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 "", err
}
ports := []string{}
for port := range i.Config.ExposedPorts {
ports = append(ports, port.Port())
}
return strings.Join(ports, ","), nil
}
func parsePorts(port string) (nat.PortSet, nat.PortMap, error) {
ports := strings.Split(port, ",")
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(log *log.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
log.Printf("Starting container listening at http://%s:%s/\n", host, port)
}
}
}
}