-
Notifications
You must be signed in to change notification settings - Fork 18
/
releaser.go
235 lines (195 loc) · 5.23 KB
/
releaser.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
/*
Package releaser aids with publishing your Go binaries efficiently and in a consistent way.
*/
package releaser
import (
"bytes"
"compress/gzip"
"context"
"crypto/sha256"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"golang.org/x/sync/errgroup"
"github.com/circleci/ex/closer"
)
var (
contentEncodingGZIP = "gzip"
contentTypeOctetStream = "application/octet-stream"
)
type Releaser struct {
s3 *s3.Client
uploader *manager.Uploader
}
func New(ctx context.Context) (*Releaser, error) {
aws, err := config.LoadDefaultConfig(ctx)
if err != nil {
return nil, err
}
r := NewWithClient(s3.NewFromConfig(aws))
return r, nil
}
func NewWithClient(client *s3.Client) *Releaser {
return &Releaser{
s3: client,
uploader: manager.NewUploader(client),
}
}
type PublishParameters struct {
Path string
Bucket string
App string
Version string
// IncludeFilter optionally allows filtering the files to be uploaded
IncludeFilter func(path string, info os.FileInfo) bool
// Tags optionally allows bucket tags to be applied
Tags map[string]string
}
func (r *Releaser) Publish(ctx context.Context, params PublishParameters) error {
err := r.uploadBinaries(ctx, params)
if err != nil {
return err
}
err = r.uploadChecksums(ctx, params)
if err != nil {
return err
}
return nil
}
type ReleaseParameters struct {
Bucket string
App string
Version string
// Environment optionally allows specifying the environment, defaults to "release"
Environment string
// Tags optionally allows bucket tags to be applied
Tags map[string]string
}
func (r *Releaser) Release(ctx context.Context, params ReleaseParameters) error {
if params.Environment == "" {
params.Environment = "release"
}
key := filepath.ToSlash(filepath.Join(params.App, params.Environment+".txt"))
fmt.Printf("Releasing: %q - %s\n", key, params.Version)
_, err := r.uploader.Upload(ctx, &s3.PutObjectInput{
Bucket: ¶ms.Bucket,
Body: strings.NewReader(params.Version),
Key: &key,
Tagging: encodeTags(params.Tags),
})
return err
}
func (r *Releaser) uploadBinaries(ctx context.Context, params PublishParameters) error {
return r.walkFiles(params.Path, params.IncludeFilter, func(path string, info os.FileInfo) (err error) {
key := fileKey(params.App, params.Version, strings.TrimPrefix(path, params.Path))
fmt.Printf("Uploading: %q\n", key)
//#nosec:G304 // Intentionally uploading file from disk
in, err := os.Open(path)
if err != nil {
return err
}
defer closer.ErrorHandler(in, &err)
g, _ := errgroup.WithContext(ctx)
defer func() {
ferr := g.Wait()
if ferr != nil {
err = ferr
}
}()
pr, pw := io.Pipe()
defer closer.ErrorHandler(pw, &err)
g.Go(func() error {
_, err := r.uploader.Upload(ctx, &s3.PutObjectInput{
Bucket: ¶ms.Bucket,
Body: pr,
Key: &key,
ContentEncoding: &contentEncodingGZIP,
ContentType: &contentTypeOctetStream,
Tagging: encodeTags(params.Tags),
})
if err != nil {
_ = pw.CloseWithError(err)
return err
}
return nil
})
gz := gzip.NewWriter(pw)
defer closer.ErrorHandler(gz, &err)
_, err = io.Copy(gz, in)
return err
})
}
func (r *Releaser) uploadChecksums(ctx context.Context, params PublishParameters) error {
var checksums bytes.Buffer
err := r.walkFiles(params.Path, params.IncludeFilter, func(path string, info os.FileInfo) (err error) {
//#nosec:G304 // Intentionally reading file from disk
f, err := os.Open(path)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return err
}
fileName := strings.TrimPrefix(path, params.Path)
fileName = strings.TrimPrefix(fileName, string(os.PathSeparator))
_, err = fmt.Fprintf(&checksums, "%x *%s\n", h.Sum(nil), filepath.ToSlash(fileName))
return err
})
if err != nil {
return err
}
checksumsFile := filepath.Join(params.Path, "checksums.txt")
fmt.Printf("Writing: %q\n", checksumsFile)
//#nosec:G306 // These permissions are intentional
err = os.WriteFile(checksumsFile, checksums.Bytes(), 0644)
if err != nil {
return err
}
key := fileKey(params.App, params.Version, "checksums.txt")
fmt.Printf("Uploading: %q\n", key)
_, err = r.uploader.Upload(ctx, &s3.PutObjectInput{
Bucket: ¶ms.Bucket,
Key: &key,
Body: &checksums,
})
return err
}
func (r *Releaser) walkFiles(basePath string, includeFn func(path string, info os.FileInfo) bool,
observerFn func(path string, info os.FileInfo) error) error {
return filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if includeFn != nil && !includeFn(path, info) {
return nil
}
return observerFn(path, info)
})
}
func fileKey(app, version, file string) string {
return filepath.ToSlash(filepath.Join(app, version, file))
}
func encodeTags(tags map[string]string) *string {
if len(tags) == 0 {
return nil
}
params := url.Values{}
for k, v := range tags {
params.Add(k, v)
}
encoded := params.Encode()
return &encoded
}