-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontainer.go
82 lines (68 loc) · 1.71 KB
/
container.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/Sirupsen/logrus"
)
type Container struct {
Id string
Names []string
Labels map[string]string
Status string
}
// Get a list of all the running containers of the given service.
func listContainers(app, service string) ([]*Container, error) {
appFilter := fmt.Sprintf("csphere_instancename=%s", app)
serviceFilter := fmt.Sprintf("csphere_servicename=%s", service)
args := map[string][]string{
"labels": {appFilter, serviceFilter},
}
u, err := url.Parse(controllerAddr)
if err != nil {
return nil, err
}
filter, _ := json.Marshal(args)
u.Path = "/api/containers"
v := url.Values{}
v.Set("filter", string(filter))
v.Set("ApiKey", apiKey)
u.RawQuery = v.Encode()
res, err := http.Get(u.String())
if err != nil {
return nil, err
}
defer res.Body.Close()
containers := []*Container{}
err = json.NewDecoder(res.Body).Decode(&containers)
if err != nil {
return nil, err
}
r := make([]*Container, 0, len(containers))
for _, c := range containers {
if strings.HasPrefix(c.Status, "Up ") {
r = append(r, c)
}
}
logrus.Debugf("Get %d runnig containers", len(r))
return r, nil
}
func scale(app, service string, n int) error {
u := controllerAddr + fmt.Sprintf("/api/instances/%s/%s/changesum?ApiKey=%s&sum=%d", app, service, apiKey, n)
logrus.Debugf("scale api url: %s", u)
req, err := http.NewRequest("PATCH", u, nil)
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusNoContent {
return nil
}
return fmt.Errorf("unexpecte http status %d", res.StatusCode)
}