-
Notifications
You must be signed in to change notification settings - Fork 86
/
build_trace_persister.go
71 lines (58 loc) · 1.86 KB
/
build_trace_persister.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
package worker
import (
"bytes"
"strconv"
gocontext "context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/travis-ci/worker/config"
)
// credentials are configured via env:
// * AWS_ACCESS_KEY_ID
// * AWS_SECRET_ACCESS_KEY
//
// or via the shared creds file ~/.aws/credentials
//
// or via the EC2 instance IAM role
// A BuildTracePersister persists a build trace. (duh)
type BuildTracePersister interface {
Persist(gocontext.Context, Job, []byte) error
}
type s3BuildTracePersister struct {
bucket string
keyPrefix string
region string
}
// NewBuildTracePersister creates a build trace persister backed by S3
func NewBuildTracePersister(cfg *config.Config) BuildTracePersister {
if !cfg.BuildTraceEnabled {
return nil
}
return &s3BuildTracePersister{
bucket: cfg.BuildTraceS3Bucket,
keyPrefix: cfg.BuildTraceS3KeyPrefix,
region: cfg.BuildTraceS3Region,
}
}
// TODO: cache aws session for reuse? goroutine pool?
// TODO: handle job restarts -- archive separate trace per run? use job UUID?
// perhaps we can rely on s3 versioning for this.
func (p *s3BuildTracePersister) Persist(ctx gocontext.Context, job Job, buf []byte) error {
sess, err := session.NewSession(&aws.Config{Region: aws.String(p.region)})
if err != nil {
return err
}
key := p.keyPrefix + strconv.FormatUint(job.Payload().Job.ID, 10)
_, err = s3.New(sess).PutObject(&s3.PutObjectInput{
Bucket: aws.String(p.bucket),
Key: aws.String(key),
ACL: aws.String("private"),
Body: bytes.NewReader(buf),
ContentLength: aws.Int64(int64(len(buf))),
ContentType: aws.String("application/octet-stream"),
ContentDisposition: aws.String("attachment"),
ServerSideEncryption: aws.String("AES256"),
})
return err
}