forked from concourse/oci-build-task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildkitd.go
196 lines (155 loc) · 3.77 KB
/
buildkitd.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
package task
import (
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"syscall"
"time"
"github.com/BurntSushi/toml"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type Buildkitd struct {
Addr string
rootDir string
proc *os.Process
}
// BuildkitdOpts to provide to Buildkitd
type BuildkitdOpts struct {
RootDir string
ConfigPath string
}
func SpawnBuildkitd(req Request, opts *BuildkitdOpts) (*Buildkitd, error) {
err := run(os.Stdout, "setup-cgroups")
if err != nil {
return nil, errors.Wrap(err, "setup cgroups")
}
rootDir := filepath.Join(os.TempDir(), "buildkitd")
if opts != nil && opts.RootDir != "" {
rootDir = opts.RootDir
}
err = os.MkdirAll(rootDir, 0755)
if err != nil {
return nil, errors.Wrap(err, "create root dir")
}
sockPath := filepath.Join(rootDir, "buildkitd.sock")
logPath := filepath.Join(rootDir, "buildkitd.log")
configPath := filepath.Join(rootDir, "builtkitd.toml")
if opts != nil && opts.ConfigPath != "" {
configPath = opts.ConfigPath
}
err = generateConfig(req, configPath)
if err != nil {
return nil, errors.Wrap(err, "generate config")
}
addr := (&url.URL{Scheme: "unix", Path: sockPath}).String()
buildkitdFlags := []string{
"--root", rootDir,
"--addr", addr,
"--config", configPath,
}
if req.Config.Debug {
buildkitdFlags = append(buildkitdFlags, "--debug")
}
var cmd *exec.Cmd
if os.Getuid() == 0 {
cmd = exec.Command("buildkitd", buildkitdFlags...)
} else {
cmd = exec.Command("rootlesskit", append([]string{"buildkitd"}, buildkitdFlags...)...)
}
// kill buildkitd on exit
cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGKILL,
}
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return nil, errors.Wrap(err, "open log file")
}
cmd.Stdout = logFile
cmd.Stderr = logFile
err = cmd.Start()
if err != nil {
return nil, errors.Wrap(err, "start buildkitd")
}
err = logFile.Close()
if err != nil {
return nil, errors.Wrap(err, "close log file")
}
for {
err := buildctl(addr, ioutil.Discard, "debug", "workers")
if err == nil {
break
}
err = cmd.Process.Signal(syscall.Signal(0))
if err != nil {
logrus.Warn("builtkitd process probe failed:", err)
logrus.Warn("dumping buildkit logs due to probe failure")
fmt.Fprintln(os.Stderr)
dumpLogFile(logPath)
os.Exit(1)
}
logrus.Debugf("waiting for buildkitd...")
time.Sleep(100 * time.Millisecond)
}
logrus.Debug("buildkitd started")
return &Buildkitd{
Addr: addr,
rootDir: rootDir,
proc: cmd.Process,
}, nil
}
func (buildkitd *Buildkitd) Cleanup() error {
err := buildkitd.proc.Signal(syscall.SIGTERM)
if err != nil {
return errors.Wrap(err, "terminate buildkitd")
}
_, err = buildkitd.proc.Wait()
if err != nil {
return errors.Wrap(err, "wait buildkitd")
}
return nil
}
func generateConfig(req Request, configPath string) error {
var config BuildkitdConfig
if len(req.Config.RegistryMirrors) > 0 {
var registryConfigs map[string]RegistryConfig
registryConfigs = make(map[string]RegistryConfig)
registryConfigs["docker.io"] = RegistryConfig{
Mirrors: req.Config.RegistryMirrors,
}
config.Registries = registryConfigs
}
err := os.MkdirAll(filepath.Dir(configPath), 0700)
if err != nil {
return err
}
f, err := os.Create(configPath)
if err != nil {
return err
}
err = toml.NewEncoder(f).Encode(config)
if err != nil {
return err
}
return f.Close()
}
func dumpLogFile(logPath string) {
logFile, err := os.Open(logPath)
if err != nil {
logrus.Warn("error opening log file:", err)
return
}
_, err = io.Copy(os.Stderr, logFile)
if err != nil {
logrus.Warn("error streaming log file:", err)
return
}
err = logFile.Close()
if err != nil {
logrus.Warn("error closing log file:", err)
}
}