2016-09-28 22:09:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-09-28 22:27:24 +00:00
|
|
|
"fmt"
|
2016-09-28 22:09:22 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/containerkit"
|
2016-09-30 17:39:26 +00:00
|
|
|
"github.com/docker/containerkit/oci"
|
2016-09-28 22:09:22 +00:00
|
|
|
"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-09-28 22:09:22 +00:00
|
|
|
// create a new runc runtime that implements the ExecutionDriver interface
|
2016-10-03 22:48:39 +00:00
|
|
|
runc, err := oci.New(oci.Opts{
|
2016-10-03 22:20:45 +00:00
|
|
|
Root: "/run/runc",
|
|
|
|
Name: "runc",
|
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-03 22:48:39 +00:00
|
|
|
container, err := containerkit.NewContainer(dockerContainer, NewBindDriver(), runc)
|
2016-09-28 22:09:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// setup some stdio for our container
|
|
|
|
container.Stdin = os.Stdin
|
|
|
|
container.Stdout = os.Stdout
|
|
|
|
container.Stderr = os.Stderr
|
|
|
|
|
|
|
|
// 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-09-28 22:33:38 +00:00
|
|
|
// start 10 exec processes giving the go var i to exec to stdout
|
2016-09-28 22:27:24 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
process, err := container.NewProcess(&specs.Process{
|
|
|
|
Args: []string{
|
|
|
|
"echo", fmt.Sprintf("sup from itteration %d", i),
|
|
|
|
},
|
2016-09-28 22:33:38 +00:00
|
|
|
Env: env,
|
2016-09-28 22:27:24 +00:00
|
|
|
Terminal: false,
|
|
|
|
Cwd: "/",
|
|
|
|
NoNewPrivileges: true,
|
2016-09-28 22:33:38 +00:00
|
|
|
Capabilities: caps,
|
2016-09-28 22:27:24 +00:00
|
|
|
})
|
2016-09-28 22:33:38 +00:00
|
|
|
|
2016-09-28 22:27:24 +00:00
|
|
|
process.Stdin = os.Stdin
|
|
|
|
process.Stdout = os.Stdout
|
|
|
|
process.Stderr = os.Stderr
|
2016-09-28 22:33:38 +00:00
|
|
|
|
2016-09-28 22:27:24 +00:00
|
|
|
if err := process.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-28 22:33:38 +00:00
|
|
|
|
2016-09-28 22:27:24 +00:00
|
|
|
procStatus, err := process.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("process %d returned with %d", i, procStatus)
|
|
|
|
}
|
|
|
|
|
2016-10-03 22:48:39 +00:00
|
|
|
container, err = containerkit.LoadContainer(dockerContainer, runc)
|
2016-09-30 20:51:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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-09-28 22:33:38 +00:00
|
|
|
// "Hooks do optional work. Drivers do mandatory work"
|
|
|
|
func main() {
|
|
|
|
if err := osutils.SetSubreaper(1); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := runContainer(); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|