Skip to content

Commit

Permalink
Merge pull request #1298 from miaoyq/add-start-cmd
Browse files Browse the repository at this point in the history
Add `start` subcommand in `ctr`.
  • Loading branch information
crosbymichael committed Aug 7, 2017
2 parents 60f7eee + ac72bba commit b20fd92
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/ctr/main.go
Expand Up @@ -87,6 +87,7 @@ containerd CLI
rootfsCommand,
runCommand,
snapshotCommand,
startCommand,
taskListCommand,
versionCommand,
}, extraCmds...)
Expand Down
87 changes: 87 additions & 0 deletions cmd/ctr/start.go
@@ -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
},
}

0 comments on commit b20fd92

Please sign in to comment.