Merge pull request #1654 from crosbymichael/ctr-daemon

[ctr] add --null-io and --detach
This commit is contained in:
Michael Crosby 2017-10-23 10:40:57 -04:00 committed by GitHub
commit 313bab3dd6
4 changed files with 35 additions and 12 deletions

View File

@ -92,6 +92,14 @@ var runCommand = cli.Command{
Name: "cwd", Name: "cwd",
Usage: "specify the working directory of the process", Usage: "specify the working directory of the process",
}, },
cli.BoolFlag{
Name: "null-io",
Usage: "send all IO to /dev/null",
},
cli.BoolFlag{
Name: "detach,d",
Usage: "detach from the task after it has started execution",
},
}, snapshotterFlags...), }, snapshotterFlags...),
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
var ( var (
@ -101,6 +109,7 @@ var runCommand = cli.Command{
id = context.Args().Get(1) id = context.Args().Get(1)
imageRef = context.Args().First() imageRef = context.Args().First()
tty = context.Bool("tty") tty = context.Bool("tty")
detach = context.Bool("detach")
) )
defer cancel() defer cancel()
@ -118,20 +127,20 @@ var runCommand = cli.Command{
if err != nil { if err != nil {
return err return err
} }
if context.Bool("rm") { if context.Bool("rm") && !detach {
defer container.Delete(ctx, containerd.WithSnapshotCleanup) defer container.Delete(ctx, containerd.WithSnapshotCleanup)
} }
task, err := newTask(ctx, client, container, context.String("checkpoint"), tty) task, err := newTask(ctx, client, container, context.String("checkpoint"), tty, context.Bool("null-io"))
if err != nil { if err != nil {
return err return err
} }
defer task.Delete(ctx) var statusC <-chan containerd.ExitStatus
if !detach {
statusC, err := task.Wait(ctx) defer task.Delete(ctx)
if err != nil { if statusC, err = task.Wait(ctx); err != nil {
return err return err
}
} }
var con console.Console var con console.Console
if tty { if tty {
con = console.Current() con = console.Current()
@ -143,6 +152,9 @@ var runCommand = cli.Command{
if err := task.Start(ctx); err != nil { if err := task.Start(ctx); err != nil {
return err return err
} }
if detach {
return nil
}
if tty { if tty {
if err := handleConsoleResize(ctx, task, con); err != nil { if err := handleConsoleResize(ctx, task, con); err != nil {
logrus.WithError(err).Error("console resize") logrus.WithError(err).Error("console resize")
@ -151,7 +163,6 @@ var runCommand = cli.Command{
sigc := forwardAllSignals(ctx, task) sigc := forwardAllSignals(ctx, task)
defer stopCatch(sigc) defer stopCatch(sigc)
} }
status := <-statusC status := <-statusC
code, _, err := status.Result() code, _, err := status.Result()
if err != nil { if err != nil {

View File

@ -114,12 +114,15 @@ func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli
return client.NewContainer(ctx, id, cOpts...) return client.NewContainer(ctx, id, cOpts...)
} }
func newTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, checkpoint string, tty bool) (containerd.Task, error) { func newTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, checkpoint string, tty, nullIO bool) (containerd.Task, error) {
if checkpoint == "" { if checkpoint == "" {
io := containerd.Stdio io := containerd.Stdio
if tty { if tty {
io = containerd.StdioTerminal io = containerd.StdioTerminal
} }
if nullIO {
io = containerd.NullIO
}
return container.NewTask(ctx, io) return container.NewTask(ctx, io)
} }
im, err := client.GetImage(ctx, checkpoint) im, err := client.GetImage(ctx, checkpoint)

View File

@ -117,10 +117,13 @@ func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli
) )
} }
func newTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, _ string, tty bool) (containerd.Task, error) { func newTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, _ string, tty, nullIO bool) (containerd.Task, error) {
io := containerd.Stdio io := containerd.Stdio
if tty { if tty {
io = containerd.StdioTerminal io = containerd.StdioTerminal
} }
if nullIO {
io = containerd.NullIO
}
return container.NewTask(ctx, io) return container.NewTask(ctx, io)
} }

View File

@ -11,6 +11,12 @@ var taskStartCommand = cli.Command{
Name: "start", Name: "start",
Usage: "start a container that have been created", Usage: "start a container that have been created",
ArgsUsage: "CONTAINER", ArgsUsage: "CONTAINER",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "null-io",
Usage: "send all IO to /dev/null",
},
},
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
var ( var (
err error err error
@ -40,7 +46,7 @@ var taskStartCommand = cli.Command{
tty := spec.Process.Terminal tty := spec.Process.Terminal
task, err := newTask(ctx, client, container, "", tty) task, err := newTask(ctx, client, container, "", tty, context.Bool("null-io"))
if err != nil { if err != nil {
return err return err
} }