Add event support to execution subsystem

The implementation relies on nats.io

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2016-12-11 11:07:32 -08:00
parent dd39b4dcf0
commit 2ef399b315
11 changed files with 441 additions and 125 deletions

View file

@ -0,0 +1,61 @@
package oci
import (
"io"
"os"
"github.com/crosbymichael/go-runc"
)
type OIO struct {
master *os.File // master holds a fd to the created pty if any
console string // console holds the path the the slave linked to master
rio runc.IO // rio holds the open fifos for stdios
}
func newOIO(stdin, stdout, stderr string, console bool) (o OIO, err error) {
defer func() {
if err != nil {
o.cleanup()
}
}()
if o.rio.Stdin, err = os.OpenFile(stdin, os.O_RDONLY, 0); err != nil {
return
}
if o.rio.Stdout, err = os.OpenFile(stdout, os.O_WRONLY, 0); err != nil {
return
}
if o.rio.Stderr, err = os.OpenFile(stderr, os.O_WRONLY, 0); err != nil {
return
}
if console {
o.master, o.console, err = newConsole(0, 0)
if err != nil {
return
}
go io.Copy(o.master, o.rio.Stdin)
go func() {
io.Copy(o.rio.Stdout, o.master)
o.master.Close()
}()
}
return
}
func (o OIO) cleanup() {
if o.master != nil {
o.master.Close()
}
if o.rio.Stdin != nil {
o.rio.Stdin.(*os.File).Close()
}
if o.rio.Stdout != nil {
o.rio.Stdout.(*os.File).Close()
}
if o.rio.Stderr != nil {
o.rio.Stderr.(*os.File).Close()
}
}

View file

@ -14,23 +14,24 @@ import (
var ErrRootEmpty = errors.New("oci: runtime root cannot be an empty string")
func New(root string) *OCIRuntime {
func New(root string) (*OCIRuntime, error) {
err := SetSubreaper(1)
if err != nil {
return nil, err
}
return &OCIRuntime{
root: root,
runc: &runc.Runc{
Root: filepath.Join(root, "runc"),
},
ios: make(map[string]OIO),
}
}, nil
}
type OCIRuntime struct {
// root holds runtime state information for the containers
root string
runc *runc.Runc
// We need to keep track of the created IO for
ios map[string]OIO
ios map[string]OIO // ios tracks created process io for cleanup purpose on delete
}
func (r *OCIRuntime) Create(ctx context.Context, id string, o execution.CreateOpts) (container *execution.Container, err error) {
@ -44,7 +45,7 @@ func (r *OCIRuntime) Create(ctx context.Context, id string, o execution.CreateOp
}
}()
if container, err = execution.NewContainer(r.root, id, o.Bundle, "created"); err != nil {
if container, err = execution.NewContainer(r.root, id, o.Bundle); err != nil {
return nil, err
}
defer func(c *execution.Container) {
@ -141,11 +142,16 @@ func (r *OCIRuntime) List(ctx context.Context) ([]*execution.Container, error) {
var containers []*execution.Container
for _, c := range runcCs {
container, err := r.load(c)
if err != nil {
return nil, err
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
container, err := r.load(c)
if err != nil {
return nil, err
}
containers = append(containers, container)
}
containers = append(containers, container)
}
return containers, nil

View file

@ -0,0 +1,43 @@
package oci
import (
"syscall"
"unsafe"
)
// PR_SET_CHILD_SUBREAPER allows setting the child subreaper.
// If arg2 is nonzero, set the "child subreaper" attribute of the
// calling process; if arg2 is zero, unset the attribute. When a
// process is marked as a child subreaper, all of the children
// that it creates, and their descendants, will be marked as
// having a subreaper. In effect, a subreaper fulfills the role
// of init(1) for its descendant processes. Upon termination of
// a process that is orphaned (i.e., its immediate parent has
// already terminated) and marked as having a subreaper, the
// nearest still living ancestor subreaper will receive a SIGCHLD
// signal and be able to wait(2) on the process to discover its
// termination status.
const prSetChildSubreaper = 36
// PR_GET_CHILD_SUBREAPER allows retrieving the current child
// subreaper.
// Return the "child subreaper" setting of the caller, in the
// location pointed to by (int *) arg2.
const prGetChildSubreaper = 37
// GetSubreaper returns the subreaper setting for the calling process
func GetSubreaper() (int, error) {
var i uintptr
if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, prGetChildSubreaper, uintptr(unsafe.Pointer(&i)), 0); err != 0 {
return -1, err
}
return int(i), nil
}
// SetSubreaper sets the value i as the subreaper setting for the calling process
func SetSubreaper(i int) error {
if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, prSetChildSubreaper, uintptr(i), 0); err != 0 {
return err
}
return nil
}