2017-01-12 18:29:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2017-01-19 22:16:50 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2017-01-24 23:03:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2017-01-19 22:16:50 +00:00
|
|
|
"github.com/docker/containerd"
|
2017-01-24 23:55:32 +00:00
|
|
|
apishim "github.com/docker/containerd/api/shim"
|
|
|
|
"github.com/docker/containerd/shim"
|
2017-01-12 18:29:37 +00:00
|
|
|
"github.com/docker/containerd/sys"
|
2017-01-19 22:16:50 +00:00
|
|
|
"github.com/docker/containerd/utils"
|
|
|
|
"github.com/urfave/cli"
|
2017-01-12 18:29:37 +00:00
|
|
|
)
|
|
|
|
|
2017-01-19 22:16:50 +00:00
|
|
|
const usage = `
|
|
|
|
__ _ __ __ _
|
|
|
|
_________ ____ / /_____ _(_)___ ___ _________/ / _____/ /_ (_)___ ___
|
|
|
|
/ ___/ __ \/ __ \/ __/ __ ` + "`" + `/ / __ \/ _ \/ ___/ __ /_____/ ___/ __ \/ / __ ` + "`" + `__ \
|
|
|
|
/ /__/ /_/ / / / / /_/ /_/ / / / / / __/ / / /_/ /_____(__ ) / / / / / / / / /
|
|
|
|
\___/\____/_/ /_/\__/\__,_/_/_/ /_/\___/_/ \__,_/ /____/_/ /_/_/_/ /_/ /_/
|
|
|
|
|
|
|
|
shim for container lifecycle and reconnection
|
|
|
|
`
|
2017-01-12 18:29:37 +00:00
|
|
|
|
|
|
|
func main() {
|
2017-01-19 22:16:50 +00:00
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "containerd-shim"
|
|
|
|
app.Version = containerd.Version
|
|
|
|
app.Usage = usage
|
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "debug",
|
|
|
|
Usage: "enable debug output in logs",
|
|
|
|
},
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|
2017-01-19 22:16:50 +00:00
|
|
|
app.Before = func(context *cli.Context) error {
|
|
|
|
if context.GlobalBool("debug") {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|
2017-01-19 22:16:50 +00:00
|
|
|
return nil
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|
2017-01-19 22:16:50 +00:00
|
|
|
app.Action = func(context *cli.Context) error {
|
|
|
|
// start handling signals as soon as possible so that things are properly reaped
|
|
|
|
// or if runtime exits before we hit the handler
|
|
|
|
signals, err := setupSignals()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|
2017-01-19 22:16:50 +00:00
|
|
|
var (
|
|
|
|
server = grpc.NewServer()
|
2017-01-24 23:55:32 +00:00
|
|
|
sv = shim.NewService()
|
2017-01-19 22:16:50 +00:00
|
|
|
)
|
2017-01-24 23:55:32 +00:00
|
|
|
apishim.RegisterShimServer(server, sv)
|
2017-01-19 22:16:50 +00:00
|
|
|
l, err := utils.CreateUnixSocket("shim.sock")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|
2017-01-19 22:16:50 +00:00
|
|
|
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 {
|
2017-01-19 23:21:25 +00:00
|
|
|
logrus.WithField("signal", s).Debug("received signal")
|
2017-01-12 18:29:37 +00:00
|
|
|
switch s {
|
|
|
|
case syscall.SIGCHLD:
|
2017-01-19 22:16:50 +00:00
|
|
|
exits, err := utils.Reap(false)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("reap exit status")
|
|
|
|
}
|
2017-01-12 18:29:37 +00:00
|
|
|
for _, e := range exits {
|
2017-01-24 23:55:32 +00:00
|
|
|
if err := sv.ProcessExit(e); err != nil {
|
2017-01-19 22:16:50 +00:00
|
|
|
return err
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-19 22:16:50 +00:00
|
|
|
case syscall.SIGTERM, syscall.SIGINT:
|
|
|
|
server.GracefulStop()
|
|
|
|
return nil
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-19 22:16:50 +00:00
|
|
|
return nil
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|
2017-01-19 22:16:50 +00:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "containerd-shim: %s\n", err)
|
|
|
|
os.Exit(1)
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 22:16:50 +00:00
|
|
|
// setupSignals creates a new signal handler for all signals and sets the shim as a
|
|
|
|
// sub-reaper so that the container processes are reparented
|
|
|
|
func setupSignals() (chan os.Signal, error) {
|
|
|
|
signals := make(chan os.Signal, 2048)
|
|
|
|
signal.Notify(signals)
|
|
|
|
// set the shim as the subreaper for all orphaned processes created by the container
|
|
|
|
if err := sys.SetSubreaper(1); err != nil {
|
|
|
|
return nil, err
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|
2017-01-19 22:16:50 +00:00
|
|
|
return signals, nil
|
2017-01-12 18:29:37 +00:00
|
|
|
}
|