-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathps.go
More file actions
148 lines (109 loc) · 2.68 KB
/
ps.go
File metadata and controls
148 lines (109 loc) · 2.68 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
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
package cmd
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/deis/deis/client/controller/api"
"github.com/deis/deis/client/controller/models/ps"
)
// PsList lists an app's processes.
func PsList(appID string, results int) error {
c, appID, err := load(appID)
if err != nil {
return err
}
if results == defaultLimit {
results = c.ResponseLimit
}
processes, count, err := ps.List(c, appID, results)
if err != nil {
return err
}
printProcesses(appID, processes, count)
return nil
}
// PsScale scales an app's processes.
func PsScale(appID string, targets []string) error {
c, appID, err := load(appID)
if err != nil {
return err
}
targetMap := make(map[string]int)
regex := regexp.MustCompile("^([A-z]+)=([0-9]+)$")
for _, target := range targets {
if regex.MatchString(target) {
captures := regex.FindStringSubmatch(target)
targetMap[captures[1]], err = strconv.Atoi(captures[2])
if err != nil {
return err
}
} else {
fmt.Printf("'%s' does not match the pattern 'type=num', ex: web=2\n", target)
}
}
fmt.Printf("Scaling processes... but first, %s!\n", drinkOfChoice())
startTime := time.Now()
quit := progress()
err = ps.Scale(c, appID, targetMap)
quit <- true
<-quit
if err != nil {
return err
}
fmt.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
processes, count, err := ps.List(c, appID, c.ResponseLimit)
if err != nil {
return err
}
printProcesses(appID, processes, count)
return nil
}
// PsRestart restarts an app's processes.
func PsRestart(appID, target string) error {
c, appID, err := load(appID)
if err != nil {
return err
}
psType := ""
psNum := -1
if target != "" {
if strings.Contains(target, ".") {
parts := strings.Split(target, ".")
psType = parts[0]
psNum, err = strconv.Atoi(parts[1])
if err != nil {
return err
}
} else {
psType = target
}
}
fmt.Printf("Restarting processes... but first, %s!\n", drinkOfChoice())
startTime := time.Now()
quit := progress()
_, err = ps.Restart(c, appID, psType, psNum)
quit <- true
<-quit
if err != nil {
return err
}
fmt.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
processes, count, err := ps.List(c, appID, c.ResponseLimit)
if err != nil {
return err
}
printProcesses(appID, processes, count)
return nil
}
func printProcesses(appID string, processes []api.Process, count int) {
psMap := ps.ByType(processes)
fmt.Printf("=== %s Processes%s", appID, limitCount(len(processes), count))
for psType, procs := range psMap {
fmt.Printf("--- %s:\n", psType)
for _, proc := range procs {
fmt.Printf("%s.%d %s (%s)\n", proc.Type, proc.Num, proc.State, proc.Release)
}
}
}