artifacts_uploader.go 3.27 KB
Newer Older
1
package helpers
2
3

import (
4
	"errors"
Kamil Trzciński's avatar
Kamil Trzciński committed
5
	"fmt"
6
7
	"io"
	"os"
8
	"path/filepath"
9
10
	"time"

11
	"github.com/sirupsen/logrus"
12
	"github.com/urfave/cli"
13

14
15
16
17
	"gitlab.com/gitlab-org/gitlab-runner/common"
	"gitlab.com/gitlab-org/gitlab-runner/helpers/archives"
	"gitlab.com/gitlab-org/gitlab-runner/helpers/formatter"
	"gitlab.com/gitlab-org/gitlab-runner/network"
18
19
)

20
21
const DefaultUploadName = "default"

22
type ArtifactsUploaderCommand struct {
23
	common.JobCredentials
Kamil Trzcinski's avatar
Kamil Trzcinski committed
24
25
26
	fileArchiver
	retryHelper
	network common.Network
27

Kamil Trzciński's avatar
Kamil Trzciński committed
28
29
	Name     string                `long:"name" description:"The name of the archive"`
	ExpireIn string                `long:"expire-in" description:"When to expire artifacts"`
30
31
	Format   common.ArtifactFormat `long:"artifact-format" description:"Format of generated artifacts"`
	Type     string                `long:"artifact-type" description:"Type of generated artifacts"`
Kamil Trzciński's avatar
Kamil Trzciński committed
32
33
34
35
36
37
38
}

func (c *ArtifactsUploaderCommand) generateZipArchive(w *io.PipeWriter) {
	err := archives.CreateZipArchive(w, c.sortedFiles())
	w.CloseWithError(err)
}

39
func (c *ArtifactsUploaderCommand) generateGzipStream(w *io.PipeWriter) {
40
	err := archives.CreateGzipArchive(w, c.sortedFiles())
41
42
43
	w.CloseWithError(err)
}

Kamil Trzciński's avatar
Kamil Trzciński committed
44
func (c *ArtifactsUploaderCommand) createReadStream() (string, io.ReadCloser, error) {
45
46
47
48
	if len(c.files) == 0 {
		return "", nil, nil
	}

49
50
51
52
53
	name := filepath.Base(c.Name)
	if name == "" || name == "." {
		name = DefaultUploadName
	}

Kamil Trzciński's avatar
Kamil Trzciński committed
54
	switch c.Format {
55
	case common.ArtifactFormatZip, common.ArtifactFormatDefault:
Kamil Trzciński's avatar
Kamil Trzciński committed
56
57
		pr, pw := io.Pipe()
		go c.generateZipArchive(pw)
58
		return name + ".zip", pr, nil
Kamil Trzciński's avatar
Kamil Trzciński committed
59

60
61
62
	case common.ArtifactFormatGzip:
		pr, pw := io.Pipe()
		go c.generateGzipStream(pw)
63
		return name + ".gz", pr, nil
Kamil Trzciński's avatar
Kamil Trzciński committed
64
65
66
67

	default:
		return "", nil, fmt.Errorf("unsupported archive format: %s", c.Format)
	}
68
69
}

Kamil Trzcinski's avatar
Kamil Trzcinski committed
70
func (c *ArtifactsUploaderCommand) createAndUpload() (bool, error) {
Kamil Trzciński's avatar
Kamil Trzciński committed
71
72
73
74
	artifactsName, stream, err := c.createReadStream()
	if err != nil {
		return false, err
	}
75
76
77
78
	if stream == nil {
		logrus.Errorln("No files to upload")
		return false, nil
	}
Kamil Trzciński's avatar
Kamil Trzciński committed
79
	defer stream.Close()
80
81

	// Create the archive
Kamil Trzciński's avatar
Kamil Trzciński committed
82
83
84
85
86
87
	options := common.ArtifactsOptions{
		BaseName: artifactsName,
		ExpireIn: c.ExpireIn,
		Format:   c.Format,
		Type:     c.Type,
	}
88

89
	// Upload the data
Kamil Trzciński's avatar
Kamil Trzciński committed
90
	switch c.network.UploadRawArtifacts(c.JobCredentials, stream, options) {
Kamil Trzcinski's avatar
Kamil Trzcinski committed
91
92
93
94
95
96
97
98
99
100
101
	case common.UploadSucceeded:
		return false, nil
	case common.UploadForbidden:
		return false, os.ErrPermission
	case common.UploadTooLarge:
		return false, errors.New("Too large")
	case common.UploadFailed:
		return true, os.ErrInvalid
	default:
		return false, os.ErrInvalid
	}
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
}

func (c *ArtifactsUploaderCommand) Execute(*cli.Context) {
	formatter.SetRunnerFormatter()

	if len(c.URL) == 0 || len(c.Token) == 0 {
		logrus.Fatalln("Missing runner credentials")
	}
	if c.ID <= 0 {
		logrus.Fatalln("Missing build ID")
	}

	// Enumerate files
	err := c.enumerate()
	if err != nil {
		logrus.Fatalln(err)
	}

	// If the upload fails, exit with a non-zero exit code to indicate an issue?
Kamil Trzcinski's avatar
Kamil Trzcinski committed
121
122
123
	err = c.doRetry(c.createAndUpload)
	if err != nil {
		logrus.Fatalln(err)
124
125
126
127
	}
}

func init() {
Kamil Trzcinski's avatar
Kamil Trzcinski committed
128
	common.RegisterCommand2("artifacts-uploader", "create and upload build artifacts (internal)", &ArtifactsUploaderCommand{
129
		network: network.NewGitLabClient(),
Kamil Trzcinski's avatar
Kamil Trzcinski committed
130
131
132
133
		retryHelper: retryHelper{
			Retry:     2,
			RetryTime: time.Second,
		},
134
		Name: "artifacts",
Kamil Trzcinski's avatar
Kamil Trzcinski committed
135
	})
136
}