-
Notifications
You must be signed in to change notification settings - Fork 0
/
artifacts_downloader.go
79 lines (68 loc) · 1.78 KB
/
artifacts_downloader.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
package helpers
import (
"io/ioutil"
"os"
"time"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/archives"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers/formatter"
"gitlab.com/gitlab-org/gitlab-ci-multi-runner/network"
)
type ArtifactsDownloaderCommand struct {
common.BuildCredentials
retryHelper
network common.Network
}
func (c *ArtifactsDownloaderCommand) download(file string) (bool, error) {
switch c.network.DownloadArtifacts(c.BuildCredentials, file) {
case common.DownloadSucceeded:
return false, nil
case common.DownloadNotFound:
return false, os.ErrNotExist
case common.DownloadForbidden:
return false, os.ErrPermission
case common.DownloadFailed:
return true, os.ErrInvalid
default:
return false, os.ErrInvalid
}
}
func (c *ArtifactsDownloaderCommand) Execute(context *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")
}
// Create temporary file
file, err := ioutil.TempFile("", "artifacts")
if err != nil {
logrus.Fatalln(err)
}
file.Close()
defer os.Remove(file.Name())
// Download artifacts file
err = c.doRetry(func() (bool, error) {
return c.download(file.Name())
})
if err != nil {
logrus.Fatalln(err)
}
// Extract artifacts file
err = archives.ExtractZipFile(file.Name())
if err != nil {
logrus.Fatalln(err)
}
}
func init() {
common.RegisterCommand2("artifacts-downloader", "download and extract build artifacts (internal)", &ArtifactsDownloaderCommand{
network: &network.GitLabClient{},
retryHelper: retryHelper{
Retry: 2,
RetryTime: time.Second,
},
})
}