2015-11-05 23:29:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-03-18 22:29:22 +00:00
|
|
|
"fmt"
|
2016-09-20 17:22:19 +00:00
|
|
|
"net/http"
|
2015-11-05 23:29:53 +00:00
|
|
|
"os"
|
2016-03-21 19:54:05 +00:00
|
|
|
"os/signal"
|
2016-08-30 16:47:21 +00:00
|
|
|
"runtime"
|
2016-03-31 03:23:54 +00:00
|
|
|
"strings"
|
2015-12-03 01:42:28 +00:00
|
|
|
"sync"
|
2016-03-21 19:54:05 +00:00
|
|
|
"syscall"
|
2015-12-02 21:39:36 +00:00
|
|
|
"time"
|
2015-11-05 23:29:53 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/codegangsta/cli"
|
2016-09-20 17:22:19 +00:00
|
|
|
"github.com/docker/containerd/api/pprof"
|
2015-12-18 00:07:04 +00:00
|
|
|
"github.com/docker/containerd/supervisor"
|
2015-11-05 23:29:53 +00:00
|
|
|
)
|
|
|
|
|
2015-12-11 22:33:48 +00:00
|
|
|
const (
|
2016-04-26 20:29:35 +00:00
|
|
|
usage = `High performance container daemon`
|
|
|
|
minRlimit = 1024
|
|
|
|
defaultStateDir = "/run/containerd"
|
|
|
|
defaultGRPCEndpoint = "unix:///run/containerd/containerd.sock"
|
2015-12-11 22:33:48 +00:00
|
|
|
)
|
2015-11-05 23:29:53 +00:00
|
|
|
|
2015-12-09 22:40:55 +00:00
|
|
|
var daemonFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "debug",
|
|
|
|
Usage: "enable debug output in the logs",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "state-dir",
|
2016-02-29 02:28:09 +00:00
|
|
|
Value: defaultStateDir,
|
2015-12-09 22:40:55 +00:00
|
|
|
Usage: "runtime state directory",
|
|
|
|
},
|
2015-12-14 23:54:11 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "listen,l",
|
2016-02-29 02:28:09 +00:00
|
|
|
Value: defaultGRPCEndpoint,
|
2016-03-31 03:23:54 +00:00
|
|
|
Usage: "proto://address on which the GRPC API will listen",
|
2015-12-14 23:54:11 +00:00
|
|
|
},
|
2016-02-25 20:59:34 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "runtime,r",
|
|
|
|
Value: "runc",
|
2016-04-06 06:42:47 +00:00
|
|
|
Usage: "name or path of the OCI compliant runtime to use when executing containers",
|
2016-02-25 20:59:34 +00:00
|
|
|
},
|
2016-03-24 20:30:27 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "runtime-args",
|
|
|
|
Value: &cli.StringSlice{},
|
|
|
|
Usage: "specify additional runtime args",
|
|
|
|
},
|
2016-04-06 06:42:47 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "shim",
|
|
|
|
Value: "containerd-shim",
|
|
|
|
Usage: "Name or path of shim",
|
|
|
|
},
|
2016-03-25 17:25:37 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pprof-address",
|
|
|
|
Usage: "http address to listen for pprof events",
|
|
|
|
},
|
2016-03-28 20:30:37 +00:00
|
|
|
cli.DurationFlag{
|
|
|
|
Name: "start-timeout",
|
|
|
|
Value: 15 * time.Second,
|
|
|
|
Usage: "timeout duration for waiting on a container to start before it is killed",
|
|
|
|
},
|
2016-04-19 00:28:13 +00:00
|
|
|
cli.IntFlag{
|
|
|
|
Name: "retain-count",
|
|
|
|
Value: 500,
|
|
|
|
Usage: "number of past events to keep in the event log",
|
|
|
|
},
|
2015-12-09 22:40:55 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 23:29:53 +00:00
|
|
|
func main() {
|
2016-04-21 01:08:23 +00:00
|
|
|
logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: time.RFC3339Nano})
|
2015-11-05 23:29:53 +00:00
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "containerd"
|
2016-09-20 17:29:42 +00:00
|
|
|
app.Version = getVersion()
|
2016-01-25 22:52:08 +00:00
|
|
|
app.Usage = usage
|
2015-12-09 22:40:55 +00:00
|
|
|
app.Flags = daemonFlags
|
2016-04-26 20:29:35 +00:00
|
|
|
app.Before = func(context *cli.Context) error {
|
|
|
|
if context.GlobalBool("debug") {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
|
|
|
if p := context.GlobalString("pprof-address"); len(p) > 0 {
|
2016-09-20 17:22:19 +00:00
|
|
|
h := pprof.New()
|
|
|
|
http.Handle("/debug", h)
|
|
|
|
go http.ListenAndServe(p, nil)
|
2016-04-26 20:29:35 +00:00
|
|
|
}
|
|
|
|
if err := checkLimits(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-02 21:39:36 +00:00
|
|
|
app.Action = func(context *cli.Context) {
|
2016-04-19 00:28:13 +00:00
|
|
|
if err := daemon(context); err != nil {
|
2015-12-02 21:39:36 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 23:29:53 +00:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 21:39:36 +00:00
|
|
|
|
2016-04-19 00:28:13 +00:00
|
|
|
func daemon(context *cli.Context) error {
|
2016-09-20 17:29:42 +00:00
|
|
|
signals := make(chan os.Signal, 2048)
|
|
|
|
signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1)
|
2016-09-20 22:30:35 +00:00
|
|
|
sv, err := supervisor.New(supervisor.Config{
|
|
|
|
StateDir: context.String("state-dir"),
|
|
|
|
Runtime: context.String("runtime"),
|
|
|
|
ShimName: context.String("shim"),
|
|
|
|
RuntimeArgs: context.StringSlice("runtime-args"),
|
|
|
|
Timeout: context.Duration("start-timeout"),
|
|
|
|
EventRetainCount: context.Int("retain-count"),
|
|
|
|
})
|
2015-12-02 21:39:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-03 01:42:28 +00:00
|
|
|
wg := &sync.WaitGroup{}
|
2016-04-19 00:28:13 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
2015-12-03 01:42:28 +00:00
|
|
|
wg.Add(1)
|
2015-12-18 00:07:04 +00:00
|
|
|
w := supervisor.NewWorker(sv, wg)
|
2015-12-03 01:42:28 +00:00
|
|
|
go w.Start()
|
|
|
|
}
|
2015-12-18 00:07:04 +00:00
|
|
|
if err := sv.Start(); err != nil {
|
2015-12-02 21:39:36 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-03-31 03:23:54 +00:00
|
|
|
// Split the listen string of the form proto://addr
|
2016-09-20 17:29:42 +00:00
|
|
|
var (
|
|
|
|
listenSpec = context.String("listen")
|
|
|
|
listenParts = strings.SplitN(listenSpec, "://", 2)
|
|
|
|
)
|
2016-03-31 03:23:54 +00:00
|
|
|
if len(listenParts) != 2 {
|
|
|
|
return fmt.Errorf("bad listen address format %s, expected proto://address", listenSpec)
|
|
|
|
}
|
|
|
|
server, err := startServer(listenParts[0], listenParts[1], sv)
|
2016-03-21 19:54:05 +00:00
|
|
|
if err != nil {
|
2015-12-14 23:54:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-09-20 17:29:42 +00:00
|
|
|
for s := range signals {
|
|
|
|
switch s {
|
|
|
|
case syscall.SIGUSR1:
|
|
|
|
var (
|
|
|
|
buf []byte
|
|
|
|
stackSize int
|
|
|
|
)
|
|
|
|
bufferLen := 16384
|
|
|
|
for stackSize == len(buf) {
|
|
|
|
buf = make([]byte, bufferLen)
|
|
|
|
stackSize = runtime.Stack(buf, true)
|
|
|
|
bufferLen *= 2
|
|
|
|
}
|
|
|
|
buf = buf[:stackSize]
|
|
|
|
logrus.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
|
|
|
|
case syscall.SIGINT, syscall.SIGTERM:
|
|
|
|
logrus.Infof("stopping containerd after receiving %s", s)
|
2016-03-21 19:54:05 +00:00
|
|
|
server.Stop()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|