-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactions.go
302 lines (253 loc) · 9.16 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
// 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
// You can remove following comments.
// It has been designed fo you, to implement the core of your plugin task.
//
// You can use use it to write your own plugin handler for additional functionnality
// Like Index which currently return a basic code.
import (
"fmt"
"log"
"net/http"
"path"
"github.com/forj-oss/goforjj"
)
const github_file = "github.yaml"
// DoCreate creating plugin task
// req_data contains the request data posted by forjj. Structure generated from 'github.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(w http.ResponseWriter, r *http.Request, req *CreateReq, ret *goforjj.PluginData) (httpCode int) {
instance := req.Forj.ForjjInstanceName
gws := GitHubStruct{
source_mount: req.Forj.ForjjSourceMount,
deployMount: req.Forj.ForjjDeployMount,
instance: instance,
deployTo: req.Forj.ForjjDeploymentEnv,
token: req.Objects.App[instance].Token,
}
check := make(map[string]bool)
check["token"] = true
check["source"] = true
log.Printf("Checking parameters : %#v", gws)
//ensure source path is writeable
if gws.verify_req_fails(ret, check) {
return
}
if a, found := req.Objects.App[instance]; !found {
ret.Errorf("Internal issue. Forjj has not given the Application information for '%s'. Aborted.")
return
} else {
gws.app = &a
}
log.Print("Checking github connection.")
if gws.github_connect(gws.app.Server, ret) == nil {
return
}
if !req.InitOrganization(&gws) {
ret.Errorf("Internal Error. Unable to define the organization")
return
}
// Build gws.github_source yaml structure.
if err := gws.create_yaml_data(req, ret); err != nil {
ret.Errorf("Unable to create. %s", err)
return
}
// A create won't be possible if repo requested already exist. The Update is the only possible option.
// The list of repository found are listed and returned in the answer.
if err := gws.repos_exists(ret); err != nil {
ret.Errorf("%s\nUnable to 'create' your forge when github already has an infra repository created. Clone it and use 'update' instead.", err)
return 419
}
if err := gws.checkSourcesExistence("create"); err != nil {
ret.Errorf("%s\nUnable to 'create' your forge", err)
return
}
ret.StatusAdd("Environment checked. Ready to be created.")
// Path in the context of GIT.
gitFile := path.Join(gws.instance, github_file)
// Save gws.github_source.
if _, err := gws.save_yaml(&gws.github_source, gws.sourceFile); err != nil {
ret.Errorf("%s", err)
return
}
log.Printf(ret.StatusAdd("Configuration saved in source Repo '%s' (%s).", gitFile, gws.source_mount))
if _, err := gws.save_yaml(&gws.githubDeploy, gws.deployFile); err != nil {
ret.Errorf("%s", err)
return
}
log.Printf(ret.StatusAdd("Configuration saved in deploy Repo '%s' (%s).", gitFile, path.Join(gws.deployMount, gws.deployTo)))
// Building final Post answer
// We assume ssh is used and forjj can push with appropriate credential.
for k, v := range gws.github_source.Urls {
ret.Services.Urls[k] = v
}
// Official application API recognized by Forjj
ret.Services.Urls["api_url"] = gws.github_source.Urls["github-base-url"]
ret.CommitMessage = fmt.Sprint("Github configuration created.")
ret.AddFile(goforjj.FilesSource, gitFile)
ret.AddFile(goforjj.FilesDeploy, gitFile)
return
}
// Do updating plugin task
// req_data contains the request data posted by forjj. Structure generated from 'github.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(w http.ResponseWriter, r *http.Request, req *UpdateReq, ret *goforjj.PluginData) (httpCode int) {
instance := req.Forj.ForjjInstanceName
log.Print("Checking Infrastructure code existence.")
var gws GitHubStruct
if a, found := req.Objects.App[instance]; !found {
ret.Errorf("Invalid request. Missing Objects/App/%s", instance)
return
} else {
gws = GitHubStruct{
source_mount: 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", gws)
if gws.verify_req_fails(ret, check) {
return
}
if err := gws.checkSourcesExistence("update"); err != nil {
ret.Errorf("%s\nUnable to 'update' your forge", err)
return
}
// Read the github.yaml file.
if err := gws.load_yaml(gws.sourceFile); err != nil {
ret.Errorf("Unable to update github instance '%s' source files. %s. Use 'create' to create it first.", instance, err)
return 419
}
if !req.InitOrganization(&gws) {
log.Printf(ret.Errorf("Unable to update. The organization was not set in the request."))
return
}
if gws.github_connect(req.Objects.App[instance].Server, ret) == nil {
return
}
ret.StatusAdd("Environment checked. Ready to be updated.")
if _, err := gws.update_yaml_data(req, ret); err != nil {
ret.Errorf("Unable to update. %s", err)
return
}
// Returns the collection of all managed repository with their existence flag.
gws.repos_exists(ret)
// Save gws.github_source.
if Updated, err := gws.save_yaml(&gws.github_source, gws.sourceFile); err != nil {
ret.Errorf("%s", err)
return
} else {
if !Updated {
log.Printf(ret.StatusAdd("Source: No github configuration update detected."))
} else {
log.Printf(ret.StatusAdd("Source: github configuration saved in '%s'.", path.Join(instance, github_file)))
ret.CommitMessage = fmt.Sprint("Source: github configuration updated.")
ret.AddFile(goforjj.FilesSource, path.Join(instance, github_file))
}
}
// Save gws.github_deploy.
if Updated, err := gws.save_yaml(&gws.githubDeploy, gws.deployFile); err != nil {
ret.Errorf("%s", err)
return
} else {
if !Updated {
log.Printf(ret.StatusAdd("Deploy: No github configuration update detected."))
} else {
log.Printf(ret.StatusAdd("Deploy: github configuration saved in '%s'.", path.Join(instance, github_file)))
ret.CommitMessage = fmt.Sprint("Deploy: github configuration updated.")
ret.AddFile(goforjj.FilesDeploy, path.Join(instance, github_file))
}
}
// Building final Post answer
// We assume ssh is used and forjj can push with appropriate credential.
for k, v := range gws.github_source.Urls {
ret.Services.Urls[k] = v
}
// Official application API recognized by Forjj
ret.Services.Urls["api_url"] = gws.github_source.Urls["github-base-url"]
return
}
// Do maintaining plugin task
// req_data contains the request data posted by forjj. Structure generated from 'github.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(w http.ResponseWriter, r *http.Request, req *MaintainReq, ret *goforjj.PluginData) (httpCode int) {
instance := req.Forj.ForjjInstanceName
var gws GitHubStruct
if a, found := req.Objects.App[instance]; !found {
ret.Errorf("Invalid request. Missing Objects/App/%s", instance)
return
} else {
gws = GitHubStruct{
deployMount: req.Forj.ForjjDeployMount,
workspace_mount: req.Forj.ForjjWorkspaceMount,
token: a.Token,
maintain_ctxt: true,
force: (req.Forj.Force == "true"),
}
}
check := make(map[string]bool)
check["token"] = true
check["workspace"] = true
check["deploy"] = true
if gws.verify_req_fails(ret, check) { // true => include workspace testing.
return
}
confFile := path.Join(gws.deployMount, req.Forj.ForjjDeploymentEnv, instance, github_file)
// Read the github.yaml file.
if err := gws.load_yaml(confFile); err != nil {
ret.Errorf("%s", err)
return
}
if gws.github_connect("", ret) == nil {
return
}
// ensure organization exist
if !gws.ensure_organization_exists(ret) {
return
}
if !gws.IsNewForge(ret) {
return
}
if !gws.setOrganizationTeams(ret) {
return
}
log.Printf(ret.StatusAdd("Organization maintained."))
if !gws.MaintainOrgHooks(ret) {
return
}
if gws.githubDeploy.NoRepos {
log.Printf(ret.StatusAdd("Repositories maintained limited to your infra repository"))
}
// loop on list of repos, and ensure they exist with minimal config and rights
for name, repo_data := range gws.githubDeploy.Repos {
if !repo_data.Infra && gws.githubDeploy.NoRepos {
log.Printf(ret.StatusAdd("Repo ignored: %s", name))
continue
}
if repo_data.Role == "infra" && !repo_data.IsDeployable {
log.Printf(ret.StatusAdd("Repo ignored: %s - Infra repo owned by '%s'", name, gws.githubDeploy.ProdOrganization))
continue
}
if err := repo_data.ensure_exists(&gws, ret); err != nil {
return
}
if !gws.MaintainHooks(&repo_data, ret) {
return
}
log.Printf(ret.StatusAdd("Repo maintained: %s", name))
}
return
}