-
Notifications
You must be signed in to change notification settings - Fork 2
/
rebuildremote.go
221 lines (211 loc) · 6.78 KB
/
rebuildremote.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
// Copyright 2024 The OSS Rebuild Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rebuild
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"path"
"strings"
"text/template"
"time"
gcb "github.com/google/oss-rebuild/internal/cloudbuild"
"github.com/pkg/errors"
cloudbuild "google.golang.org/api/cloudbuild/v1"
)
// RemoteOptions provides the configuration to execute rebuilds on Cloud Build.
type RemoteOptions struct {
Project string
BuildServiceAccount string
LogsBucket string
MetadataBucket string
UtilPrebuildBucket string
// TODO: Consider moving this to Strategy.
UseTimewarp bool
}
type rebuildContainerArgs struct {
Instructions
UseTimewarp bool
UtilPrebuildBucket string
}
var rebuildContainerTpl = template.Must(
template.New(
"rebuild container",
).Funcs(template.FuncMap{
"indent": func(s string) string { return strings.ReplaceAll(s, "\n", "\n ") },
"join": func(sep string, s []string) string { return strings.Join(s, sep) },
}).Parse(
// NOTE: For syntax docs, see https://docs.docker.com/build/dockerfile/release-notes/
`#syntax=docker/dockerfile:1.4
{{- if .UseTimewarp}}
FROM gcr.io/cloud-builders/gsutil AS timewarp_provider
RUN gsutil cp -P gs://{{.UtilPrebuildBucket}}/timewarp .
{{- end}}
FROM alpine:3.19
{{- if .UseTimewarp}}
COPY --from=timewarp_provider ./timewarp .
{{- end}}
RUN <<'EOF'
set -eux
{{- if .UseTimewarp}}
./timewarp -port 8080 &
while ! nc -z localhost 8080;do sleep 1;done
{{- end}}
apk add {{join " " .Instructions.SystemDeps}}
mkdir /src && cd /src
{{.Instructions.Source| indent}}
{{.Instructions.Deps | indent}}
EOF
RUN cat <<'EOF' >build
set -eux
{{.Instructions.Build | indent}}
mkdir /out && cp /src/{{.Instructions.OutputPath}} /out/
EOF
WORKDIR "/src"
ENTRYPOINT ["/bin/sh","/build"]
`))
func makeBuild(t Target, dockerfile, imageUploadPath, rebuildUploadPath string, opts RemoteOptions) *cloudbuild.Build {
return &cloudbuild.Build{
LogsBucket: opts.LogsBucket,
Options: &cloudbuild.BuildOptions{Logging: "GCS_ONLY"},
ServiceAccount: opts.BuildServiceAccount,
Steps: []*cloudbuild.BuildStep{
{
Name: "gcr.io/cloud-builders/docker",
Script: "cat <<'EOS' | docker buildx build --tag=img -\n" + dockerfile + "\nEOS",
},
{
Name: "gcr.io/cloud-builders/docker",
Args: []string{"run", "--name=container", "img"},
},
{
Name: "gcr.io/cloud-builders/docker",
Args: []string{"cp", "container:" + path.Join("/out", t.Artifact), path.Join("/workspace", t.Artifact)},
},
{
Name: "gcr.io/cloud-builders/docker",
Script: "docker save img | gzip > /workspace/image.tgz",
},
{
Name: "gcr.io/cloud-builders/gsutil",
Script: fmt.Sprintf(
"gsutil cp -P gs://%s/gsutil_writeonly . && ./gsutil_writeonly %s && ./gsutil_writeonly %s",
opts.UtilPrebuildBucket,
strings.Join([]string{
"cp",
"/workspace/image.tgz",
imageUploadPath,
}, " "),
strings.Join([]string{
"cp",
path.Join("/workspace", t.Artifact),
rebuildUploadPath,
}, " "),
),
},
},
}
}
func doCloudBuild(ctx context.Context, client gcb.Client, build *cloudbuild.Build, opts RemoteOptions, bi *BuildInfo) error {
build, err := gcb.DoBuild(ctx, client, opts.Project, build)
if err != nil {
return errors.Wrap(err, "doing build")
}
bi.BuildEnd, err = time.Parse(time.RFC3339, build.FinishTime)
if err != nil {
return errors.Wrap(err, "extracting FinishTime")
}
bi.BuildID = build.Id
bi.Steps = build.Steps
bi.BuildImages = make(map[string]string)
for i, s := range bi.Steps {
bi.BuildImages[s.Name] = build.Results.BuildStepImages[i]
}
return nil
}
func makeDockerfile(input Input, opts RemoteOptions) (string, error) {
env := BuildEnv{HasRepo: false, PreferPreciseToolchain: true}
if opts.UseTimewarp {
env.TimewarpHost = "localhost:8080"
}
instructions, err := input.Strategy.GenerateFor(input.Target, env)
if err != nil {
return "", errors.Wrap(err, "failed to generate strategy")
}
dockerfile := new(bytes.Buffer)
err = rebuildContainerTpl.Execute(dockerfile, rebuildContainerArgs{
UseTimewarp: opts.UseTimewarp,
UtilPrebuildBucket: opts.UtilPrebuildBucket,
Instructions: instructions,
})
if err != nil {
return "", errors.Wrap(err, "populating template")
}
return dockerfile.String(), nil
}
// RebuildRemote executes the given target strategy on a remote builder.
func RebuildRemote(ctx context.Context, input Input, id string, opts RemoteOptions) error {
t := input.Target
bi := BuildInfo{Target: t, ID: id, Builder: os.Getenv("K_REVISION"), BuildStart: time.Now()}
metadata, err := NewGCSStore(context.WithValue(ctx, RunID, id), "gs://"+opts.MetadataBucket)
if err != nil {
return errors.Wrap(err, "creating metadata uploader")
}
dockerfile, err := makeDockerfile(input, opts)
if err != nil {
return errors.Wrap(err, "creating dockerfile")
}
{
w, _, err := metadata.Writer(ctx, Asset{Target: t, Type: DockerfileAsset})
if err != nil {
return errors.Wrap(err, "creating writer for Dockerfile")
}
defer w.Close()
if _, err := io.WriteString(w, dockerfile); err != nil {
return errors.Wrap(err, "writing Dockerfile")
}
}
// NOTE: Ignore the local writer since GCS doesn't flush writes until Close.
// TODO: Could be resolved by adding ResourcePath() method.
_, imageUploadPath, err := metadata.Writer(ctx, Asset{Target: t, Type: ContainerImageAsset})
if err != nil {
return errors.Wrap(err, "creating dummy writer for container image")
}
_, rebuildUploadPath, err := metadata.Writer(ctx, Asset{Target: t, Type: RebuildAsset})
if err != nil {
return errors.Wrap(err, "creating dummy writer for rebuild")
}
build := makeBuild(t, dockerfile, imageUploadPath, rebuildUploadPath, opts)
svc, err := cloudbuild.NewService(ctx)
if err != nil {
return errors.Wrap(err, "creating CloudBuild service")
}
if err := doCloudBuild(ctx, &gcb.Service{Service: svc}, build, opts, &bi); err != nil {
return errors.Wrap(err, "performing build")
}
{
w, _, err := metadata.Writer(ctx, Asset{Target: t, Type: BuildInfoAsset})
if err != nil {
return errors.Wrap(err, "creating writer for build info")
}
defer w.Close()
if err := json.NewEncoder(w).Encode(bi); err != nil {
return errors.Wrap(err, "marshalling and writing build info")
}
}
return nil
}