-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathps.go
More file actions
79 lines (61 loc) · 1.65 KB
/
ps.go
File metadata and controls
79 lines (61 loc) · 1.65 KB
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
package ps
import (
"encoding/json"
"fmt"
"strconv"
"github.com/deis/workflow/client/controller/api"
"github.com/deis/workflow/client/controller/client"
)
// List an app's processes.
func List(c *client.Client, appID string, results int) ([]api.Pods, int, error) {
u := fmt.Sprintf("/v2/apps/%s/pods/", appID)
body, count, err := c.LimitedRequest(u, results)
if err != nil {
return []api.Pods{}, -1, err
}
var procs []api.Pods
if err = json.Unmarshal([]byte(body), &procs); err != nil {
return []api.Pods{}, -1, err
}
return procs, count, nil
}
// Scale an app's processes.
func Scale(c *client.Client, appID string, targets map[string]int) error {
u := fmt.Sprintf("/v2/apps/%s/scale/", appID)
body, err := json.Marshal(targets)
if err != nil {
return err
}
_, err = c.BasicRequest("POST", u, body)
return err
}
// Restart an app's processes.
func Restart(c *client.Client, appID string, procType string, num int) ([]api.Process, error) {
u := fmt.Sprintf("/v2/apps/%s/containers/", appID)
if procType == "" {
u += "restart/"
} else {
if num == -1 {
u += procType + "/restart/"
} else {
u += procType + "/" + strconv.Itoa(num) + "/restart/"
}
}
body, err := c.BasicRequest("POST", u, nil)
if err != nil {
return []api.Process{}, err
}
procs := []api.Process{}
if err = json.Unmarshal([]byte(body), &procs); err != nil {
return []api.Process{}, err
}
return procs, nil
}
// ByType organizes processes of an app by process type.
func ByType(processes []api.Pods) map[string][]api.Pods {
psMap := make(map[string][]api.Pods)
for _, ps := range processes {
psMap[ps.Type] = append(psMap[ps.Type], ps)
}
return psMap
}