forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attach_artifacts.go
139 lines (112 loc) · 3.41 KB
/
attach_artifacts.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package command
import (
"context"
"os"
"path/filepath"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/artifact"
"github.com/evergreen-ci/evergreen/rest/client"
"github.com/evergreen-ci/evergreen/util"
"github.com/mitchellh/mapstructure"
"github.com/mongodb/grip"
"github.com/pkg/errors"
)
type attachArtifacts struct {
Files []string `mapstructure:"files" plugin:"expand"`
// Optional, when set to true, causes this command to be skipped over without an error when
// the path specified in files does not exist. Defaults to false, which triggers errors
// for missing files.
Optional bool `mapstructure:"optional"`
base
}
func attachArtifactsFactory() Command { return &attachArtifacts{} }
func (c *attachArtifacts) Name() string { return "attach.artifacts" }
func (c *attachArtifacts) ParseParams(params map[string]interface{}) error {
if err := mapstructure.Decode(params, c); err != nil {
return errors.Wrapf(err, "error decoding '%s' params", c.Name())
}
if len(c.Files) == 0 {
return errors.Errorf("error validating params: must specify at least one "+
"file pattern to parse: '%+v'", params)
}
return nil
}
func (c *attachArtifacts) Execute(ctx context.Context,
comm client.Communicator, logger client.LoggerProducer, conf *model.TaskConfig) error {
var err error
if err = util.ExpandValues(c, conf.Expansions); err != nil {
err = errors.Wrap(err, "error expanding params")
logger.Task().Error(err)
return err
}
c.Files, err = util.BuildFileList(conf.WorkDir, c.Files...)
if err != nil {
err = errors.Wrap(err, "problem building wildcard paths")
logger.Task().Error(err)
return err
}
if len(c.Files) == 0 {
err = errors.New("expanded file specification had no items")
logger.Task().Error(err)
if c.Optional {
return nil
}
return err
}
catcher := grip.NewBasicCatcher()
missedSegments := 0
files := []*artifact.File{}
var segment []*artifact.File
for idx := range c.Files {
segment, err = readArtifactsFile(conf.WorkDir, c.Files[idx])
if err != nil {
if c.Optional && os.IsNotExist(errors.Cause(err)) {
// pass;
} else {
catcher.Add(err)
}
continue
}
if segment == nil {
missedSegments++
continue
}
files = append(files, segment...)
}
if catcher.HasErrors() {
err = errors.Wrap(catcher.Resolve(), "encountered errors reading artifact json files")
logger.Task().Error(err)
return err
}
if missedSegments > 0 {
logger.Task().Noticef("encountered %d empty file definitions", missedSegments)
}
if len(files) == 0 {
logger.Task().Warning("no artifacts defined")
return nil
}
td := client.TaskData{ID: conf.Task.Id, Secret: conf.Task.Secret}
if err = comm.AttachFiles(ctx, td, files); err != nil {
return errors.Wrap(err, "attach artifacts failed")
}
logger.Task().Infof("'%s' attached %d resources to task", c.Name(), len(files))
return nil
}
func readArtifactsFile(wd, fn string) ([]*artifact.File, error) {
if !filepath.IsAbs(fn) {
fn = filepath.Join(wd, fn)
}
if _, err := os.Stat(fn); os.IsNotExist(err) {
return nil, errors.Wrapf(err, "file '%s' does not exist", fn)
}
file, err := os.Open(fn)
if err != nil {
return nil, errors.Wrapf(err, "problem opening file '%s'", fn)
}
defer file.Close()
out := []*artifact.File{}
if err = util.ReadJSONInto(file, &out); err != nil {
return nil, errors.Wrapf(err, "problem reading JSON from file '%s'", fn)
}
return out, nil
}