-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathworkflow_exporter.go
148 lines (124 loc) · 4.53 KB
/
workflow_exporter.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
package workflow
import (
"context"
"fmt"
"github.com/go-gorp/gorp"
"github.com/ovh/cds/engine/api/application"
"github.com/ovh/cds/engine/api/environment"
"github.com/ovh/cds/engine/cache"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/exportentities"
v2 "github.com/ovh/cds/sdk/exportentities/v2"
"github.com/ovh/cds/sdk/telemetry"
)
// Export a workflow
func Export(ctx context.Context, db gorp.SqlExecutor, cache cache.Store, proj sdk.Project, name string, opts ...v2.ExportOptions) (exportentities.Workflow, error) {
ctx, end := telemetry.Span(ctx, "workflow.Export")
defer end()
wf, err := Load(ctx, db, cache, proj, name, LoadOptions{})
if err != nil {
return v2.Workflow{}, sdk.WrapError(err, "cannot load workflow %s", name)
}
// If repo is from as-code do not export WorkflowSkipIfOnlyOneRepoWebhook
if wf.FromRepository != "" {
opts = append(opts, v2.WorkflowSkipIfOnlyOneRepoWebhook)
}
wkf, err := exportentities.NewWorkflow(ctx, *wf, opts...)
if err != nil {
return v2.Workflow{}, sdk.WrapError(err, "unable to export workflow")
}
return wkf, nil
}
// Pull a workflow with all it dependencies; it writes a tar buffer in the writer
func Pull(ctx context.Context, db gorp.SqlExecutor, cache cache.Store, proj sdk.Project, name string,
encryptFunc sdk.EncryptFunc, opts ...v2.ExportOptions) (exportentities.WorkflowComponents, error) {
ctx, end := telemetry.Span(ctx, "workflow.Pull")
defer end()
var wp exportentities.WorkflowComponents
wf, err := Load(ctx, db, cache, proj, name, LoadOptions{
DeepPipeline: true,
WithTemplate: true,
})
if err != nil {
return wp, sdk.WrapError(err, "cannot load workflow %s", name)
}
if wf.TemplateInstance != nil {
return exportentities.WorkflowComponents{
Template: exportentities.TemplateInstance{
Name: wf.Name,
From: fmt.Sprintf("%s@%d", wf.TemplateInstance.Template.Path(), wf.TemplateInstance.WorkflowTemplateVersion),
Parameters: wf.TemplateInstance.Request.Parameters,
},
}, nil
}
// Reload app to retrieve secrets
for i := range wf.Applications {
app := wf.Applications[i]
vars, err := application.LoadAllVariablesWithDecrytion(ctx, db, app.ID)
if err != nil {
return wp, sdk.WrapError(err, "cannot load application variables %s", app.Name)
}
app.Variables = vars
keys, err := application.LoadAllKeysWithPrivateContent(ctx, db, app.ID)
if err != nil {
return wp, sdk.WrapError(err, "cannot load application keys %s", app.Name)
}
app.Keys = keys
deploymentStrategies, err := application.LoadDeploymentStrategies(ctx, db, app.ID, true)
if err != nil {
return wp, sdk.WrapError(err, "cannot load deployment strategies for application %s", app.Name)
}
app.DeploymentStrategies = deploymentStrategies
wf.Applications[i] = app
}
// Reload env to retrieve secrets
for i := range wf.Environments {
env := wf.Environments[i]
vars, err := environment.LoadAllVariablesWithDecrytion(db, env.ID)
if err != nil {
return wp, sdk.WrapError(err, "cannot load environment variables %s", env.Name)
}
env.Variables = vars
keys, err := environment.LoadAllKeysWithPrivateContent(db, env.ID)
if err != nil {
return wp, sdk.WrapError(err, "cannot load environment keys %s", env.Name)
}
env.Keys = keys
wf.Environments[i] = env
}
// If the repository is "as-code", hide the hook
if wf.FromRepository != "" {
opts = append(opts, v2.WorkflowSkipIfOnlyOneRepoWebhook)
}
wp.Workflow, err = exportentities.NewWorkflow(ctx, *wf, opts...)
if err != nil {
return wp, sdk.WrapError(err, "unable to export workflow")
}
for _, a := range wf.Applications {
if a.FromRepository != wf.FromRepository { // don't export if coming from an other repository
continue
}
app, err := application.ExportApplication(ctx, db, a, encryptFunc, fmt.Sprintf("appID:%d", a.ID))
if err != nil {
return wp, sdk.WrapError(err, "unable to export app %s", a.Name)
}
wp.Applications = append(wp.Applications, app)
}
for _, e := range wf.Environments {
if e.FromRepository != wf.FromRepository { // don't export if coming from an other repository
continue
}
env, err := environment.ExportEnvironment(ctx, db, e, encryptFunc, fmt.Sprintf("env:%d", e.ID))
if err != nil {
return wp, sdk.WrapError(err, "unable to export env %s", e.Name)
}
wp.Environments = append(wp.Environments, env)
}
for _, p := range wf.Pipelines {
if p.FromRepository != wf.FromRepository { // don't export if coming from an other repository
continue
}
wp.Pipelines = append(wp.Pipelines, exportentities.NewPipelineV1(p))
}
return wp, nil
}