forked from forj-oss/forjj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
334 lines (290 loc) · 11.1 KB
/
driver.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
package main
import (
"fmt"
"forjj/creds"
"forjj/drivers"
"forjj/forjfile"
"forjj/git"
"forjj/utils"
"log"
"os"
"path"
"strings"
"time"
"github.com/forj-oss/forjj-modules/cli"
"github.com/forj-oss/forjj-modules/trace"
"github.com/forj-oss/goforjj"
)
const (
defaultTimeout = 32 * time.Second
default_socket_baseurl = "http:///anyhost"
default_mount_path = "/src"
)
// Execute the driver task, without commit.
// Used in create/update case.
// It uses the driver (or forjj) flag to ensure proper task is executed by the driver.
// if exist in create, fails and ask to abort
// If ! exist in update, fails
func (a *Forj) do_driver_task(action, instance string) (err error, aborted bool) {
if action != "create" && action != "update" {
return fmt.Errorf("Internal error: Invalid action '%s'. Supports only 'create' and 'update'.", action), false
}
if err = a.driver_init(instance); err != nil {
return
}
d := a.CurrentPluginDriver
// Add ref to this driver in the forjj infra repo
//a.o.Drivers[instance] = d
// check flag for create
if err := d.CheckFlagBefore(instance, action); err != nil {
return err, (action == "create") // Abort-able if create, because the resource exist and we can use it. So, forjj can continue the task.
}
// Calling upstream driver - To create plugin source files for the current upstream infra repository
// When the plugin inform that resource already exist, it returns an error with aborted = true
if err, aborted = a.driver_do(d, instance, action); err != nil && !aborted {
return
}
// The driver has created or aborted his task.
if a.InfraPluginDriver == d { // Infra upstream instance case
if v, found := a.InfraPluginDriver.Plugin.Result.Data.Repos[a.w.Infra.Name]; found {
// Saving infra repository information to the workspace
a.w.Infra = &v
} else {
return fmt.Errorf("Unable to find Infra repository '%s' from driver '%s'", a.w.Infra.Name, a.w.Instance), false
}
}
if aborted {
// Do not do any normal GIT tasks as everything already exists
// Do not test the flag file as nothing done by the driver. If aborted, we assume the flag file already exists in the existing upstream repo
return
}
// Check the flag file
if err = d.CheckFlagAfter(); err != nil {
return
}
return
}
func (a *Forj) moveTo(where string) (cur_dir string, _ error) {
if v, err := os.Getwd(); err != nil {
return "", fmt.Errorf("Unable to get the current directory. %s", err)
} else {
cur_dir = v
}
if where == goforjj.FilesSource {
err := os.Chdir(a.f.InfraPath())
if err != nil {
return "", fmt.Errorf("Unable to move to '%s'. %s", a.f.InfraPath(), err)
}
gotrace.Trace("Moved to %s repo (%s)", where, a.f.InfraPath())
}
if where == goforjj.FilesDeploy {
err := os.Chdir(a.d.GetRepoPath())
if err != nil {
return "", fmt.Errorf("Unable to move to '%s'. %s", a.d.GetRepoPath(), err)
}
gotrace.Trace("Moved to %s repo (%s)", where, a.d.GetRepoPath())
}
return
}
// do driver add files
func (a *Forj) do_driver_add(d *drivers.Driver) error {
if len(d.Plugin.Result.Data.Files) == 0 {
gotrace.Trace("No files to add/commit returned by the driver.")
return nil
}
gotrace.Trace("----- Do GIT tasks in the INFRA repository.")
// Add source files
if err := d.GitAddPluginFiles(a.moveTo); err != nil {
return fmt.Errorf("Issue to add driver '%s' generated files. %s", a.CurrentPluginDriver.Name, err)
}
// Check about uncontrolled files. Existing if one uncontrolled file is found
if status := git.GetStatus(); status.Err != nil {
return fmt.Errorf("Issue to check git status. %s", status.Err)
} else {
if num := status.CountUntracked(); num > 0 {
log.Print("Following files created by the plugin are not controlled by the plugin. You must fix it manually and contact the plugin maintainer to fix this issue.")
log.Printf("files: %s", strings.Join(status.Untracked(), ", "))
return fmt.Errorf("Unable to complete commit process. '%d' Uncontrolled files found", num)
}
}
return nil
}
// Define starting on this driver
// Forj.CurrentPluginDriver set
func (a *Forj) driver_init(instance string) error {
d, found := a.drivers[instance]
if !found {
return fmt.Errorf("Internal error: Unable to find %s from drivers.", instance)
}
a.CurrentPluginDriver = d
return nil
}
func (a *Forj) driver_cleanup_all() {
gotrace.Trace("Stopping all running loaded services...")
for instance, d := range a.drivers {
gotrace.Trace("- %s", instance)
d.Plugin.PluginStopService()
}
}
// Create the flag to a kingpin Command. (create/update/maintain)
func (a *Forj) init_driver_flags_for(d *drivers.Driver, option_name, command, forjj_option_name, forjj_option_help string,
opts *cli.ForjOpts) {
if command == "" {
// Add to the Application layer.
gotrace.Trace("Set App flag '%s(%s)'", forjj_option_name, option_name)
a.cli.AddAppFlag(cli.String, forjj_option_name, forjj_option_help, opts)
return
}
// No value by default. Will be set later after complete parse.
d.InitCmdFlag(command, forjj_option_name, option_name)
// Create flag 'option_name' on kingpin cmd or app
if forjj_option_name != option_name {
gotrace.Trace("Set action '%s' flag '%s(%s)'", command, forjj_option_name, option_name)
} else {
gotrace.Trace("Set action '%s' flag '%s'", command, forjj_option_name)
}
a.cli.OnActions(command).AddFlag(cli.String, forjj_option_name, forjj_option_help, opts)
return
}
// Start driver task.
// Forj.CurrentPluginDriver is set to the current driver
func (a *Forj) driver_do(d *drivers.Driver, instance_name, action string, args ...string) (err error, aborted bool) {
defer log.Print("-------------------------------------------")
log.Print("-------------------------------------------")
log.Printf("Running %s on %s...", action, instance_name)
if err := d.Plugin.PluginInit(a.w.Organization + "_" + instance_name); err != nil {
return err, false
}
if found, _ := goforjj.InArray(instance_name, a.debug_instances); found {
d.Plugin.RunningFromDebugger()
}
d.Plugin.PluginSetSource(path.Join(a.i.Path(), "apps", d.DriverType))
d.Plugin.PluginSetDeployment(a.d.GetReposPath())
d.Plugin.PluginSetDeploymentName(a.d.Name())
d.Plugin.PluginSetVersion(d.DriverVersion)
d.Plugin.PluginSetWorkspace(a.w.Path())
d.Plugin.PluginSocketPath(path.Join(a.w.Path(), "lib"))
if v, found, _, _ := a.cli.GetStringValue(workspace, "", "docker-exe-path"); found && v != "" {
a.w.DockerBinPath = v
}
if err := d.Plugin.PluginDockerBin(a.w.DockerBinPath); err != nil {
return err, false
}
// Set default envs from the forjj process environment.
if d.Plugin.Yaml.Runtime.Docker.Env == nil {
d.Plugin.Yaml.Runtime.Docker.Env = make(map[string]string)
}
d.Plugin.ServiceAddEnv("LOGNAME", "$LOGNAME", false)
d.Plugin.Yaml.Runtime.Docker.Env["LOGNAME"] = "$LOGNAME"
if v := os.Getenv("http_proxy"); v != "" {
d.Plugin.Yaml.Runtime.Docker.Env["http_proxy"] = v
d.Plugin.Yaml.Runtime.Docker.Env["https_proxy"] = v
}
if v := os.Getenv("no_proxy"); v != "" {
d.Plugin.Yaml.Runtime.Docker.Env["no_proxy"] = v
}
if v, b, _ := d.Plugin.GetDockerDoodParameters(); v != nil {
d.Plugin.Yaml.Runtime.Docker.Env["DOCKER_DOOD"] = strings.Join(v, " ")
d.Plugin.Yaml.Runtime.Docker.Env["DOCKER_DOOD_BECOME"] = strings.Join(b, " ")
}
if err := d.Plugin.PluginStartService(); err != nil {
return err, false
}
plugin_payload := goforjj.NewReqData()
// Load all internal Forjj data, identified by 'forjj-*'
a.LoadInternalData()
a.GetForjjFlags(plugin_payload, d, common_acts)
a.GetForjjFlags(plugin_payload, d, action)
if err := a.GetObjectsData(plugin_payload, d, action); err != nil {
return fmt.Errorf("Unable to Get Object data on '%s'. %s", instance_name, err), aborted
}
err = a.AddReqDeployment(plugin_payload)
if err != nil {
return fmt.Errorf("Unable to %s. %s. You may need to execute a forjj update to a deployment environment", action, err), false
}
d.Plugin.Result, err = d.Plugin.PluginRunAction(action, plugin_payload)
if err != nil {
return fmt.Errorf("Internal Error: %s", err), false
}
if d.Plugin.Result == nil {
return fmt.Errorf("An error occured in '%s' plugin. No data has been returned. Please check plugin logs.", instance_name), false
}
termBrown, termReset := utils.DefColor(33)
for _, line := range strings.Split(d.Plugin.Result.Data.Status, "\n") {
log.Println(termBrown, line, termReset)
}
if d.Plugin.Result.Data.ErrorMessage != "" {
termRed, _ := utils.DefColor(31)
for _, line := range strings.Split(d.Plugin.Result.Data.ErrorMessage, "\n") {
log.Println(termRed, line, termReset)
}
}
if d.Plugin.Result.State_code != 0 || err != nil {
if err == nil {
err = fmt.Errorf("The plugin has returned an error. Please review and fix.")
}
if d.Plugin.Result.State_code == 419 { // The plugin won't do the task because of requirement not met. This is not an error which requires Forjj to exit.
aborted = true // So, when a plugin return 419, the plugin task is considered as aborted. So forjj can continue if it is possible. (create/update action case)
}
return err, aborted
}
// Dispatch driver information in Forjj
// Deliver list of Remotes in Internal Forjfile
if d.DriverType == "upstream" {
for Name, Repo := range d.Plugin.Result.Data.Repos {
var repo_obj *forjfile.RepoStruct
ffd := a.f.InMemForjfile()
if r, ok := ffd.GetObjectInstance(repo, Name).(*forjfile.RepoStruct); !ok {
continue
} else {
repo_obj = r
}
repo_obj.Set("forjj", "remote", Repo.Remotes["origin"].Ssh)
repo_obj.Set("forjj", "remote-url", Repo.Remotes["origin"].Url)
repo_obj.SetInstanceOwner(Repo.Owner)
repo_obj.SetPluginOwner(d)
if a.f.GetInfraName() == Name { // 'infra' *Repostruct is a copy, not a ref to the repos["<infra>"] *RepoStruct
ffd.Set("forjj", "infra", Name, "remote", Repo.Remotes["origin"].Ssh)
ffd.Set("forjj", "infra", Name, "remote-url", Repo.Remotes["origin"].Url)
}
// New repo code which could be created with repo-templates
if !Repo.Exist && !repo_obj.IsCurrentDeploy() && !repo_obj.IsInfra() {
codeRepoPath := path.Join(a.w.Path(), "git", Name)
git.EnsureRepoExist(codeRepoPath)
git.RunInPath(codeRepoPath, func() (_ error) {
git.EnsureRemoteIs("origin", Repo.Remotes["origin"].Ssh)
syncRemoteBranch := "origin" + "/" + "master"
git.Do("branch", "--set-upstream-to="+syncRemoteBranch)
return
})
}
// Current deploy only
if deployName, found, _ := repo_obj.Get(forjfile.FieldRepoDeployName); found && repo_obj.IsCurrentDeploy() {
deployObj, _ := a.f.GetADeployment(deployName.GetString())
deployObj.RunInContext(func() (_ error) {
deployObj.GitDefineRemote("origin", Repo.Remotes["origin"].Ssh)
deployObj.GitSyncFrom("origin", "master")
return
})
}
}
if err := a.FlowApply(); err != nil {
return err, false
}
if err := a.scanAndSetDefaults(a.f.InMemForjfile(), creds.Global); err != nil {
return err, false
}
}
// Collect application API if published by the driver.
if u, found := d.Plugin.Result.Data.Services.Urls["api_url"]; found {
d.DriverAPIUrl = u
}
return
}
func (a *Forj) DriverGet(instance string) (d *drivers.Driver) {
var found bool
if d, found = a.drivers[instance]; found {
return
}
return nil
}