/
util.go
327 lines (286 loc) · 8.95 KB
/
util.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package pipelineymlv1
import (
"fmt"
"path/filepath"
"regexp"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
)
func (y *PipelineYml) FindDockerImageByResourceName(name string) (repository, tag string, err error) {
resMap := y.validResourceMap()
res, ok := resMap[name]
if !ok {
err = errors.Errorf("cannot find resource_type of resource: %s", name)
return
}
resTypeMap := y.validResTypesMap()
resType, ok := resTypeMap[res.Type]
if !ok {
err = errors.Errorf("cannot find docker image of resource_type: %s", name)
return
}
type imageSource struct {
Repository string
Tag string
}
var source imageSource
if err = mapstructure.Decode(&resType.Source, &source); err != nil {
return
}
repository = source.Repository
tag = source.Tag
if tag == "" {
tag = y.option.builtinResourceTypeDockerImageTag
}
return
}
func (y *PipelineYml) FindResourceByName(name string) (Resource, bool) {
resMap := y.validResourceMap()
res, ok := resMap[name]
if !ok {
return Resource{}, false
}
return res, true
}
func ResourceDir(suffix string) string {
return filepath.Join("/tmp", "build", suffix)
}
func GenerateFlowUUID(yUUID string) string {
return fmt.Sprintf("ccflow-%s", yUUID)
}
func GenerateStageUUID(stageName string, index int, yUUID string) string {
return strings.ToLower(fmt.Sprintf("ccstage-%d-%s-%s", index, stageName, yUUID))
}
// GenerateTaskUUID have some constrains
// 如果存在多个 job 执行器,需要同时满足约束
func GenerateTaskUUID(stageIndex int, stageName string, taskIndex int, taskName string, yUUID string) string {
taskUUID := strings.Replace(strings.ToLower(fmt.Sprintf("ccTask-%d-%s-%d-%s-%s", stageIndex, stageName, taskIndex, taskName, yUUID)), "_", "-", -1)
// satisfy constraints
/*
metronome:
Unique identifier for the job consisting of a series of names separated by dots.
Each name must be at least 1 character and may only contain digits (`0-9`), dashes (`-`), and lowercase letters (`a-z`).
The name may not begin or end with a dash."
*/
doMetronomeConstraints := func(taskUUID string) string {
metronomeRegexp := regexp.MustCompile(`[^a-zA-Z0-9-]+`)
taskUUID = metronomeRegexp.ReplaceAllString(taskUUID, "")
taskUUID = strings.TrimLeft(strings.TrimRight(taskUUID, "-"), "-")
return taskUUID
}
/*
demo
*/
doDemoConstraints := func(taskUUID string) string {
return taskUUID
}
return doDemoConstraints(doMetronomeConstraints(taskUUID))
}
// UpdatePipelineOnGlobal update pipeline object and byteData together.
func (y *PipelineYml) UpdatePipelineOnGlobal(kvs map[string]string) error {
// update global envs, just replace
y.obj.Envs = kvs
newYmlByte, err := y.YAML()
if err != nil {
return err
}
y.byteData = []byte(newYmlByte)
if err = y.Parse(); err != nil {
return err
}
return nil
}
func (y *PipelineYml) GetGlobalEnvs() map[string]string {
return y.obj.Envs
}
func (y *PipelineYml) GetTaskEnvs(taskUUID string) (map[string]string, error) {
for si, stage := range y.obj.Stages {
for ti, task := range stage.Tasks {
tmpUUID := GenerateTaskUUID(si, stage.Name, ti, task.Name(), y.metadata.instanceID)
if tmpUUID == taskUUID {
return task.GetEnvs(), nil
}
}
}
return nil, errors.Errorf("failed to get task envs, no matched task found, task uuid: %s", taskUUID)
}
// Update workspace into bp_args
func (y *PipelineYml) UpdatePipelineWorkspaceOnBpArgs(env string) error {
var checkType = func(t string) bool {
var rTypes = []string{string(RES_TYPE_BUILDPACK), string(RES_TYPE_BP_COMPILE)}
for _, rt := range rTypes {
if t == rt {
return true
}
}
return false
}
for i := range y.obj.Resources {
if checkType(y.obj.Resources[i].Type) {
if y.obj.Resources[i].Source["bp_args"] != nil {
if val, ok := y.obj.Resources[i].Source["bp_args"].(map[string]interface{}); ok {
val["DICE_WORKSPACE"] = strings.ToLower(env)
} else {
return errors.Errorf("invalid type of bp_args in Source of Resource %s", y.obj.Resources[i].Name)
}
} else {
y.obj.Resources[i].Source["bp_args"] = map[string]interface{}{"DICE_WORKSPACE": strings.ToLower(env)}
}
}
}
newYmlByte, err := y.YAML()
if err != nil {
return err
}
y.byteData = []byte(newYmlByte)
if err = y.Parse(); err != nil {
return err
}
return nil
}
// Update bp_args, docker_args.
func (y *PipelineYml) UpdatePipelineOnBpArgs(resource string, bp_args, bp_repo_args map[string]string) error {
for i := range y.obj.Resources {
if y.obj.Resources[i].Name == resource {
logrus.Infof("bp-args settings: %v", bp_args)
y.obj.Resources[i].Source["bp_args"] = bp_args
y.obj.Resources[i].Source["bp_repo"] = bp_repo_args["bp_repo"]
y.obj.Resources[i].Source["bp_ver"] = bp_repo_args["bp_ver"]
}
}
newYmlByte, err := y.YAML()
if err != nil {
return err
}
y.byteData = []byte(newYmlByte)
if err = y.Parse(); err != nil {
return err
}
return nil
}
func (y *PipelineYml) GetBpArgs(sourceId int) map[string]string {
if _, ok := y.obj.Resources[sourceId].Source["bp_args"]; !ok {
return nil
}
if val, ok := y.obj.Resources[sourceId].Source["bp_args"].(map[string]interface{}); ok {
result := make(map[string]string)
for k, v := range val {
result[k] = fmt.Sprint(v)
}
return result
}
return nil
}
func (y *PipelineYml) GetBpRepoArgs(sourceId int) map[string]string {
repo := make(map[string]string)
if _, ok := y.obj.Resources[sourceId].Source["bp_repo"]; !ok {
repo["bp_repo"] = ""
} else {
repo["bp_repo"] = y.obj.Resources[sourceId].Source["bp_repo"].(string)
}
if _, ok := y.obj.Resources[sourceId].Source["bp_ver"]; !ok {
repo["bp_ver"] = ""
} else {
repo["bp_ver"] = y.obj.Resources[sourceId].Source["bp_ver"].(string)
}
if repo["bp_repo"] == "" && repo["bp_ver"] == "" {
return nil
}
return repo
}
func (y *PipelineYml) GetResourceID(taskName string) int {
for id := range y.obj.Resources {
if y.obj.Resources[id].Name == taskName {
resourceType := y.obj.Resources[id].Type
if strings.HasPrefix(resourceType, string(RES_TYPE_BUILDPACK)) ||
strings.HasPrefix(resourceType, string(RES_TYPE_BP_COMPILE)) ||
strings.HasPrefix(resourceType, string(RES_TYPE_BP_IMAGE)) {
return id
}
}
}
return -1
}
func (y *PipelineYml) GetTaskDisableStatus(taskUUID string) (bool, error) {
for si, stage := range y.obj.Stages {
for ti, task := range stage.Tasks {
tmpUUID := GenerateTaskUUID(si, stage.Name, ti, task.Name(), y.metadata.instanceID)
if tmpUUID == taskUUID {
return task.IsDisable(), nil
}
}
}
return false, errors.Errorf("failed to get task disable status, no matched task found, task uuid: %s", taskUUID)
}
func (y *PipelineYml) GetTaskPauseStatus(taskUUID string) (bool, error) {
for si, stage := range y.obj.Stages {
for ti, task := range stage.Tasks {
tmpUUID := GenerateTaskUUID(si, stage.Name, ti, task.Name(), y.metadata.instanceID)
if tmpUUID == taskUUID {
return task.IsPause(), nil
}
}
}
return false, errors.Errorf("failed to get task pause status, no matched task found, task uuid: %s", taskUUID)
}
func (y *PipelineYml) GetTaskForceBuildpackStatus(taskUUID string) bool {
for si, stage := range y.obj.Stages {
for ti, task := range stage.Tasks {
tmpUUID := GenerateTaskUUID(si, stage.Name, ti, task.Name(), y.metadata.instanceID)
if tmpUUID == taskUUID {
if task.GetTaskParams() != nil {
if v, ok := task.GetTaskParams()["force_buildpack"]; ok {
if forceBuildpack, ok := v.(bool); ok {
return forceBuildpack
}
}
return false
}
}
}
}
return false
}
// ApplyKVsWithPriority generates a result map with the rule that
// the latter will overwrite the former.
// For example:
// priority: e1 > e2 > e3
// you should pass param `envs` as: e3, e2, e1
func ApplyKVsWithPriority(kvs ...map[string]string) map[string]string {
result := make(map[string]string)
for _, env := range kvs {
for k, v := range env {
result[k] = v
}
}
return result
}
func MetadataFields2Map(metas []apistructs.MetadataField) map[string]string {
m := make(map[string]string, len(metas))
for _, meta := range metas {
m[meta.Name] = meta.Value
}
return m
}
func Map2MetadataFields(m map[string]string) []apistructs.MetadataField {
var metas []apistructs.MetadataField
for k, v := range m {
metas = append(metas, apistructs.MetadataField{Name: k, Value: v})
}
return metas
}