Making containers run and delete
This commit is contained in:
parent
2af0f297fe
commit
5f5f4904e0
5 changed files with 55 additions and 25 deletions
|
@ -15,8 +15,8 @@ func NewServer(supervisor *containerd.Supervisor) http.Handler {
|
|||
r: r,
|
||||
}
|
||||
r.HandleFunc("/containers", s.containers).Methods("GET")
|
||||
r.HandleFunc("/containers/{id:*}", s.createContainer).Methods("POST")
|
||||
r.HandleFunc("/containers/{id:*}", s.deleteContainer).Methods("DELETE")
|
||||
r.HandleFunc("/containers/{id:.*}", s.createContainer).Methods("POST")
|
||||
r.HandleFunc("/containers/{id:.*}", s.deleteContainer).Methods("DELETE")
|
||||
return s
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package containerd
|
||||
|
||||
type Container interface {
|
||||
ID() string
|
||||
Pid() (int, error)
|
||||
SetExited(status int)
|
||||
Delete() error
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
|
@ -11,6 +12,7 @@ import (
|
|||
"github.com/Sirupsen/logrus"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/crosbymichael/containerd"
|
||||
"github.com/crosbymichael/containerd/api/v1"
|
||||
"github.com/opencontainers/runc/libcontainer/utils"
|
||||
"github.com/rcrowley/go-metrics"
|
||||
)
|
||||
|
@ -55,7 +57,11 @@ func daemon(stateDir string, concurrency, bufferSize int) error {
|
|||
events := make(chan containerd.Event, bufferSize)
|
||||
// start the signal handler in the background.
|
||||
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) {
|
||||
|
@ -67,6 +73,7 @@ func startSignalHandler(supervisor *containerd.Supervisor, bufferSize int) {
|
|||
switch s {
|
||||
case syscall.SIGTERM, syscall.SIGINT, syscall.SIGSTOP:
|
||||
supervisor.Stop()
|
||||
os.Exit(0)
|
||||
case syscall.SIGCHLD:
|
||||
exits, err := reap()
|
||||
if err != nil {
|
||||
|
|
|
@ -165,6 +165,14 @@ type libcontainerContainer struct {
|
|||
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) {
|
||||
c.exitStatus = status
|
||||
// meh
|
||||
|
|
|
@ -53,37 +53,50 @@ type Supervisor struct {
|
|||
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.
|
||||
//
|
||||
// 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 {
|
||||
return ErrEventChanNil
|
||||
}
|
||||
s.events = events
|
||||
for evt := range events {
|
||||
logrus.WithField("event", evt).Debug("containerd: processing event")
|
||||
switch e := evt.(type) {
|
||||
case *ExitEvent:
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"pid": e.Pid,
|
||||
"status": e.Status,
|
||||
}).Debug("containerd: process exited")
|
||||
if container, ok := s.processes[e.Pid]; ok {
|
||||
container.SetExited(e.Status)
|
||||
go func() {
|
||||
for evt := range events {
|
||||
logrus.WithField("event", evt).Debug("containerd: processing event")
|
||||
switch e := evt.(type) {
|
||||
case *ExitEvent:
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"pid": e.Pid,
|
||||
"status": e.Status,
|
||||
}).Debug("containerd: process exited")
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue