forked from stelligent/mu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_upsert.go
165 lines (140 loc) · 5.89 KB
/
pipeline_upsert.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
package workflows
import (
"bytes"
"fmt"
"github.com/stelligent/mu/common"
"github.com/stelligent/mu/templates"
"regexp"
"strconv"
"strings"
)
// NewPipelineUpserter create a new workflow for upserting a pipeline
func NewPipelineUpserter(ctx *common.Context, tokenProvider func(bool) string) Executor {
workflow := new(pipelineWorkflow)
workflow.codeRevision = ctx.Config.Repo.Revision
workflow.codeBranch = ctx.Config.Repo.Branch
workflow.repoName = ctx.Config.Repo.Slug
return newPipelineExecutor(
workflow.serviceFinder("", ctx),
workflow.pipelineBucket(ctx.StackManager, ctx.StackManager),
workflow.pipelineUpserter(tokenProvider, ctx.StackManager, ctx.StackManager),
)
}
// Setup the artifact bucket
func (workflow *pipelineWorkflow) pipelineBucket(stackUpserter common.StackUpserter, stackWaiter common.StackWaiter) Executor {
return func() error {
bucketStackName := common.CreateStackName(common.StackTypeBucket, "codepipeline")
overrides := common.GetStackOverrides(bucketStackName)
template, err := templates.NewTemplate("bucket.yml", nil, overrides)
if err != nil {
return err
}
log.Noticef("Upserting Bucket for CodePipeline")
bucketParams := make(map[string]string)
bucketParams["BucketPrefix"] = "codepipeline"
err = stackUpserter.UpsertStack(bucketStackName, template, bucketParams, buildPipelineTags(workflow.serviceName, common.StackTypeBucket, workflow.codeRevision, workflow.repoName))
if err != nil {
return err
}
log.Debugf("Waiting for stack '%s' to complete", bucketStackName)
stack := stackWaiter.AwaitFinalStatus(bucketStackName)
if stack == nil {
return fmt.Errorf("Unable to create stack %s", bucketStackName)
}
if strings.HasSuffix(stack.Status, "ROLLBACK_COMPLETE") || !strings.HasSuffix(stack.Status, "_COMPLETE") {
return fmt.Errorf("Ended in failed status %s %s", stack.Status, stack.StatusReason)
}
return nil
}
}
func (workflow *pipelineWorkflow) pipelineUpserter(tokenProvider func(bool) string, stackUpserter common.StackUpserter, stackWaiter common.StackWaiter) Executor {
return func() error {
pipelineStackName := common.CreateStackName(common.StackTypePipeline, workflow.serviceName)
pipelineStack := stackWaiter.AwaitFinalStatus(pipelineStackName)
overrides := common.GetStackOverrides(pipelineStackName)
log.Noticef("Upserting Pipeline for service '%s' ...", workflow.serviceName)
template, err := templates.NewTemplate("pipeline.yml", nil, overrides)
if err != nil {
return err
}
pipelineParams := make(map[string]string)
pipelineParams["MuFile"] = workflow.muFile
pipelineParams["SourceProvider"] = workflow.pipelineConfig.Source.Provider
pipelineParams["SourceRepo"] = workflow.pipelineConfig.Source.Repo
pipelineParams["SourceBranch"] = workflow.codeBranch
if workflow.pipelineConfig.Source.Provider == "GitHub" {
pipelineParams["GitHubToken"] = tokenProvider(pipelineStack == nil)
}
if workflow.pipelineConfig.Build.Type != "" {
pipelineParams["BuildType"] = workflow.pipelineConfig.Build.Type
}
if workflow.pipelineConfig.Build.ComputeType != "" {
pipelineParams["BuildComputeType"] = workflow.pipelineConfig.Build.ComputeType
}
if workflow.pipelineConfig.Build.Image != "" {
pipelineParams["BuildImage"] = workflow.pipelineConfig.Build.Image
}
if workflow.pipelineConfig.Acceptance.Type != "" {
pipelineParams["TestType"] = workflow.pipelineConfig.Acceptance.Type
}
if workflow.pipelineConfig.Acceptance.ComputeType != "" {
pipelineParams["TestComputeType"] = workflow.pipelineConfig.Acceptance.ComputeType
}
if workflow.pipelineConfig.Acceptance.Image != "" {
pipelineParams["TestImage"] = workflow.pipelineConfig.Acceptance.Image
}
if workflow.pipelineConfig.Acceptance.Environment != "" {
pipelineParams["AcptEnv"] = workflow.pipelineConfig.Acceptance.Environment
}
if workflow.pipelineConfig.Production.Environment != "" {
pipelineParams["ProdEnv"] = workflow.pipelineConfig.Production.Environment
}
if workflow.pipelineConfig.MuBaseurl != "" {
pipelineParams["MuDownloadBaseurl"] = workflow.pipelineConfig.MuBaseurl
}
pipelineParams["EnableBuildStage"] = strconv.FormatBool(!workflow.pipelineConfig.Build.Disabled)
pipelineParams["EnableAcptStage"] = strconv.FormatBool(!workflow.pipelineConfig.Acceptance.Disabled)
pipelineParams["EnableProdStage"] = strconv.FormatBool(!workflow.pipelineConfig.Production.Disabled)
// get default buildspec
buildspec, err := templates.NewTemplate("buildspec.yml", nil, nil)
if err != nil {
return err
}
buildspecBytes := new(bytes.Buffer)
buildspecBytes.ReadFrom(buildspec)
newlineRegexp := regexp.MustCompile(`\r?\n`)
buildspecString := newlineRegexp.ReplaceAllString(buildspecBytes.String(), "\\n")
pipelineParams["DefaultBuildspec"] = buildspecString
version := workflow.pipelineConfig.MuVersion
if version == "" {
version = common.GetVersion()
if version == "0.0.0-local" {
version = ""
}
}
if version != "" {
pipelineParams["MuDownloadVersion"] = version
}
err = stackUpserter.UpsertStack(pipelineStackName, template, pipelineParams, buildPipelineTags(workflow.serviceName, common.StackTypePipeline, workflow.codeRevision, workflow.repoName))
if err != nil {
return err
}
log.Debugf("Waiting for stack '%s' to complete", pipelineStackName)
stack := stackWaiter.AwaitFinalStatus(pipelineStackName)
if stack == nil {
return fmt.Errorf("Unable to create stack %s", pipelineStackName)
}
if strings.HasSuffix(stack.Status, "ROLLBACK_COMPLETE") || !strings.HasSuffix(stack.Status, "_COMPLETE") {
return fmt.Errorf("Ended in failed status %s %s", stack.Status, stack.StatusReason)
}
return nil
}
}
func buildPipelineTags(serviceName string, stackType common.StackType, codeRevision string, repoName string) map[string]string {
return map[string]string{
"type": string(stackType),
"service": serviceName,
"revision": codeRevision,
"repo": repoName,
}
}