2016-07-08 19:04:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-09-17 15:26:56 +00:00
|
|
|
"fmt"
|
2016-07-08 19:04:00 +00:00
|
|
|
"net"
|
2016-07-22 20:44:27 +00:00
|
|
|
"os"
|
2016-09-21 08:59:37 +00:00
|
|
|
"path/filepath"
|
2016-07-08 19:04:00 +00:00
|
|
|
|
2016-09-17 15:26:56 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-26 23:55:12 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/server"
|
2016-10-05 13:29:30 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/selinux"
|
2016-07-20 01:30:05 +00:00
|
|
|
"github.com/urfave/cli"
|
2016-07-08 19:04:00 +00:00
|
|
|
"google.golang.org/grpc"
|
2016-09-23 07:31:31 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
2016-07-08 19:04:00 +00:00
|
|
|
)
|
|
|
|
|
2016-09-21 08:59:37 +00:00
|
|
|
const (
|
2016-09-28 19:49:46 +00:00
|
|
|
ocidRoot = "/var/lib/ocid"
|
|
|
|
conmonPath = "/usr/libexec/ocid/conmon"
|
2016-10-02 09:11:07 +00:00
|
|
|
pausePath = "/usr/libexec/ocid/pause"
|
2016-09-21 08:59:37 +00:00
|
|
|
)
|
|
|
|
|
2016-07-08 19:04:00 +00:00
|
|
|
func main() {
|
2016-07-20 01:30:05 +00:00
|
|
|
app := cli.NewApp()
|
2016-09-23 07:31:31 +00:00
|
|
|
app.Name = "ocid"
|
|
|
|
app.Usage = "ocid server"
|
|
|
|
app.Version = "0.0.1"
|
2016-07-20 01:30:05 +00:00
|
|
|
|
|
|
|
app.Flags = []cli.Flag{
|
2016-09-28 19:49:46 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "conmon",
|
|
|
|
Value: conmonPath,
|
|
|
|
Usage: "path to the conmon executable",
|
|
|
|
},
|
2016-10-02 09:11:07 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pause",
|
|
|
|
Value: pausePath,
|
|
|
|
Usage: "path to the pause executable",
|
|
|
|
},
|
2016-09-21 08:59:37 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "root",
|
|
|
|
Value: ocidRoot,
|
|
|
|
Usage: "ocid root dir",
|
|
|
|
},
|
2016-07-20 01:30:05 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "sandboxdir",
|
2016-09-21 08:59:37 +00:00
|
|
|
Value: filepath.Join(ocidRoot, "sandboxes"),
|
2016-07-20 01:30:05 +00:00
|
|
|
Usage: "ocid pod sandbox dir",
|
|
|
|
},
|
2016-08-01 22:08:21 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "containerdir",
|
2016-09-21 08:59:37 +00:00
|
|
|
Value: filepath.Join(ocidRoot, "containers"),
|
2016-08-01 22:08:21 +00:00
|
|
|
Usage: "ocid container dir",
|
|
|
|
},
|
2016-09-20 10:31:10 +00:00
|
|
|
cli.StringFlag{
|
2016-10-10 09:57:17 +00:00
|
|
|
Name: "listen",
|
2016-09-20 10:31:10 +00:00
|
|
|
Value: "/var/run/ocid.sock",
|
|
|
|
Usage: "path to ocid socket",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "runtime",
|
|
|
|
Value: "/usr/bin/runc",
|
|
|
|
Usage: "OCI runtime path",
|
|
|
|
},
|
2016-09-17 15:26:56 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "debug",
|
|
|
|
Usage: "enable debug output for logging",
|
|
|
|
},
|
2016-10-05 13:29:30 +00:00
|
|
|
cli.BoolFlag{
|
2016-10-10 09:57:17 +00:00
|
|
|
Name: "selinux",
|
2016-10-05 13:29:30 +00:00
|
|
|
Usage: "enable selinux support",
|
|
|
|
},
|
2016-09-17 15:26:56 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "log",
|
|
|
|
Value: "",
|
|
|
|
Usage: "set the log file path where internal debug information is written",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "log-format",
|
|
|
|
Value: "text",
|
|
|
|
Usage: "set the format used by logs ('text' (default), or 'json')",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Before = func(c *cli.Context) error {
|
|
|
|
if c.GlobalBool("debug") {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
2016-10-10 09:57:17 +00:00
|
|
|
if !c.GlobalBool("selinux") {
|
2016-10-05 13:29:30 +00:00
|
|
|
selinux.SetDisabled()
|
|
|
|
}
|
2016-09-17 15:26:56 +00:00
|
|
|
if path := c.GlobalString("log"); path != "" {
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_SYNC, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.SetOutput(f)
|
|
|
|
}
|
2016-10-06 16:14:54 +00:00
|
|
|
if _, err := os.Stat(c.GlobalString("runtime")); os.IsNotExist(err) {
|
|
|
|
// path to runtime does not exist
|
|
|
|
return fmt.Errorf("invalid --runtime value %q", err)
|
|
|
|
}
|
2016-09-17 15:26:56 +00:00
|
|
|
switch c.GlobalString("log-format") {
|
|
|
|
case "text":
|
|
|
|
// retain logrus's default.
|
|
|
|
case "json":
|
|
|
|
logrus.SetFormatter(new(logrus.JSONFormatter))
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown log-format %q", c.GlobalString("log-format"))
|
|
|
|
}
|
2016-10-10 09:57:17 +00:00
|
|
|
|
2016-09-17 15:26:56 +00:00
|
|
|
return nil
|
2016-07-20 01:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.Action = func(c *cli.Context) error {
|
2016-10-10 09:57:17 +00:00
|
|
|
socketPath := c.String("listen")
|
2016-07-20 01:30:05 +00:00
|
|
|
// Remove the socket if it already exists
|
2016-09-20 10:31:10 +00:00
|
|
|
if _, err := os.Stat(socketPath); err == nil {
|
|
|
|
if err := os.Remove(socketPath); err != nil {
|
2016-09-19 07:21:14 +00:00
|
|
|
logrus.Fatal(err)
|
2016-07-20 01:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 10:31:10 +00:00
|
|
|
lis, err := net.Listen("unix", socketPath)
|
2016-07-20 01:30:05 +00:00
|
|
|
if err != nil {
|
2016-09-19 07:21:14 +00:00
|
|
|
logrus.Fatalf("failed to listen: %v", err)
|
2016-07-20 01:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := grpc.NewServer()
|
|
|
|
|
2016-08-01 22:08:21 +00:00
|
|
|
containerDir := c.String("containerdir")
|
2016-07-20 01:30:05 +00:00
|
|
|
sandboxDir := c.String("sandboxdir")
|
2016-10-02 09:11:16 +00:00
|
|
|
conmonPath := c.String("conmon")
|
2016-10-02 09:11:07 +00:00
|
|
|
pausePath := c.String("pause")
|
|
|
|
service, err := server.New(c.String("runtime"), c.String("root"), sandboxDir, containerDir, conmonPath, pausePath)
|
2016-07-20 01:30:05 +00:00
|
|
|
if err != nil {
|
2016-09-19 07:21:14 +00:00
|
|
|
logrus.Fatal(err)
|
2016-07-22 20:44:27 +00:00
|
|
|
}
|
2016-07-20 01:30:05 +00:00
|
|
|
|
|
|
|
runtime.RegisterRuntimeServiceServer(s, service)
|
|
|
|
runtime.RegisterImageServiceServer(s, service)
|
2016-09-19 07:21:14 +00:00
|
|
|
if err := s.Serve(lis); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2016-07-20 01:30:05 +00:00
|
|
|
return nil
|
2016-07-22 20:44:27 +00:00
|
|
|
}
|
2016-07-20 01:30:05 +00:00
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
2016-09-19 07:21:14 +00:00
|
|
|
logrus.Fatal(err)
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|
2016-07-08 19:04:00 +00:00
|
|
|
}
|