-
Notifications
You must be signed in to change notification settings - Fork 7
/
project.go
216 lines (192 loc) · 5.2 KB
/
project.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
package main
import (
"bytes"
"fmt"
"regexp"
"strings"
"text/template"
yaml "gopkg.in/yaml.v2"
)
type PayloadVars struct {
Tag string
}
func (self PayloadVars) Parse(s string) (string, error) {
b := bytes.NewBuffer([]byte(""))
tmpl, err := template.New("").Parse(s)
if err != nil {
return "", err
}
err = tmpl.Execute(b, self)
return b.String(), err
}
type DeployPhase struct {
Name string `yaml:"name"`
Kind string `yaml:"kind"`
Path string `yaml:"path"` // for job
AutoDeploy bool `yaml:"autoDeploy"`
NotifyChannel string `yaml:"notifyChannel"`
Payload string `yaml:"payload"`
Destination Destination `yaml:"destination"`
}
func (p DeployPhase) None() bool {
return p.Name == ""
}
type DeployProject struct {
// ID is the name of the configmap that defines the project.
ID string
Kind string
jenkinsJob string
funcName string // for Lambda
gitHubRepository string
defaultBranch string
dockerRegistry string
filterRegexp string
targetRegexp string
DisableBranchDeploy bool
steps []string
Alias string
Phases []DeployPhase
}
func (p DeployProject) FindPhase(name string) DeployPhase {
for _, phase := range p.Phases {
if phase.Name == name {
return phase
}
}
return DeployPhase{}
}
func (pj DeployProject) JenkinsJob() string {
return pj.jenkinsJob
}
func (pj DeployProject) GitHubRepository() string {
return pj.gitHubRepository
}
func (pj DeployProject) FuncName() string {
return pj.funcName
}
func (pj DeployProject) Steps() []string {
return pj.steps
}
// ImageTagRegexp returns the regexp to filter image tags.
// This regexp is used to find the image tag from ECR.
// The regexp is parsed as a template, and the following variables are available:
//
// {{.Branch}}: The branch name of the target commit.
// {{.Phase}}: The phase name.
//
// See ECRClient.FindImageTagByRegexp for more details on how the regexp is used.
func (pj DeployProject) ImageTagRegexp() string {
if pj.filterRegexp == "" {
return "^{{.Branch}}$"
}
return pj.filterRegexp
}
func (pj DeployProject) TargetRegexp() string {
if pj.targetRegexp == "" {
return `\b[0-9a-f]{5,40}\b`
}
return pj.targetRegexp
}
func (pj DeployProject) DockerRepository() string {
return pj.dockerRegistry
}
func (pj DeployProject) DefaultBranch() string {
if pj.defaultBranch == "" {
return "master"
}
return pj.defaultBranch
}
func (pj DeployProject) ECRRepository() string {
path := strings.Split(pj.dockerRegistry, "/")
if len(path) < 2 {
return ""
}
return strings.Join(path[1:], "/")
}
func (pj DeployProject) ECRRegistryId() string {
path := strings.Split(pj.dockerRegistry, ".")
if len(path) < 2 {
return ""
}
return path[0]
}
type ProjectList struct {
Items []DeployProject
}
func NewProjectList() (pl ProjectList) {
pl.Reload()
return
}
func (p *ProjectList) Reload() {
var tmp []DeployProject
cml := getConfigMapList("project")
for _, cm := range cml.Items {
pj := DeployProject{}
pj.ID = cm.Name
pj.Kind = cm.Data["Kind"]
pj.jenkinsJob = cm.Data["JenkinsJob"]
pj.gitHubRepository = cm.Data["GitHubRepository"]
pj.dockerRegistry = cm.Data["DockerRegistry"]
pj.defaultBranch = cm.Data["DefaultBranch"]
pj.filterRegexp = cm.Data["FilterRegexp"]
pj.targetRegexp = cm.Data["TargetRegexp"]
pj.funcName = cm.Data["FuncName"]
// Note that, although this is named Alias, it is actually treated as a
// mandatory ID of the project, which is used to identify the project.
// to be deployed in some places.
pj.Alias = cm.Data["Alias"]
pj.DisableBranchDeploy = cm.Data["DisableBranchDeploy"] == "true"
if err := yaml.Unmarshal([]byte(cm.Data["Steps"]), &pj.steps); err != nil {
fmt.Printf("[ERROR] Failed to parse steps for %s: %s\n", pj.ID, err)
}
if err := yaml.Unmarshal([]byte(cm.Data["Phases"]), &pj.Phases); err != nil {
fmt.Printf("[ERROR] Failed to parse phases for %s: %s\n", pj.ID, err)
}
for i, phase := range pj.Phases {
if phase.Kind == "" {
pj.Phases[i].Kind = pj.Kind
}
if phase.Destination.Kind == "" {
pj.Phases[i].Destination.Kind = pj.Phases[i].Kind
}
if phase.Destination.Kustomize.Path == "" {
pj.Phases[i].Destination.Kustomize.Path = phase.Path
}
if phase.Destination.Kustomize.Image == "" {
pj.Phases[i].Destination.Kustomize.Image = pj.DockerRepository()
}
if phase.Destination.ECS.Image == "" {
pj.Phases[i].Destination.ECS.Image = pj.DockerRepository()
}
}
tmp = append(tmp, pj)
}
p.Items = tmp
}
func (p ProjectList) FindAll(ids []string) (o []DeployProject) {
for _, id := range ids {
for _, pj := range p.Items {
if pj.ID == id {
o = append(o, pj)
}
}
}
return o
}
func (p ProjectList) Find(id string) DeployProject {
for _, pj := range p.Items {
if pj.ID == id {
return pj
}
}
fmt.Printf("[ERROR] No Such Project. ID: %s", id)
return DeployProject{}
}
func (p ProjectList) FindByAlias(id string) (DeployProject, error) {
for _, pj := range p.Items {
if regexp.MustCompile(pj.Alias).Match([]byte(id)) {
return pj, nil
}
}
return DeployProject{}, fmt.Errorf("[ERROR] No Such Project. ID: %s", id)
}