Skip to content

Commit 275b094

Browse files
committed
handle container sattus
1 parent 63ea4f9 commit 275b094

File tree

2 files changed

+44
-60
lines changed

2 files changed

+44
-60
lines changed

internal/command/cli.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ import (
88
"strings"
99
"time"
1010

11-
dockerImage "github.com/docker/docker/api/types/image"
12-
1311
"github.com/docker/docker/api/types"
1412
"github.com/docker/docker/api/types/container"
13+
"github.com/docker/docker/api/types/events"
1514
"github.com/docker/docker/api/types/filters"
15+
dockerImage "github.com/docker/docker/api/types/image"
1616
"github.com/docker/docker/api/types/mount"
1717
"github.com/docker/docker/api/types/strslice"
18+
"github.com/docker/docker/client"
1819
"github.com/docker/docker/pkg/jsonmessage"
1920
"github.com/moby/term"
2021
"github.com/pkg/errors"
2122
"github.com/sirupsen/logrus"
23+
2224
"github.com/zeromake/docker-debug/internal/config"
2325
"github.com/zeromake/docker-debug/pkg/opts"
24-
"github.com/zeromake/docker-debug/pkg/tty"
25-
26-
"github.com/docker/docker/client"
2726
"github.com/zeromake/docker-debug/pkg/stream"
27+
"github.com/zeromake/docker-debug/pkg/tty"
2828
)
2929

3030
const (
@@ -446,6 +446,32 @@ func (cli *DebugCli) ExecStart(options execOptions, execID string) error {
446446
return getExecExitStatus(cli.ctx, cli.client, execID)
447447
}
448448

449+
// WatchContainer watch container
450+
func (cli *DebugCli) WatchContainer(ctx context.Context, containerID string) error {
451+
subCtx, cancel := context.WithCancel(ctx)
452+
defer cancel()
453+
454+
filterArgs := filters.NewArgs()
455+
filterArgs.Add("container", containerID)
456+
messages, errs := cli.client.Events(subCtx, events.ListOptions{
457+
Filters: filterArgs,
458+
})
459+
460+
for {
461+
select {
462+
case event := <-messages:
463+
if event.Type == events.ContainerEventType {
464+
switch event.Action {
465+
case events.ActionDestroy, events.ActionDie, events.ActionKill, events.ActionStop:
466+
return nil
467+
}
468+
}
469+
case err := <-errs:
470+
return err
471+
}
472+
}
473+
}
474+
449475
func getExecExitStatus(ctx context.Context, apiClient client.ContainerAPIClient, execID string) error {
450476
resp, err := apiClient.ContainerExecInspect(ctx, execID)
451477
if err != nil {

internal/command/root.go

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ package command
22

33
import (
44
"context"
5-
"fmt"
6-
"os"
7-
"os/signal"
8-
"syscall"
95

106
"github.com/pkg/errors"
117
"github.com/sirupsen/logrus"
128
"github.com/spf13/cobra"
9+
1310
"github.com/zeromake/docker-debug/internal/config"
14-
"github.com/zeromake/docker-debug/pkg/tty"
1511
)
1612

1713
var rootCmd = newExecCommand()
@@ -114,63 +110,33 @@ func runExec(options execOptions) error {
114110
var ctx, cancel = context.WithCancel(context.Background())
115111
defer cancel()
116112
logrus.SetLevel(logrus.ErrorLevel)
117-
var containerID string
113+
118114
cli, err := buildCli(ctx, options)
119115
if err != nil {
120116
return err
121117
}
122-
go func() {
123-
c := make(chan os.Signal, 1)
124-
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
125-
<-c
126-
cancel()
127-
}()
118+
defer cli.Close()
119+
128120
conf := cli.Config()
129-
defer func() {
130-
cc := cli.ctx
131-
if err != nil {
132-
logrus.Errorf("Error: %s", err.Error())
133-
if errors.Is(err, context.Canceled) {
134-
cc = context.Background()
135-
}
136-
}
137-
if containerID != "" {
138-
err = cli.ContainerClean(cc, containerID)
139-
if err != nil {
140-
logrus.Debugf("%+v", err)
141-
}
142-
}
143-
err = cli.Close()
144-
if err != nil {
145-
logrus.Debugf("%+v", err)
146-
}
147-
}()
148-
_, err = cli.Ping()
149-
if err != nil {
150-
return err
151-
}
152121
// find image
153122
images, err := cli.FindImage(conf.Image)
154123
if err != nil {
155124
return err
156125
}
157-
158126
if len(images) == 0 {
159127
// pull image
160128
err = cli.PullImage(conf.Image)
161129
if err != nil {
162130
return err
163131
}
164132
}
165-
var originContainerID string
166-
originContainerID, err = cli.FindContainer(options.container)
167-
if err != nil {
168-
return err
169-
}
170-
containerID, err = cli.CreateContainer(originContainerID, options)
133+
134+
containerID, err := cli.CreateContainer(options.container, options)
171135
if err != nil {
172136
return err
173137
}
138+
defer cli.ContainerClean(ctx, containerID)
139+
174140
resp, err := cli.ExecCreate(options, containerID)
175141
if err != nil {
176142
return err
@@ -180,21 +146,13 @@ func runExec(options execOptions) error {
180146
defer close(errCh)
181147

182148
go func() {
183-
errCh <- func() error {
184-
return cli.ExecStart(options, resp.ID)
185-
}()
149+
errCh <- cli.ExecStart(options, resp.ID)
150+
}()
151+
go func() {
152+
errCh <- cli.WatchContainer(ctx, options.container)
186153
}()
187-
if cli.In().IsTerminal() {
188-
if err = tty.MonitorTtySize(ctx, cli.Client(), cli.Out(), resp.ID, true); err != nil {
189-
_, _ = fmt.Fprintln(cli.Err(), "Error monitoring TTY size:", err)
190-
}
191-
}
192154

193-
if err = <-errCh; err != nil {
194-
logrus.Debugf("Error hijack: %s", err)
195-
return err
196-
}
197-
return nil
155+
return <-errCh
198156
}
199157

200158
// Execute main func

0 commit comments

Comments
 (0)