forked from forj-oss/forjj-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins_plugin.go
379 lines (317 loc) · 12.1 KB
/
jenkins_plugin.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package main
import (
"bytes"
"crypto/md5"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/forj-oss/forjj/utils"
"github.com/forj-oss/goforjj"
yaml "gopkg.in/yaml.v2"
)
type JenkinsPluginSourceModel struct {
Source YamlJenkins
}
var JPS_Model *JenkinsPluginSourceModel
type JenkinsPlugin struct {
yaml YamlJenkins // jenkins.yaml deploy file.
yamlPlugin YamlJenkinsPlugin // jenkins.yaml source file
source_path string // Source Path (sourceMountPath + InstanceName)
deploysParentPath string // Deployment repository path
deployPath string // Deployment Path (deployRepoPath + deployEnv + InstanceName)
deployEnv string // Deployment environment where files have to be generated.
InstanceName string // Instance name where files have to be generated.
template_dir string
template_file string
templates_def YamlTemplates // See templates.go. templates.yaml structure.
run DeployStepsRunStruct
runTasks []string // Tasks to execute at update time.
sources map[string]TmplSource
templates map[string]TmplSource
built map[string]TmplSource
generated map[string]TmplSource
auths *DockerAuths
}
type YamlSSLStruct struct {
CaCertificate string `yaml:"ca-certificate,omitempty"` // CA root certificate which certify your jenkins instance.
Certificate string `yaml:"certificate,omitempty"` // SSL Certificate file to certify your jenkins instance.
key string // key for the SSL certificate.
Method string // SSL Method used in templates (none, selfsigned, manual, ...)
}
const jenkins_file = "forjj-jenkins.yaml"
const jenkinsDeployFile = "forjj-deploy.yaml"
const maintain_cmds_file = "maintain-cmd.yaml"
func newPlugin(srcRepoPath, deploysMountPath string) (p *JenkinsPlugin) {
p = new(JenkinsPlugin)
p.source_path = srcRepoPath
p.deploysParentPath = deploysMountPath
return
}
// setEnv set the deployment environment where deploy files have to be generated.
func (p *JenkinsPlugin) setEnv(deployEnv, instanceName string) {
p.deployPath = path.Join(p.deploysParentPath, deployEnv, instanceName)
p.deployEnv = deployEnv
p.InstanceName = instanceName
}
// setRunTasks get a list of tasks from a formatted string
func (p *JenkinsPlugin) setRunTasks(runTasks string) error {
tasks := strings.Trim(runTasks, " \n")
if tasks == "" {
return nil
}
p.runTasks = strings.Split(tasks, ", ")
return nil
}
func (p *JenkinsPlugin) defineTemplateDir(jenkins_instance AppInstanceStruct) error {
// Analyze Forjfile input.
if jenkins_instance.SourceTemplates != "" {
if v, err := utils.Abs(path.Join(p.source_path, "templates", jenkins_instance.SourceTemplates)); err != nil {
return fmt.Errorf("Unable to define template directory. %s", err)
} else {
p.yamlPlugin.TemplatePath = v
}
if _, err := os.Stat(p.yamlPlugin.TemplatePath); err != nil {
return fmt.Errorf("Unable to define template directory. Template path '%s' is inexistent or inaccessible. %s", p.yamlPlugin.TemplatePath, err)
}
}
// Set template_dir
if p.yamlPlugin.TemplatePath != "" {
p.SetTemplateDir(p.yamlPlugin.TemplatePath)
log.Printf("Using custom templates: %s", p.yamlPlugin.TemplatePath)
} else if *cliApp.params.template_dir != templateDirDefault {
p.SetTemplateDir(*cliApp.params.template_dir)
log.Printf("Using templates defined by the cli: %s", *cliApp.params.template_dir)
} else {
p.SetTemplateDir(cliApp.templateDefaultPath)
log.Printf("Using default templates: %s", cliApp.templateDefaultPath)
}
return nil
}
// GetMaintainData prepare the Model with maintain data (usually credentials)
func (p *JenkinsPlugin) GetMaintainData(req *MaintainReq, ret *goforjj.PluginData) (_ bool) {
// TODO:
// Get the list of exclusive maintain data in Creds to facilitate the copy to Model
// The MaintainData is already centralized...
model := p.Model()
if v, found := req.Objects.App[p.InstanceName]; !found {
ret.Errorf("Request issue. App instance '%s' is missing in list of object.", p.InstanceName)
return
} else {
if p.yaml.Deploy.Ssl.Method == "manual" {
if p.yaml.Deploy.Ssl.Certificate == "" {
ret.Errorf("SSL - manual method: Certificate data missing. Update your Forjfile, then do `forjj update`")
return
}
if v.SslPrivateKey == "" {
ret.Errorf("SSL - manual method: A RSA SSL private key missing. Update your forjj credential data, then do `forjj update`")
return
}
}
if model.Creds == nil {
model.Creds = make(map[string]string)
}
if model.Env == nil {
model.Env = make(map[string]string)
}
model.loadCreds(req.Forj.ForjjUsername, p.InstanceName, req.Creds)
}
return true
}
// At create time: create jenkins source from req
func (p *JenkinsPlugin) initialize_from(r *CreateReq, ret *goforjj.PluginData) (err error) {
if _, found := r.Objects.App[p.InstanceName]; !found {
err = fmt.Errorf("Request format issue. Unable to find the jenkins instance '%s'", p.InstanceName)
ret.Errorf("%s", err)
return
}
jenkins_instance := r.Objects.App[p.InstanceName]
if v := jenkins_instance.SourceTemplates; v != "" {
p.yamlPlugin.TemplatePath = v
}
if err = p.defineTemplateDir(jenkins_instance); err != nil {
err = fmt.Errorf("Unable to define your template source path. %s", err)
ret.Errorf("Unable to define your template source path. %s", err)
return
}
p.yaml.AppExtent = jenkins_instance.Extent
p.yaml.Deploy.Name = r.Forj.ForjjDeploymentEnv
p.yaml.Deploy.Type = r.Forj.ForjjDeploymentType
p.yaml.Deploy.Deployment.SetFrom(&jenkins_instance.DeployStruct)
// Initialize deployment data and set default values
if p.yaml.Deploy.Deployment.To == "" {
p.yaml.Deploy.Deployment.To = "docker"
ret.StatusAdd("Default to 'docker' Deployment.")
}
if p.yaml.Deploy.Deployment.ServiceAddr == "" {
p.yaml.Deploy.Deployment.ServiceAddr = "localhost"
ret.StatusAdd("Default to 'localhost' deployment service name.")
}
if p.yaml.Deploy.Deployment.ServicePort == "" {
p.yaml.Deploy.Deployment.ServicePort = "8080"
ret.StatusAdd("Default to '8080' deployment service port.")
}
// Set SSL data
p.yaml.Deploy.Ssl.SetFrom(&jenkins_instance.SslStruct)
// Define default public url if not set.
p.yaml.Deploy.DefineDefaultPublicURL()
// Forjj predefined settings (instance/organization) are set at create time only.
// I do not recommend to update them, manually by hand in the `forjj-jenkins.yaml`.
// Updating the instance name could be possible but not for now.
// As well Moving an instance to another organization could be possible, but I do not see a real use case.
// So, they are fixed and saved at create time. Update/maintain won't never update them later.
if err = p.DefineDeployCommand(); err != nil {
ret.Errorf("Unable to define the default deployment command. %s", err)
return
}
// Initialize Dockerfile data and set default values
p.yaml.Dockerfile.SetFrom(&jenkins_instance.DockerfileStruct)
// Initialize Jenkins Image data and set default values
p.yaml.JenkinsImage.SetFrom(&jenkins_instance.FinalImageStruct)
if err = p.add_projects(r, ret); err != nil {
return
}
if p.yaml.GithubUser.SetFrom(&jenkins_instance.GithubUserStruct) {
ret.StatusAdd("github-user defined")
log.Printf("github-user defined with '%s'", p.yaml.GithubUser.Name)
}
return
}
func (p *JenkinsPlugin) DefineDeployCommand() error {
if err := p.LoadTemplatesDef(); err != nil {
return fmt.Errorf("%s", err)
}
if _, ok := p.templates_def.Run[p.yaml.Deploy.Deployment.To]; !ok {
list := make([]string, 0, len(p.templates_def.Run))
for element := range p.templates_def.Run {
list = append(list, element)
}
return fmt.Errorf("'%s' deploy type is unknown (templates.yaml). Valid are %s", p.yaml.Deploy.Deployment.To, list)
}
return nil
}
// TODO: Detect if the commands was manually updated to avoid updating it if end user did it alone.
// At update time: Update jenkins source from req or forjj-jenkins.yaml input.
func (p *JenkinsPlugin) update_from(r *UpdateReq, ret *goforjj.PluginData, status *bool) error {
instance := r.Forj.ForjjInstanceName
instance_data := r.Objects.App[instance]
if v := instance_data.SourceTemplates; v != "" {
p.yamlPlugin.TemplatePath = v
}
if err := p.defineTemplateDir(instance_data); err != nil {
err = fmt.Errorf("Unable to define your template source path. %s", err)
ret.Errorf("Unable to define your template source path. %s", err)
return err
}
p.yaml.AppExtent = instance_data.Extent
p.yaml.Deploy.Name = r.Forj.ForjjDeploymentEnv
p.yaml.Deploy.Type = r.Forj.ForjjDeploymentType
var deploy DeployStruct = p.yaml.Deploy.Deployment
if ok := deploy.UpdateFrom(&instance_data.DeployStruct); ok {
ret.StatusAdd("Deployment to '%s' updated.", instance_data.To)
IsUpdated(status)
}
p.yaml.Deploy.Deployment = deploy
var Ssl YamlSSLStruct = p.yaml.Deploy.Ssl
if ok := Ssl.UpdateFrom(&instance_data.SslStruct); ok {
ret.StatusAdd("'%s' SSL configuration updated.", instance_data.To)
IsUpdated(status)
}
p.yaml.Deploy.Ssl = Ssl
// Define default public url if not set.
p.yaml.Deploy.DefineDefaultPublicURL()
if err := p.DefineDeployCommand(); err != nil {
ret.Errorf("Unable to update the deployement command. %s", err)
return err
}
if p.yaml.Dockerfile.UpdateFrom(&instance_data.DockerfileStruct) {
ret.StatusAdd("Dockerfile updated.")
IsUpdated(status)
}
// Org used only if no set anymore.
if p.yaml.JenkinsImage.UpdateFrom(&instance_data.FinalImageStruct) {
ret.StatusAdd("Jenkins master docker image data updated.")
IsUpdated(status)
}
if p.yaml.GithubUser.UpdateFrom(&instance_data.GithubUserStruct) {
ret.StatusAdd("Jenkins github-user credential updated.")
IsUpdated(status)
}
return nil
}
func (p *JenkinsPlugin) saveYaml(where, fileName string, data interface{}, ret *goforjj.PluginData, status *bool) (_ error) {
destPath := p.source_path
if where == goforjj.FilesDeploy {
destPath = p.deployPath
}
file := path.Join(destPath, fileName)
if f, err := os.Stat(destPath); err != nil {
if err := os.MkdirAll(destPath, 0755); err != nil {
return err
}
} else {
if !f.IsDir() {
return fmt.Errorf(ret.Errorf("path '%s' is not a directory.", destPath))
}
}
origMD5, _ := md5sum(file)
d, err := yaml.Marshal(data)
if err != nil {
ret.Errorf("Unable to encode forjj-jenkins configuration data in yaml. %s", err)
return err
}
finalMD5 := md5.New().Sum(d)
if bytes.Equal(origMD5, finalMD5) {
return
}
if err = ioutil.WriteFile(file, d, 0644); err != nil {
ret.Errorf("Unable to save '%s'. %s", file, err)
return err
}
// Be careful to not introduce the local mount which in containers can be totally different (due to docker -v)
ret.AddFile(where, path.Join(p.InstanceName, fileName))
ret.StatusAdd("%s: '%s' instance saved (%s).", where, p.InstanceName, path.Join(p.InstanceName, fileName))
log.Printf("%s: '%s' instance saved.", where, file)
IsUpdated(status)
return
}
func (p *JenkinsPlugin) loadYaml(where, fileName string, data interface{}, ret *goforjj.PluginData, ignored bool) (status bool) {
destPath := p.source_path
if where == goforjj.FilesDeploy {
destPath = p.deployPath
}
file := path.Join(destPath, fileName)
log.Printf("Loading '%s'...", file)
if d, err := ioutil.ReadFile(file); err != nil {
if ignored {
log.Printf("Unable to read '%s'. %s", file, err)
return true
}
ret.Errorf("Unable to read '%s'. %s", file, err)
return
} else {
if err = yaml.Unmarshal(d, data); err != nil {
ret.Errorf("Unable to decode '%s' configuration data from yaml. %s", fileName, err)
return
}
}
log.Printf("'%s' instance loaded.", file)
return true
}
func (p *JenkinsPlugin) saveRunYaml(ret *goforjj.PluginData, status *bool) (_ error) {
run, found := p.templates_def.Run[p.yaml.Deploy.Deployment.To]
if !found {
ret.Errorf("Deployment '%s' command not found.", p.yaml.Deploy.Deployment.To)
return
}
return p.saveYaml(goforjj.FilesDeploy, maintain_cmds_file, &run, ret, status)
}
func (p *JenkinsPlugin) loadRunYaml(ret *goforjj.PluginData) (_ bool) {
return p.loadYaml(goforjj.FilesDeploy, maintain_cmds_file, &p.run, ret, false)
}
func (p *JenkinsPlugin) SetTemplateDir(templatePath string) (err error) {
p.template_dir, err = utils.Abs(templatePath)
return
}