-
Notifications
You must be signed in to change notification settings - Fork 12
/
marathon.go
163 lines (136 loc) · 5.05 KB
/
marathon.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
package marathon
import (
"encoding/json"
"errors"
"fmt"
"github.com/CiscoCloud/mantl-api/utils/http"
log "github.com/Sirupsen/logrus"
)
type Marathon struct {
httpClient *http.HttpClient
}
type PortMapping struct {
ContainerPort int `json:"containerPort,omitempty"`
HostPort int `json:"hostPort,omitempty"`
ServicePort int `json:"servicePort,omitempty"`
Protocol string `json:"protocol,omitempty"`
}
type Parameter struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
type Docker struct {
Image string `json:"image,omitempty"`
Parameters []Parameter `json:"parameters,omitempty"`
Privileged bool `json:"privileged,omitempty"`
Network string `json:"network,omitempty"`
PortMappings []PortMapping `json:"portMappings,omitempty"`
ForcePullImage bool `json:"forcePullImage,omitempty"`
}
type Volume struct {
ContainerPath string `json:"containerPath,omitempty"`
HostPath string `json:"hostPath,omitempty"`
Mode string `json:"mode,omitempty"`
}
type Container struct {
Docker *Docker `json:"docker,omitempty"`
Type string `json:"type,omitempty"`
Volumes []Volume `json:"volumes,omitempty"`
}
type HealthCheck struct {
Path string `json:"path,omitempty"`
PortIndex int `json:"portIndex,omitempty"`
Protocol string `json:"protocol,omitempty"`
GracePeriodSeconds int `json:"gracePeriodSeconds,omitempty"`
IntervalSeconds int `json:"intervalSeconds,omitempty"`
TimeoutSeconds int `json:"timeoutSeconds,omitempty"`
MaxConsecutiveFailures int `json:"maxConsecutiveFailures,omitempty"`
Command *HealthCheckCommand `json:"command,omitempty"`
}
type HealthCheckCommand struct {
Value string `json:"value,omitempty"`
}
type UpgradeStrategy struct {
MinimumHealthCapacity float64 `json:"minimumHealthCapacity,omitempty"`
MaximumOverCapacity float64 `json:"maximumOverCapacity,omitempty"`
}
type App struct {
Args []string `json:"args,omitempty"`
BackoffFactor float64 `json:"backoffFactor,omitempty"`
BackoffSeconds int `json:"backoffSeconds,omitempty"`
Cmd string `json:"cmd,omitempty"`
Constraints [][]string `json:"constraints,omitempty"`
Container *Container `json:"container,omitempty"`
CPUs float64 `json:"cpus,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
Disk float64 `json:"disk,omitempty"`
Env map[string]string `json:"env,omitempty"`
Executor string `json:"executor,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
HealthChecks []HealthCheck `json:"healthChecks,omitempty"`
ID string `json:"id,omitempty"`
Instances int `json:"instances,omitempty"`
Mem float64 `json:"mem,omitempty"`
Ports []int `json:"ports,omitempty"`
RequirePorts bool `json:"requirePorts,omitempty"`
StoreUrls []string `json:"storeUrls,omitempty"`
UpgradeStrategy UpgradeStrategy `json:"upgradeStrategy,omitempty"`
Uris []string `json:"uris,omitempty"`
User string `json:"user,omitempty"`
Version string `json:"version,omitempty"`
}
type AppResponse struct {
Apps []*App `json:"apps"`
}
func NewMarathon(url string, username string, password string, noVerifySsl bool) (*Marathon, error) {
httpClient, err := http.NewHttpClient(url, username, password, noVerifySsl)
if err != nil {
return nil, err
}
return &Marathon{
httpClient: httpClient,
}, nil
}
func (m Marathon) ToApp(appJson string) (*App, error) {
app := &App{}
err := json.Unmarshal([]byte(appJson), app)
return app, err
}
func (m Marathon) Apps() ([]*App, error) {
httpReq, err := m.httpClient.Get("/v2/apps")
if err != nil || (httpReq.Response.StatusCode != 200) {
return nil, err
}
body := httpReq.ResponseBody
apps := &AppResponse{}
err = json.Unmarshal(body, apps)
return apps.Apps, err
}
func (m Marathon) CreateApp(app *App) (string, error) {
jsonBlob, err := json.Marshal(app)
if err != nil {
return "", err
}
log.Debugf("app json: %s", string(jsonBlob))
httpReq, err := m.httpClient.Post("/v2/apps", jsonBlob)
if err != nil {
return "", err
}
switch httpReq.Response.StatusCode {
case 409:
return "", errors.New("409 Conflict - application already exists")
default:
return httpReq.ResponseText, nil
}
}
func (m Marathon) DestroyApp(appId string) (string, error) {
httpReq, err := m.httpClient.Delete("/v2/apps" + appId)
if err != nil {
return "", err
}
responseText := httpReq.ResponseText
if httpReq.Response.StatusCode != 200 {
return responseText, errors.New(fmt.Sprintf("Failed deleting %s from marathon: %s", appId, responseText))
}
return responseText, nil
}