github.com/ssube/gitlab-ci-multi-runner@v1.2.1-0.20160607142738-b8d1285632e6/commands/helpers/cache_archiver.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os"
     7  	"path/filepath"
     8  	"time"
     9  
    10  	"github.com/Sirupsen/logrus"
    11  	"github.com/codegangsta/cli"
    12  
    13  	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
    14  	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/archives"
    15  )
    16  
    17  type CacheArchiverCommand struct {
    18  	fileArchiver
    19  	retryHelper
    20  	File string `long:"file" description:"The path to file"`
    21  	URL  string `long:"url" description:"Download artifacts instead of uploading them"`
    22  }
    23  
    24  func (c *CacheArchiverCommand) upload() (bool, error) {
    25  	logrus.Infoln("Uploading", filepath.Base(c.File))
    26  
    27  	file, err := os.Open(c.File)
    28  	if err != nil {
    29  		return false, err
    30  	}
    31  	defer file.Close()
    32  
    33  	fi, err := file.Stat()
    34  	if err != nil {
    35  		return false, err
    36  	}
    37  
    38  	req, err := http.NewRequest("PUT", c.URL, file)
    39  	if err != nil {
    40  		return true, err
    41  	}
    42  	req.Header.Set("Content-Type", "application/octet-stream")
    43  	req.Header.Set("Last-Modified", fi.ModTime().Format(http.TimeFormat))
    44  	req.ContentLength = fi.Size()
    45  
    46  	resp, err := http.DefaultClient.Do(req)
    47  	if err != nil {
    48  		return true, err
    49  	}
    50  	defer resp.Body.Close()
    51  
    52  	if resp.StatusCode/100 != 2 {
    53  		// Retry on server errors
    54  		retry := resp.StatusCode/100 == 5
    55  		return retry, fmt.Errorf("Received: %s", resp.Status)
    56  	}
    57  
    58  	return false, nil
    59  }
    60  
    61  func (c *CacheArchiverCommand) Execute(*cli.Context) {
    62  	if c.File == "" {
    63  		logrus.Fatalln("Missing --file")
    64  	}
    65  
    66  	// Enumerate files
    67  	err := c.enumerate()
    68  	if err != nil {
    69  		logrus.Fatalln(err)
    70  	}
    71  
    72  	// Check if list of files changed
    73  	if !c.isFileChanged(c.File) {
    74  		logrus.Infoln("Archive is up to date!")
    75  		return
    76  	}
    77  
    78  	// Create archive
    79  	err = archives.CreateZipFile(c.File, c.sortedFiles())
    80  	if err != nil {
    81  		logrus.Fatalln(err)
    82  	}
    83  
    84  	// Upload archive if needed
    85  	if c.URL != "" {
    86  		err := c.doRetry(c.upload)
    87  		if err != nil {
    88  			logrus.Warningln(err)
    89  		}
    90  	}
    91  }
    92  
    93  func init() {
    94  	common.RegisterCommand2("cache-archiver", "create and upload cache artifacts (internal)", &CacheArchiverCommand{
    95  		retryHelper: retryHelper{
    96  			Retry:     2,
    97  			RetryTime: time.Second,
    98  		},
    99  	})
   100  }