2016-11-30 00:52:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-06 22:57:43 +00:00
|
|
|
"encoding/json"
|
2016-11-30 00:52:04 +00:00
|
|
|
"fmt"
|
2016-12-07 18:44:05 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-12-15 18:40:24 +00:00
|
|
|
"time"
|
2016-12-07 18:44:05 +00:00
|
|
|
|
2016-12-06 00:36:15 +00:00
|
|
|
gocontext "context"
|
2016-11-30 00:52:04 +00:00
|
|
|
|
2017-02-06 22:57:43 +00:00
|
|
|
"github.com/crosbymichael/console"
|
2016-12-06 00:36:15 +00:00
|
|
|
"github.com/docker/containerd/api/execution"
|
2016-12-11 19:07:32 +00:00
|
|
|
execEvents "github.com/docker/containerd/execution"
|
|
|
|
"github.com/nats-io/go-nats"
|
2017-02-06 22:57:43 +00:00
|
|
|
"github.com/nats-io/go-nats-streaming"
|
|
|
|
"github.com/pkg/errors"
|
2016-11-30 00:52:04 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var runCommand = cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "run a container",
|
2016-12-06 00:36:15 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "bundle, b",
|
|
|
|
Usage: "path to the container's bundle",
|
|
|
|
},
|
2016-12-09 17:17:34 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "tty, t",
|
|
|
|
Usage: "allocate a TTY for the container",
|
|
|
|
},
|
2016-12-06 00:36:15 +00:00
|
|
|
},
|
2016-11-30 00:52:04 +00:00
|
|
|
Action: func(context *cli.Context) error {
|
2016-12-07 18:44:05 +00:00
|
|
|
id := context.Args().First()
|
2016-11-30 00:52:04 +00:00
|
|
|
if id == "" {
|
2016-12-06 00:36:15 +00:00
|
|
|
return fmt.Errorf("container id must be provided")
|
2016-11-30 00:52:04 +00:00
|
|
|
}
|
2016-12-06 00:36:15 +00:00
|
|
|
executionService, err := getExecutionService(context)
|
2016-11-30 00:52:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
|
2016-12-11 19:07:32 +00:00
|
|
|
// setup our event subscriber
|
2017-02-06 22:57:43 +00:00
|
|
|
sc, err := stan.Connect("containerd", "ctr", stan.ConnectWait(5*time.Second))
|
2016-12-11 19:07:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-06 22:57:43 +00:00
|
|
|
defer sc.Close()
|
2016-12-11 19:07:32 +00:00
|
|
|
|
2017-02-06 16:46:37 +00:00
|
|
|
evCh := make(chan *execEvents.ContainerEvent, 64)
|
2017-02-06 22:57:43 +00:00
|
|
|
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
|
2016-12-11 19:07:32 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
2016-12-07 18:44:05 +00:00
|
|
|
tmpDir, err := getTempDir(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2017-01-12 18:23:29 +00:00
|
|
|
bundle, err := filepath.Abs(context.String("bundle"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
crOpts := &execution.CreateContainerRequest{
|
2016-12-06 00:36:15 +00:00
|
|
|
ID: id,
|
2017-01-12 18:23:29 +00:00
|
|
|
BundlePath: bundle,
|
2016-12-09 17:17:34 +00:00
|
|
|
Console: context.Bool("tty"),
|
2016-12-07 18:44:05 +00:00
|
|
|
Stdin: filepath.Join(tmpDir, "stdin"),
|
|
|
|
Stdout: filepath.Join(tmpDir, "stdout"),
|
|
|
|
Stderr: filepath.Join(tmpDir, "stderr"),
|
|
|
|
}
|
|
|
|
|
2017-01-12 19:37:03 +00:00
|
|
|
if crOpts.Console {
|
2017-02-06 22:57:43 +00:00
|
|
|
con := console.Current()
|
|
|
|
defer con.Reset()
|
|
|
|
if err := con.SetRaw(); err != nil {
|
2017-01-12 19:37:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 18:24:22 +00:00
|
|
|
fwg, err := prepareStdio(crOpts.Stdin, crOpts.Stdout, crOpts.Stderr, crOpts.Console)
|
2016-12-06 00:36:15 +00:00
|
|
|
if err != nil {
|
2016-11-30 00:52:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
|
2017-02-02 22:40:23 +00:00
|
|
|
cr, err := executionService.CreateContainer(gocontext.Background(), crOpts)
|
2016-12-07 18:44:05 +00:00
|
|
|
if err != nil {
|
2017-02-06 22:57:43 +00:00
|
|
|
return errors.Wrap(err, "CreateContainer RPC failed")
|
2016-12-07 18:44:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 22:40:23 +00:00
|
|
|
if _, err := executionService.StartContainer(gocontext.Background(), &execution.StartContainerRequest{
|
2016-12-06 00:36:15 +00:00
|
|
|
ID: cr.Container.ID,
|
|
|
|
}); err != nil {
|
2017-02-06 22:57:43 +00:00
|
|
|
return errors.Wrap(err, "StartContainer RPC failed")
|
2016-11-30 00:52:04 +00:00
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
|
2016-12-11 19:07:32 +00:00
|
|
|
var ec uint32
|
2016-12-15 18:40:24 +00:00
|
|
|
eventLoop:
|
2016-12-07 18:44:05 +00:00
|
|
|
for {
|
2016-12-15 18:40:24 +00:00
|
|
|
select {
|
|
|
|
case e, more := <-evCh:
|
|
|
|
if !more {
|
|
|
|
break eventLoop
|
|
|
|
}
|
|
|
|
|
2017-02-06 16:46:37 +00:00
|
|
|
if e.ID == cr.Container.ID && e.Pid == cr.InitProcess.Pid {
|
|
|
|
ec = e.ExitStatus
|
2016-12-15 18:40:24 +00:00
|
|
|
break eventLoop
|
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
2017-02-06 22:57:43 +00:00
|
|
|
if sc.NatsConn().Status() != nats.CONNECTED {
|
2016-12-15 18:40:24 +00:00
|
|
|
break eventLoop
|
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 22:40:23 +00:00
|
|
|
if _, err := executionService.DeleteContainer(gocontext.Background(), &execution.DeleteContainerRequest{
|
2016-12-06 00:36:15 +00:00
|
|
|
ID: cr.Container.ID,
|
|
|
|
}); err != nil {
|
2017-02-06 22:57:43 +00:00
|
|
|
return errors.Wrap(err, "DeleteContainer RPC failed")
|
2016-11-30 00:52:04 +00:00
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
|
|
|
|
// Ensure we read all io
|
|
|
|
fwg.Wait()
|
|
|
|
|
2017-02-06 22:57:43 +00:00
|
|
|
if ec != 0 {
|
|
|
|
return cli.NewExitError("", int(ec))
|
|
|
|
}
|
2016-12-11 19:07:32 +00:00
|
|
|
|
2016-11-30 00:52:04 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|