forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_manifest.go
263 lines (216 loc) · 7.06 KB
/
generate_manifest.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package manifest
import (
"errors"
"fmt"
"code.cloudfoundry.org/cli/cf/models"
"gopkg.in/yaml.v2"
"io"
. "code.cloudfoundry.org/cli/cf/i18n"
)
//go:generate counterfeiter . App
type App interface {
BuildpackURL(string, string)
DiskQuota(string, int64)
Memory(string, int64)
Service(string, string)
StartCommand(string, string)
EnvironmentVars(string, string, string)
HealthCheckTimeout(string, int)
HealthCheckType(string, string)
HealthCheckHTTPEndpoint(string, string)
Instances(string, int)
Route(string, string, string, string, int)
GetContents() []models.Application
Stack(string, string)
AppPorts(string, []int)
Save(f io.Writer) error
}
type Application struct {
Name string `yaml:"name"`
Instances int `yaml:"instances,omitempty"`
Memory string `yaml:"memory,omitempty"`
DiskQuota string `yaml:"disk_quota,omitempty"`
AppPorts []int `yaml:"app-ports,omitempty"`
Routes []map[string]string `yaml:"routes,omitempty"`
NoRoute bool `yaml:"no-route,omitempty"`
Buildpack string `yaml:"buildpack,omitempty"`
Command string `yaml:"command,omitempty"`
Env map[string]interface{} `yaml:"env,omitempty"`
Services []string `yaml:"services,omitempty"`
Stack string `yaml:"stack,omitempty"`
Timeout int `yaml:"timeout,omitempty"`
HealthCheckType string `yaml:"health-check-type,omitempty"`
HealthCheckHTTPEndpoint string `yaml:"health-check-http-endpoint,omitempty"`
}
type Applications struct {
Applications []Application `yaml:"applications"`
}
type appManifest struct {
contents []models.Application
}
func NewGenerator() App {
return &appManifest{}
}
func (m *appManifest) Stack(appName string, stackName string) {
i := m.findOrCreateApplication(appName)
m.contents[i].Stack = &models.Stack{
Name: stackName,
}
}
func (m *appManifest) Memory(appName string, memory int64) {
i := m.findOrCreateApplication(appName)
m.contents[i].Memory = memory
}
func (m *appManifest) DiskQuota(appName string, diskQuota int64) {
i := m.findOrCreateApplication(appName)
m.contents[i].DiskQuota = diskQuota
}
func (m *appManifest) StartCommand(appName string, cmd string) {
i := m.findOrCreateApplication(appName)
m.contents[i].Command = cmd
}
func (m *appManifest) BuildpackURL(appName string, url string) {
i := m.findOrCreateApplication(appName)
m.contents[i].BuildpackURL = url
}
func (m *appManifest) HealthCheckTimeout(appName string, timeout int) {
i := m.findOrCreateApplication(appName)
m.contents[i].HealthCheckTimeout = timeout
}
func (m *appManifest) HealthCheckType(appName string, healthCheckType string) {
i := m.findOrCreateApplication(appName)
m.contents[i].HealthCheckType = healthCheckType
}
func (m *appManifest) HealthCheckHTTPEndpoint(appName string, healthCheckHTTPEndpoint string) {
i := m.findOrCreateApplication(appName)
m.contents[i].HealthCheckHTTPEndpoint = healthCheckHTTPEndpoint
}
func (m *appManifest) Instances(appName string, instances int) {
i := m.findOrCreateApplication(appName)
m.contents[i].InstanceCount = instances
}
func (m *appManifest) Service(appName string, name string) {
i := m.findOrCreateApplication(appName)
m.contents[i].Services = append(m.contents[i].Services, models.ServicePlanSummary{
GUID: "",
Name: name,
})
}
func (m *appManifest) Route(appName, host, domain, path string, port int) {
i := m.findOrCreateApplication(appName)
m.contents[i].Routes = append(m.contents[i].Routes, models.RouteSummary{
Host: host,
Domain: models.DomainFields{
Name: domain,
},
Path: path,
Port: port,
})
}
func (m *appManifest) EnvironmentVars(appName string, key, value string) {
i := m.findOrCreateApplication(appName)
m.contents[i].EnvironmentVars[key] = value
}
func (m *appManifest) AppPorts(appName string, appPorts []int) {
i := m.findOrCreateApplication(appName)
m.contents[i].AppPorts = appPorts
}
func (m *appManifest) GetContents() []models.Application {
return m.contents
}
func generateAppMap(app models.Application) (Application, error) {
if app.Stack == nil {
return Application{}, errors.New(T("required attribute 'stack' missing"))
}
if app.Memory == 0 {
return Application{}, errors.New(T("required attribute 'memory' missing"))
}
if app.DiskQuota == 0 {
return Application{}, errors.New(T("required attribute 'disk_quota' missing"))
}
if app.InstanceCount == 0 {
return Application{}, errors.New(T("required attribute 'instances' missing"))
}
var services []string
for _, s := range app.Services {
services = append(services, s.Name)
}
var routes []map[string]string
for _, routeSummary := range app.Routes {
routes = append(routes, buildRoute(routeSummary))
}
m := Application{
Name: app.Name,
Services: services,
Buildpack: app.BuildpackURL,
Memory: fmt.Sprintf("%dM", app.Memory),
Command: app.Command,
Env: app.EnvironmentVars,
Timeout: app.HealthCheckTimeout,
Instances: app.InstanceCount,
DiskQuota: fmt.Sprintf("%dM", app.DiskQuota),
Stack: app.Stack.Name,
AppPorts: app.AppPorts,
Routes: routes,
HealthCheckType: app.HealthCheckType,
HealthCheckHTTPEndpoint: app.HealthCheckHTTPEndpoint,
}
if len(app.Routes) == 0 {
m.NoRoute = true
}
return m, nil
}
func (m *appManifest) Save(f io.Writer) error {
apps := Applications{}
for _, app := range m.contents {
appMap, mapErr := generateAppMap(app)
if mapErr != nil {
return fmt.Errorf(T("Error saving manifest: {{.Error}}", map[string]interface{}{
"Error": mapErr.Error(),
}))
}
apps.Applications = append(apps.Applications, appMap)
}
contents, err := yaml.Marshal(apps)
if err != nil {
return err
}
_, err = f.Write(contents)
if err != nil {
return err
}
return nil
}
func buildRoute(routeSummary models.RouteSummary) map[string]string {
var route string
if routeSummary.Host != "" {
route = fmt.Sprintf("%s.", routeSummary.Host)
}
route = fmt.Sprintf("%s%s", route, routeSummary.Domain.Name)
if routeSummary.Path != "" {
route = fmt.Sprintf("%s%s", route, routeSummary.Path)
}
if routeSummary.Port != 0 {
route = fmt.Sprintf("%s:%d", route, routeSummary.Port)
}
return map[string]string{
"route": route,
}
}
func (m *appManifest) findOrCreateApplication(name string) int {
for i, app := range m.contents {
if app.Name == name {
return i
}
}
m.addApplication(name)
return len(m.contents) - 1
}
func (m *appManifest) addApplication(name string) {
m.contents = append(m.contents, models.Application{
ApplicationFields: models.ApplicationFields{
Name: name,
EnvironmentVars: make(map[string]interface{}),
},
})
}