-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_importer.go
235 lines (208 loc) · 7.3 KB
/
application_importer.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
package application
import (
"github.com/ovh/cds/engine/api/database"
"github.com/ovh/cds/engine/api/environment"
"github.com/ovh/cds/engine/api/group"
"github.com/ovh/cds/engine/api/hook"
"github.com/ovh/cds/engine/api/msg"
"github.com/ovh/cds/engine/api/pipeline"
"github.com/ovh/cds/engine/api/repositoriesmanager"
"github.com/ovh/cds/engine/api/trigger"
"github.com/ovh/cds/engine/log"
"github.com/ovh/cds/sdk"
)
//Import is able to create a new application and all its components
func Import(db database.QueryExecuter, proj *sdk.Project, app *sdk.Application, repomanager *sdk.RepositoriesManager, user *sdk.User, msgChan chan<- msg.Message) error {
//Save application in database
if err := InsertApplication(db, proj, app); err != nil {
return err
}
if msgChan != nil {
msgChan <- msg.New(msg.AppCreated, app.Name)
}
//Inherit project groups if not provided
if app.ApplicationGroups == nil {
if msgChan != nil {
msgChan <- msg.New(msg.AppGroupInheritPermission, app.Name)
}
app.ApplicationGroups = proj.ProjectGroups
}
//Insert group permission on application
for i := range app.ApplicationGroups {
//Load the group by name
g, err := group.LoadGroup(db, app.ApplicationGroups[i].Group.Name)
if err != nil {
return err
}
log.Debug("application.Import> Insert group %d in application", g.ID)
if err := group.InsertGroupInApplication(db, app.ID, g.ID, app.ApplicationGroups[i].Permission); err != nil {
return err
}
if msgChan != nil {
msgChan <- msg.New(msg.AppGroupSetPermission, g.Name, app.Name)
}
}
if err := importVariables(db, proj, app, user, msgChan); err != nil {
return err
}
if err := ImportPipelines(db, proj, app, msgChan); err != nil {
return err
}
//Set repositories manager
app.RepositoriesManager = repomanager
if app.RepositoriesManager != nil && app.RepositoryFullname != "" && len(app.Pipelines) > 0 {
if err := repositoriesmanager.InsertForApplication(db, app, proj.Key); err != nil {
return err
}
//Manage hook
if _, err := hook.CreateHook(db, proj.Key, repomanager, app.RepositoryFullname, app, &app.Pipelines[0].Pipeline); err != nil {
return err
}
if msgChan != nil {
msgChan <- msg.New(msg.HookCreated, app.RepositoryFullname, app.Pipelines[0].Pipeline.Name)
}
}
return nil
}
//importVariables is able to create variable on an existing application
func importVariables(db database.QueryExecuter, proj *sdk.Project, app *sdk.Application, user *sdk.User, msgChan chan<- msg.Message) error {
for _, newVar := range app.Variable {
var errCreate error
switch newVar.Type {
case sdk.KeyVariable:
errCreate = AddKeyPairToApplication(db, app, newVar.Name)
break
default:
errCreate = InsertVariable(db, app, newVar)
break
}
if errCreate != nil {
log.Warning("importVariables> Cannot add variable %s in application %s: %s\n", newVar.Name, app.Name, errCreate)
return errCreate
}
if err := CreateAudit(db, newVar.Name, app, user); err != nil {
log.Warning("importVariabitles> Cannot create variable audit for application %s: %s\n", app.Name, err)
return err
}
}
if msgChan != nil {
msgChan <- msg.New(msg.AppVariablesCreated, app.Name)
}
return nil
}
//ImportPipelines is able to create pipelines on an existing application
func ImportPipelines(db database.QueryExecuter, proj *sdk.Project, app *sdk.Application, msgChan chan<- msg.Message) error {
//Import pipelines
for i := range app.Pipelines {
//Import pipeline
log.Debug("application.Import> Import pipeline %s", app.Pipelines[i].Pipeline.Name)
if err := pipeline.Import(db, proj, &app.Pipelines[i].Pipeline, msgChan); err != nil {
return err
}
//Check if application is attached to the pipeline
attached, err := IsAttached(db, proj.ID, app.ID, app.Pipelines[i].Pipeline.Name)
if err != nil {
return err
}
//Attach pipeline
if !attached {
log.Debug("application.Import> Attach pipeline %s", app.Pipelines[i].Pipeline.Name)
if err := AttachPipeline(db, app.ID, app.Pipelines[i].Pipeline.ID); err != nil {
return err
}
if msgChan != nil {
msgChan <- msg.New(msg.PipelineAttached, app.Pipelines[i].Pipeline.Name, app.Name)
}
}
}
//Insert triggers
for i := range app.Pipelines {
for j := range app.Pipelines[i].Triggers {
t := &app.Pipelines[i].Triggers[j]
// You have an existing build pipeline. You want to create a template
// for create a deploy package, and this template add trigger with only srcApp.
// so, if SrcApplication.Name != "" -> load existing application.
if t.SrcApplication.Name == "" {
//Source application is the current application
t.SrcApplication = *app
log.Debug("ImportPipelines> current app")
} else {
log.Debug("Load t.SrcApplication.Name:%s", t.SrcApplication.Name)
srcApp, err := LoadApplicationByName(db, proj.Key, t.SrcApplication.Name)
if err != nil {
return err
}
t.SrcApplication = *srcApp
}
// Same explanation for pipeline
if t.SrcPipeline.Name == "" {
//Source pipeline is the current pipeline
t.SrcPipeline = app.Pipelines[i].Pipeline
log.Debug("ImportPipelines> current pipeline")
} else {
log.Debug("ImportPipelines> Load t.SrcPipeline.Name:%s", t.SrcApplication.Name)
srcPipeline, err := pipeline.LoadPipeline(db, proj.Key, t.SrcPipeline.Name, false)
if err != nil {
return err
}
t.SrcPipeline = *srcPipeline
}
//Load destination App
if t.DestApplication.Name == "" {
t.DestApplication = *app
} else {
dest, err := LoadApplicationByName(db, proj.Key, t.DestApplication.Name)
if err != nil {
return err
}
t.DestApplication = *dest
}
//Load dest pipeline
if t.DestPipeline.Name == "" {
t.DestPipeline = app.Pipelines[i].Pipeline
} else {
destPipeline, err := pipeline.LoadPipeline(db, proj.Key, t.DestPipeline.Name, false)
if err != nil {
return err
}
t.DestPipeline = *destPipeline
}
//Load or import source environmment
if t.SrcEnvironment.Name == "" {
t.SrcEnvironment = sdk.DefaultEnv
} else {
if err := environment.Import(db, proj, &t.SrcEnvironment, msgChan); err != nil {
return err
}
}
//Load or import destination environment
if t.DestEnvironment.Name == "" {
t.DestEnvironment = sdk.DefaultEnv
} else {
if err := environment.Import(db, proj, &t.DestEnvironment, msgChan); err != nil {
return err
}
}
//Check if environment and pipeline type are compatible
if t.DestEnvironment.ID == sdk.DefaultEnv.ID && t.DestPipeline.Type == sdk.DeploymentPipeline {
return sdk.ErrNoEnvironmentProvided
}
log.Debug("application.Import> creating trigger SrcApp=%d SrpPip=%d SrcEnv=%d DestApp=%d DestPip=%d DestEnv=%d", t.SrcApplication.ID, t.SrcPipeline.ID, t.SrcEnvironment.ID, t.DestApplication.ID, t.DestPipeline.ID, t.DestEnvironment.ID)
//Check if trigger exists
exists, err := trigger.Exists(db, t.SrcApplication.ID, t.SrcPipeline.ID, t.SrcEnvironment.ID, t.DestApplication.ID, t.DestPipeline.ID, t.DestEnvironment.ID)
if err != nil {
return err
}
if !exists {
//Insert trigger
if err := trigger.InsertTrigger(db, t); err != nil {
return err
}
if msgChan != nil {
msgChan <- msg.New(msg.PipelineTriggerCreated, t.SrcPipeline.Name, t.SrcApplication.Name, t.DestPipeline.Name, t.DestApplication.Name)
}
}
}
}
return nil
}