Skip to content

Commit 10fd56f

Browse files
author
lijianguo
committed
feat(ps):add ps:stop/start command
1 parent ac710f5 commit 10fd56f

9 files changed

Lines changed: 172 additions & 28 deletions

File tree

cmd/apps_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ func TestAppsInfo(t *testing.T) {
141141
"started": "2016-08-22T17:42:16Z",
142142
"name": "lorem-ipsum-cmd-1911796442-48b58",
143143
"release": "v2",
144-
"type": "cmd"
144+
"type": "cmd",
145+
"replicas": 1
145146
}
146147
]
147148
}`)
@@ -204,7 +205,7 @@ owner: dolar-sit-amet
204205
id: lorem-ipsum
205206
206207
=== lorem-ipsum Processes
207-
--- cmd:
208+
--- cmd (started): 1
208209
lorem-ipsum-cmd-1911796442-48b58 up (v2)
209210
210211
=== lorem-ipsum Domains

cmd/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ type Commander interface {
7070
PsList(string, int) error
7171
PsScale(string, []string) error
7272
PsRestart(string, string) error
73+
PsStop(string, []string) error
74+
PsStart(string, []string) error
7375
RegistryList(string) error
7476
RegistrySet(string, []string) error
7577
RegistryUnset(string, []string) error

cmd/ps.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,65 @@ func (d *DryccCmd) PsScale(appID string, targets []string) error {
6868
return nil
6969
}
7070

71+
// PsStop stop an app's processes.
72+
func (d *DryccCmd) PsStop(appID string, targets []string) error {
73+
s, appID, err := load(d.ConfigFile, appID)
74+
if err != nil {
75+
return err
76+
}
77+
78+
d.Printf("Stopping processes... but first, %s!\n", drinkOfChoice())
79+
startTime := time.Now()
80+
quit := progress(d.WOut)
81+
tps := map[string][]string{"types": targets}
82+
err = ps.Stop(s.Client, appID, tps)
83+
quit <- true
84+
<-quit
85+
if d.checkAPICompatibility(s.Client, err) != nil {
86+
return err
87+
}
88+
89+
d.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
90+
91+
processes, _, err := ps.List(s.Client, appID, s.Limit)
92+
if err != nil {
93+
return err
94+
}
95+
96+
printProcesses(appID, processes, d.WOut)
97+
return nil
98+
}
99+
100+
// PsStart start an app's processes.
101+
func (d *DryccCmd) PsStart(appID string, targets []string) error {
102+
s, appID, err := load(d.ConfigFile, appID)
103+
if err != nil {
104+
return err
105+
}
106+
107+
d.Printf("Starting processes... but first, %s!\n", drinkOfChoice())
108+
startTime := time.Now()
109+
quit := progress(d.WOut)
110+
111+
tps := map[string][]string{"types": targets}
112+
err = ps.Start(s.Client, appID, tps)
113+
quit <- true
114+
<-quit
115+
if d.checkAPICompatibility(s.Client, err) != nil {
116+
return err
117+
}
118+
119+
d.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
120+
121+
processes, _, err := ps.List(s.Client, appID, s.Limit)
122+
if err != nil {
123+
return err
124+
}
125+
126+
printProcesses(appID, processes, d.WOut)
127+
return nil
128+
}
129+
71130
// PsRestart restarts an app's processes.
72131
func (d *DryccCmd) PsRestart(appID, target string) error {
73132
s, appID, err := load(d.ConfigFile, appID)
@@ -109,7 +168,7 @@ func printProcesses(appID string, input []api.Pods, wOut io.Writer) {
109168
fmt.Fprintf(wOut, "=== %s Processes\n", appID)
110169

111170
for _, process := range processes {
112-
fmt.Fprintf(wOut, "--- %s:\n", process.Type)
171+
fmt.Fprintf(wOut, "--- %s (%s): %d\n", process.Type, process.Status, process.Replicas)
113172

114173
for _, pod := range process.PodsList {
115174
fmt.Fprintf(wOut, "%s %s (%s)\n", pod.Name, pod.State, pod.Release)

cmd/ps_test.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,29 @@ func TestPrintProcesses(t *testing.T) {
5454

5555
pods := []api.Pods{
5656
{
57-
Release: "v3",
58-
Name: "benign-quilting-web-4084101150-c871y",
59-
Type: "web",
60-
State: "up",
61-
Started: time.Time{},
57+
Release: "v3",
58+
Name: "benign-quilting-web-4084101150-c871y",
59+
Type: "web",
60+
State: "up",
61+
Started: time.Time{},
62+
Replicas: 1,
6263
},
6364
{
64-
Release: "v3",
65-
Name: "benign-quilting-worker-4084101150-c871y",
66-
Type: "worker",
67-
State: "up",
68-
Started: time.Time{},
65+
Release: "v3",
66+
Name: "benign-quilting-worker-4084101150-c871y",
67+
Type: "worker",
68+
State: "up",
69+
Started: time.Time{},
70+
Replicas: 1,
6971
},
7072
}
7173

7274
printProcesses("appname", pods, &b)
7375

7476
assert.Equal(t, b.String(), `=== appname Processes
75-
--- web:
77+
--- web (started): 1
7678
benign-quilting-web-4084101150-c871y up (v3)
77-
--- worker:
79+
--- worker (started): 1
7880
benign-quilting-worker-4084101150-c871y up (v3)
7981
`, "output")
8082
}
@@ -101,7 +103,8 @@ func TestPsList(t *testing.T) {
101103
"type": "web",
102104
"name": "foo-web-4084101150-c871y",
103105
"state": "up",
104-
"started": "2016-02-13T00:47:52"
106+
"started": "2016-02-13T00:47:52",
107+
"replicas": 1
105108
}
106109
]
107110
}`)
@@ -111,7 +114,7 @@ func TestPsList(t *testing.T) {
111114
assert.NoErr(t, err)
112115

113116
assert.Equal(t, b.String(), `=== foo Processes
114-
--- web:
117+
--- web (started): 1
115118
foo-web-4084101150-c871y up (v2)
116119
`, "output")
117120
}
@@ -171,7 +174,8 @@ func TestPsScale(t *testing.T) {
171174
"type": "web",
172175
"name": "foo-web-4084101150-c871y",
173176
"state": "up",
174-
"started": "2016-02-13T00:47:52"
177+
"started": "2016-02-13T00:47:52",
178+
"replicas": 1
175179
}
176180
]
177181
}`)
@@ -189,7 +193,7 @@ func TestPsScale(t *testing.T) {
189193
assert.Equal(t, testutil.StripProgress(b.String()), `Scaling processes... but first, coffee!
190194
done in 0s
191195
=== foo Processes
192-
--- web:
196+
--- web (started): 1
193197
foo-web-4084101150-c871y up (v2)
194198
`, "output")
195199
}
@@ -212,7 +216,8 @@ func TestPsRestart(t *testing.T) {
212216
"type": "web",
213217
"name": "foo-web-4084101150-c871y",
214218
"state": "up",
215-
"started": "2016-02-13T00:47:52"
219+
"started": "2016-02-13T00:47:52",
220+
"replicas": 1
216221
}
217222
]`)
218223
})
@@ -223,7 +228,7 @@ func TestPsRestart(t *testing.T) {
223228
assert.Equal(t, testutil.StripProgress(b.String()), `Restarting processes... but first, coffee!
224229
done in 0s
225230
=== foo Processes
226-
--- web:
231+
--- web (started): 1
227232
foo-web-4084101150-c871y up (v2)
228233
`, "output")
229234

@@ -248,7 +253,8 @@ Could not find any processes to restart
248253
"type": "web",
249254
"name": "testapp-web-4084101150-c871y",
250255
"state": "up",
251-
"started": "2016-02-13T00:47:52"
256+
"started": "2016-02-13T00:47:52",
257+
"replicas": 1
252258
}
253259
]`)
254260
})
@@ -260,7 +266,7 @@ Could not find any processes to restart
260266
assert.Equal(t, testutil.StripProgress(b.String()), `Restarting processes... but first, coffee!
261267
done in 0s
262268
=== testapp Processes
263-
--- web:
269+
--- web (started): 1
264270
testapp-web-4084101150-c871y up (v2)
265271
`, "output")
266272

@@ -272,7 +278,8 @@ testapp-web-4084101150-c871y up (v2)
272278
"type": "web",
273279
"name": "newapp-web-4084101150-c871y",
274280
"state": "up",
275-
"started": "2016-02-13T00:47:52"
281+
"started": "2016-02-13T00:47:52",
282+
"replicas": 1
276283
}
277284
]`)
278285
})
@@ -284,7 +291,7 @@ testapp-web-4084101150-c871y up (v2)
284291
assert.Equal(t, testutil.StripProgress(b.String()), `Restarting processes... but first, coffee!
285292
done in 0s
286293
=== newapp Processes
287-
--- web:
294+
--- web (started): 1
288295
newapp-web-4084101150-c871y up (v2)
289296
`, "output")
290297

