supervisor: implement monitoring
Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
parent
31f26fed18
commit
78d7e8b256
13 changed files with 1311 additions and 276 deletions
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -8,10 +9,12 @@ import (
|
|||
|
||||
gocontext "context"
|
||||
|
||||
"github.com/crosbymichael/console"
|
||||
"github.com/docker/containerd/api/execution"
|
||||
execEvents "github.com/docker/containerd/execution"
|
||||
"github.com/docker/docker/pkg/term"
|
||||
"github.com/nats-io/go-nats"
|
||||
"github.com/nats-io/go-nats-streaming"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
|
@ -39,20 +42,23 @@ var runCommand = cli.Command{
|
|||
}
|
||||
|
||||
// setup our event subscriber
|
||||
nc, err := nats.Connect(nats.DefaultURL)
|
||||
sc, err := stan.Connect("containerd", "ctr", stan.ConnectWait(5*time.Second))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nec, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
|
||||
if err != nil {
|
||||
nc.Close()
|
||||
return err
|
||||
}
|
||||
defer nec.Close()
|
||||
defer sc.Close()
|
||||
|
||||
evCh := make(chan *execEvents.ContainerEvent, 64)
|
||||
sub, err := nec.Subscribe(execEvents.ContainersEventsSubjectSubscriber, func(e *execEvents.ContainerEvent) {
|
||||
evCh <- e
|
||||
sub, err := sc.Subscribe(fmt.Sprintf("containers.%s", id), func(m *stan.Msg) {
|
||||
var e execEvents.ContainerEvent
|
||||
|
||||
err := json.Unmarshal(m.Data, &e)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to unmarshal event: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
evCh <- &e
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -78,19 +84,12 @@ var runCommand = cli.Command{
|
|||
Stderr: filepath.Join(tmpDir, "stderr"),
|
||||
}
|
||||
|
||||
var oldState *term.State
|
||||
restoreTerm := func() {
|
||||
if oldState != nil {
|
||||
term.RestoreTerminal(os.Stdin.Fd(), oldState)
|
||||
}
|
||||
}
|
||||
|
||||
if crOpts.Console {
|
||||
oldState, err = term.SetRawTerminal(os.Stdin.Fd())
|
||||
if err != nil {
|
||||
con := console.Current()
|
||||
defer con.Reset()
|
||||
if err := con.SetRaw(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer restoreTerm()
|
||||
}
|
||||
|
||||
fwg, err := prepareStdio(crOpts.Stdin, crOpts.Stdout, crOpts.Stderr, crOpts.Console)
|
||||
|
@ -100,13 +99,13 @@ var runCommand = cli.Command{
|
|||
|
||||
cr, err := executionService.CreateContainer(gocontext.Background(), crOpts)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Wrap(err, "CreateContainer RPC failed")
|
||||
}
|
||||
|
||||
if _, err := executionService.StartContainer(gocontext.Background(), &execution.StartContainerRequest{
|
||||
ID: cr.Container.ID,
|
||||
}); err != nil {
|
||||
return err
|
||||
return errors.Wrap(err, "StartContainer RPC failed")
|
||||
}
|
||||
|
||||
var ec uint32
|
||||
|
@ -123,7 +122,7 @@ var runCommand = cli.Command{
|
|||
break eventLoop
|
||||
}
|
||||
case <-time.After(1 * time.Second):
|
||||
if nec.Conn.Status() != nats.CONNECTED {
|
||||
if sc.NatsConn().Status() != nats.CONNECTED {
|
||||
break eventLoop
|
||||
}
|
||||
}
|
||||
|
@ -132,14 +131,15 @@ var runCommand = cli.Command{
|
|||
if _, err := executionService.DeleteContainer(gocontext.Background(), &execution.DeleteContainerRequest{
|
||||
ID: cr.Container.ID,
|
||||
}); err != nil {
|
||||
return err
|
||||
return errors.Wrap(err, "DeleteContainer RPC failed")
|
||||
}
|
||||
|
||||
// Ensure we read all io
|
||||
fwg.Wait()
|
||||
|
||||
restoreTerm()
|
||||
os.Exit(int(ec))
|
||||
if ec != 0 {
|
||||
return cli.NewExitError("", int(ec))
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
|
|
|
@ -14,8 +14,8 @@ import (
|
|||
|
||||
gocontext "context"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/docker/containerd/api/execution"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tonistiigi/fifo"
|
||||
"github.com/urfave/cli"
|
||||
"google.golang.org/grpc"
|
||||
|
@ -39,7 +39,6 @@ func prepareStdio(stdin, stdout, stderr string, console bool) (*sync.WaitGroup,
|
|||
}(f)
|
||||
go func(w io.WriteCloser) {
|
||||
io.Copy(w, os.Stdin)
|
||||
logrus.Info("stdin copy finished")
|
||||
w.Close()
|
||||
}(f)
|
||||
|
||||
|
@ -56,7 +55,6 @@ func prepareStdio(stdin, stdout, stderr string, console bool) (*sync.WaitGroup,
|
|||
go func(r io.ReadCloser) {
|
||||
io.Copy(os.Stdout, r)
|
||||
r.Close()
|
||||
logrus.Info("stdout copy finished")
|
||||
wg.Done()
|
||||
}(f)
|
||||
|
||||
|
@ -74,7 +72,6 @@ func prepareStdio(stdin, stdout, stderr string, console bool) (*sync.WaitGroup,
|
|||
go func(r io.ReadCloser) {
|
||||
io.Copy(os.Stderr, r)
|
||||
r.Close()
|
||||
logrus.Info("stderr copy finished")
|
||||
wg.Done()
|
||||
}(f)
|
||||
}
|
||||
|
@ -99,7 +96,7 @@ func getGRPCConnection(context *cli.Context) (*grpc.ClientConn, error) {
|
|||
|
||||
conn, err := grpc.Dial(fmt.Sprintf("unix://%s", bindSocket), dialOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrapf(err, "failed to dial %q", bindSocket)
|
||||
}
|
||||
|
||||
grpcConn = conn
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue