-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplates.go
195 lines (168 loc) · 4.74 KB
/
templates.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
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"text/template"
"gopkg.in/yaml.v2"
)
const template_file = "templates.yaml"
// Contains functions to manage source code from templates
type YamlTemplates struct {
Defaults DefaultsStruct
Features TmplFeatures
Sources TmplSources
Run map[string]DeployStepsRunStruct `yaml:"run_deploy"`
Build map[string]BuildStepsRunStruct `yaml:"run_build"`
Variants map[string]string
}
type DefaultsStruct struct {
Dockerfile DockerfileStruct
JenkinsImage FinalImageStruct
}
type TmplFeatures struct {
Common TmplFeaturesStruct
Deploy map[string]TmplFeaturesStruct
}
type TmplFeaturesStruct []string
type TmplSources struct {
Common TmplSourcesStruct
Deploy map[string]TmplSourcesStruct
}
type TmplSourcesStruct map[string]TmplSource
type EnvStruct struct {
Value string
If string
}
// SourceModel creates the Model data used by gotemplates in create/update context.
// The model is not updated until call to CleanModel()
func (p *JenkinsPlugin) SourceModel() *JenkinsPluginSourceModel {
if JPS_Model != nil {
return JPS_Model
}
JPS_Model = new(JenkinsPluginSourceModel)
JPS_Model.Source = p.yaml
return JPS_Model
}
func (p *JenkinsPlugin) CleanSourceModel() {
JPS_Model = nil
}
func Evaluate(value string, data interface{}) (string, error) {
var doc bytes.Buffer
tmpl := template.New("jenkins_plugin_data")
if !strings.Contains(value, "{{") {
return value, nil
}
if _, err := tmpl.Parse(value); err != nil {
return "", err
}
if err := tmpl.Execute(&doc, data); err != nil {
return "", err
}
ret := doc.String()
log.Printf("'%s' were interpreted to '%s'", value, ret)
return ret, nil
}
//Load templates definition file from template dir.
func (p *JenkinsPlugin) LoadTemplatesDef() error {
templatef := path.Join(p.template_dir, template_file)
if _, err := os.Stat(templatef); err != nil {
return fmt.Errorf("Unable to find templates definition file '%s'. %s.", templatef, err)
}
if d, err := ioutil.ReadFile(templatef); err != nil {
return fmt.Errorf("Unable to load '%s'. %s.", templatef, err)
} else {
if err := yaml.Unmarshal(d, &p.templates_def); err != nil {
return fmt.Errorf("Unable to load yaml file format '%s'. %s.", templatef, err)
}
}
return nil
}
// Load list of files to copy and files to generate
func (p *JenkinsPlugin) DefineSources() error {
// load all features
p.yaml.Features = make([]string, 0, 5)
for _, f := range p.templates_def.Features.Common {
if v, err := Evaluate(f, p.SourceModel()); err != nil {
return fmt.Errorf("Unable to evaluate '%s'. %s", f, err)
} else {
if v == "" {
log.Printf("INFO! No feature defined with '%s'.", f)
continue
}
f = v
}
if f != "" {
p.yaml.Features = append(p.yaml.Features, f)
}
}
if deploy_features, ok := p.templates_def.Features.Deploy[p.yaml.Deploy.Deployment.To]; ok {
for _, f := range deploy_features {
if f != "" {
p.yaml.Features = append(p.yaml.Features, f)
}
}
}
// TODO: Load additionnal features from maintainer source path or file. This will permit adding more features and let the plugin manage generated path from update task.
// Load all sources
p.sources = make(map[string]TmplSource)
p.templates = make(map[string]TmplSource)
p.built = make(map[string]TmplSource)
p.generated = make(map[string]TmplSource)
choose_file := func(file string, f TmplSource) error {
if file == "" {
return nil
}
if f.If != "" {
if v, err := Evaluate(f.If, p.SourceModel()); err != nil {
return fmt.Errorf("Unable to evaluate the '%s' condition '%s'. %s", file, f.If, err)
} else {
if v == "" || strings.ToLower(v) == "false" {
log.Printf("Condition '%s' negative (false or empty). '%s' ignored.", f.If, file)
return nil
}
}
}
if f.Source != "" {
p.sources[file] = f
log.Printf("SRC : selected: %s", file)
} else if f.Template != "" {
p.templates[file] = f
log.Printf("TMPL: selected: %s", file)
} else if f.Built != "" {
p.built[file] = f
log.Printf("BUILT: selected: %s", file)
} else if f.Generated != "" {
p.generated[file] = f
log.Printf("GENERATED: selected: %s", file)
f.storeMD5(path.Join(p.source_path, f.Generated))
}
return nil
}
for file, f := range p.templates_def.Sources.Common {
if err := choose_file(file, f); err != nil {
return err
}
}
if deploy_sources, ok := p.templates_def.Sources.Deploy[p.yaml.Deploy.Deployment.To]; ok {
for file, f := range deploy_sources {
if err := choose_file(file, f); err != nil {
return err
}
}
}
p.CleanSourceModel()
return nil
}
func lookup(m map[string]interface{}, key string) (interface{}, error) {
val, ok := m[key]
if !ok {
return nil, errors.New("missing key " + key)
}
return val, nil
}