cmd/resources.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ func maxNum(tempArray ...int) int {
269269
// parseParams transfer params to map
270270
func parseParams(params []string) (map[string]interface{}, error) {
271271
paramsMap := make(map[string]interface{})
272-
273-
regex := regexp.MustCompile(`^([A-z_]+[A-z0-9_]*)=([\s\S]*)$`)
272+
regex := regexp.MustCompile(`^([A-z_]+[A-z0-9_]*)((\.){1}([A-z0-9_]+))*=([\s\S]*)$`)
274273
for _, param := range params {
275274
if regex.MatchString(param) {
276275
captures := regex.FindStringSubmatch(param)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.13
55
require (
66
github.com/arschles/assert v1.0.1-0.20191213221312-71f210f9375a
77
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
8-
github.com/drycc/controller-sdk-go v0.0.0-20201013084049-82230b1dc54f
8+
github.com/drycc/controller-sdk-go v0.0.0-20201015015256-370f01036885
99
github.com/drycc/pkg v0.0.0-20200811173146-1f2b2781a852
1010
github.com/olekukonko/tablewriter v0.0.4
1111
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ github.com/drycc/controller-sdk-go v0.0.0-20200914062048-dcf027e6dd1a h1:sQEQ8KU
4242
github.com/drycc/controller-sdk-go v0.0.0-20200914062048-dcf027e6dd1a/go.mod h1:mF8/kZ/E4oDHCtfUVS790Frm8oVzZkoCGCKYT2eWQro=
4343
github.com/drycc/controller-sdk-go v0.0.0-20201013084049-82230b1dc54f h1:Lwm3ijm8W+STLHOOmumLSQRvCkUTuya+XkGM+VNFfGA=
4444
github.com/drycc/controller-sdk-go v0.0.0-20201013084049-82230b1dc54f/go.mod h1:mF8/kZ/E4oDHCtfUVS790Frm8oVzZkoCGCKYT2eWQro=
45+
github.com/drycc/controller-sdk-go v0.0.0-20201015015256-370f01036885 h1:lA6ERNPzsqPeEcoPX5s/G7eMu7NH6cJh8ZyOtXAcVxM=
46+
github.com/drycc/controller-sdk-go v0.0.0-20201015015256-370f01036885/go.mod h1:mF8/kZ/E4oDHCtfUVS790Frm8oVzZkoCGCKYT2eWQro=
4547
github.com/drycc/pkg v0.0.0-20190129033019-bcdffff0fcb3 h1:QRAbL97/hJecXQzaSONMCiwt1JjBKAAMKK0qJW5sgIM=
4648
github.com/drycc/pkg v0.0.0-20190129033019-bcdffff0fcb3/go.mod h1:zNgTT6kuGZOKMIdSrjskIw5fctdbbszqiTPEt3ffxHw=
4749
github.com/drycc/pkg v0.0.0-20200811173146-1f2b2781a852 h1:OsWH7jrv2i32rkvHFuj3yVpGMR+A6heRge9tm01XR/U=

parser/ps.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Valid commands for processes:
1313
ps:list list application processes
1414
ps:restart restart an application or its process types
1515
ps:scale scale processes (e.g. web=4 worker=2)
16+
ps:stop stop processes (e.g. web worker)
17+
ps:start start processes (e.g. web worker)
1618
1719
Use 'drycc help [command]' to learn more.
1820
`
@@ -24,6 +26,10 @@ Use 'drycc help [command]' to learn more.
2426
return psRestart(argv, cmdr)
2527
case "ps:scale":
2628
return psScale(argv, cmdr)
29+
case "ps:stop":
30+
return psStop(argv, cmdr)
31+
case "ps:start":
32+
return psStart(argv, cmdr)
2733
default:
2834
if printHelp(argv, usage) {
2935
return nil
@@ -113,3 +119,55 @@ Options:
113119
apps := safeGetValue(args, "--app")
114120
return cmdr.PsScale(apps, args["<type>=<num>"].([]string))
115121
}
122+
123+
func psStop(argv []string, cmdr cmd.Commander) error {
124+
usage := `
125+
Stop an application's processes by type.
126+
127+
Usage: drycc ps:stop <type>... [options]
128+
129+
Arguments:
130+
<type>
131+
the process name as defined in your Procfile, such as 'web' or 'worker'.
132+
Note that Dockerfile apps have a default 'cmd' process type.
133+
134+
Options:
135+
-a --app=<app>
136+
the uniquely identifiable name for the application.
137+
`
138+
139+
args, err := docopt.Parse(usage, argv, true, "", false, true)
140+
141+
if err != nil {
142+
return err
143+
}
144+
145+
apps := safeGetValue(args, "--app")
146+
return cmdr.PsStop(apps, args["<type>"].([]string))
147+
}
148+
149+
func psStart(argv []string, cmdr cmd.Commander) error {
150+
usage := `
151+
Start an application's processes by type.
152+
153+
Usage: drycc ps:start <type>... [options]
154+
155+
Arguments:
156+
<type>
157+
the process name as defined in your Procfile, such as 'web' or 'worker'.
158+
Note that Dockerfile apps have a default 'cmd' process type.
159+
160+
Options:
161+
-a --app=<app>
162+
the uniquely identifiable name for the application.
163+
`
164+
165+
args, err := docopt.Parse(usage, argv, true, "", false, true)
166+
167+
if err != nil {
168+
return err
169+
}
170+
171+
apps := safeGetValue(args, "--app")
172+
return cmdr.PsStart(apps, args["<type>"].([]string))
173+
}

parser/ps_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ func (d FakeDryccCmd) PsRestart(string, string) error {
2424
return errors.New("ps:restart")
2525
}
2626

27+
func (d FakeDryccCmd) PsStop(string, []string) error {
28+
return errors.New("ps:stop")
29+
}
30+
31+
func (d FakeDryccCmd) PsStart(string, []string) error {
32+
return errors.New("ps:start")
33+
}
34+
2735
func TestPs(t *testing.T) {
2836
t.Parallel()
2937

@@ -61,6 +69,14 @@ func TestPs(t *testing.T) {
6169
args: []string{"ps"},
6270
expected: "ps:list",
6371
},
72+
{
73+
args: []string{"ps:stop", "web"},
74+
expected: "",
75+
},
76+
{
77+
args: []string{"ps:start", "web"},
78+
expected: "",
79+
},
6480
}
6581

6682
// For each case, check that calling the route with the arguments

0 commit comments

Comments
 (0)