/
builds.go
108 lines (95 loc) · 2.78 KB
/
builds.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
package dipod
import (
"context"
"encoding/json"
"errors"
"io"
"io/ioutil"
"os"
"github.com/EricHripko/dipod/iopodman"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/backend"
"github.com/varlink/go/varlink"
)
type buildsBackend struct{}
func (*buildsBackend) Build(ctx context.Context, config backend.BuildConfig) (id string, err error) {
if len(config.Options.Tags) < 1 {
err = errors.New("dipod: cannot build image without tag")
return
}
// stash context tarball to a temp file
var buildContext *os.File
buildContext, err = ioutil.TempFile("", "dipod-build")
if err != nil {
return
}
io.Copy(buildContext, config.Source)
defer config.Source.Close()
defer os.Remove(buildContext.Name())
// translate options
opts := iopodman.BuildInfo{
AdditionalTags: config.Options.Tags[1:],
BuildArgs: make(map[string]string),
BuildOptions: iopodman.BuildOptions{
AddHosts: config.Options.ExtraHosts,
CgroupParent: config.Options.CgroupParent,
CpuPeriod: config.Options.CPUPeriod,
CpuQuota: config.Options.CPUQuota,
CpuShares: config.Options.CPUShares,
CpusetCpus: config.Options.CPUSetCPUs,
CpusetMems: config.Options.CPUSetMems,
Memory: config.Options.Memory,
MemorySwap: config.Options.MemorySwap,
},
ContextDir: buildContext.Name(),
Dockerfiles: []string{config.Options.Dockerfile},
ForceRmIntermediateCtrs: config.Options.ForceRemove,
Nocache: config.Options.NoCache,
Squash: config.Options.Squash,
Output: config.Options.Tags[0],
}
for name, value := range config.Options.BuildArgs {
opts.BuildArgs[name] = *value
}
for _, ulimit := range config.Options.Ulimits {
opts.BuildOptions.Ulimit = append(opts.BuildOptions.Ulimit, ulimit.String())
}
for name, value := range config.Options.Labels {
opts.Label = append(opts.Label, name+"="+value)
}
if config.Options.PullParent {
opts.PullPolicy = "PullAlways"
}
json.NewEncoder(os.Stdout).Encode(config.Options)
// build
var recv recvFunc
recv, err = iopodman.BuildImage().Send(ctx, podman, varlink.More, opts)
if err != nil {
return
}
for {
var status iopodman.MoreResponse
var flags uint64
status, flags, err = recv(ctx)
if err != nil {
return
}
id = status.Id
for _, log := range status.Logs {
_, err = config.ProgressWriter.StdoutFormatter.Write([]byte(log))
if err != nil {
return
}
}
if flags&varlink.Continues != varlink.Continues {
break
}
}
return
}
func (*buildsBackend) PruneCache(context.Context, types.BuildCachePruneOptions) (*types.BuildCachePruneReport, error) {
return nil, errors.New("not implemented")
}
func (*buildsBackend) Cancel(context.Context, string) error {
return errors.New("not implemented")
}