forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_manifest.go
279 lines (232 loc) · 6.78 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package manifest
import (
"errors"
"fmt"
"github.com/cloudfoundry/cli/cf/models"
"gopkg.in/yaml.v2"
"io"
. "github.com/cloudfoundry/cli/cf/i18n"
)
//go:generate counterfeiter . AppManifest
type AppManifest interface {
BuildpackUrl(string, string)
DiskQuota(string, int64)
Memory(string, int64)
Service(string, string)
StartCommand(string, string)
EnvironmentVars(string, string, string)
HealthCheckTimeout(string, int)
Instances(string, int)
Domain(string, string, string)
GetContents() []models.Application
Stack(string, string)
AppPorts(string, []int)
Save(f io.Writer) error
}
type ManifestApplication 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"`
Host string `yaml:"host,omitempty"`
Hosts []string `yaml:"hosts,omitempty"`
Domain string `yaml:"domain,omitempty"`
Domains []string `yaml:"domains,omitempty"`
NoHostname bool `yaml:"no-hostname,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"`
}
type ManifestApplications struct {
Applications []ManifestApplication `yaml:"applications"`
}
type appManifest struct {
contents []models.Application
}
func NewGenerator() AppManifest {
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) 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) Domain(appName string, host string, domain string) {
i := m.findOrCreateApplication(appName)
m.contents[i].Routes = append(m.contents[i].Routes, models.RouteSummary{
Host: host,
Domain: models.DomainFields{
Name: domain,
},
})
}
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) (ManifestApplication, error) {
if app.Stack == nil {
return ManifestApplication{}, errors.New(T("required attribute 'stack' missing"))
}
if app.Memory == 0 {
return ManifestApplication{}, errors.New(T("required attribute 'memory' missing"))
}
if app.DiskQuota == 0 {
return ManifestApplication{}, errors.New(T("required attribute 'disk_quota' missing"))
}
if app.InstanceCount == 0 {
return ManifestApplication{}, errors.New(T("required attribute 'instances' missing"))
}
var services []string
for _, s := range app.Services {
services = append(services, s.Name)
}
m := ManifestApplication{
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,
}
switch len(app.Routes) {
case 0:
m.NoRoute = true
case 1:
const noHostname = ""
m.Domain = app.Routes[0].Domain.Name
host := app.Routes[0].Host
if host == noHostname {
m.NoHostname = true
} else {
m.Host = host
}
default:
hosts, domains := separateHostsAndDomains(app.Routes)
switch len(hosts) {
case 0:
m.NoHostname = true
case 1:
m.Host = hosts[0]
default:
m.Hosts = hosts
}
switch len(domains) {
case 1:
m.Domain = domains[0]
default:
m.Domains = domains
}
}
return m, nil
}
func (m *appManifest) Save(f io.Writer) error {
apps := ManifestApplications{}
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 (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{}),
},
})
}
func separateHostsAndDomains(routes []models.RouteSummary) ([]string, []string) {
var (
hostSlice []string
domainSlice []string
hostPSlice *[]string
domainPSlice *[]string
hosts []string
domains []string
)
for i := 0; i < len(routes); i++ {
hostSlice = append(hostSlice, routes[i].Host)
domainSlice = append(domainSlice, routes[i].Domain.Name)
}
hostPSlice = removeDuplicatedValue(hostSlice)
domainPSlice = removeDuplicatedValue(domainSlice)
if hostPSlice != nil {
hosts = *hostPSlice
}
if domainPSlice != nil {
domains = *domainPSlice
}
return hosts, domains
}