forked from ContainX/depcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
267 lines (229 loc) · 7.07 KB
/
application.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
package marathon
import (
"errors"
"fmt"
"github.com/gondor/depcon/pkg/encoding"
"github.com/gondor/depcon/pkg/envsubst"
"github.com/gondor/depcon/pkg/httpclient"
"github.com/gondor/depcon/utils"
"os"
"time"
)
const (
ActionRestart = "restart"
ActionVersions = "versions"
PathTasks = "tasks"
)
var (
ErrorAppExists = errors.New("The application already exists")
ErrorGroupExists = errors.New("The group already exists")
ErrorInvalidAppId = errors.New("The application identifier is invalid")
ErrorInvalidGroupId = errors.New("The group identifier is invalid")
ErrorNoAppExists = errors.New("The application does not exist. Create an application before updating")
ErrorGropAppExists = errors.New("The group does not exist. Create a group before updating")
ErrorAppParamsMissing = errors.New("One or more ${PARAMS} that were defined in the app configuration could not be resolved.")
)
func (c *MarathonClient) CreateApplicationFromFile(filename string, opts *CreateOptions) (*Application, error) {
app, options, err := c.ParseApplicationFromFile(filename, opts)
if err != nil {
return app, err
}
return c.CreateApplication(app, options.Wait, options.Force)
}
func (c *MarathonClient) ParseApplicationFromFile(filename string, opts *CreateOptions) (*Application, *CreateOptions, error) {
log.Info("Creating Application from file: %s", filename)
options := initCreateOptions(opts)
file, err := os.Open(filename)
if err != nil {
return nil, nil, fmt.Errorf("Error opening filename %s, %s", filename, err.Error())
}
var encoder encoding.Encoder
encoder, err = encoding.NewEncoderFromFileExt(filename)
if err != nil {
return nil, nil, err
}
parsed, missing := envsubst.SubstFileTokens(file, filename, options.EnvParams)
if options.ErrorOnMissingParams && missing {
return nil, nil, ErrorAppParamsMissing
}
app := new(Application)
err = encoder.UnMarshalStr(parsed, &app)
if err != nil {
return nil, nil, err
}
return app, options, nil
}
func (c *MarathonClient) CreateApplication(app *Application, wait, force bool) (*Application, error) {
log.Info("Creating Application '%s', wait: %v, force: %v", app.ID, wait, force)
result := new(Application)
resp := c.http.HttpPost(c.marathonUrl(API_APPS), app, result)
if resp.Error != nil {
if resp.Error == httpclient.ErrorMessage {
if resp.Status == 409 {
if force {
return c.UpdateApplication(app, wait)
}
return nil, ErrorAppExists
}
if resp.Status == 422 {
return nil, ErrorInvalidAppId
}
return nil, fmt.Errorf("Error occurred (Status %v) Body -> %s", resp.Status, resp.Content)
}
return nil, resp.Error
}
if wait {
err := c.WaitForApplication(result.ID, c.determineTimeout(app))
if err != nil {
return result, err
}
}
app, err := c.GetApplication(result.ID)
if err == nil {
return app, nil
}
return result, nil
}
func (c *MarathonClient) UpdateApplication(app *Application, wait bool) (*Application, error) {
log.Info("Update Application '%s', wait = %v", app.ID, wait)
result := new(DeploymentID)
id := utils.TrimRootPath(app.ID)
app.ID = ""
resp := c.http.HttpPut(c.marathonUrl(API_APPS, id), app, result)
if resp.Error != nil {
if resp.Error == httpclient.ErrorMessage {
if resp.Status == 422 {
return nil, ErrorNoAppExists
}
}
return nil, resp.Error
}
if wait {
if err := c.WaitForDeployment(result.DeploymentID, c.determineTimeout(app)); err != nil {
return nil, err
}
err := c.WaitForApplication(id, c.determineTimeout(app))
if err != nil {
return nil, err
}
}
// Get the latest version of the application to return
app, err := c.GetApplication(id)
return app, err
}
func (c *MarathonClient) ListApplications() (*Applications, error) {
log.Debug("Enter: ListApplications")
apps := new(Applications)
resp := c.http.HttpGet(c.marathonUrl(API_APPS), apps)
if resp.Error != nil {
return nil, resp.Error
}
return apps, nil
}
func (c *MarathonClient) GetApplication(id string) (*Application, error) {
log.Debug("Enter: GetApplication: %s", id)
app := new(AppById)
resp := c.http.HttpGet(c.marathonUrl(API_APPS, id), app)
if resp.Error != nil {
return nil, resp.Error
}
return &app.App, nil
}
func (c *MarathonClient) HasApplication(id string) (bool, error) {
app, err := c.GetApplication(id)
if err != nil {
if err == httpclient.ErrorNotFound {
return false, nil
}
return false, err
}
return app != nil, nil
}
func (c *MarathonClient) DestroyApplication(id string) (*DeploymentID, error) {
log.Info("Deleting Application '%s'", id)
deploymentId := new(DeploymentID)
resp := c.http.HttpDelete(c.marathonUrl(API_APPS, id), nil, deploymentId)
if resp.Error != nil {
return nil, resp.Error
}
return deploymentId, nil
}
func (c *MarathonClient) RestartApplication(id string, force bool) (*DeploymentID, error) {
log.Info("Restarting Application '%s', force: %v", id, force)
deploymentId := new(DeploymentID)
uri := fmt.Sprintf("%s?force=%v", c.marathonUrl(API_APPS, id, ActionRestart), force)
resp := c.http.HttpPost(uri, nil, deploymentId)
if resp.Error != nil {
return nil, resp.Error
}
return deploymentId, nil
}
func (c *MarathonClient) ScaleApplication(id string, instances int) (*DeploymentID, error) {
log.Info("Scale Application '%s' to %v instances", id, instances)
update := new(Application)
update.ID = id
update.Instances = instances
deploymentID := new(DeploymentID)
resp := c.http.HttpPut(c.marathonUrl(API_APPS, id), &update, deploymentID)
if resp.Error != nil {
return nil, resp.Error
}
return deploymentID, nil
}
func (c *MarathonClient) ListVersions(id string) (*Versions, error) {
versions := new(Versions)
resp := c.http.HttpGet(c.marathonUrl(API_APPS, id, ActionVersions), versions)
if resp.Error != nil {
return nil, resp.Error
}
return versions, nil
}
func NewApplication(id string) *Application {
application := new(Application)
application.ID = id
return application
}
// The number of instances that the application should run
// {count} - number of instances
func (app *Application) Count(count int) *Application {
app.Instances = count
return app
}
// The amount of memory in MB to assign per instance
// {memory} - memory in MB
func (app *Application) Memory(memory float64) *Application {
app.Mem = memory
return app
}
// The amount of CPU shares to assign per instance
// {cpu} - the CPU shares
func (app *Application) CPU(cpu float64) *Application {
app.CPUs = cpu
return app
}
// Rolls back an application to a specific version
// {version} - the version to rollback
func (app *Application) RollbackVersion(version string) *Application {
app.Version = version
return app
}
func (c *MarathonClient) determineTimeout(app *Application) time.Duration {
if c.opts != nil && c.opts.WaitTimeout > 0 {
return c.opts.WaitTimeout
}
if app == nil {
return DefaultTimeout
}
max := DefaultTimeout
if len(app.HealthChecks) > 0 {
for _, h := range app.HealthChecks {
grace := time.Duration(h.GracePeriodSeconds) * time.Second
if grace > max {
max = grace
}
}
log.Debug("determineTimeout: Max is %d\n", max)
return max
}
return DefaultTimeout
}