Add reconnect for attach events
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
fe38efda50
commit
6808dbc02f
1 changed files with 34 additions and 28 deletions
|
@ -4,6 +4,8 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -18,10 +20,13 @@ import (
|
||||||
"github.com/opencontainers/specs"
|
"github.com/opencontainers/specs"
|
||||||
netcontext "golang.org/x/net/context"
|
netcontext "golang.org/x/net/context"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/grpclog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: parse flags and pass opts
|
// TODO: parse flags and pass opts
|
||||||
func getClient(ctx *cli.Context) types.APIClient {
|
func getClient(ctx *cli.Context) types.APIClient {
|
||||||
|
// reset the logger for grpc to log to dev/null so that it does not mess with our stdio
|
||||||
|
grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags))
|
||||||
dialOpts := []grpc.DialOption{grpc.WithInsecure()}
|
dialOpts := []grpc.DialOption{grpc.WithInsecure()}
|
||||||
dialOpts = append(dialOpts,
|
dialOpts = append(dialOpts,
|
||||||
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
||||||
|
@ -99,10 +104,6 @@ var attachCommand = cli.Command{
|
||||||
fatal("container id cannot be empty", 1)
|
fatal("container id cannot be empty", 1)
|
||||||
}
|
}
|
||||||
c := getClient(context)
|
c := getClient(context)
|
||||||
events, err := c.Events(netcontext.Background(), &types.EventsRequest{})
|
|
||||||
if err != nil {
|
|
||||||
fatal(err.Error(), 1)
|
|
||||||
}
|
|
||||||
type bundleState struct {
|
type bundleState struct {
|
||||||
Bundle string `json:"bundle"`
|
Bundle string `json:"bundle"`
|
||||||
}
|
}
|
||||||
|
@ -134,22 +135,19 @@ var attachCommand = cli.Command{
|
||||||
); err != nil {
|
); err != nil {
|
||||||
fatal(err.Error(), 1)
|
fatal(err.Error(), 1)
|
||||||
}
|
}
|
||||||
go func() {
|
closer := func() {
|
||||||
io.Copy(stdin, os.Stdin)
|
|
||||||
if state != nil {
|
if state != nil {
|
||||||
term.RestoreTerminal(os.Stdin.Fd(), state)
|
term.RestoreTerminal(os.Stdin.Fd(), state)
|
||||||
}
|
}
|
||||||
stdin.Close()
|
stdin.Close()
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
io.Copy(stdin, os.Stdin)
|
||||||
|
closer()
|
||||||
}()
|
}()
|
||||||
for {
|
if err := waitForExit(c, id, closer); err != nil {
|
||||||
e, err := events.Recv()
|
|
||||||
if err != nil {
|
|
||||||
fatal(err.Error(), 1)
|
fatal(err.Error(), 1)
|
||||||
}
|
}
|
||||||
if e.Id == id && e.Type == "exit" && e.Pid == pid {
|
|
||||||
os.Exit(int(e.Status))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,10 +181,6 @@ var startCommand = cli.Command{
|
||||||
fatal(fmt.Sprintf("cannot get the absolute path of the bundle: %v", err), 1)
|
fatal(fmt.Sprintf("cannot get the absolute path of the bundle: %v", err), 1)
|
||||||
}
|
}
|
||||||
c := getClient(context)
|
c := getClient(context)
|
||||||
events, err := c.Events(netcontext.Background(), &types.EventsRequest{})
|
|
||||||
if err != nil {
|
|
||||||
fatal(err.Error(), 1)
|
|
||||||
}
|
|
||||||
r := &types.CreateContainerRequest{
|
r := &types.CreateContainerRequest{
|
||||||
Id: id,
|
Id: id,
|
||||||
BundlePath: bpath,
|
BundlePath: bpath,
|
||||||
|
@ -223,17 +217,9 @@ var startCommand = cli.Command{
|
||||||
io.Copy(stdin, os.Stdin)
|
io.Copy(stdin, os.Stdin)
|
||||||
restoreAndCloseStdin()
|
restoreAndCloseStdin()
|
||||||
}()
|
}()
|
||||||
for {
|
if err := waitForExit(c, id, restoreAndCloseStdin); err != nil {
|
||||||
e, err := events.Recv()
|
|
||||||
if err != nil {
|
|
||||||
restoreAndCloseStdin()
|
|
||||||
fatal(err.Error(), 1)
|
fatal(err.Error(), 1)
|
||||||
}
|
}
|
||||||
if e.Id == id && e.Type == "exit" && e.Pid == "init" {
|
|
||||||
restoreAndCloseStdin()
|
|
||||||
os.Exit(int(e.Status))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -420,3 +406,23 @@ var statsCommand = cli.Command{
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func waitForExit(c types.APIClient, id string, closer func()) error {
|
||||||
|
events, err := c.Events(netcontext.Background(), &types.EventsRequest{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
e, err := events.Recv()
|
||||||
|
if err != nil {
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
events, _ = c.Events(netcontext.Background(), &types.EventsRequest{})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if e.Id == id && e.Type == "exit" && e.Pid == "init" {
|
||||||
|
closer()
|
||||||
|
os.Exit(int(e.Status))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue