Skip to content

Commit f32752e

Browse files
iQQBotcsweichel
authored andcommitted
minio support
1 parent bc6c6dd commit f32752e

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

cmd/experiemental-unmount.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build linux
12
// +build linux
23

34
package cmd

cmd/experimental-mount.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build linux
12
// +build linux
23

34
package cmd

cmd/root.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ const (
1919
// EnvvarWorkspaceRoot names the environment variable we check for the workspace root path
2020
EnvvarWorkspaceRoot = "LEEWAY_WORKSPACE_ROOT"
2121

22-
// EnvvarRemoteCacheBucket configures a GCP bucket name. This enables the use of GSUtilRemoteStorage
22+
// EnvvarRemoteCacheBucket configures a bucket name. This enables the use of RemoteStorage
2323
EnvvarRemoteCacheBucket = "LEEWAY_REMOTE_CACHE_BUCKET"
24+
25+
// EnvvarRemoteCacheStorage configures a Remote Storage Provider. Default is GCP
26+
EnvvarRemoteCacheStorage = "LEEWAY_REMOTE_CACHE_STORAGE"
2427
)
2528

2629
const (
@@ -171,10 +174,23 @@ func getBuildArgs() (leeway.Arguments, error) {
171174

172175
func getRemoteCache() leeway.RemoteCache {
173176
remoteCacheBucket := os.Getenv(EnvvarRemoteCacheBucket)
177+
remoteStorage := os.Getenv(EnvvarRemoteCacheStorage)
174178
if remoteCacheBucket != "" {
175-
return leeway.GSUtilRemoteCache{
176-
BucketName: remoteCacheBucket,
179+
switch remoteStorage {
180+
case "GCP":
181+
return leeway.GSUtilRemoteCache{
182+
BucketName: remoteCacheBucket,
183+
}
184+
case "MINIO":
185+
return leeway.MinioRemoteCache{
186+
BucketName: remoteCacheBucket,
187+
}
188+
default:
189+
return leeway.GSUtilRemoteCache{
190+
BucketName: remoteCacheBucket,
191+
}
177192
}
193+
178194
}
179195

180196
return leeway.NoRemoteCache{}

pkg/leeway/cache.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,82 @@ func gsutilTransfer(target string, files []string) error {
154154
}
155155
return nil
156156
}
157+
158+
// MinioRemoteCache uses the mc command to implement a remote cache
159+
type MinioRemoteCache struct {
160+
BucketName string
161+
}
162+
163+
// Download makes a best-effort attempt at downloading previously cached build artifacts
164+
func (rs MinioRemoteCache) Download(dst Cache, pkgs []*Package) error {
165+
fmt.Printf("☁️ minio checking remote cache for past build artifacts\n")
166+
var (
167+
files []string
168+
dest string
169+
)
170+
for _, pkg := range pkgs {
171+
fn, exists := dst.Location(pkg)
172+
if exists {
173+
continue
174+
}
175+
if dest == "" {
176+
dest = filepath.Dir(fn)
177+
} else if dest != filepath.Dir(fn) {
178+
return xerrors.Errorf("gsutil only supports one target folder, not %s and %s", dest, filepath.Dir(fn))
179+
}
180+
181+
files = append(files, fmt.Sprintf("minio/%s/%s", rs.BucketName, filepath.Base(fn)))
182+
}
183+
return minioTransfer(dest, files)
184+
}
185+
186+
// Upload makes a best effort to upload the build arfitacts to a remote cache
187+
func (rs MinioRemoteCache) Upload(src Cache, pkgs []*Package) error {
188+
fmt.Printf("☁️ minio uploading build artifacts to remote cache\n")
189+
var files []string
190+
for _, pkg := range pkgs {
191+
file, exists := src.Location(pkg)
192+
if !exists {
193+
continue
194+
}
195+
files = append(files, file)
196+
}
197+
return minioTransfer(fmt.Sprintf("minio/%s", rs.BucketName), files)
198+
}
199+
200+
func minioTransfer(target string, files []string) error {
201+
if len(files) == 0 {
202+
return nil
203+
}
204+
log.WithField("target", target).WithField("files", files).Debug("transfering files using gsutil")
205+
runCopyCommand := func(source, target string) error {
206+
args := make([]string, 0, 3)
207+
args = append(args, "cp")
208+
args = append(args, source)
209+
args = append(args, target)
210+
cmd := exec.Command("mc", args...)
211+
cmd.Stdout = os.Stdout
212+
err := cmd.Start()
213+
if err != nil {
214+
return err
215+
}
216+
err = cmd.Wait()
217+
if err != nil {
218+
if exiterr, ok := err.(*exec.ExitError); ok {
219+
if _, ok := exiterr.Sys().(syscall.WaitStatus); ok {
220+
// we just swallow non-zero exit codes here as remote caching is best effort
221+
return nil
222+
}
223+
}
224+
return err
225+
}
226+
return nil
227+
}
228+
for _, source := range files {
229+
err := runCopyCommand(source, target)
230+
if err != nil {
231+
return err
232+
}
233+
}
234+
return nil
235+
}

0 commit comments

Comments
 (0)