-
Notifications
You must be signed in to change notification settings - Fork 168
/
stages.go
72 lines (60 loc) · 1.9 KB
/
stages.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
package templates
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/caicloud/nirvana/log"
"k8s.io/apimachinery/pkg/util/yaml"
"github.com/caicloud/cyclone/pkg/apis/cyclone/v1alpha1"
"github.com/caicloud/cyclone/pkg/server/biz/utils"
)
const (
// TemplatesPathEnvName is templates path environment name
TemplatesPathEnvName = "TEMPLATES_PATH"
// DefaultTemplatesPath is default path of stage templates
DefaultTemplatesPath = "/root/templates"
)
// StageTemplatesLoader loads stage templates from manifest files.
type StageTemplatesLoader struct {
// TemplatesDir is path of the folder holding templates manifest files.
// If not set, default value DefaultTemplatesPath will be used.
TemplatesDir string
}
// LoadStageTemplates loads templates from the manifest files.
// scene - Scene of the Workflow, for example, cicd. If provided, only templates
// for that scene would be loaded, otherwise all templates will be loaded.
func (l *StageTemplatesLoader) LoadStageTemplates(scene string) ([]*v1alpha1.Stage, error) {
templatesPath := l.TemplatesDir
if l.TemplatesDir == "" {
templatesPath = DefaultTemplatesPath
}
if scene != "" {
templatesPath = templatesPath + string(os.PathSeparator) + scene
}
log.Infof("Load stage templates from '%s'", templatesPath)
files, err := utils.ListAllFiles(templatesPath)
if err != nil {
return nil, err
}
var results []*v1alpha1.Stage
for _, f := range files {
data, err := ioutil.ReadFile(f)
if err != nil {
log.Errorf("Load templates %s failed: %v", f, err)
continue
}
templates := make([]*v1alpha1.Stage, 0)
jsonData, err := yaml.ToJSON(data)
if err != nil {
log.Errorf("Convert template %s from YAML to JSON error: %v", f, err)
continue
}
err = json.Unmarshal(jsonData, &templates)
if err != nil {
log.Errorf("unmarshal template failed: %v", err)
continue
}
results = append(results, templates...)
}
return results, nil
}