github.com/secure-build/gitlab-runner@v12.5.0+incompatible/commands/helpers/cache_archiver.go (about) 1 package helpers 2 3 import ( 4 "net/http" 5 "os" 6 "path/filepath" 7 "time" 8 9 "github.com/sirupsen/logrus" 10 "github.com/urfave/cli" 11 12 "gitlab.com/gitlab-org/gitlab-runner/common" 13 "gitlab.com/gitlab-org/gitlab-runner/helpers/archives" 14 "gitlab.com/gitlab-org/gitlab-runner/helpers/url" 15 "gitlab.com/gitlab-org/gitlab-runner/log" 16 ) 17 18 type CacheArchiverCommand struct { 19 fileArchiver 20 retryHelper 21 File string `long:"file" description:"The path to file"` 22 URL string `long:"url" description:"URL of remote cache resource"` 23 Timeout int `long:"timeout" description:"Overall timeout for cache uploading request (in minutes)"` 24 25 client *CacheClient 26 } 27 28 func (c *CacheArchiverCommand) getClient() *CacheClient { 29 if c.client == nil { 30 c.client = NewCacheClient(c.Timeout) 31 } 32 33 return c.client 34 } 35 36 func (c *CacheArchiverCommand) upload() error { 37 logrus.Infoln("Uploading", filepath.Base(c.File), "to", url_helpers.CleanURL(c.URL)) 38 39 file, err := os.Open(c.File) 40 if err != nil { 41 return err 42 } 43 defer file.Close() 44 45 fi, err := file.Stat() 46 if err != nil { 47 return err 48 } 49 50 req, err := http.NewRequest("PUT", c.URL, file) 51 if err != nil { 52 return retryableErr{err: err} 53 } 54 req.Header.Set("Content-Type", "application/octet-stream") 55 req.Header.Set("Last-Modified", fi.ModTime().Format(http.TimeFormat)) 56 req.ContentLength = fi.Size() 57 58 resp, err := c.getClient().Do(req) 59 if err != nil { 60 return retryableErr{err: err} 61 } 62 defer resp.Body.Close() 63 64 return retryOnServerError(resp) 65 } 66 67 func (c *CacheArchiverCommand) Execute(*cli.Context) { 68 log.SetRunnerFormatter() 69 70 if c.File == "" { 71 logrus.Fatalln("Missing --file") 72 } 73 74 // Enumerate files 75 err := c.enumerate() 76 if err != nil { 77 logrus.Fatalln(err) 78 } 79 80 // Check if list of files changed 81 if !c.isFileChanged(c.File) { 82 logrus.Infoln("Archive is up to date!") 83 84 return 85 } 86 87 // Create archive 88 err = archives.CreateZipFile(c.File, c.sortedFiles()) 89 if err != nil { 90 logrus.Fatalln(err) 91 } 92 93 // Upload archive if needed 94 if c.URL != "" { 95 err := c.doRetry(c.upload) 96 if err != nil { 97 logrus.Fatalln(err) 98 } 99 } else { 100 logrus.Infoln("No URL provided, cache will be not uploaded to shared cache server. Cache will be stored only locally.") 101 } 102 } 103 104 func init() { 105 common.RegisterCommand2("cache-archiver", "create and upload cache artifacts (internal)", &CacheArchiverCommand{ 106 retryHelper: retryHelper{ 107 Retry: 2, 108 RetryTime: time.Second, 109 }, 110 }) 111 }