-
Notifications
You must be signed in to change notification settings - Fork 2
/
buildpackage.go
266 lines (237 loc) · 4.98 KB
/
buildpackage.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package buildpackage
import (
"errors"
"io"
"log"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"golang.org/x/net/context"
)
type Builder struct {
cli *client.Client
ctx context.Context
version string
dockerImageName string
localImage bool
srcDir string
destDir string
pkgDir string
removeContainer bool
containerID string
}
type MakeBuilderOption func(*Builder)
func WithClient(cli *client.Client) MakeBuilderOption {
return func(b *Builder) {
b.cli = cli
}
}
func WithContext(ctx context.Context) MakeBuilderOption {
return func(b *Builder) {
b.ctx = ctx
}
}
func Version(version string) MakeBuilderOption {
return func(b *Builder) {
b.version = version
}
}
func ImageName(name string) MakeBuilderOption {
return func(b *Builder) {
b.dockerImageName = name
}
}
func LocalImage() MakeBuilderOption {
return func(b *Builder) {
b.localImage = true
}
}
func SourceDirectory(srcDir string) MakeBuilderOption {
return func(b *Builder) {
b.srcDir = srcDir
}
}
func DestinationDirectory(destDir string) MakeBuilderOption {
return func(b *Builder) {
b.destDir = destDir
}
}
func PreferredPackageDirectory(pkgDir string) MakeBuilderOption {
return func(b *Builder) {
b.pkgDir = pkgDir
}
}
func RemoveContainer(remove bool) MakeBuilderOption {
return func(b *Builder) {
b.removeContainer = remove
}
}
func MakeBuilder(opts ...MakeBuilderOption) (*Builder, error) {
b := new(Builder)
b.version = "latest"
b.removeContainer = true
b.dockerImageName = "jsouthworth/danos-buildpackage"
for _, opt := range opts {
opt(b)
}
if b.cli == nil {
cli, err := client.NewEnvClient()
if err != nil {
return nil, err
}
b.cli = cli
}
if b.ctx == nil {
b.ctx = context.Background()
}
if b.srcDir == "" {
return nil, errors.New("must supply Source Directory")
}
if !b.srcDirIsDebian() {
return nil, errors.New("must be run from the top level of a debian package tree")
}
if b.destDir == "" {
return nil, errors.New("must supply Destination Directory")
}
return b, nil
}
func (b *Builder) srcDirIsDebian() bool {
_, err := os.Stat(b.srcDir + "/debian/control")
return !os.IsNotExist(err)
}
func (b *Builder) canonicalImageName() string {
if b.localImage {
return b.imageName()
}
return "registry.hub.docker.com/" + b.imageName()
}
func (b *Builder) imageName() string {
return b.dockerImageName + ":" + b.version
}
func (b *Builder) pullEnvironment() error {
if b.localImage {
return nil
}
log.Println("pulling environment", b.canonicalImageName())
r, err := b.cli.ImagePull(
b.ctx,
b.canonicalImageName(),
types.ImagePullOptions{},
)
if err != nil {
return err
}
io.Copy(os.Stdout, r)
return nil
}
func (b *Builder) getBindMounts() []string {
binds := []string{b.srcDir + ":/mnt/src", b.destDir + ":/mnt/output"}
if b.pkgDir != "" {
binds = append(binds, b.pkgDir+":/mnt/pkgs")
}
return binds
}
func (b *Builder) createEnvironment() error {
log.Println("creating environment", b.canonicalImageName())
createResp, err := b.cli.ContainerCreate(
b.ctx,
&container.Config{
Image: b.canonicalImageName(),
AttachStdout: true,
AttachStderr: true,
},
&container.HostConfig{
Binds: b.getBindMounts(),
},
nil,
"",
)
if err != nil {
return err
}
b.containerID = createResp.ID
log.Println("containerID", b.containerID)
return nil
}
func (b *Builder) buildPackage() error {
log.Println("building package", b.containerID)
out, err := b.cli.ContainerAttach(
b.ctx,
b.containerID,
types.ContainerAttachOptions{
Stream: true,
Stdout: true,
Stderr: true,
},
)
if err != nil {
return err
}
defer out.Close()
go stdcopy.StdCopy(os.Stdout, os.Stderr, out.Reader)
err = b.cli.ContainerStart(
b.ctx,
b.containerID,
types.ContainerStartOptions{},
)
if err != nil {
return err
}
_, err = b.cli.ContainerWait(b.ctx, b.containerID)
if err != nil {
return err
}
data, err := b.cli.ContainerInspect(b.ctx, b.containerID)
if err != nil {
return err
}
state := data.State
if state.ExitCode == 0 {
return nil
}
return errors.New("Build failed: " + state.Error)
}
func (b *Builder) deleteEnvironment() error {
if b.containerID == "" {
return nil
}
if !b.removeContainer {
log.Println("preserving environment", b.containerID)
return nil
}
log.Println("deleting environment", b.containerID)
return b.cli.ContainerRemove(
b.ctx,
b.containerID,
types.ContainerRemoveOptions{},
)
}
func (b *Builder) Build() (err error) {
type buildStep func() error
defer func() {
e := b.deleteEnvironment()
if err == nil {
err = e
}
}()
steps := []buildStep{
b.pullEnvironment,
b.createEnvironment,
b.buildPackage,
}
for _, step := range steps {
err := step()
if err != nil {
return err
}
}
return nil
}
func (b *Builder) Close() error {
if b.cli != nil {
return b.cli.Close()
}
return nil
}