forked from forj-oss/forjj-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.go
201 lines (169 loc) · 5.12 KB
/
actions.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
// This file has been created by "go generate" as initial code. go generate will never update it, EXCEPT if you remove it.
package main
import (
log "forjj-jenkins/reportlogs"
logRef "log"
"net/http"
"github.com/forj-oss/forjj-modules/trace"
"github.com/forj-oss/goforjj"
)
// DoCreate Do creating plugin task
// req_data contains the request data posted by forjj. Structure generated from 'jenkins.yaml'.
// ret_data contains the response structure to return back to forjj.
//
func DoCreate(r *http.Request, req *CreateReq, ret *goforjj.PluginData) (httpCode int) {
if req.Forj.Debug != "" {
gotrace.SetDebugMode(req.Forj.Debug)
} else {
gotrace.SetWarning()
}
p, code := req.check_source_existence(ret)
if p == nil {
return code
}
log.SetLogsFunc(map[string]func(string, ...interface{}){
"reportLog": func(format string, parameters ...interface{}) {
logRef.Print(ret.StatusAdd(format, parameters...))
},
"reportError": func(format string, parameters ...interface{}) {
gotrace.Error(ret.Errorf(format, parameters...))
},
"pluginLog": func(format string, parameters ...interface{}) {
logRef.Printf(format, parameters...)
},
})
if p.initialize_from(req, ret) != nil {
return
}
if p.create_jenkins_sources(ret) != nil {
return
}
p.auths = NewDockerAuths(req.Objects.App[p.InstanceName].RegistryAuth)
if err := p.runBuildDeploy(req.Forj.ForjjUsername, req.Creds, true); err != nil {
log.Errorf("%s", err)
return
}
if err := p.addBuiltFiles(ret, nil); err != nil {
log.Errorf("%s", err)
return
}
if err := p.addGeneratedFiles(ret, nil); err != nil {
log.Errorf("%s", err)
return
}
if p.saveYaml(goforjj.FilesSource, jenkins_file, &p.yamlPlugin, ret, nil) != nil {
return
}
if p.saveYaml(goforjj.FilesDeploy, jenkinsDeployFile, &p.yaml, ret, nil) != nil {
return
}
p.saveRunYaml(ret, nil)
ret.CommitMessage = "Creating initial jenkins source files as defined by the Forjfile."
return
}
// DoUpdate is the update plugin task
// req_data contains the request data posted by forjj. Structure generated from 'jenkins.yaml'.
// ret_data contains the response structure to return back to forjj.
// forjj-jenkins.yaml is loaded by default.
//
func DoUpdate(r *http.Request, req *UpdateReq, ret *goforjj.PluginData) (_ int) {
if req.Forj.Debug != "" {
gotrace.SetDebugMode(req.Forj.Debug)
} else {
gotrace.SetWarning()
}
p, ok := req.checkSourceExistence(ret)
if !ok {
return
}
log.SetLogsFunc(map[string]func(string, ...interface{}){
"reportLog": func(format string, parameters ...interface{}) {
logRef.Print(ret.StatusAdd(format, parameters...))
},
"reportError": func(format string, parameters ...interface{}) {
gotrace.Error(ret.Errorf(format, parameters...))
},
"pluginLog": func(format string, parameters ...interface{}) {
logRef.Printf(format, parameters...)
},
})
if !p.loadYaml(goforjj.FilesSource, jenkins_file, &p.yamlPlugin, ret, false) {
return
}
if !p.loadYaml(goforjj.FilesDeploy, jenkinsDeployFile, &p.yaml, ret, true) {
return
}
// TODO: Use the GithubStruct.UpdateFrom(...)
var updated bool
if p.update_from(req, ret, &updated) != nil {
return
}
if p.update_projects(req, ret, &updated) != nil {
return
}
if p.update_jenkins_sources(ret, &updated) != nil {
return
}
p.auths = NewDockerAuths(req.Objects.App[p.InstanceName].RegistryAuth)
if ret.ErrorMessage != "" {
return
}
p.setRunTasks(req.Forj.RunTasks)
if err := p.runBuildDeploy(req.Forj.ForjjUsername, req.Creds, false); err != nil {
log.Errorf("%s", err)
return
}
if err := p.addBuiltFiles(ret, &updated); err != nil {
log.Errorf("%s", err)
return
}
if err := p.addGeneratedFiles(ret, &updated); err != nil {
log.Errorf("%s", err)
return
}
if p.saveYaml(goforjj.FilesSource, jenkins_file, &p.yamlPlugin, ret, &updated) != nil {
return
}
if p.saveYaml(goforjj.FilesDeploy, jenkinsDeployFile, &p.yaml, ret, &updated) != nil {
return
}
if p.saveRunYaml(ret, &updated) != nil {
return
}
if updated {
ret.CommitMessage = "Updating jenkins source files requested by Forjfile."
} else {
log.Reportf("No update detected. Jenkins source files hasn't been updated.")
}
return
}
// DoMaintain Do maintaining plugin task
// req_data contains the request data posted by forjj. Structure generated from 'jenkins.yaml'.
// ret_data contains the response structure to return back to forjj.
//
func DoMaintain(r *http.Request, req *MaintainReq, ret *goforjj.PluginData) (httpCode int) {
if req.Forj.Debug != "" {
gotrace.SetDebugMode(req.Forj.Debug)
} else {
gotrace.SetWarning()
}
if !req.checkSourceExistence(ret) {
return
}
log.SetLogsFunc(map[string]func(string, ...interface{}){
"reportLog": func(format string, parameters ...interface{}) {
logRef.Print(ret.StatusAdd(format, parameters...))
},
"reportError": func(format string, parameters ...interface{}) {
gotrace.Error(ret.Errorf(format, parameters...))
},
"pluginLog": func(format string, parameters ...interface{}) {
logRef.Printf(format, parameters...)
},
})
// loop on list of jenkins instances defined by a collection of */jenkins.yaml
if !req.Instantiate(req, ret) {
return
}
return
}