Skip to content

Commit 58ee9a9

Browse files
committed
chore(workflow-cli): use new restart api
1 parent 03004b6 commit 58ee9a9

5 files changed

Lines changed: 11 additions & 155 deletions

File tree

cmd/ps.go

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import (
88
"log"
99
"regexp"
1010
"strconv"
11-
"strings"
1211
"time"
1312

1413
"github.com/containerd/console"
15-
drycc "github.com/drycc/controller-sdk-go"
1614
"github.com/drycc/controller-sdk-go/api"
1715
"github.com/drycc/controller-sdk-go/ps"
1816
"github.com/gorilla/websocket"
@@ -99,31 +97,18 @@ func (d *DryccCmd) PsRestart(appID, target string) error {
9997
return err
10098
}
10199

102-
psType, psName := "", ""
103-
if target != "" {
104-
psType, psName = parseType(target, appID)
105-
}
106-
107100
d.Printf("Restarting processes... but first, %s!\n", drinkOfChoice())
108101
startTime := time.Now()
109102
quit := progress(d.WOut)
110103

111-
processes, err := ps.Restart(s.Client, appID, psType, psName)
104+
err = ps.Restart(s.Client, appID, target)
112105
quit <- true
113106
<-quit
114-
if err == drycc.ErrPodNotFound {
115-
return fmt.Errorf("Could not find process type %s in app %s", psType, appID)
116-
} else if d.checkAPICompatibility(s.Client, err) != nil {
107+
if err != nil {
117108
return err
118109
}
119110

120-
if len(processes) == 0 {
121-
d.Println("Could not find any processes to restart")
122-
} else {
123-
d.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
124-
printProcesses(appID, processes, d.WOut)
125-
}
126-
111+
d.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
127112
return nil
128113
}
129114

@@ -212,29 +197,6 @@ func streamExec(conn *websocket.Conn, tty bool) error {
212197
}
213198
}
214199

