-
Notifications
You must be signed in to change notification settings - Fork 2
/
actions.go
304 lines (244 loc) · 8.32 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
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
// 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 (
"net/http"
"log"
"fmt"
"path"
"github.com/forj-oss/goforjj"
)
//DoCreate --> forjj create
// Do creating plugin task
// req_data contains the request data posted by forjj. Structure generated from 'gitlab.yaml'.
// ret_data contains the response structure to return back to forjj.
//
// By default, if httpCode is not set (ie equal to 0), the function caller will set it to 422 in case of errors (error_message != "") or 200
func DoCreate(r *http.Request, req *CreateReq, ret *goforjj.PluginData) (httpCode int) {
//Get instance name
instance := req.Forj.ForjjInstanceName
//init GitlabPlugin
gls := GitlabPlugin{
sourcePath: req.Forj.ForjjSourceMount,
deployMount: req.Forj.ForjjDeployMount,
instance: req.Forj.ForjjInstanceName,
deployTo: req.Forj.ForjjDeploymentEnv,
token: req.Objects.App[instance].Token,
group: req.Objects.App[instance].Group,
}
log.Printf("Checking parameters : %#v", gls)
//Check token and source
check := make(map[string]bool)
check["token"] = true
check["source"] = true
//ensure source path is writeable && token isn't empty
if gls.verifyReqFails(ret, check){
return
}
//verify if instance existe
if a, found := req.Objects.App[instance]; !found{
ret.Errorf("Internal issue. Forjj has not given the Application information for '%s'. Aborted.", instance)
return
} else {
gls.app = &a
}
//Check Gitlab connection
log.Println("Checking gitlab connection.")
ret.StatusAdd("Connect to gitlab...")
if git := gls.gitlabConnect("X"/*"myserv"*/, ret); git == nil{
return
}
// Init Group of project
if !req.InitGroup(&gls){
ret.Errorf("Internal Error. Unable to define the group.")
}
//Create yaml data for maintain function
if err := gls.createYamlData(req, ret); err != nil {
ret.Errorf("Unable to create. %s",err)
return
}
//repos exist ?
if err := gls.projectsExists(ret); err != nil {
ret.Errorf("%s\nUnable to 'create' your forge when gitlab already has an infra project created. Clone it and use 'update' instead.", err)
return 419
}
//CheckSourceExistence
if err := gls.checkSourcesExistence("create"); err != nil{
ret.Errorf("%s\nUnable to 'create' your forge", err)
return
}
ret.StatusAdd("Environment checked. Ready to be created.")
//Path in ctxt git
gitFile := path.Join(gls.instance, gitlabFile)
//Save gitlab source
if _, err := gls.saveYaml(&gls.gitlabSource, gls.sourceFile/*ret, instance*/); err != nil {
ret.Errorf("%s", err)
return
}
log.Printf(ret.StatusAdd("Configuration saved in source project '%s' (%s).", gitFile, gls.sourcePath)) // ! \\
//Save gitlab deploy
if _, err := gls.saveYaml(&gls.gitlabDeploy, gls.deployFile); err != nil{
ret.Errorf("%s", err)
return
}
log.Printf(ret.StatusAdd("Configuration saved in deploy project '%s' (%s).", gitFile, path.Join(gls.deployMount, gls.deployTo)))
//Build final post answer
for k, v := range gls.gitlabSource.Urls{
ret.Services.Urls[k] = v
}
//API by forjj
ret.Services.Urls["api_url"] = gls.gitlabSource.Urls["gitlab-base-url"]
ret.CommitMessage = fmt.Sprint("Gitlab configuration created.")
ret.AddFile(goforjj.FilesSource, gitFile)
ret.AddFile(goforjj.FilesDeploy, gitFile)
log.Println(ret.StatusAdd("end"))
return
}
//DoUpdate --> forjj update
// Do updating plugin task
// req_data contains the request data posted by forjj. Structure generated from 'gitlab.yaml'.
// ret_data contains the response structure to return back to forjj.
//
// By default, if httpCode is not set (ie equal to 0), the function caller will set it to 422 in case of errors (error_message != "") or 200
func DoUpdate(r *http.Request, req *UpdateReq, ret *goforjj.PluginData) (httpCode int) {
instance := req.Forj.ForjjInstanceName
log.Print("Checking Infrastructure code existence.")
var gls GitlabPlugin
if a, found := req.Objects.App[instance]; !found {
ret.Errorf("Invalid request. Missing Objects/App/%s", instance)
return
} else {
gls = GitlabPlugin{
sourcePath: req.Forj.ForjjSourceMount,
deployMount: req.Forj.ForjjDeployMount,
instance: instance,
deployTo: req.Forj.ForjjDeploymentEnv,
token: a.Token,
app: &a,
}
}
check := make(map[string]bool)
check["token"] = true
check["source"] = true
log.Printf("Checking parameters : %#v", gls)
if gls.verifyReqFails(ret, check){
return
}
if err := gls.checkSourcesExistence("update"); err != nil {
ret.Errorf("%s\nUnable to 'update' your forge", err)
return
}
if err := gls.loadYaml(gls.sourceFile); err != nil {
ret.Errorf("Unable to update gitlab instance '%s' source files. %s. Use 'create' to create it first.", instance, err)
return 419
}
if !req.InitGroup(&gls){
log.Printf(ret.Errorf("Unable to update. The group was not set in the request."))
return
}
if git := gls.gitlabConnect("X", ret); git == nil{
return
}
ret.StatusAdd("Environment checked. Ready to be updated.")
if _, err := gls.updateYamlData(req, ret); err != nil{ //fct TODO
ret.Errorf("Unable to update. %s", err)
return
}
gls.projectsExists(ret)
if Updated, err := gls.saveYaml(&gls.gitlabSource, gls.sourceFile); err != nil {
ret.Errorf("%s", err)
return
} else {
if !Updated {
log.Printf(ret.StatusAdd("Source: No gitlab configuration update detected."))
} else {
log.Printf(ret.StatusAdd("Source: gitlab configuration saved in '%s'.", path.Join(instance, gitlabFile)))
ret.CommitMessage = fmt.Sprint("Source: gitlab configuration updated.")
ret.AddFile(goforjj.FilesSource, path.Join(instance, gitlabFile))
}
}
//Save gls.gitlabDeploy
if Updated, err := gls.saveYaml(&gls.gitlabDeploy, gls.deployFile); err != nil {
ret.Errorf("%s", err)
return
} else {
if !Updated {
log.Printf(ret.StatusAdd("Deploy: No gitlab configuration update detected."))
} else {
log.Printf(ret.StatusAdd("Deploy: gitlab configuration seved in '%s'.", path.Join(instance, gitlabFile)))
ret.CommitMessage = fmt.Sprint("Deploy: gitlab configuration updated.")
ret.AddFile(goforjj.FilesDeploy, path.Join(instance, gitlabFile))
}
}
//Building final post answer
for k, v := range gls.gitlabSource.Urls{
ret.Services.Urls[k] = v
}
//
ret.Services.Urls["api-url"] = gls.gitlabSource.Urls["gitlab-base-url"]
return
}
//DoMaintain --> forjj maintain
// Do maintaining plugin task
// req_data contains the request data posted by forjj. Structure generated from 'gitlab.yaml'.
// ret_data contains the response structure to return back to forjj.
//
// By default, if httpCode is not set (ie equal to 0), the function caller will set it to 422 in case of errors (error_message != "") or 200
func DoMaintain(r *http.Request, req *MaintainReq, ret *goforjj.PluginData) (httpCode int) {
instance := req.Forj.ForjjInstanceName
var gls GitlabPlugin
if a, found := req.Objects.App[instance]; !found {
ret.Errorf("Invalid request. Missing Objects/App/%s", instance)
return
} else {
gls = GitlabPlugin{
deployMount: req.Forj.ForjjDeployMount,
workspaceMount: req.Forj.ForjjWorkspaceMount,
token: a.Token,
maintainCtxt: true,
force: req.Forj.Force == "true",
}
}
check := make(map[string]bool)
check["token"] = true
check["workspace"] = true
check["deploy"] = true
if gls.verifyReqFails(ret, check) {
return
}
confFile := path.Join(gls.deployMount, req.Forj.ForjjDeploymentEnv, instance, gitlabFile)
//read yaml file
if err := gls.loadYaml(confFile/*ret, instance*/); err != nil{
ret.Errorf("%s"/*, err*/)
return
}
if gls.gitlabConnect("", ret) == nil{
return
}
if !gls.ensureGroupExists(ret){
return
}
if !gls.IsNewForge(ret){
return
}
if gls.gitlabDeploy.NoProjects{
log.Printf(ret.StatusAdd("Projects maintained limited to your infra project"))
}
//loop verif
for name, projectData := range gls.gitlabDeploy.Projects{
if !projectData.Infra && gls.gitlabDeploy.NoProjects{
log.Printf(ret.StatusAdd("Project ignored: %s", name))
continue
}
if projectData.Role == "infra" && !projectData.IsDeployable{
log.Printf(ret.StatusAdd("Project ignored: %s - Infra project owned by '%s'", name, gls.gitlabDeploy.ProdGroup))
continue
}
if err := projectData.ensureExists(&gls, ret); err != nil{
return
}
//...
log.Printf(ret.StatusAdd("Project maintained: %s", name))
}
return
}