Making containers run and delete

This commit is contained in:
Michael Crosby 2015-11-05 16:40:57 -08:00
parent 2af0f297fe
commit 5f5f4904e0
5 changed files with 55 additions and 25 deletions

View file

@ -15,8 +15,8 @@ func NewServer(supervisor *containerd.Supervisor) http.Handler {
r: r, r: r,
} }
r.HandleFunc("/containers", s.containers).Methods("GET") r.HandleFunc("/containers", s.containers).Methods("GET")
r.HandleFunc("/containers/{id:*}", s.createContainer).Methods("POST") r.HandleFunc("/containers/{id:.*}", s.createContainer).Methods("POST")
r.HandleFunc("/containers/{id:*}", s.deleteContainer).Methods("DELETE") r.HandleFunc("/containers/{id:.*}", s.deleteContainer).Methods("DELETE")
return s return s
} }

View file

@ -1,6 +1,8 @@
package containerd package containerd
type Container interface { type Container interface {
ID() string
Pid() (int, error)
SetExited(status int) SetExited(status int)
Delete() error Delete() error
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"log" "log"
"net/http"
"os" "os"
"os/signal" "os/signal"
"runtime" "runtime"
@ -11,6 +12,7 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/crosbymichael/containerd" "github.com/crosbymichael/containerd"
"github.com/crosbymichael/containerd/api/v1"
"github.com/opencontainers/runc/libcontainer/utils" "github.com/opencontainers/runc/libcontainer/utils"
"github.com/rcrowley/go-metrics" "github.com/rcrowley/go-metrics"
) )
@ -55,7 +57,11 @@ func daemon(stateDir string, concurrency, bufferSize int) error {
events := make(chan containerd.Event, bufferSize) events := make(chan containerd.Event, bufferSize)
// start the signal handler in the background. // start the signal handler in the background.
go startSignalHandler(supervisor, bufferSize) go startSignalHandler(supervisor, bufferSize)
return supervisor.Run(events) if err := supervisor.Start(events); err != nil {
return err
}
server := v1.NewServer(supervisor)
return http.ListenAndServe("localhost:8888", server)
} }
func startSignalHandler(supervisor *containerd.Supervisor, bufferSize int) { func startSignalHandler(supervisor *containerd.Supervisor, bufferSize int) {
@ -67,6 +73,7 @@ func startSignalHandler(supervisor *containerd.Supervisor, bufferSize int) {
switch s { switch s {
case syscall.SIGTERM, syscall.SIGINT, syscall.SIGSTOP: case syscall.SIGTERM, syscall.SIGINT, syscall.SIGSTOP:
supervisor.Stop() supervisor.Stop()
os.Exit(0)
case syscall.SIGCHLD: case syscall.SIGCHLD:
exits, err := reap() exits, err := reap()
if err != nil { if err != nil {

View file

@ -165,6 +165,14 @@ type libcontainerContainer struct {
exited bool exited bool
} }
func (c *libcontainerContainer) ID() string {
return c.c.ID()
}
func (c *libcontainerContainer) Pid() (int, error) {
return c.initProcess.Pid()
}
func (c *libcontainerContainer) SetExited(status int) { func (c *libcontainerContainer) SetExited(status int) {
c.exitStatus = status c.exitStatus = status
// meh // meh

View file

@ -53,37 +53,50 @@ type Supervisor struct {
workerGroup sync.WaitGroup workerGroup sync.WaitGroup
} }
// Run is a blocking call that runs the supervisor for monitoring contianer processes and // Start is a non-blocking call that runs the supervisor for monitoring contianer processes and
// executing new containers. // executing new containers.
// //
// This event loop is the only thing that is allowed to modify state of containers and processes. // This event loop is the only thing that is allowed to modify state of containers and processes.
func (s *Supervisor) Run(events chan Event) error { func (s *Supervisor) Start(events chan Event) error {
if events == nil { if events == nil {
return ErrEventChanNil return ErrEventChanNil
} }
s.events = events s.events = events
for evt := range events { go func() {
logrus.WithField("event", evt).Debug("containerd: processing event") for evt := range events {
switch e := evt.(type) { logrus.WithField("event", evt).Debug("containerd: processing event")
case *ExitEvent: switch e := evt.(type) {
logrus.WithFields(logrus.Fields{ case *ExitEvent:
"pid": e.Pid, logrus.WithFields(logrus.Fields{
"status": e.Status, "pid": e.Pid,
}).Debug("containerd: process exited") "status": e.Status,
if container, ok := s.processes[e.Pid]; ok { }).Debug("containerd: process exited")
container.SetExited(e.Status) if container, ok := s.processes[e.Pid]; ok {
container.SetExited(e.Status)
if err := container.Delete(); err != nil {
logrus.WithField("error", err).Error("containerd: deleting container")
}
delete(s.processes, e.Pid)
delete(s.containers, container.ID())
}
case *StartedEvent:
s.containers[e.ID] = e.Container
pid, err := e.Container.Pid()
if err != nil {
logrus.WithField("error", err).Error("containerd: getting container pid")
continue
}
s.processes[pid] = e.Container
case *CreateContainerEvent:
j := &CreateJob{
ID: e.ID,
BundlePath: e.BundlePath,
Err: e.Err,
}
s.jobs <- j
} }
case *StartedEvent:
s.containers[e.ID] = e.Container
case *CreateContainerEvent:
j := &CreateJob{
ID: e.ID,
BundlePath: e.BundlePath,
Err: e.Err,
}
s.jobs <- j
} }
} }()
return nil return nil
} }