215-
func parseType(target string, appID string) (string, string) {
216-
var psType, psName string
217-
218-
if strings.Contains(target, "-") {
219-
replaced := strings.Replace(target, appID+"-", "", 1)
220-
parts := strings.Split(replaced, "-")
221-
// the API requires the type, for now
222-
// regex matches against how Deployment pod name is constructed
223-
regex := regexp.MustCompile("[a-z0-9]{8,10}-[a-z0-9]{5}$")
224-
if regex.MatchString(replaced) || len(parts) == 2 {
225-
psType = parts[0]
226-
} else {
227-
psType = parts[1]
228-
}
229-
// process name is the full pod
230-
psName = target
231-
} else {
232-
psType = target
233-
}
234-
235-
return psType, psName
236-
}
237-
238200
func parsePsTargets(targets []string) (map[string]int, error) {
239201
targetMap := make(map[string]int)
240202
regex := regexp.MustCompile(`^([a-z0-9]+(?:-[a-z0-9]+)*)=([0-9]+)$`)

cmd/ps_test.go

Lines changed: 3 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,6 @@ import (
1414
"github.com/stretchr/testify/assert"
1515
)
1616

17-
func TestParseType(t *testing.T) {
18-
t.Parallel()
19-
20-
var input = map[string]string{
21-
// RC pod name
22-
"earthy-underdog": "earthy-underdog-v2-cmd-8yngj",
23-
// Deployment pod name - they are longer due to hash
24-
"nonfat-yearbook": "nonfat-yearbook-cmd-2180299075-7na91",
25-
// newer style of Deployment pod name
26-
"foo-bar": "foo-bar-cmd-57f6c4bb68-7na91",
27-
// same as above but leaving out the app-name from the pod name
28-
"earthy-underdog2": "cmd-8yngj",
29-
"nonfat-yearbook2": "cmd-2180299075-7na91",
30-
"foo-bar2": "cmd-57f6c4bb68-7na91",
31-
// same as above but with app names without hyphens
32-
"earthy": "earthy-v2-cmd-8yngj",
33-
"nonfat": "nonfat-cmd-2180299075-7na91",
34-
"foo": "foo-cmd-57f6c4bb68-7na91",
35-
"earthy2": "cmd-8yngj",
36-
"nonfat2": "cmd-2180299075-7na91",
37-
"foo2": "cmd-57f6c4bb68-7na91",
38-
}
39-
40-
for appID, podName := range input {
41-
psType, psName := parseType(podName, appID)
42-
if psType != "cmd" || psName != podName {
43-
t.Errorf("parseType(%#v, %#v): type was not cmd (got %s) or psName was not %s (got %s)", podName, appID, psType, podName, psName)
44-
}
45-
}
46-
47-
// test type by itself
48-
psType, psName := parseType("cmd", "fake")
49-
if psType != "cmd" || psName != "" {
50-
t.Error("type was not cmd")
51-
}
52-
}
53-
5417
func TestPrintProcesses(t *testing.T) {
5518
var b bytes.Buffer
5619

@@ -247,98 +210,30 @@ func TestPsRestart(t *testing.T) {
247210

248211
server.Mux.HandleFunc("/v2/apps/foo/pods/restart/", func(w http.ResponseWriter, r *http.Request) {
249212
testutil.SetHeaders(w)
250-
fmt.Fprintf(w, `[
251-
{
252-
"release": "v2",
253-
"type": "web",
254-
"name": "foo-web-4084101150-c871y",
255-
"state": "up",
256-
"started": "2016-02-13T00:47:52"
257-
}
258-
]`)
213+
w.WriteHeader(http.StatusNoContent)
259214
})
260215

261216
b.Reset()
262217
err = cmdr.PsRestart("foo", "")
263218
assert.NoError(t, err)
264-
assert.Equal(t, testutil.StripProgress(b.String()), `Restarting processes... but first, coffee!
265-
done in 0s
266-
=== foo Processes
267-
--- web:
268-
foo-web-4084101150-c871y up (v2)
269-
`, "output")
270219

271220
server.Mux.HandleFunc("/v2/apps/coolapp/pods/restart/", func(w http.ResponseWriter, r *http.Request) {
272221
testutil.SetHeaders(w)
273-
fmt.Fprintf(w, `[]`)
222+
w.WriteHeader(http.StatusNoContent)
274223
})
275224

276225
b.Reset()
277226

278227
err = cmdr.PsRestart("coolapp", "")
279228
assert.NoError(t, err)
280-
assert.Equal(t, testutil.StripProgress(b.String()), `Restarting processes... but first, coffee!
281-
Could not find any processes to restart
282-
`, "output")
283229

284230
server.Mux.HandleFunc("/v2/apps/testapp/pods/web/restart/", func(w http.ResponseWriter, r *http.Request) {
285231
testutil.SetHeaders(w)
286-
fmt.Fprintf(w, `[
287-
{
288-
"release": "v2",
289-
"type": "web",
290-
"name": "testapp-web-4084101150-c871y",
291-
"state": "up",
292-
"started": "2016-02-13T00:47:52"
293-
}
294-
]`)
232+
w.WriteHeader(http.StatusNoContent)
295233
})
296234

297235
b.Reset()
298236

299237
err = cmdr.PsRestart("testapp", "web")
300238
assert.NoError(t, err)
301-
assert.Equal(t, testutil.StripProgress(b.String()), `Restarting processes... but first, coffee!
302-
done in 0s
303-
=== testapp Processes
304-
--- web:
305-
testapp-web-4084101150-c871y up (v2)
306-
`, "output")
307-
308-
server.Mux.HandleFunc("/v2/apps/newapp/pods/web/newapp-web-4084101150-c871y/restart/", func(w http.ResponseWriter, r *http.Request) {
309-
testutil.SetHeaders(w)
310-
fmt.Fprintf(w, `[
311-
{
312-
"release": "v2",
313-
"type": "web",
314-
"name": "newapp-web-4084101150-c871y",
315-
"state": "up",
316-
"started": "2016-02-13T00:47:52"
317-
}
318-
]`)
319-
})
320-
321-
b.Reset()
322-
323-
err = cmdr.PsRestart("newapp", "newapp-web-4084101150-c871y")
324-
assert.NoError(t, err)
325-
assert.Equal(t, testutil.StripProgress(b.String()), `Restarting processes... but first, coffee!
326-
done in 0s
327-
=== newapp Processes
328-
--- web:
329-
newapp-web-4084101150-c871y up (v2)
330-
`, "output")
331-
332-
server.Mux.HandleFunc("/v2/apps/newapp/pods/ghost/restart/", func(w http.ResponseWriter, r *http.Request) {
333-
testutil.SetHeaders(w)
334-
w.WriteHeader(http.StatusBadRequest)
335-
fmt.Fprintf(w, `{
336-
"detail": "Container type ghost does not exist in application"
337-
}`)
338-
})
339-
340-
b.Reset()
341-
342-
err = cmdr.PsRestart("newapp", "ghost")
343-
assert.Equal(t, err.Error(), "Could not find process type ghost in app newapp", "error")
344239
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.19
55
require (
66
github.com/containerd/console v1.0.3
77
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
8-
github.com/drycc/controller-sdk-go v0.0.0-20220830032209-a334918d2242
8+
github.com/drycc/controller-sdk-go v0.0.0-20221102055456-444409b1af71
99
github.com/drycc/pkg v0.0.0-20220830031116-26c11ff8667c
1010
github.com/gorilla/websocket v1.5.0
1111
github.com/olekukonko/tablewriter v0.0.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
99
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1010
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
1111
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
12-
github.com/drycc/controller-sdk-go v0.0.0-20220830032209-a334918d2242 h1:UduAyToNW6QxHwDd7DrJNpjO0yl0vfFvSZxXl9O7A8M=
13-
github.com/drycc/controller-sdk-go v0.0.0-20220830032209-a334918d2242/go.mod h1:mTPPOfS1LzvsGpLuNpgz4wt9CGloCF12MtTYjQa5i8o=
12+
github.com/drycc/controller-sdk-go v0.0.0-20221102055456-444409b1af71 h1:VI6mc75e4FdUjoLPNdDxNZNtmKH7EvXovTnmsQGCVZU=
13+
github.com/drycc/controller-sdk-go v0.0.0-20221102055456-444409b1af71/go.mod h1:mTPPOfS1LzvsGpLuNpgz4wt9CGloCF12MtTYjQa5i8o=
1414
github.com/drycc/pkg v0.0.0-20220830031116-26c11ff8667c h1:M4veAIz8VfKEMpyegbNMvAv7McrXqW4QwAoC813lG0w=
1515
github.com/drycc/pkg v0.0.0-20220830031116-26c11ff8667c/go.mod h1:W2lIi2Micx/4vPIlfzIsQ7RDZXxNmKGpGcfiWkw8Qls=
1616
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=

parser/ps.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Valid commands for processes:
1515
1616
ps:list list application processes
1717
ps:exec execute a command in a container
18-
ps:restart restart an application or its process types
18+
ps:restart restart an application or process type
1919
ps:scale scale processes (e.g. web=4 worker=2)
2020
2121
Use 'drycc help [command]' to learn more.
@@ -95,14 +95,13 @@ Options:
9595

9696
func psRestart(argv []string, cmdr cmd.Commander) error {
9797
usage := `
98-
Restart an application, a process type or a specific process.
98+
Restart an application or process type.
9999
100100
Usage: drycc ps:restart [<type>] [options]
101101
102102
Arguments:
103103
<type>
104104
the process name as defined in your Procfile, such as 'web' or 'worker'.
105-
To restart a particular process, use 'web-asdfg' or 'app-v2-web-asdfg'.
106105
107106
Options:
108107
-a --app=<app>

0 commit comments

Comments
 (0)