forked from forj-oss/forjj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maintain.go
188 lines (157 loc) · 5.21 KB
/
maintain.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
package main
import (
"log"
"fmt"
"forjj/creds"
"forjj/git"
"os"
"github.com/forj-oss/forjj-modules/trace"
)
func (a *Forj) maintainAction(string) {
if err := a.Maintain(); err != nil {
log.Fatalf("Forjj maintain issue. %s", err)
}
println("FORJJ - maintain ", a.w.Organization, " DONE") // , cmd.ProcessState.Sys().WaitStatus)
}
// Maintain call docker to create the Solution source code from scratch with validated parameters.
// This container do the real stuff (git/call drivers)
// I would expect to have this go tool to have a do_create to replace the shell script.
// But this would be a next version and needs to be validated before this decision is made.
// Workspace information already loaded by the cli context.
func (a *Forj) Maintain() error {
if _, err := a.w.Check_exist(); err != nil {
return fmt.Errorf("Invalid workspace. %s. Please create it with 'forjj create'", err)
}
// Validate from source
if err := a.ValidateForjfile(); err != nil {
return fmt.Errorf("Your Forjfile is having issues. %s Maintain aborted", err)
}
if err := a.f.BuildForjfileInMem(); err != nil {
return err
}
gotrace.Trace("Infra upstream selected: '%s'", a.w.Instance)
ffd := a.f.InMemForjfile()
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 with missing repos.
if err := a.FlowInit(); err != nil {
return err
}
// Dispatch information between Forjfile, cli and creds.
// Forjfile or creds are not saved and stay in memory.
if err := a.scanCreds(ffd, creds.Global, true); err != nil {
return err
}
if err := a.FlowApply(); err != nil {
return err
}
if err := a.scanAndSetDefaults(ffd, creds.Global); err != nil {
return fmt.Errorf("Unable to maintain. Issue on global cli/forjfile/creds dispatch. %s", err)
}
if err := a.get_infra_repo(); err != nil {
return fmt.Errorf("Invalid workspace. %s. Please create it with 'forjj create'", err)
}
// Now, we are in the infra repo root directory and at least, the 1st commit exist.
// Load drivers from forjj-options.yml
// loop from options/Repos and keep them in a.drivers
return a.do_maintain()
}
func (a *Forj) do_maintain() error {
// Loop on instances to maintain them
instances := a.define_drivers_execution_order()
for _, instance := range instances {
if err := a.doInstanceMaintain(instance); err != nil {
return fmt.Errorf("Unable to maintain requested resources of %s. %s", instance, err)
}
}
return nil
}
func (a *Forj) doInstanceMaintain(instance string) error {
if instance == "none" {
return nil
}
gotrace.Trace("Start maintaining instance '%s'", instance)
if err := a.driver_init(instance); err != nil {
return err
}
d := a.CurrentPluginDriver
// Ensure remote upstream exists - calling upstream driver - maintain
// This will create/update the upstream service
if err, _ := a.driver_do(d, instance, "maintain"); err != nil {
return fmt.Errorf("Driver issue. %s", err)
}
if a.f.GetInfraInstance() == instance {
// Update git remote and 'master' branch to infra repository.
var infra_name string
if i, found, err := a.GetPrefs(infra_name_f); err != nil {
return err
} else {
if !found {
return nil
}
infra_name = i
}
if r, found := d.Plugin.Result.Data.Repos[infra_name]; found {
for name, remote := range r.Remotes {
a.i.EnsureGitRemote(remote.Ssh, name)
}
for branch, remote := range r.BranchConnect {
status, err := a.i.EnsureBranchConnected(branch, remote)
if err != nil {
return err
}
switch status {
case "-1":
return fmt.Errorf("Warning! Remote branch is most recent than your local branch. " +
"Do a git pull and restart 'forjj maintain'")
case "+1":
git.Push()
case "-1+1":
return fmt.Errorf("Local and remote branch has diverged. You must fix it before going on")
}
}
}
// after the first upstream maintain call remote repo should exist
// So, we can sync it up if the sync was not done successfully before.
if err := a.d.RunInContext(func() error {
if !a.d.InSync() {
if err := a.d.GitSyncUp(); err != nil {
return err
}
// Create basic README.md file on deploy repos if no commit found
if _, err := git.Get("log", "-1", "--pretty=%H"); err != nil {
a.createInitialCommit()
git.Add([]string{"README.md"})
if err := git.Commit("Initial Source Deploy commit.", true); err != nil {
return err
}
}
return a.d.GitPush(false)
}
return nil
}); err != nil {
return err
}
}
return nil
}
// Must be in the current repo dir
func (a *Forj) createInitialCommit() error {
fd, err := os.Create("README.md")
if err != nil {
return err
}
defer fd.Close()
_, err = fd.WriteString("# Information\n\nThis repository (source Deployment repository) has been created by forjj. \n\nUse forjj update to update it from your infra source repository.\n\nForj team.\n")
if err != nil {
return err
}
return nil
}
// get_infra_repo detect in the path given contains the infra repository.
func (a *Forj) get_infra_repo() error {
return a.i.Use(a.f.InfraPath())
}