Add start
subcommand in ctr
.
Signed-off-by: Yanqiang Miao <miao.yanqiang@zte.com.cn>
This commit is contained in:
parent
0fa76584f8
commit
ac72bbab26
@ -81,6 +81,7 @@ containerd CLI
|
||||
rootfsCommand,
|
||||
runCommand,
|
||||
snapshotCommand,
|
||||
startCommand,
|
||||
taskListCommand,
|
||||
versionCommand,
|
||||
}, extraCmds...)
|
||||
|
87
cmd/ctr/start.go
Normal file
87
cmd/ctr/start.go
Normal file
@ -0,0 +1,87 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/containerd/console"
|
||||
"github.com/opencontainers/go-digest"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var startCommand = cli.Command{
|
||||
Name: "start",
|
||||
Usage: "start a container that have been created",
|
||||
ArgsUsage: "CONTAINER",
|
||||
Action: func(context *cli.Context) error {
|
||||
var (
|
||||
err error
|
||||
|
||||
ctx, cancel = appContext(context)
|
||||
id = context.Args().Get(0)
|
||||
)
|
||||
|
||||
defer cancel()
|
||||
|
||||
if id == "" {
|
||||
return errors.New("container id must be provided")
|
||||
}
|
||||
client, err := newClient(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
container, err := client.LoadContainer(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
spec, err := container.Spec()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tty := spec.Process.Terminal
|
||||
|
||||
task, err := newTask(ctx, container, digest.Digest(""), tty)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer task.Delete(ctx)
|
||||
|
||||
statusC := make(chan uint32, 1)
|
||||
go func() {
|
||||
status, err := task.Wait(ctx)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("wait process")
|
||||
}
|
||||
statusC <- status
|
||||
}()
|
||||
var con console.Console
|
||||
if tty {
|
||||
con = console.Current()
|
||||
defer con.Reset()
|
||||
if err := con.SetRaw(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := task.Start(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if tty {
|
||||
if err := handleConsoleResize(ctx, task, con); err != nil {
|
||||
logrus.WithError(err).Error("console resize")
|
||||
}
|
||||
} else {
|
||||
sigc := forwardAllSignals(ctx, task)
|
||||
defer stopCatch(sigc)
|
||||
}
|
||||
|
||||
status := <-statusC
|
||||
if _, err := task.Delete(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if status != 0 {
|
||||
return cli.NewExitError("", int(status))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
Loading…
Reference in New Issue
Block a user