-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
543 lines (499 loc) · 13.8 KB
/
upload.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// Copyright 2023 Dolthub, Inc.
//
// 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 main
import (
"archive/tar"
"context"
"crypto/sha256"
"encoding/base32"
"encoding/hex"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/google/uuid"
"github.com/schollz/progressbar/v3"
"golang.org/x/sync/errgroup"
)
// Download a supported C compiler targeting linux-arm64 so we can build a
// statically compiled dolt binary. Return environment variables for
// configuring CGO to compile with this compiler.
func StageCompiler() ([]string, error) {
type location struct {
url string
sha string
}
var urls = map[string]location{
"darwin-arm64": {
url: "https://dolthub-tools.s3.us-west-2.amazonaws.com/gcc/host=aarch64-darwin/target=linux-musl/20240515_0.0.2.tar.xz",
sha: "c3fe69b5f412c17f18efc8ddcdec4128f0103242c76b99adb3cdcf8a2c45ec89",
},
"darwin-amd64": {
url: "https://dolthub-tools.s3.us-west-2.amazonaws.com/gcc/host=x86_64-darwin/target=linux-musl/20240515_0.0.2.tar.xz",
sha: "f1eda39fa81a3eaab4f79f0f010a2d6bf0aea395e65b3a6e87541f55cf2ac853",
},
"linux-arm64": {
url: "https://dolthub-tools.s3.us-west-2.amazonaws.com/gcc/host=aarch64-linux/target=linux-musl/20240515_0.0.2.tar.xz",
sha: "b603a5c636547e1cd0dc6cf1bba5a1f67aacb8dd21f1b12582786497311f1fa9",
},
"linux-amd64": {
url: "https://dolthub-tools.s3.us-west-2.amazonaws.com/gcc/host=x86_64-linux/target=linux-musl/20240515_0.0.2.tar.xz",
sha: "befaa4d83d843b8a57ea0e6a16980ffa5b5ba575f4428adec1f7f5b1aa7671f1",
},
}
plat := runtime.GOOS + "-" + runtime.GOARCH
loc, ok := urls[plat]
if !ok {
return nil, fmt.Errorf("unsupported runtime platform for lambda bats, %s; lambdabats needs to download a C toolchain targetting aarch64-linux-musl to run successfully", plat)
}
dest := filepath.Join(os.TempDir(), loc.sha)
finalVars := []string{
"CGO_ENABLED=1",
fmt.Sprintf("PATH=%s/bin%c%s", dest, filepath.ListSeparator, os.Getenv("PATH")),
"CC=aarch64-linux-musl-gcc",
"AS=aarch64-linux-musl-as",
"CGO_LDFLAGS=-static -s",
}
_, err := os.Stat(dest)
if err == nil {
// Toolchain is already downloaded and extracted.
return finalVars, nil
}
f, err := os.CreateTemp("", "lambdabats-toolchain-download")
if err != nil {
return nil, fmt.Errorf("could not create temp file for toolchain download: %w", err)
}
defer os.Remove(f.Name())
resp, err := http.Get(loc.url)
if err != nil {
return nil, fmt.Errorf("could not HTTP GET toolchain url: %s: %w", loc.url, err)
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("unexpected HTTP status for HTTP GET toolchain url: %s: %d", loc.url, resp.Status)
}
h := sha256.New()
w := io.MultiWriter(f, h)
_, err = io.Copy(w, resp.Body)
resp.Body.Close()
f.Close()
if err != nil {
return nil, fmt.Errorf("could not copy all bytes from response body of toolchain url: %s: %w", loc.url, err)
}
if hashRes := hex.EncodeToString(h.Sum(nil)); hashRes != loc.sha {
return nil, fmt.Errorf("downloading toolchain failed; download checksum (%s) did not match expected checksum (%s)", hashRes, loc.sha)
}
dir, err := os.MkdirTemp("", "extracted-lambdabats-toolchain-download")
if err != nil {
return nil, fmt.Errorf("could not create temp directory for toolchain extraction: %w", err)
}
defer os.RemoveAll(dir)
out, err := exec.Command("tar", "Jx", "-C", dir, "--strip-components", "1", "-f", f.Name()).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("could not create extract downloaded toolchain: %s: %w", string(out), err)
}
err = os.Rename(dir, dest)
if err != nil {
return nil, fmt.Errorf("could not rename extracted toolchain to final destination: %w", err)
}
return finalVars, nil
}
func BuildTestsFile(doltSrcDir string) (UploadArtifacts, error) {
binDir := filepath.Join(os.TempDir(), uuid.New().String())
defer os.RemoveAll(binDir)
doltBinFilePath := filepath.Join(binDir, "dolt")
compileEnv := append(os.Environ(), "GOOS=linux", "GOARCH=arm64")
err := RunWithSpinner("downloading toolchain...", func() error {
vars, err := StageCompiler()
if err != nil {
return fmt.Errorf("unable to stage compiler toolchain: %w", err)
}
compileEnv = append(compileEnv, vars...)
return nil
})
if err != nil {
return UploadArtifacts{}, err
}
err = RunWithSpinner("building dolt...", func() error {
compileDolt := exec.Command("go")
compileDolt.Args = []string{
"go", "build", "-ldflags=-linkmode external -s -w", "-o", doltBinFilePath, "./cmd/dolt",
}
compileDolt.Dir = filepath.Join(doltSrcDir, "go")
compileDolt.Env = compileEnv
out, err := compileDolt.CombinedOutput()
if err != nil {
return fmt.Errorf("error running go build dolt: %w\n%s", err, string(out))
}
return nil
})
if err != nil {
return UploadArtifacts{}, err
}
remotesrvBinFilePath := filepath.Join(binDir, "remotesrv")
err = RunWithSpinner("building remotesrv...", func() error {
compileRemotesrv := exec.Command("go")
compileRemotesrv.Args = []string{
"go", "build", "-ldflags=-linkmode external -s -w", "-o", remotesrvBinFilePath, "./utils/remotesrv",
}
compileRemotesrv.Dir = filepath.Join(doltSrcDir, "go")
compileRemotesrv.Env = compileEnv
err = compileRemotesrv.Run()
if err != nil {
return fmt.Errorf("error building remotesrv: %w", err)
}
return nil
})
if err != nil {
return UploadArtifacts{}, err
}
batsTarFilePath := filepath.Join(os.TempDir(), uuid.New().String())
defer os.RemoveAll(batsTarFilePath)
err = RunWithSpinner("building bats.tar...", func() error {
f, err := os.Create(batsTarFilePath)
if err != nil {
return err
}
defer f.Close()
w := tar.NewWriter(f)
defer w.Close()
dfs := os.DirFS(filepath.Join(doltSrcDir, "integration-tests"))
err = fs.WalkDir(dfs, "bats", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
fi, err := d.Info()
if err != nil {
return err
}
if d.IsDir() {
return w.WriteHeader(&tar.Header{
Name: path + "/",
Mode: int64(fi.Mode()),
})
} else {
return WriteFileToTar(w, &tar.Header{
Name: path,
Mode: int64(fi.Mode()),
}, filepath.Join(filepath.Join(doltSrcDir, "integration-tests"), path))
}
})
if err != nil {
return fmt.Errorf("error taring up bats tests: %w", err)
}
return nil
})
if err != nil {
return UploadArtifacts{}, err
}
doltHash := sha256.New()
f, err := os.Open(doltBinFilePath)
if err != nil {
return UploadArtifacts{}, err
}
_, err = io.Copy(doltHash, f)
f.Close()
if err != nil {
return UploadArtifacts{}, err
}
doltHashStr := base32.HexEncoding.EncodeToString(doltHash.Sum(nil))
doltTarPath := filepath.Join(os.TempDir(), doltHashStr+".tar")
err = func() error {
f, err := os.Create(doltTarPath)
if err != nil {
return err
}
defer f.Close()
w := tar.NewWriter(f)
defer w.Close()
err = w.WriteHeader(&tar.Header{
Name: "bin/",
Mode: 0777,
})
err = WriteFileToTar(w, &tar.Header{
Name: "bin/dolt",
Mode: 0777,
}, doltBinFilePath)
if err != nil {
return err
}
return nil
}()
if err != nil {
return UploadArtifacts{}, err
}
binHash := sha256.New()
f, err = os.Open(remotesrvBinFilePath)
if err != nil {
return UploadArtifacts{}, err
}
_, err = io.Copy(binHash, f)
f.Close()
if err != nil {
return UploadArtifacts{}, err
}
binHashStr := base32.HexEncoding.EncodeToString(binHash.Sum(nil))
binTarPath := filepath.Join(os.TempDir(), binHashStr+".tar")
err = func() error {
f, err := os.Create(binTarPath)
if err != nil {
return err
}
defer f.Close()
w := tar.NewWriter(f)
defer w.Close()
err = w.WriteHeader(&tar.Header{
Name: "bin/",
Mode: 0777,
})
err = WriteFileToTar(w, &tar.Header{
Name: "bin/remotesrv",
Mode: 0777,
}, remotesrvBinFilePath)
if err != nil {
return err
}
return nil
}()
if err != nil {
return UploadArtifacts{}, err
}
batsHash := sha256.New()
f, err = os.Open(batsTarFilePath)
if err != nil {
return UploadArtifacts{}, err
}
_, err = io.Copy(batsHash, f)
f.Close()
if err != nil {
return UploadArtifacts{}, err
}
batsHashStr := base32.HexEncoding.EncodeToString(batsHash.Sum(nil))
batsTarPath := filepath.Join(os.TempDir(), batsHashStr+".tar")
err = func() error {
f, err := os.Create(batsTarPath)
if err != nil {
return err
}
defer f.Close()
src, err := os.Open(batsTarFilePath)
if err != nil {
return err
}
defer src.Close()
_, err = io.Copy(f, src)
return err
}()
return UploadArtifacts{
DoltTarPath: doltTarPath,
BinTarPath: binTarPath,
TestsTarPath: batsTarPath,
}, nil
}
type UploadArtifacts struct {
DoltTarPath string
BinTarPath string
TestsTarPath string
}
type UploadLocations struct {
DoltPath string
BinPath string
TestsPath string
}
func WriteFileToTar(w *tar.Writer, header *tar.Header, path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return err
}
header.Size = stat.Size()
if header.Mode == 0 {
header.Mode = int64(stat.Mode())
}
err = w.WriteHeader(header)
if err != nil {
return err
}
_, err = io.Copy(w, f)
return err
}
func UploadTests(ctx context.Context, uploader Uploader, doltSrcDir string) (UploadLocations, error) {
artifacts, err := BuildTestsFile(doltSrcDir)
if err != nil {
return UploadLocations{}, err
}
defer os.RemoveAll(artifacts.DoltTarPath)
defer os.RemoveAll(artifacts.BinTarPath)
defer os.RemoveAll(artifacts.TestsTarPath)
err = uploader.Upload(ctx, artifacts)
if err != nil {
return UploadLocations{}, err
}
return UploadLocations{
DoltPath: filepath.Base(strings.TrimSuffix(artifacts.DoltTarPath, ".tar")),
BinPath: filepath.Base(strings.TrimSuffix(artifacts.BinTarPath, ".tar")),
TestsPath: filepath.Base(strings.TrimSuffix(artifacts.TestsTarPath, ".tar")),
}, nil
}
type Uploader interface {
Upload(ctx context.Context, artifacts UploadArtifacts) error
}
type CopyingUploader struct {
dir string
}
func (c *CopyingUploader) Upload(ctx context.Context, artifacts UploadArtifacts) error {
for _, path := range []string{artifacts.DoltTarPath, artifacts.BinTarPath, artifacts.TestsTarPath} {
destPath := filepath.Join(c.dir, filepath.Base(path))
src, err := os.Open(path)
if err != nil {
return err
}
defer src.Close()
dest, err := os.Create(destPath)
defer dest.Close()
if err != nil {
return err
}
_, err = io.Copy(dest, src)
if err != nil {
return err
}
}
// Sleep here to deal with macOS FUSE nonsense?
time.Sleep(1 * time.Second)
return nil
}
type S3Uploader struct {
s3client *s3.Client
bucket string
}
func NewS3Uploader(ctx context.Context, cfg aws.Config, bucket string) (*S3Uploader, error) {
return &S3Uploader{
s3client: s3.NewFromConfig(cfg),
bucket: bucket,
}, nil
}
func (d *S3Uploader) Upload(ctx context.Context, artifacts UploadArtifacts) error {
doltF, err := os.Open(artifacts.DoltTarPath)
if err != nil {
return err
}
defer doltF.Close()
fi, err := doltF.Stat()
if err != nil {
return err
}
doltSize := fi.Size()
binF, err := os.Open(artifacts.BinTarPath)
if err != nil {
return err
}
defer binF.Close()
fi, err = binF.Stat()
if err != nil {
return err
}
binSize := fi.Size()
testsF, err := os.Open(artifacts.TestsTarPath)
if err != nil {
return err
}
defer testsF.Close()
fi, err = testsF.Stat()
if err != nil {
return err
}
testsSize := fi.Size()
size := doltSize + binSize + testsSize
bar := progressbar.DefaultBytes(size, "uploading tests to s3")
uploader := manager.NewUploader(d.s3client)
eg, egCtx := errgroup.WithContext(ctx)
eg.Go(func() error {
key := aws.String(filepath.Base(strings.TrimSuffix(artifacts.DoltTarPath, ".tar")))
bucket := aws.String(d.bucket)
_, err := d.s3client.HeadObject(egCtx, &s3.HeadObjectInput{
Key: key,
Bucket: bucket,
})
if err == nil {
// File is already uploaded.
bar.Add(int(doltSize))
return nil
}
rd := NewProgressBarReader(doltF, bar)
_, err = uploader.Upload(egCtx, &s3.PutObjectInput{
Key: key,
Bucket: bucket,
Body: rd,
ContentLength: aws.Int64(doltSize),
})
return err
})
eg.Go(func() error {
key := aws.String(filepath.Base(strings.TrimSuffix(artifacts.BinTarPath, ".tar")))
bucket := aws.String(d.bucket)
_, err := d.s3client.HeadObject(egCtx, &s3.HeadObjectInput{
Key: key,
Bucket: bucket,
})
if err == nil {
// File is already uploaded.
bar.Add(int(binSize))
return nil
}
rd := NewProgressBarReader(binF, bar)
_, err = uploader.Upload(egCtx, &s3.PutObjectInput{
Key: key,
Bucket: bucket,
Body: rd,
ContentLength: aws.Int64(binSize),
})
return err
})
eg.Go(func() error {
key := aws.String(filepath.Base(strings.TrimSuffix(artifacts.TestsTarPath, ".tar")))
bucket := aws.String(d.bucket)
_, err := d.s3client.HeadObject(egCtx, &s3.HeadObjectInput{
Key: key,
Bucket: bucket,
})
if err == nil {
// File is already uploaded.
bar.Add(int(testsSize))
return nil
}
rd := NewProgressBarReader(testsF, bar)
_, err = uploader.Upload(egCtx, &s3.PutObjectInput{
Key: key,
Bucket: bucket,
Body: rd,
ContentLength: aws.Int64(testsSize),
})
return err
})
return eg.Wait()
}