2016-11-30 00:52:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
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"
|
2016-11-30 00:52:04 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
type runConfig struct {
|
|
|
|
Image string `toml:"image"`
|
|
|
|
Process struct {
|
|
|
|
Args []string `toml:"args"`
|
|
|
|
Env []string `toml:"env"`
|
|
|
|
Cwd string `toml:"cwd"`
|
|
|
|
Uid int `toml:"uid"`
|
|
|
|
Gid int `toml:"gid"`
|
|
|
|
Tty bool `toml:"tty"`
|
|
|
|
} `toml:"process"`
|
|
|
|
Network struct {
|
|
|
|
Type string `toml:"type"`
|
|
|
|
IP string `toml:"ip"`
|
|
|
|
Gateway string `toml:"gateway"`
|
|
|
|
} `toml:"network"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
// var config runConfig
|
|
|
|
// if _, err := toml.DecodeFile(context.Args().First(), &config); err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
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
|
|
|
|
nc, err := nats.Connect(nats.DefaultURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nec, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
|
|
|
|
if err != nil {
|
|
|
|
nc.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer nec.Close()
|
|
|
|
|
|
|
|
evCh := make(chan *execEvents.ContainerExitEvent, 64)
|
|
|
|
sub, err := nec.Subscribe(execEvents.ContainersEventsSubjectSubscriber, func(e *execEvents.ContainerExitEvent) {
|
|
|
|
evCh <- e
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
|
|
|
|
crOpts := &execution.CreateContainerRequest{
|
2016-12-06 00:36:15 +00:00
|
|
|
ID: id,
|
|
|
|
BundlePath: context.String("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"),
|
|
|
|
}
|
|
|
|
|
|
|
|
fwg, err := prepareStdio(crOpts.Stdin, crOpts.Stdout, crOpts.Stderr)
|
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
|
|
|
|
|
|
|
cr, err := executionService.Create(gocontext.Background(), crOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-07 22:00:20 +00:00
|
|
|
if _, err := executionService.Start(gocontext.Background(), &execution.StartContainerRequest{
|
2016-12-06 00:36:15 +00:00
|
|
|
ID: cr.Container.ID,
|
|
|
|
}); err != nil {
|
2016-11-30 00:52:04 +00:00
|
|
|
return err
|
|
|
|
}
|
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 {
|
|
|
|
fmt.Println("No More!")
|
|
|
|
break eventLoop
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.ID == cr.Container.ID && e.PID == cr.InitProcess.ID {
|
|
|
|
ec = e.StatusCode
|
|
|
|
break eventLoop
|
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
if nec.Conn.Status() != nats.CONNECTED {
|
|
|
|
break eventLoop
|
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 00:36:15 +00:00
|
|
|
if _, err := executionService.Delete(gocontext.Background(), &execution.DeleteContainerRequest{
|
|
|
|
ID: cr.Container.ID,
|
|
|
|
}); err != nil {
|
2016-11-30 00:52:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-12-07 18:44:05 +00:00
|
|
|
|
|
|
|
// Ensure we read all io
|
|
|
|
fwg.Wait()
|
|
|
|
|
2016-12-11 19:07:32 +00:00
|
|
|
os.Exit(int(ec))
|
|
|
|
|
2016-11-30 00:52:04 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|