Add working shim exec driver for start
Still need to implement a working Wait() on the process using epoll because of the non-blocking exit fifo Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
c76f883ccd
commit
90e4f130c8
6 changed files with 223 additions and 76 deletions
|
@ -1,21 +1,26 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/docker/containerd/shim"
|
||||
"github.com/docker/containerkit"
|
||||
"github.com/docker/containerkit/oci"
|
||||
"github.com/docker/containerkit/osutils"
|
||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||
)
|
||||
|
||||
func runContainer() error {
|
||||
// create a new runc runtime that implements the ExecutionDriver interface
|
||||
runc, err := oci.New(oci.Opts{
|
||||
Root: "/run/runc",
|
||||
Name: "runc",
|
||||
// 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,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -23,14 +28,14 @@ func runContainer() error {
|
|||
dockerContainer := &testConfig{}
|
||||
|
||||
// create a new container
|
||||
container, err := containerkit.NewContainer(dockerContainer, NewBindDriver(), runc)
|
||||
container, err := containerkit.NewContainer(dockerContainer, NewBindDriver(), runtime)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// setup some stdio for our container
|
||||
container.Stdin = os.Stdin
|
||||
container.Stdout = os.Stdout
|
||||
container.Stderr = os.Stderr
|
||||
container.Stdin = Stdin()
|
||||
container.Stdout = Stdout()
|
||||
container.Stderr = Stderr()
|
||||
|
||||
// go ahead and set the container in the create state and have it ready to start
|
||||
logrus.Info("create container")
|
||||
|
@ -44,37 +49,39 @@ func runContainer() error {
|
|||
return err
|
||||
}
|
||||
|
||||
// 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,
|
||||
})
|
||||
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,
|
||||
})
|
||||
|
||||
process.Stdin = os.Stdin
|
||||
process.Stdout = os.Stdout
|
||||
process.Stderr = os.Stderr
|
||||
process.Stdin = os.Stdin
|
||||
process.Stdout = os.Stdout
|
||||
process.Stderr = os.Stderr
|
||||
|
||||
if err := process.Start(); err != nil {
|
||||
return err
|
||||
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)
|
||||
}
|
||||
|
||||
procStatus, err := process.Wait()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("process %d returned with %d", i, procStatus)
|
||||
}
|
||||
|
||||
container, err = containerkit.LoadContainer(dockerContainer, runc)
|
||||
if err != nil {
|
||||
return err
|
||||
if load {
|
||||
if container, err = containerkit.LoadContainer(dockerContainer, runtime); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// wait for it to exit and get the exit status
|
||||
|
@ -93,8 +100,16 @@ func runContainer() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
exec bool
|
||||
load bool
|
||||
)
|
||||
|
||||
// "Hooks do optional work. Drivers do mandatory work"
|
||||
func main() {
|
||||
flag.BoolVar(&exec, "exec", false, "run the execs")
|
||||
flag.BoolVar(&load, "load", false, "reload the container")
|
||||
flag.Parse()
|
||||
if err := osutils.SetSubreaper(1); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"github.com/docker/containerkit"
|
||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||
|
@ -177,3 +181,48 @@ func (t *testConfig) Spec(m *containerkit.Mount) (*specs.Spec, error) {
|
|||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Stdin() *os.File {
|
||||
abs, err := filepath.Abs("stdin")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := unix.Mkfifo(abs, 0755); err != nil && !os.IsExist(err) {
|
||||
panic(err)
|
||||
}
|
||||
f, err := os.OpenFile(abs, syscall.O_RDWR, 0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func Stdout() *os.File {
|
||||
abs, err := filepath.Abs("stdout")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := unix.Mkfifo(abs, 0755); err != nil && !os.IsExist(err) {
|
||||
panic(err)
|
||||
}
|
||||
f, err := os.OpenFile(abs, syscall.O_RDWR, 0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func Stderr() *os.File {
|
||||
abs, err := filepath.Abs("stderr")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := unix.Mkfifo(abs, 0755); err != nil && !os.IsExist(err) {
|
||||
panic(err)
|
||||
}
|
||||
f, err := os.OpenFile(abs, syscall.O_RDWR, 0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue