Remove bundles from API
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
1dc5d652ac
commit
ab8586b7c5
48 changed files with 3287 additions and 5946 deletions
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
|
@ -52,6 +53,9 @@ func main() {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := setupRoot(); err != nil {
|
||||
return err
|
||||
}
|
||||
var (
|
||||
server = grpc.NewServer()
|
||||
sv = shim.New()
|
||||
|
@ -84,7 +88,7 @@ func setupSignals() (chan os.Signal, error) {
|
|||
// 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)
|
||||
l, err := net.FileListener(os.NewFile(3, "socket"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -126,3 +130,8 @@ func handleSignals(signals chan os.Signal, server *grpc.Server, service *shim.Se
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// setupRoot sets up the root as the shim is started in its own mount namespace
|
||||
func setupRoot() error {
|
||||
return syscall.Mount("", "/", "", syscall.MS_SLAVE|syscall.MS_REC, "")
|
||||
}
|
||||
|
|
|
@ -3,16 +3,12 @@ package main
|
|||
import (
|
||||
_ "expvar"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
gocontext "golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
|
@ -20,17 +16,13 @@ import (
|
|||
"github.com/Sirupsen/logrus"
|
||||
"github.com/docker/containerd"
|
||||
api "github.com/docker/containerd/api/services/execution"
|
||||
"github.com/docker/containerd/events"
|
||||
_ "github.com/docker/containerd/linux"
|
||||
"github.com/docker/containerd/log"
|
||||
"github.com/docker/containerd/supervisor"
|
||||
"github.com/docker/containerd/services/execution"
|
||||
"github.com/docker/containerd/utils"
|
||||
metrics "github.com/docker/go-metrics"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli"
|
||||
|
||||
natsd "github.com/nats-io/gnatsd/server"
|
||||
"github.com/nats-io/go-nats"
|
||||
stand "github.com/nats-io/nats-streaming-server/server"
|
||||
)
|
||||
|
||||
const usage = `
|
||||
|
@ -43,10 +35,7 @@ const usage = `
|
|||
high performance container runtime
|
||||
`
|
||||
|
||||
const (
|
||||
StanClusterID = "containerd"
|
||||
stanClientID = "containerd"
|
||||
)
|
||||
var global = log.WithModule(gocontext.Background(), "containerd")
|
||||
|
||||
func main() {
|
||||
app := cli.NewApp()
|
||||
|
@ -83,108 +72,36 @@ func main() {
|
|||
Usage: "tcp address to serve metrics on",
|
||||
Value: "127.0.0.1:7897",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "events-address, e",
|
||||
Usage: "nats address to serve events on",
|
||||
Value: nats.DefaultURL,
|
||||
},
|
||||
}
|
||||
app.Before = func(context *cli.Context) error {
|
||||
if context.GlobalBool("debug") {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
}
|
||||
if logLevel := context.GlobalString("log-level"); logLevel != "" {
|
||||
lvl, err := logrus.ParseLevel(logLevel)
|
||||
if err != nil {
|
||||
lvl = logrus.InfoLevel
|
||||
fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n, and being defaulted to info", logLevel)
|
||||
}
|
||||
logrus.SetLevel(lvl)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
app.Before = before
|
||||
app.Action = func(context *cli.Context) error {
|
||||
start := time.Now()
|
||||
// start the signal handler as soon as we can to make sure that
|
||||
// we don't miss any signals during boot
|
||||
signals := make(chan os.Signal, 2048)
|
||||
signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1)
|
||||
|
||||
ctx := log.WithModule(gocontext.Background(), "containerd")
|
||||
if address := context.GlobalString("metrics-address"); address != "" {
|
||||
log.G(ctx).WithField("metrics-address", address).Info("listening and serving metrics")
|
||||
go serveMetrics(ctx, address)
|
||||
}
|
||||
|
||||
ea := context.GlobalString("events-address")
|
||||
log.G(ctx).WithField("events-address", ea).Info("starting nats-streaming-server")
|
||||
s, err := startNATSServer(ea)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer s.Shutdown()
|
||||
|
||||
debugPath := context.GlobalString("debug-socket")
|
||||
if debugPath == "" {
|
||||
return errors.New("--debug-socket path cannot be empty")
|
||||
}
|
||||
d, err := utils.CreateUnixSocket(debugPath)
|
||||
log.G(global).Info("starting containerd boot...")
|
||||
runtimes, err := loadRuntimes(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//publish profiling and debug socket.
|
||||
log.G(ctx).WithField("socket", debugPath).Info("starting profiler handlers")
|
||||
log.G(ctx).WithFields(logrus.Fields{"expvars": "/debug/vars", "socket": debugPath}).Debug("serving expvars requests")
|
||||
log.G(ctx).WithFields(logrus.Fields{"pprof": "/debug/pprof", "socket": debugPath}).Debug("serving pprof requests")
|
||||
go serveProfiler(ctx, d)
|
||||
|
||||
path := context.GlobalString("socket")
|
||||
if path == "" {
|
||||
return errors.New("--socket path cannot be empty")
|
||||
}
|
||||
l, err := utils.CreateUnixSocket(path)
|
||||
supervisor, err := containerd.NewSupervisor(log.WithModule(global, "execution"), runtimes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get events publisher
|
||||
natsPoster, err := events.NewNATSPoster(StanClusterID, stanClientID)
|
||||
if err != nil {
|
||||
// start debug and metrics APIs
|
||||
if err := serveDebugAPI(context); err != nil {
|
||||
return err
|
||||
}
|
||||
execCtx := log.WithModule(ctx, "execution")
|
||||
execCtx = events.WithPoster(execCtx, natsPoster)
|
||||
execService, err := supervisor.New(execCtx, context.GlobalString("root"))
|
||||
if err != nil {
|
||||
serveMetricsAPI(context)
|
||||
// start the GRPC api with the execution service registered
|
||||
server := newGRPCServer(execution.New(supervisor))
|
||||
if err := serveGRPC(context, server); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Intercept the GRPC call in order to populate the correct module path
|
||||
interceptor := func(ctx gocontext.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
||||
ctx = log.WithModule(ctx, "containerd")
|
||||
switch info.Server.(type) {
|
||||
case api.ExecutionServiceServer:
|
||||
ctx = log.WithModule(ctx, "execution")
|
||||
ctx = events.WithPoster(ctx, natsPoster)
|
||||
default:
|
||||
fmt.Printf("Unknown type: %#v\n", info.Server)
|
||||
}
|
||||
return handler(ctx, req)
|
||||
}
|
||||
server := grpc.NewServer(grpc.UnaryInterceptor(interceptor))
|
||||
api.RegisterExecutionServiceServer(server, execService)
|
||||
log.G(ctx).WithField("socket", l.Addr()).Info("start serving GRPC API")
|
||||
go serveGRPC(ctx, server, l)
|
||||
|
||||
for s := range signals {
|
||||
switch s {
|
||||
case syscall.SIGUSR1:
|
||||
dumpStacks(ctx)
|
||||
default:
|
||||
log.G(ctx).WithField("signal", s).Info("stopping GRPC server")
|
||||
server.Stop()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
log.G(global).Infof("containerd successfully booted in %fs", time.Now().Sub(start).Seconds())
|
||||
return handleSignals(signals, server)
|
||||
}
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "containerd: %s\n", err)
|
||||
|
@ -192,81 +109,123 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
func serveMetrics(ctx gocontext.Context, address string) {
|
||||
func before(context *cli.Context) error {
|
||||
if context.GlobalBool("debug") {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
}
|
||||
if l := context.GlobalString("log-level"); l != "" {
|
||||
lvl, err := logrus.ParseLevel(l)
|
||||
if err != nil {
|
||||
lvl = logrus.InfoLevel
|
||||
fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n, and being defaulted to info", l)
|
||||
}
|
||||
logrus.SetLevel(lvl)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func serveMetricsAPI(context *cli.Context) {
|
||||
if addr := context.GlobalString("metrics-address"); addr != "" {
|
||||
log.G(global).WithField("metrics", addr).Info("starting metrics API...")
|
||||
h := newMetricsHandler()
|
||||
go func() {
|
||||
if err := http.ListenAndServe(addr, h); err != nil {
|
||||
log.G(global).WithError(err).Fatal("serve metrics API")
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func newMetricsHandler() http.Handler {
|
||||
m := http.NewServeMux()
|
||||
m.Handle("/metrics", metrics.Handler())
|
||||
if err := http.ListenAndServe(address, m); err != nil {
|
||||
log.G(ctx).WithError(err).Fatal("metrics server failure")
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func serveGRPC(ctx gocontext.Context, server *grpc.Server, l net.Listener) {
|
||||
defer l.Close()
|
||||
if err := server.Serve(l); err != nil {
|
||||
log.G(ctx).WithError(err).Fatal("GRPC server failure")
|
||||
func serveDebugAPI(context *cli.Context) error {
|
||||
path := context.GlobalString("debug-socket")
|
||||
if path == "" {
|
||||
return errors.New("--debug-socket path cannot be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func serveProfiler(ctx gocontext.Context, l net.Listener) {
|
||||
defer l.Close()
|
||||
if err := http.Serve(l, nil); err != nil {
|
||||
log.G(ctx).WithError(err).Fatal("profiler server failure")
|
||||
l, err := utils.CreateUnixSocket(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// DumpStacks dumps the runtime stack.
|
||||
func dumpStacks(ctx gocontext.Context) {
|
||||
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]
|
||||
log.G(ctx).Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
|
||||
}
|
||||
|
||||
func startNATSServer(address string) (s *stand.StanServer, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
s = nil
|
||||
if _, ok := r.(error); !ok {
|
||||
err = fmt.Errorf("failed to start NATS server: %v", r)
|
||||
} else {
|
||||
err = r.(error)
|
||||
}
|
||||
log.G(global).WithField("debug", path).Info("starting debug API...")
|
||||
go func() {
|
||||
defer l.Close()
|
||||
// pprof and expvars are imported and automatically register their endpoints
|
||||
// under /debug
|
||||
if err := http.Serve(l, nil); err != nil {
|
||||
log.G(global).WithError(err).Fatal("serve debug API")
|
||||
}
|
||||
}()
|
||||
so, no, err := getServerOptions(address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s = stand.RunServerWithOpts(so, no)
|
||||
|
||||
return s, err
|
||||
return nil
|
||||
}
|
||||
|
||||
func getServerOptions(address string) (*stand.Options, *natsd.Options, error) {
|
||||
url, err := url.Parse(address)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrapf(err, "failed to parse address url %q", address)
|
||||
func loadRuntimes(context *cli.Context) (map[string]containerd.Runtime, error) {
|
||||
var (
|
||||
root = context.GlobalString("root")
|
||||
o = make(map[string]containerd.Runtime)
|
||||
)
|
||||
for _, name := range containerd.Runtimes() {
|
||||
r, err := containerd.NewRuntime(name, root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
o[name] = r
|
||||
log.G(global).WithField("runtime", name).Info("load runtime")
|
||||
}
|
||||
|
||||
no := stand.DefaultNatsServerOptions
|
||||
parts := strings.Split(url.Host, ":")
|
||||
if len(parts) == 2 {
|
||||
no.Port, err = strconv.Atoi(parts[1])
|
||||
} else {
|
||||
no.Port = nats.DefaultPort
|
||||
}
|
||||
no.Host = parts[0]
|
||||
|
||||
so := stand.GetDefaultOptions()
|
||||
so.ID = StanClusterID
|
||||
|
||||
return so, &no, nil
|
||||
return o, nil
|
||||
}
|
||||
|
||||
func newGRPCServer(service api.ContainerServiceServer) *grpc.Server {
|
||||
s := grpc.NewServer(grpc.UnaryInterceptor(interceptor))
|
||||
api.RegisterContainerServiceServer(s, service)
|
||||
return s
|
||||
}
|
||||
|
||||
func serveGRPC(context *cli.Context, server *grpc.Server) error {
|
||||
path := context.GlobalString("socket")
|
||||
if path == "" {
|
||||
return errors.New("--socket path cannot be empty")
|
||||
}
|
||||
l, err := utils.CreateUnixSocket(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go func() {
|
||||
defer l.Close()
|
||||
if err := server.Serve(l); err != nil {
|
||||
log.G(global).WithError(err).Fatal("serve GRPC")
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func interceptor(ctx gocontext.Context,
|
||||
req interface{},
|
||||
info *grpc.UnaryServerInfo,
|
||||
handler grpc.UnaryHandler,
|
||||
) (interface{}, error) {
|
||||
ctx = log.WithModule(ctx, "containerd")
|
||||
switch info.Server.(type) {
|
||||
case api.ContainerServiceServer:
|
||||
ctx = log.WithModule(global, "execution")
|
||||
default:
|
||||
fmt.Printf("unknown GRPC server type: %#v\n", info.Server)
|
||||
}
|
||||
return handler(global, req)
|
||||
}
|
||||
|
||||
func handleSignals(signals chan os.Signal, server *grpc.Server) error {
|
||||
for s := range signals {
|
||||
log.G(global).WithField("signal", s).Debug("received signal")
|
||||
switch s {
|
||||
default:
|
||||
server.Stop()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -10,43 +10,20 @@ import (
|
|||
|
||||
var deleteCommand = cli.Command{
|
||||
Name: "delete",
|
||||
Usage: "delete a process from containerd store",
|
||||
Usage: "delete an existing container",
|
||||
ArgsUsage: "CONTAINER",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "pid, p",
|
||||
Usage: "process id to be deleted",
|
||||
},
|
||||
},
|
||||
Action: func(context *cli.Context) error {
|
||||
executionService, err := getExecutionService(context)
|
||||
containers, err := getExecutionService(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
id := context.Args().First()
|
||||
if id == "" {
|
||||
return errors.New("container id must be provided")
|
||||
return errors.New(" id must be provided")
|
||||
}
|
||||
|
||||
pid := uint32(context.Int64("pid"))
|
||||
if pid != 0 {
|
||||
_, err = executionService.DeleteProcess(gocontext.Background(), &execution.DeleteProcessRequest{
|
||||
ContainerID: id,
|
||||
Pid: pid,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := executionService.DeleteContainer(gocontext.Background(), &execution.DeleteContainerRequest{
|
||||
_, err = containers.Delete(gocontext.Background(), &execution.DeleteRequest{
|
||||
ID: id,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1,61 +1,47 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
gocontext "context"
|
||||
"fmt"
|
||||
"os"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/nats-io/go-nats"
|
||||
"github.com/docker/containerd/api/services/execution"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var eventsCommand = cli.Command{
|
||||
Name: "events",
|
||||
Usage: "display containerd events",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "subject, s",
|
||||
Usage: "subjects filter",
|
||||
Value: "containerd.>",
|
||||
},
|
||||
},
|
||||
Action: func(context *cli.Context) error {
|
||||
nc, err := nats.Connect(nats.DefaultURL)
|
||||
containers, err := getExecutionService(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nec, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
|
||||
if err != nil {
|
||||
nc.Close()
|
||||
return err
|
||||
}
|
||||
defer nec.Close()
|
||||
|
||||
evCh := make(chan *nats.Msg, 64)
|
||||
sub, err := nec.Subscribe(context.String("subject"), func(e *nats.Msg) {
|
||||
evCh <- e
|
||||
})
|
||||
events, err := containers.Events(gocontext.Background(), &execution.EventsRequest{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sub.Unsubscribe()
|
||||
|
||||
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
|
||||
fmt.Fprintln(w, "TYPE\tID\tPID\tEXIT_STATUS")
|
||||
for {
|
||||
e, more := <-evCh
|
||||
if !more {
|
||||
break
|
||||
}
|
||||
|
||||
var prettyJSON bytes.Buffer
|
||||
|
||||
err := json.Indent(&prettyJSON, e.Data, "", "\t")
|
||||
e, err := events.Recv()
|
||||
if err != nil {
|
||||
fmt.Println(string(e.Data))
|
||||
} else {
|
||||
fmt.Println(prettyJSON.String())
|
||||
return err
|
||||
}
|
||||
if _, err := fmt.Fprintf(w,
|
||||
"%s\t%s\t%d\t%d\n",
|
||||
e.Type.String(),
|
||||
e.ID,
|
||||
e.Pid,
|
||||
e.ExitStatus,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1,87 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
gocontext "context"
|
||||
|
||||
"github.com/docker/containerd/api/services/execution"
|
||||
"github.com/docker/containerd/api/types/process"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var execCommand = cli.Command{
|
||||
Name: "exec",
|
||||
Usage: "exec a new process in a running container",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "target container id",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "cwd, c",
|
||||
Usage: "current working directory for the process",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "tty, t",
|
||||
Usage: "create a terminal for the process",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "env, e",
|
||||
Value: &cli.StringSlice{},
|
||||
Usage: "environment variables for the process",
|
||||
},
|
||||
},
|
||||
Action: func(context *cli.Context) error {
|
||||
executionService, err := getExecutionService(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tmpDir, err := getTempDir(time.Now().Format("2006-02-01_15:04:05"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
id := context.String("id")
|
||||
sOpts := &execution.StartProcessRequest{
|
||||
ContainerID: id,
|
||||
Process: &process.Process{
|
||||
Cwd: context.String("cwd"),
|
||||
Terminal: context.Bool("tty"),
|
||||
Args: context.Args(),
|
||||
Env: context.StringSlice("env"),
|
||||
},
|
||||
Stdin: filepath.Join(tmpDir, "stdin"),
|
||||
Stdout: filepath.Join(tmpDir, "stdout"),
|
||||
Stderr: filepath.Join(tmpDir, "stderr"),
|
||||
Console: context.Bool("tty"),
|
||||
}
|
||||
|
||||
fwg, err := prepareStdio(sOpts.Stdin, sOpts.Stdout, sOpts.Stderr, sOpts.Console)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sr, err := executionService.StartProcess(gocontext.Background(), sOpts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = executionService.DeleteProcess(gocontext.Background(), &execution.DeleteProcessRequest{
|
||||
ContainerID: id,
|
||||
Pid: sr.Process.Pid,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Ensure we read all io
|
||||
fwg.Wait()
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
gocontext "context"
|
||||
"errors"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/docker/containerd/api/services/execution"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var inspectCommand = cli.Command{
|
||||
Name: "inspect",
|
||||
Usage: "inspect a container",
|
||||
ArgsUsage: "CONTAINER",
|
||||
Action: func(context *cli.Context) error {
|
||||
executionService, err := getExecutionService(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id := context.Args().First()
|
||||
if id == "" {
|
||||
return errors.New("container id must be provided")
|
||||
}
|
||||
getResponse, err := executionService.GetContainer(gocontext.Background(),
|
||||
&execution.GetContainerRequest{ID: id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
listProcResponse, err := executionService.ListProcesses(gocontext.Background(),
|
||||
&execution.ListProcessesRequest{ContainerID: id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dumper := spew.NewDefaultConfig()
|
||||
dumper.Indent = "\t"
|
||||
dumper.DisableMethods = true
|
||||
dumper.DisablePointerAddresses = true
|
||||
dumper.Dump(getResponse, listProcResponse)
|
||||
return nil
|
||||
},
|
||||
}
|
|
@ -3,6 +3,8 @@ package main
|
|||
import (
|
||||
gocontext "context"
|
||||
"fmt"
|
||||
"os"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/docker/containerd/api/services/execution"
|
||||
"github.com/urfave/cli"
|
||||
|
@ -12,29 +14,27 @@ var listCommand = cli.Command{
|
|||
Name: "list",
|
||||
Usage: "list containers",
|
||||
Action: func(context *cli.Context) error {
|
||||
executionService, err := getExecutionService(context)
|
||||
containers, err := getExecutionService(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
listResponse, err := executionService.ListContainers(gocontext.Background(), &execution.ListContainersRequest{
|
||||
Owner: []string{},
|
||||
})
|
||||
response, err := containers.List(gocontext.Background(), &execution.ListRequest{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("ID\tSTATUS\tPROCS\tBUNDLE")
|
||||
for _, c := range listResponse.Containers {
|
||||
listProcResponse, err := executionService.ListProcesses(gocontext.Background(),
|
||||
&execution.ListProcessesRequest{ContainerID: c.ID})
|
||||
if err != nil {
|
||||
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
|
||||
fmt.Fprintln(w, "ID\tPID\tSTATUS")
|
||||
for _, c := range response.Containers {
|
||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\n",
|
||||
c.ID,
|
||||
c.Pid,
|
||||
c.Status.String(),
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("%s\t%s\t%d\t%s\n",
|
||||
c.ID,
|
||||
c.State,
|
||||
len(listProcResponse.Processes),
|
||||
c.Bundle,
|
||||
)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
|
|
@ -35,11 +35,9 @@ containerd client
|
|||
}
|
||||
app.Commands = []cli.Command{
|
||||
runCommand,
|
||||
execCommand,
|
||||
eventsCommand,
|
||||
deleteCommand,
|
||||
listCommand,
|
||||
inspectCommand,
|
||||
shimCommand,
|
||||
pprofCommand,
|
||||
}
|
||||
|
|
262
cmd/ctr/run.go
262
cmd/ctr/run.go
|
@ -3,148 +3,232 @@ package main
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
gocontext "context"
|
||||
|
||||
"runtime"
|
||||
|
||||
"github.com/crosbymichael/console"
|
||||
"github.com/docker/containerd/api/services/execution"
|
||||
execEvents "github.com/docker/containerd/execution"
|
||||
"github.com/nats-io/go-nats"
|
||||
"github.com/nats-io/go-nats-streaming"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/docker/containerd/api/types/mount"
|
||||
protobuf "github.com/gogo/protobuf/types"
|
||||
"github.com/opencontainers/runtime-spec/specs-go"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var rwm = "rwm"
|
||||
|
||||
func spec(id string, args []string, tty bool) *specs.Spec {
|
||||
return &specs.Spec{
|
||||
Version: specs.Version,
|
||||
Platform: specs.Platform{
|
||||
OS: runtime.GOOS,
|
||||
Arch: runtime.GOARCH,
|
||||
},
|
||||
Root: specs.Root{
|
||||
Path: "rootfs",
|
||||
Readonly: true,
|
||||
},
|
||||
Process: specs.Process{
|
||||
Args: args,
|
||||
Env: []string{
|
||||
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
||||
},
|
||||
Terminal: tty,
|
||||
Cwd: "/",
|
||||
NoNewPrivileges: true,
|
||||
},
|
||||
Mounts: []specs.Mount{
|
||||
{
|
||||
Destination: "/proc",
|
||||
Type: "proc",
|
||||
Source: "proc",
|
||||
},
|
||||
{
|
||||
Destination: "/dev",
|
||||
Type: "tmpfs",
|
||||
Source: "tmpfs",
|
||||
Options: []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
|
||||
},
|
||||
{
|
||||
Destination: "/dev/pts",
|
||||
Type: "devpts",
|
||||
Source: "devpts",
|
||||
Options: []string{"nosuid", "noexec", "newinstance", "ptmxmode=0666", "mode=0620", "gid=5"},
|
||||
},
|
||||
{
|
||||
Destination: "/dev/shm",
|
||||
Type: "tmpfs",
|
||||
Source: "shm",
|
||||
Options: []string{"nosuid", "noexec", "nodev", "mode=1777", "size=65536k"},
|
||||
},
|
||||
{
|
||||
Destination: "/dev/mqueue",
|
||||
Type: "mqueue",
|
||||
Source: "mqueue",
|
||||
Options: []string{"nosuid", "noexec", "nodev"},
|
||||
},
|
||||
{
|
||||
Destination: "/sys",
|
||||
Type: "sysfs",
|
||||
Source: "sysfs",
|
||||
Options: []string{"nosuid", "noexec", "nodev"},
|
||||
},
|
||||
{
|
||||
Destination: "/run",
|
||||
Type: "tmpfs",
|
||||
Source: "tmpfs",
|
||||
Options: []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
|
||||
},
|
||||
{
|
||||
Destination: "/etc/resolv.conf",
|
||||
Type: "bind",
|
||||
Source: "/etc/resolv.conf",
|
||||
Options: []string{"rbind", "ro"},
|
||||
},
|
||||
{
|
||||
Destination: "/etc/hosts",
|
||||
Type: "bind",
|
||||
Source: "/etc/hosts",
|
||||
Options: []string{"rbind", "ro"},
|
||||
},
|
||||
{
|
||||
Destination: "/etc/localtime",
|
||||
Type: "bind",
|
||||
Source: "/etc/localtime",
|
||||
Options: []string{"rbind", "ro"},
|
||||
},
|
||||
},
|
||||
Hostname: id,
|
||||
Linux: &specs.Linux{
|
||||
Resources: &specs.LinuxResources{
|
||||
Devices: []specs.LinuxDeviceCgroup{
|
||||
{
|
||||
Allow: false,
|
||||
Access: &rwm,
|
||||
},
|
||||
},
|
||||
},
|
||||
Namespaces: []specs.LinuxNamespace{
|
||||
{
|
||||
Type: "pid",
|
||||
},
|
||||
{
|
||||
Type: "ipc",
|
||||
},
|
||||
{
|
||||
Type: "uts",
|
||||
},
|
||||
{
|
||||
Type: "mount",
|
||||
},
|
||||
{
|
||||
Type: "network",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var runCommand = cli.Command{
|
||||
Name: "run",
|
||||
Usage: "run a container",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "bundle, b",
|
||||
Usage: "path to the container's bundle",
|
||||
Name: "id",
|
||||
Usage: "id of the container",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "tty, t",
|
||||
Name: "tty,t",
|
||||
Usage: "allocate a TTY for the container",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "rootfs,r",
|
||||
Usage: "path to the container's root filesystem",
|
||||
},
|
||||
},
|
||||
Action: func(context *cli.Context) error {
|
||||
id := context.Args().First()
|
||||
id := context.String("id")
|
||||
if id == "" {
|
||||
return fmt.Errorf("container id must be provided")
|
||||
}
|
||||
executionService, err := getExecutionService(context)
|
||||
|
||||
containers, err := getExecutionService(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// setup our event subscriber
|
||||
sc, err := stan.Connect("containerd", "ctr", stan.ConnectWait(5*time.Second))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sc.Close()
|
||||
|
||||
evCh := make(chan *execEvents.ContainerEvent, 64)
|
||||
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
|
||||
}
|
||||
defer sub.Unsubscribe()
|
||||
|
||||
tmpDir, err := getTempDir(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
bundle, err := filepath.Abs(context.String("bundle"))
|
||||
events, err := containers.Events(gocontext.Background(), &execution.EventsRequest{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
crOpts := &execution.CreateContainerRequest{
|
||||
ID: id,
|
||||
BundlePath: bundle,
|
||||
Console: context.Bool("tty"),
|
||||
Stdin: filepath.Join(tmpDir, "stdin"),
|
||||
Stdout: filepath.Join(tmpDir, "stdout"),
|
||||
Stderr: filepath.Join(tmpDir, "stderr"),
|
||||
// for ctr right now just do a bind mount
|
||||
rootfs := []*mount.Mount{
|
||||
{
|
||||
Type: "bind",
|
||||
Source: context.String("rootfs"),
|
||||
Options: []string{
|
||||
"rw",
|
||||
"rbind",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if crOpts.Console {
|
||||
s := spec(id, []string(context.Args()), context.Bool("tty"))
|
||||
data, err := json.Marshal(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
create := &execution.CreateRequest{
|
||||
ID: id,
|
||||
Spec: &protobuf.Any{
|
||||
TypeUrl: specs.Version,
|
||||
Value: data,
|
||||
},
|
||||
Rootfs: rootfs,
|
||||
Runtime: "linux",
|
||||
Terminal: context.Bool("tty"),
|
||||
Stdin: filepath.Join(tmpDir, "stdin"),
|
||||
Stdout: filepath.Join(tmpDir, "stdout"),
|
||||
Stderr: filepath.Join(tmpDir, "stderr"),
|
||||
}
|
||||
if create.Terminal {
|
||||
con := console.Current()
|
||||
defer con.Reset()
|
||||
if err := con.SetRaw(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
fwg, err := prepareStdio(crOpts.Stdin, crOpts.Stdout, crOpts.Stderr, crOpts.Console)
|
||||
fwg, err := prepareStdio(create.Stdin, create.Stdout, create.Stderr, create.Terminal)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cr, err := executionService.CreateContainer(gocontext.Background(), crOpts)
|
||||
response, err := containers.Create(gocontext.Background(), create)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "CreateContainer RPC failed")
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := executionService.StartContainer(gocontext.Background(), &execution.StartContainerRequest{
|
||||
ID: cr.Container.ID,
|
||||
if _, err := containers.Start(gocontext.Background(), &execution.StartRequest{
|
||||
ID: response.ID,
|
||||
}); err != nil {
|
||||
return errors.Wrap(err, "StartContainer RPC failed")
|
||||
return err
|
||||
}
|
||||
|
||||
var ec uint32
|
||||
eventLoop:
|
||||
for {
|
||||
select {
|
||||
case e, more := <-evCh:
|
||||
if !more {
|
||||
break eventLoop
|
||||
}
|
||||
|
||||
if e.Type != "exit" {
|
||||
continue
|
||||
}
|
||||
|
||||
if e.ID == cr.Container.ID && e.Pid == cr.InitProcess.Pid {
|
||||
ec = e.ExitStatus
|
||||
break eventLoop
|
||||
}
|
||||
case <-time.After(1 * time.Second):
|
||||
if sc.NatsConn().Status() != nats.CONNECTED {
|
||||
break eventLoop
|
||||
}
|
||||
}
|
||||
status, err := waitContainer(events, response)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := executionService.DeleteContainer(gocontext.Background(), &execution.DeleteContainerRequest{
|
||||
ID: cr.Container.ID,
|
||||
if _, err := containers.Delete(gocontext.Background(), &execution.DeleteRequest{
|
||||
ID: response.ID,
|
||||
}); err != nil {
|
||||
return errors.Wrap(err, "DeleteContainer RPC failed")
|
||||
return err
|
||||
}
|
||||
|
||||
// Ensure we read all io
|
||||
fwg.Wait()
|
||||
|
||||
if ec != 0 {
|
||||
return cli.NewExitError("", int(ec))
|
||||
if status != 0 {
|
||||
return cli.NewExitError("", int(status))
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import (
|
|||
gocontext "context"
|
||||
|
||||
"github.com/docker/containerd/api/services/execution"
|
||||
"github.com/docker/containerd/api/types/container"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tonistiigi/fifo"
|
||||
"github.com/urfave/cli"
|
||||
|
@ -103,12 +104,12 @@ func getGRPCConnection(context *cli.Context) (*grpc.ClientConn, error) {
|
|||
return grpcConn, nil
|
||||
}
|
||||
|
||||
func getExecutionService(context *cli.Context) (execution.ExecutionServiceClient, error) {
|
||||
func getExecutionService(context *cli.Context) (execution.ContainerServiceClient, error) {
|
||||
conn, err := getGRPCConnection(context)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return execution.NewExecutionServiceClient(conn), nil
|
||||
return execution.NewContainerServiceClient(conn), nil
|
||||
}
|
||||
|
||||
func getTempDir(id string) (string, error) {
|
||||
|
@ -122,3 +123,19 @@ func getTempDir(id string) (string, error) {
|
|||
}
|
||||
return tmpDir, nil
|
||||
}
|
||||
|
||||
func waitContainer(events execution.ContainerService_EventsClient, respose *execution.CreateResponse) (uint32, error) {
|
||||
for {
|
||||
e, err := events.Recv()
|
||||
if err != nil {
|
||||
return 255, err
|
||||
}
|
||||
if e.Type != container.Event_EXIT {
|
||||
continue
|
||||
}
|
||||
if e.ID == respose.ID &&
|
||||
e.Pid == respose.Pid {
|
||||
return e.ExitStatus, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ var (
|
|||
// becomes the M declarations at the end of the declaration.
|
||||
packageMap = map[string]string{
|
||||
"google/protobuf/timestamp.proto": "github.com/gogo/protobuf/types",
|
||||
"google/protobuf/any.proto": "github.com/gogo/protobuf/types",
|
||||
"google/protobuf/descriptor.proto": "github.com/gogo/protobuf/protoc-gen-gogo/descriptor",
|
||||
"gogoproto/gogo.proto": "github.com/gogo/protobuf/gogoproto",
|
||||
}
|
||||
|
@ -148,8 +149,9 @@ func main() {
|
|||
// pass to sh -c so we don't need to re-split here.
|
||||
args := []string{"-c", arg}
|
||||
cmd := exec.Command("sh", args...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
log.Fatalln(err)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Fatalf("%s %s\n", out, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue