forked from forj-oss/forjj-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
161 lines (136 loc) · 4.64 KB
/
update.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
// This file has been created by "go generate" as initial code. go generate will never update it, EXCEPT if you remove it.
// So, update it for your need.
package main
import (
"fmt"
log "forjj-jenkins/reportlogs"
"strings"
"github.com/forj-oss/goforjj"
)
func (jp *JenkinsPlugin) update_jenkins_sources(ret *goforjj.PluginData, updated *bool) (err error) {
if err = jp.DefineSources(); err != nil {
log.Printf(ret.Errorf("%s", err))
return
}
log.Printf("Start copying source files...")
if err = jp.copy_source_files(ret, updated); err != nil {
return
}
log.Printf("Start copying existing tasks generated source files...")
if err = jp.copyGeneratedSourceFiles(ret, updated); err != nil {
return
}
log.Printf("Start Generating source files...")
if err = jp.generate_source_files(ret, updated); err != nil {
return
}
if err = jp.generate_jobsdsl(ret, updated); err != nil {
return
}
return
}
func IsUpdated(updated *bool) {
if updated != nil {
*updated = true
}
}
// Function which adds maintain options as part of the plugin answer in create/update phase.
// forjj won't add any driver name because 'maintain' phase read the list of drivers to use from forjj-maintain.yml
// So --git-us is not available for forjj maintain.
func (r *UpdateArgReq) SaveMaintainOptions(ret *goforjj.PluginData) {
if ret.Options == nil {
ret.Options = make(map[string]goforjj.PluginOption)
}
}
func addMaintainOptionValue(options map[string]goforjj.PluginOption, option, value, defaultv, help string) goforjj.PluginOption {
opt, ok := options[option]
if ok && value != "" {
opt.Value = value
return opt
}
if !ok {
opt = goforjj.PluginOption{Help: help}
if value == "" {
opt.Value = defaultv
} else {
opt.Value = value
}
}
return opt
}
// update_projects add project data in the jenkins.yaml file
func (jp *JenkinsPlugin) update_projects(req *UpdateReq, ret *goforjj.PluginData, status *bool) error {
projects := ProjectInfo{}
projects.set_project_info(req.Forj.ForjCommonStruct)
instanceData := req.Objects.App[req.Forj.ForjjInstanceName]
projects.setDslInfo(instanceData.SeedJobStruct)
// TODO: Information not used. To clean it up.
projects.setIsProDeploy(strings.ToLower(instanceData.ProDeployment) == "true")
return projects.set_projects_to(req.Objects.Projects, jp, ret, status, req.Forj.ForjjInfra, instanceData.JenkinsfilePath)
}
func (jp *JenkinsPlugin) runBuildDeploy(username string, creds map[string]string, createSteps bool) (err error) {
deployTo := jp.yaml.Deploy.Deployment.To
run, found := jp.templates_def.Build[deployTo]
if !found {
log.Printf("No run_build section defined for deploy-to=%s. No build processed. If you need one, create run_build/%s: in templates.yaml", jp.deployEnv, jp.deployEnv)
return
}
model := jp.Model()
model.loadCreds(username, jp.InstanceName, creds)
var runNormalSteps, runFailureSteps []string
if createSteps {
runNormalSteps = run.Steps.WhenCreate
runFailureSteps = run.Steps.WhenCreateFailed
} else {
runNormalSteps = run.Steps.WhenUpdate
runFailureSteps = run.Steps.WhenUpdateFailed
}
if jp.runTasks != nil && len(jp.runTasks) > 0 {
runNormalSteps = jp.runTasks
runFailureSteps = []string{}
}
if runNormalSteps == nil || len(runNormalSteps) == 0 {
if run.RunCommand == "" {
log.Printf("yaml:/run_build/%s/run is depreciated. Use yaml:/run_build/%s/steps and yaml:/run_build/%s/tasks", deployTo, deployTo, deployTo)
}
if err = run.run(jp.InstanceName, jp.source_path, jp.deployPath, model, jp.auths); err != nil {
log.Errorf("Unable to build to %s. %s", jp.yaml.Deploy.Deployment.To, err)
}
return
}
defer func() {
if err == nil {
return
}
jp.runSteps(deployTo, runFailureSteps, run.Tasks, model)
}()
err = jp.runSteps(deployTo, runNormalSteps, run.Tasks, model)
return
}
func (jp *JenkinsPlugin) runSteps(deployTo string, steps []string, tasks map[string]RunStruct, model *JenkinsPluginModel) (err error) {
tasksList := "None"
if len(tasks) > 0 {
for name, task := range tasks {
tasksList += "- " + name
if task.Description != "" {
tasksList += " - " + task.Description
} else {
tasksList += " - (description empty)"
}
tasksList += "\n"
}
}
for _, stepName := range steps {
step, found := tasks[stepName]
if !found {
err = fmt.Errorf("Cannot run '%s' Step. '%s' was not defined in the `templates.yaml` under `run_build/%s`. Possible tasks are:\n%s",
stepName, stepName, deployTo, tasksList)
return
}
if err = step.run(jp.InstanceName, jp.source_path, jp.deployPath, model, jp.auths); err != nil {
err = fmt.Errorf("Unable to build %s to %s. %s", stepName, jp.yaml.Deploy.Deployment.To, err)
return
}
}
return
}