forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
181 lines (165 loc) · 3.96 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
package pack
import (
"context"
"crypto/md5"
"fmt"
"io"
"log"
"path/filepath"
"strconv"
"strings"
"github.com/buildpack/pack/config"
"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 {
AppDir string
Builder string
RunImage string
Port string
}
type RunConfig struct {
Port string
Build *BuildConfig
// All below are from BuildConfig
AppDir string
Builder string
RunImage string
RepoName string
Publish bool
// Above are copied from BuildFlags are set by init
Cli Docker
Stdout io.Writer
Stderr io.Writer
Log *log.Logger
FS FS
Config *config.Config
Images Images
// Above are copied from BuildFactory
WorkspaceVolume string
CacheVolume string
}
func (bf *BuildFactory) RunConfigFromFlags(f *RunFlags) (*RunConfig, error) {
bc, err := bf.BuildConfigFromFlags(&BuildFlags{
AppDir: f.AppDir,
Builder: f.Builder,
RunImage: f.RunImage,
RepoName: f.repoName(),
Publish: false,
NoPull: false,
})
if err != nil {
return nil, err
}
rc := &RunConfig{
Build: bc,
Port: f.Port,
// All below are from BuildConfig
AppDir: bc.AppDir,
Builder: bc.Builder,
RunImage: bc.RunImage,
RepoName: bc.RepoName,
Publish: bc.Publish,
// Above are copied from BuildFlags are set by init
Cli: bc.Cli,
Stdout: bc.Stdout,
Stderr: bc.Stderr,
Log: bc.Log,
FS: bc.FS,
Config: bc.Config,
Images: bc.Images,
// Above are copied from BuildFactory
WorkspaceVolume: bc.WorkspaceVolume,
CacheVolume: bc.CacheVolume,
}
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{
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:")
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(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 *RunFlags) repoName() string {
dir, _ := filepath.Abs(r.AppDir)
// we can ignore errors here because they will be caught later by the Build command
h := md5.New()
io.WriteString(h, dir)
return fmt.Sprintf("%x", h.Sum(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 8080 inside the container
p = fmt.Sprintf("127.0.0.1:%s:8080/tcp", p)
}
ports[i] = p
}
return nat.ParsePortSpecs(ports)
}
func logContainerListening(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"
}
fmt.Printf("Starting container listening at http://%s:%s/\n", host, port)
}
}
}
}