Skip to content

Commit 1c06300

Browse files
committed
feat(apps): change drycc run to async
1 parent da59896 commit 1c06300

9 files changed

Lines changed: 110 additions & 21 deletions

File tree

cmd/apps.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,9 @@ func (d *DryccCmd) AppRun(appID, command string, volumeVars []string) error {
250250
return err
251251
}
252252

253-
out, err := apps.Run(s.Client, appID, command, volumeMap)
254-
if d.checkAPICompatibility(s.Client, err) != nil {
253+
if err := apps.Run(s.Client, appID, command, volumeMap); d.checkAPICompatibility(s.Client, err) != nil {
255254
return err
256255
}
257-
258-
if out.ReturnCode == 0 {
259-
d.Print(out.Output)
260-
} else {
261-
d.PrintErr(out.Output)
262-
}
263-
264-
os.Exit(out.ReturnCode)
265256
return nil
266257
}
267258

cmd/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type Commander interface {
7878
PermCreate(string, string, bool) error
7979
PermDelete(string, string, bool) error
8080
PsList(string, int) error
81+
PsLogs(string, string, int, bool, string) error
8182
PsExec(string, string, bool, bool, []string) error
8283
PsScale(string, []string) error
8384
PsRestart(string, string) error

cmd/ps.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import (
55
"fmt"
66
"io"
77
"log"
8+
"os"
89
"regexp"
910
"strconv"
11+
"strings"
1012
"time"
1113

1214
"github.com/containerd/console"
1315
"github.com/drycc/controller-sdk-go/api"
1416
"github.com/drycc/controller-sdk-go/ps"
17+
"github.com/drycc/workflow-cli/pkg/logging"
1518
"golang.org/x/net/websocket"
1619
yaml "gopkg.in/yaml.v3"
1720
)
@@ -45,6 +48,37 @@ func (d *DryccCmd) PsList(appID string, results int) error {
4548
return nil
4649
}
4750

51+
// PodLogs returns the logs from an pod.
52+
func (d *DryccCmd) PsLogs(appID, podID string, lines int, follow bool, container string) error {
53+
s, appID, err := load(d.ConfigFile, appID)
54+
55+
if err != nil {
56+
return err
57+
}
58+
request := api.PodLogsRequest{
59+
Lines: lines,
60+
Follow: follow,
61+
Container: container,
62+
}
63+
conn, err := ps.Logs(s.Client, appID, podID, request)
64+
if err != nil {
65+
return err
66+
}
67+
defer conn.Close()
68+
for {
69+
var message string
70+
err := websocket.Message.Receive(conn, &message)
71+
if err != nil {
72+
if err != io.EOF {
73+
log.Printf("error: %v", err)
74+
}
75+
break
76+
}
77+
logging.PrintLog(os.Stdout, strings.TrimRight(string(message), "\n"))
78+
}
79+
return nil
80+
}
81+
4882
// PsList lists an app's processes.
4983
func (d *DryccCmd) PsExec(appID, podID string, tty, stdin bool, command []string) error {
5084
s, appID, err := load(d.ConfigFile, appID)
@@ -173,12 +207,12 @@ func runRecvTask(conn *websocket.Conn, c console.Console, recvChan, sendChan cha
173207
cancel()
174208
break
175209
}
176-
if message, err := parseChannelMessage(data); err != nil {
210+
message, err := parseChannelMessage(data)
211+
if err != nil {
177212
cancel()
178213
break
179-
} else {
180-
recvChan <- message
181214
}
215+
recvChan <- message
182216
}
183217
}()
184218
go func() {
@@ -272,12 +306,10 @@ func parseChannelMessage(data string) (string, error) {
272306
if value, hasKey := data["message"]; hasKey {
273307
if message, ok := value.(string); ok {
274308
return message, nil
275-
} else {
276-
return "", fmt.Errorf("message is not string, type: %T", message)
277309
}
278-
} else {
279-
return "", nil
310+
return "", fmt.Errorf("message is not string, type: %T", message)
280311
}
312+
return "", nil
281313
}
282314
return message, nil
283315
}

cmd/ps_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ func TestPsExec(t *testing.T) {
106106
assert.NoError(t, err)
107107
}
108108

109+
func TestPsLogs(t *testing.T) {
110+
t.Parallel()
111+
cf, server, err := testutil.NewTestServerAndClient()
112+
if err != nil {
113+
t.Fatal(err)
114+
}
115+
defer server.Close()
116+
var b bytes.Buffer
117+
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
118+
server.Mux.Handle(
119+
"/v2/apps/foo/pods/foo-web-111/logs/",
120+
websocket.Handler(func(conn *websocket.Conn) {
121+
conn.WriteClose(100)
122+
}),
123+
)
124+
err = cmdr.PsLogs("foo", "foo-web-111", 300, true, "runner")
125+
assert.NoError(t, err)
126+
}
127+
109128
type psTargetCases struct {
110129
Targets []string
111130
ExpectedError bool

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22
55
require (
66
github.com/containerd/console v1.0.4
77
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
8-
github.com/drycc/controller-sdk-go v0.0.0-20240222015357-c385680d03cd
8+
github.com/drycc/controller-sdk-go v0.0.0-20240301062701-f528b91c1759
99
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f
1010
github.com/olekukonko/tablewriter v0.0.5
1111
github.com/stretchr/testify v1.8.4

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
66
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
7-
github.com/drycc/controller-sdk-go v0.0.0-20240222015357-c385680d03cd h1:e0473hmWPUVROSR51wiIOWb3Jvt5GJb571q8BNAPInw=
8-
github.com/drycc/controller-sdk-go v0.0.0-20240222015357-c385680d03cd/go.mod h1:EAzlvB5kQq4pFP+h+zl0mx2q1GUlHgzdQ92tlYtDQfQ=
7+
github.com/drycc/controller-sdk-go v0.0.0-20240301062701-f528b91c1759 h1:yBhvNVINJ+FSwZupsik4QzJR/DXPiJ8SvUFrZ/89ISA=
8+
github.com/drycc/controller-sdk-go v0.0.0-20240301062701-f528b91c1759/go.mod h1:EAzlvB5kQq4pFP+h+zl0mx2q1GUlHgzdQ92tlYtDQfQ=
99
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f h1:kgjvUQJeAszDoU1Vo4vTTE92KI8Av3JPb6Qn890niXg=
1010
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f/go.mod h1:n+QxGif6ha9CEoxVnlipxb9IdmerybcUSzTEDFkvjiA=
1111
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=

parser/apps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ Options:
187187
if lines <= 0 {
188188
lines = 300
189189
}
190-
follow := args["--follow"].(bool)
190+
follow := safeGetBool(args, "--follow")
191191
timeout := safeGetInt(args, "--timeout")
192192
if timeout <= 0 {
193193
timeout = 300

parser/ps.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func Ps(argv []string, cmdr cmd.Commander) error {
1414
Valid commands for processes:
1515
1616
ps:list list application processes
17+
ps:logs print the logs for a container
1718
ps:exec execute a command in a container
1819
ps:restart restart an application or process type
1920
ps:scale scale processes (e.g. web=4 worker=2)
@@ -24,6 +25,8 @@ Use 'drycc help [command]' to learn more.
2425
switch argv[0] {
2526
case "ps:list":
2627
return psList(argv, cmdr)
28+
case "ps:logs":
29+
return psLogs(argv, cmdr)
2730
case "ps:exec":
2831
return psExec(argv, cmdr)
2932
case "ps:restart":
@@ -65,6 +68,41 @@ Options:
6568
return cmdr.PsList(safeGetString(args, "--app"), 1000)
6669
}
6770

71+
func psLogs(argv []string, cmdr cmd.Commander) error {
72+
usage := `
73+
Print the logs for a container in a pod or specified resource.
74+
75+
Usage: drycc ps:logs <pod> [options]
76+
77+
Options:
78+
-a --app=<app>
79+
the uniquely identifiable name for the application.
80+
-n --lines=<lines>
81+
the number of lines to display.
82+
-f --follow
83+
specify if the logs should be streamed.
84+
-t --container=<container>
85+
print the logs of this container.
86+
`
87+
88+
args, err := docopt.ParseArgs(usage, argv, "")
89+
90+
if err != nil {
91+
return err
92+
}
93+
94+
app := safeGetString(args, "--app")
95+
lines := safeGetInt(args, "--lines")
96+
if lines <= 0 {
97+
lines = 300
98+
}
99+
follow := safeGetBool(args, "--follow")
100+
podID := safeGetString(args, "<pod>")
101+
container := safeGetString(args, "--container")
102+
103+
return cmdr.PsLogs(app, podID, lines, follow, container)
104+
}
105+
68106
func psExec(argv []string, cmdr cmd.Commander) error {
69107
usage := `
70108
Execute a command in a container.

parser/ps_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ func (d FakeDryccCmd) PsList(string, int) error {
1616
return errors.New("ps:list")
1717
}
1818

19+
func (d FakeDryccCmd) PsLogs(string, string, int, bool, string) error {
20+
return errors.New("ps:logs")
21+
}
22+
1923
func (d FakeDryccCmd) PsExec(string, string, bool, bool, []string) error {
2024
return errors.New("ps:exec")
2125
}
@@ -49,6 +53,10 @@ func TestPs(t *testing.T) {
4953
args: []string{"ps:list"},
5054
expected: "",
5155
},
56+
{
57+
args: []string{"ps:logs", "myapp-web-111"},
58+
expected: "",
59+
},
5260
{
5361
args: []string{"ps:restart", "web"},
5462
expected: "",

0 commit comments

Comments
 (0)