Add events api to shim

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-01-25 13:27:48 -08:00
parent 462bdd7669
commit 07c81ccac4
10 changed files with 663 additions and 211 deletions

View file

@ -55,37 +55,12 @@ func main() {
server = grpc.NewServer()
sv = shim.NewService()
)
logrus.Debug("registering grpc server")
apishim.RegisterShimServer(server, sv)
l, err := utils.CreateUnixSocket("shim.sock")
if err != nil {
if err := serve(server, "shim.sock"); err != nil {
return err
}
go func() {
defer l.Close()
if err := server.Serve(l); err != nil {
l.Close()
logrus.WithError(err).Fatal("containerd-shim: GRPC server failure")
}
}()
for s := range signals {
logrus.WithField("signal", s).Debug("received signal")
switch s {
case syscall.SIGCHLD:
exits, err := utils.Reap(false)
if err != nil {
logrus.WithError(err).Error("reap exit status")
}
for _, e := range exits {
if err := sv.ProcessExit(e); err != nil {
return err
}
}
case syscall.SIGTERM, syscall.SIGINT:
server.GracefulStop()
return nil
}
}
return nil
return handleSignals(signals, server, sv)
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "containerd-shim: %s\n", err)
@ -104,3 +79,49 @@ func setupSignals() (chan os.Signal, error) {
}
return signals, nil
}
// serve serves the grpc API over a unix socket at the provided path
// this function does not block
func serve(server *grpc.Server, path string) error {
l, err := utils.CreateUnixSocket(path)
if err != nil {
return err
}
logrus.WithField("socket", path).Debug("serving api on unix socket")
go func() {
defer l.Close()
if err := server.Serve(l); err != nil {
l.Close()
logrus.WithError(err).Fatal("containerd-shim: GRPC server failure")
}
}()
return nil
}
func handleSignals(signals chan os.Signal, server *grpc.Server, service *shim.Service) error {
for s := range signals {
logrus.WithField("signal", s).Debug("received signal")
switch s {
case syscall.SIGCHLD:
exits, err := utils.Reap(false)
if err != nil {
logrus.WithError(err).Error("reap exit status")
}
for _, e := range exits {
logrus.WithFields(logrus.Fields{
"status": e.Status,
"pid": e.Pid,
}).Debug("process exited")
if err := service.ProcessExit(e); err != nil {
return err
}
}
case syscall.SIGTERM, syscall.SIGINT:
// TODO: should we forward signals to the processes if they are still running?
// i.e. machine reboot
server.GracefulStop()
return nil
}
}
return nil
}

View file

@ -18,6 +18,21 @@ import (
"github.com/urfave/cli"
)
var fifoFlags = []cli.Flag{
cli.StringFlag{
Name: "stdin",
Usage: "specify the path to the stdin fifo",
},
cli.StringFlag{
Name: "stdout",
Usage: "specify the path to the stdout fifo",
},
cli.StringFlag{
Name: "stderr",
Usage: "specify the path to the stderr fifo",
},
}
var shimCommand = cli.Command{
Name: "shim",
Usage: "interact with a shim directly",
@ -41,7 +56,7 @@ var shimCommand = cli.Command{
var shimCreateCommand = cli.Command{
Name: "create",
Usage: "create a container with a shim",
Flags: []cli.Flag{
Flags: append(fifoFlags,
cli.StringFlag{
Name: "bundle",
Usage: "bundle path for the container",
@ -51,7 +66,11 @@ var shimCreateCommand = cli.Command{
Value: "runc",
Usage: "runtime to use for the container",
},
},
cli.BoolFlag{
Name: "attach,a",
Usage: "stay attached to the container and open the fifos",
},
),
Action: func(context *cli.Context) error {
id := context.Args().First()
if id == "" {
@ -61,15 +80,25 @@ var shimCreateCommand = cli.Command{
if err != nil {
return err
}
wg, err := prepareStdio(context.String("stdin"), context.String("stdout"), context.String("stderr"), false)
if err != nil {
return err
}
r, err := service.Create(gocontext.Background(), &shim.CreateRequest{
ID: id,
Bundle: context.String("bundle"),
Runtime: context.String("runtime"),
Stdin: context.String("stdin"),
Stdout: context.String("stdout"),
Stderr: context.String("stderr"),
})
if err != nil {
return err
}
fmt.Printf("container created with id %s and pid %d\n", id, r.Pid)
if context.Bool("attach") {
wg.Wait()
}
return nil
},
}
@ -99,7 +128,7 @@ var shimDeleteCommand = cli.Command{
if err != nil {
return err
}
fmt.Printf("container deleted and returned exit status %d", r.ExitStatus)
fmt.Printf("container deleted and returned exit status %d\n", r.ExitStatus)
return nil
},
}

View file

@ -115,7 +115,6 @@ func getTempDir(id string) (string, error) {
if err != nil {
return "", err
}
tmpDir, err := ioutil.TempDir(filepath.Join(os.TempDir(), "ctr"), fmt.Sprintf("%s-", id))
if err != nil {
return "", err