forked from forj-oss/forjj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
145 lines (113 loc) · 4.11 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
package main
import (
"fmt"
"forjj/creds"
"log"
"regexp"
"github.com/forj-oss/forjj-modules/trace"
)
func (a *Forj) updateAction(string) {
if err := a.Update(); err != nil {
log.Fatalf("Forjj update issue. %s", err)
}
println("FORJJ - update ", a.w.Organization, " DONE") // , cmd.ProcessState.Sys().WaitStatus)
}
// Execute an update on the workspace given.
//
// Workspace data has been initialized or loaded.
// forjj-options has been initialized or loaded
func (a *Forj) Update() error {
if _, err := a.w.Ensure_exist(); err != nil {
return fmt.Errorf("Invalid workspace. %s. Please create it with 'forjj create'", err)
}
defer func() {
// save infra repository location in the workspace.
a.w.Save()
/* Disable until forjj propose a way to change it from cli.
if err := a.s.Save(); err != nil {
log.Printf("%s", err)
}*/
}()
if err := a.ValidateForjfile(); err != nil {
return fmt.Errorf("Your Forjfile is having issues. %s Try to fix and retry", err)
}
// Set plugin defaults for objects defined by plugins loaded.
if err := a.scanAndSetDefaults(a.f.DeployForjfile(), creds.Global); err != nil {
return fmt.Errorf("Unable to update. Global dispatch issue. %s", err)
}
// Build in memory representation from source files loaded.
if err := a.f.BuildForjfileInMem(); err != nil {
return err
}
// Get it
ffd := a.f.InMemForjfile()
// Add missing deployment Repositories and warn
if err := a.DefineDeployRepositories(ffd, true); err != nil {
return fmt.Errorf("Issues to automatically add your deployment repositories. %s", err)
}
// Defining information about current deployment repository
a.defineDeployContext()
// Load flow identified by Forjfile source with missing repos.
if err := a.FlowInit(); err != nil {
return err
}
if err := a.define_infra_upstream(); err != nil {
return fmt.Errorf("Unable to identify a valid infra repository upstream. %s", err)
}
gotrace.Trace("Infra upstream selected: '%s'", a.w.Instance)
// Apply the flow to the inMemForjfile
if err := a.FlowApply(); err != nil {
return fmt.Errorf("Unable to apply flows. %s", err)
}
// Set plugin defaults for objects added dynamically in the in memory Forjfile.
if err := a.scanAndSetDefaults(ffd, creds.Global); err != nil {
return fmt.Errorf("Unable to update. Global dispatch issue. %s", err)
}
// Checking infra repository: A valid infra repo is a git repository with at least one commit and
// a Forjfile from repo root.
if err := a.i.Use(a.f.InfraPath()); err != nil {
return fmt.Errorf("Failed to update your infra repository. %s", err)
}
// Now, we are in the infra repo root directory and at least, the 1st commit exist.
// Disabled as not ready.
//if err := a.MoveToFixBranch(*a.Actions["update"].argsv["branch"]) ; err != nil {
// return fmt.Errorf("Unable to move to your feature branch. %s", err)
//}
instances := a.define_drivers_execution_order()
// Loop on drivers requested like github or jenkins
for _, instance := range instances {
d := a.drivers[instance]
if err, aborted := a.do_driver_task("update", instance); err != nil {
if !aborted {
return fmt.Errorf("Failed to update '%s' source files. %s", instance, err)
}
log.Printf("Warning. %s", err)
continue
}
if d.HasNoFiles() {
gotrace.Info("No files to add/commit.")
continue
}
// Committing source code.
if err := a.do_driver_add(d); err != nil {
return fmt.Errorf("Failed to Add '%s' source files. %s", instance, err)
}
}
commitMsg := fmt.Sprintf("Forge '%s' updated.", a.w.Organization)
if deployPublish, found, _ := a.cli.GetBoolValue("_app", "forjj", "deploy-publish"); found && deployPublish {
if err := a.d.GitCommit(commitMsg); err != nil {
return fmt.Errorf("Failed to commit deploy files. %s", err)
}
if err := a.d.GitPush(false); err != nil {
return fmt.Errorf("Failed to push deploy commits. %s", err)
}
}
return nil
}
func (a *Forj) MoveToFixBranch(branch string) error {
a.Branch = branch
if ok, _ := regexp.MatchString(`^[\w_-]+$`, branch); !ok {
return fmt.Errorf("Invalid git branch name '%s'. alphanumeric, '_' and '-' are supported.", branch)
}
return nil
}