Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1298 from miaoyq/add-start-cmd
Add `start` subcommand in `ctr`.
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}, | ||
} |