forked from ovh/cds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marathon.go
139 lines (117 loc) · 3.77 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path"
"strings"
)
// Application is the definition for an application in marathon
type Application struct {
ID string `json:"id,omitempty"`
Cmd string `json:"cmd,omitempty"`
Args []string `json:"args"`
Constraints [][]string `json:"constraints"`
CPUs float64 `json:"cpus,omitempty"`
Disk float64 `json:"disk,omitempty"`
Env map[string]string `json:"env"`
Executor string `json:"executor,omitempty"`
Instances int `json:"instances,omitempty"`
Mem float64 `json:"mem,omitempty"`
Ports []int `json:"ports"`
RequirePorts bool `json:"requirePorts,omitempty"`
BackoffSeconds float64 `json:"backoffSeconds,omitempty"`
BackoffFactor float64 `json:"backoffFactor,omitempty"`
MaxLaunchDelaySeconds float64 `json:"maxLaunchDelaySeconds,omitempty"`
Dependencies []string `json:"dependencies"`
TasksRunning int `json:"tasksRunning,omitempty"`
TasksStaged int `json:"tasksStaged,omitempty"`
TasksHealthy int `json:"tasksHealthy,omitempty"`
TasksUnhealthy int `json:"tasksUnhealthy,omitempty"`
User string `json:"user,omitempty"`
Uris []string `json:"uris"`
Version string `json:"version,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
AcceptedResourceRoles []string `json:"acceptedResourceRoles,omitempty"`
}
// Applications is a collection of applications
type Applications struct {
Apps []Application `json:"apps"`
}
func deleteApp(url, user, password string, appID string) error {
uri := url + path.Join("/v2/apps", appID)
req, err := http.NewRequest("DELETE", uri, nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(user, password)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
return fmt.Errorf("%s", resp.Status)
}
return nil
}
func getApps(url, user, password string, env string) ([]Application, error) {
req, err := http.NewRequest("GET", url+"/v2/apps?embed=apps.counts&id="+env, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(user, password)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var apps Applications
err = json.Unmarshal(body, &apps)
if err != nil {
return nil, err
}
return apps.Apps, nil
}
func countOf(model string, apps []Application) int {
var count int
for i := range apps {
if strings.Contains(apps[i].ID, "/"+strings.ToLower(model)+"-") {
count++
}
}
return count
}
func getDeployments(url, user, password string, env string) ([]Application, error) {
req, err := http.NewRequest("GET", url+"/v2/deployments", nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(user, password)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var apps []Application
err = json.Unmarshal(body, &apps)
if err != nil {
return nil, err
}
return apps, nil
}