2016-09-28 22:09:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-10-06 18:39:12 +00:00
|
|
|
"flag"
|
2016-09-28 22:27:24 +00:00
|
|
|
"fmt"
|
2016-09-28 22:09:22 +00:00
|
|
|
"os"
|
2016-10-06 18:39:12 +00:00
|
|
|
"time"
|
2016-09-28 22:09:22 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-10-06 18:39:12 +00:00
|
|
|
"github.com/docker/containerd/shim"
|
2016-09-28 22:09:22 +00:00
|
|
|
"github.com/docker/containerkit"
|
|
|
|
"github.com/docker/containerkit/osutils"
|
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
)
|
|
|
|
|
2016-09-28 22:33:38 +00:00
|
|
|
func runContainer() error {
|
2016-10-06 18:39:12 +00:00
|
|
|
// create a new runtime runtime that implements the ExecutionDriver interface
|
|
|
|
runtime, err := shim.New(shim.Opts{
|
|
|
|
Root: "/run/cshim/test",
|
|
|
|
Name: "containerd-shim",
|
|
|
|
RuntimeName: "runc",
|
|
|
|
RuntimeRoot: "/run/runc",
|
|
|
|
Timeout: 5 * time.Second,
|
2016-09-30 20:51:10 +00:00
|
|
|
})
|
2016-09-28 22:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-03 22:48:39 +00:00
|
|
|
dockerContainer := &testConfig{}
|
|
|
|
|
2016-09-28 22:09:22 +00:00
|
|
|
// create a new container
|
2016-10-06 18:39:12 +00:00
|
|
|
container, err := containerkit.NewContainer(dockerContainer, NewBindDriver(), runtime)
|
2016-09-28 22:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// setup some stdio for our container
|
2016-10-06 18:39:12 +00:00
|
|
|
container.Stdin = Stdin()
|
|
|
|
container.Stdout = Stdout()
|
|
|
|
container.Stderr = Stderr()
|
2016-09-28 22:09:22 +00:00
|
|
|
|
|
|
|
// go ahead and set the container in the create state and have it ready to start
|
|
|
|
logrus.Info("create container")
|
|
|
|
if err := container.Create(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// start the user defined process in the container
|
|
|
|
logrus.Info("start container")
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-06 18:39:12 +00:00
|
|
|
if exec {
|
|
|
|
// start 10 exec processes giving the go var i to exec to stdout
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
process, err := container.NewProcess(&specs.Process{
|
|
|
|
Args: []string{
|
|
|
|
"echo", fmt.Sprintf("sup from itteration %d", i),
|
|
|
|
},
|
|
|
|
Env: env,
|
|
|
|
Terminal: false,
|
|
|
|
Cwd: "/",
|
|
|
|
NoNewPrivileges: true,
|
|
|
|
Capabilities: caps,
|
|
|
|
})
|
2016-09-28 22:33:38 +00:00
|
|
|
|
2016-10-06 18:39:12 +00:00
|
|
|
process.Stdin = os.Stdin
|
|
|
|
process.Stdout = os.Stdout
|
|
|
|
process.Stderr = os.Stderr
|
2016-09-28 22:33:38 +00:00
|
|
|
|
2016-10-06 18:39:12 +00:00
|
|
|
if err := process.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
procStatus, err := process.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("process %d returned with %d", i, procStatus)
|
2016-09-28 22:27:24 +00:00
|
|
|
}
|
2016-10-06 18:39:12 +00:00
|
|
|
}
|
2016-09-28 22:33:38 +00:00
|
|
|
|
2016-10-06 18:39:12 +00:00
|
|
|
if load {
|
|
|
|
if container, err = containerkit.LoadContainer(dockerContainer, runtime); err != nil {
|
2016-09-28 22:27:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-09-30 20:51:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 22:09:22 +00:00
|
|
|
// wait for it to exit and get the exit status
|
|
|
|
logrus.Info("wait container")
|
|
|
|
status, err := container.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the container after it is done
|
|
|
|
logrus.Info("delete container")
|
|
|
|
if container.Delete(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("exit status %d", status)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-06 18:39:12 +00:00
|
|
|
var (
|
|
|
|
exec bool
|
|
|
|
load bool
|
|
|
|
)
|
|
|
|
|
2016-09-28 22:33:38 +00:00
|
|
|
// "Hooks do optional work. Drivers do mandatory work"
|
|
|
|
func main() {
|
2016-10-06 18:39:12 +00:00
|
|
|
flag.BoolVar(&exec, "exec", false, "run the execs")
|
|
|
|
flag.BoolVar(&load, "load", false, "reload the container")
|
|
|
|
flag.Parse()
|
2016-09-28 22:33:38 +00:00
|
|
|
if err := osutils.SetSubreaper(1); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := runContainer(); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|