2016-07-08 19:04:00 +00:00
|
|
|
package server
|
|
|
|
|
2016-07-29 22:35:10 +00:00
|
|
|
import (
|
2016-08-01 23:50:45 +00:00
|
|
|
"fmt"
|
2016-08-04 14:34:30 +00:00
|
|
|
"os"
|
2016-08-01 23:50:45 +00:00
|
|
|
|
2016-07-29 22:35:10 +00:00
|
|
|
"github.com/mrunalp/ocid/oci"
|
2016-08-01 23:50:45 +00:00
|
|
|
"github.com/mrunalp/ocid/utils"
|
2016-07-29 22:35:10 +00:00
|
|
|
)
|
|
|
|
|
2016-07-19 18:53:57 +00:00
|
|
|
const (
|
|
|
|
runtimeAPIVersion = "v1alpha1"
|
2016-08-04 14:34:30 +00:00
|
|
|
imageStore = "/var/lib/ocid/images"
|
2016-07-08 19:04:00 +00:00
|
|
|
)
|
|
|
|
|
2016-07-19 18:53:57 +00:00
|
|
|
// Server implements the RuntimeService and ImageService
|
|
|
|
type Server struct {
|
2016-08-01 23:05:37 +00:00
|
|
|
runtime *oci.Runtime
|
|
|
|
sandboxDir string
|
|
|
|
state *serverState
|
2016-07-08 19:04:00 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 18:53:57 +00:00
|
|
|
// New creates a new Server with options provided
|
2016-08-01 22:08:21 +00:00
|
|
|
func New(runtimePath, sandboxDir, containerDir string) (*Server, error) {
|
2016-08-01 23:50:45 +00:00
|
|
|
// TODO: This will go away later when we have wrapper process or systemd acting as
|
|
|
|
// subreaper.
|
|
|
|
if err := utils.SetSubreaper(1); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to set server as subreaper: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:57:41 +00:00
|
|
|
utils.StartReaper()
|
|
|
|
|
2016-08-04 14:34:30 +00:00
|
|
|
if err := os.MkdirAll(imageStore, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-01 23:05:37 +00:00
|
|
|
r, err := oci.New(runtimePath, containerDir)
|
2016-07-19 18:53:57 +00:00
|
|
|
if err != nil {
|
2016-07-29 22:35:10 +00:00
|
|
|
return nil, err
|
2016-07-19 18:53:57 +00:00
|
|
|
}
|
2016-08-01 23:05:37 +00:00
|
|
|
sandboxes := make(map[string]*sandbox)
|
2016-08-01 17:39:42 +00:00
|
|
|
containers := make(map[string]*oci.Container)
|
2016-07-29 22:35:10 +00:00
|
|
|
return &Server{
|
2016-08-01 23:05:37 +00:00
|
|
|
runtime: r,
|
|
|
|
sandboxDir: sandboxDir,
|
|
|
|
state: &serverState{
|
2016-08-01 17:39:42 +00:00
|
|
|
sandboxes: sandboxes,
|
|
|
|
containers: containers,
|
2016-08-01 23:05:37 +00:00
|
|
|
},
|
2016-07-29 22:35:10 +00:00
|
|
|
}, nil
|
2016-07-08 19:04:00 +00:00
|
|
|
}
|
2016-07-20 01:30:05 +00:00
|
|
|
|
2016-08-01 23:05:37 +00:00
|
|
|
type serverState struct {
|
2016-08-01 17:39:42 +00:00
|
|
|
sandboxes map[string]*sandbox
|
|
|
|
containers map[string]*oci.Container
|
2016-08-01 23:05:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 01:30:05 +00:00
|
|
|
type sandbox struct {
|
2016-08-01 17:39:42 +00:00
|
|
|
name string
|
|
|
|
logDir string
|
|
|
|
labels map[string]string
|
|
|
|
containers []*oci.Container
|
2016-07-20 01:30:05 +00:00
|
|
|
}
|
2016-08-01 23:05:37 +00:00
|
|
|
|
|
|
|
func (s *Server) addSandbox(sb *sandbox) {
|
|
|
|
s.state.sandboxes[sb.name] = sb
|
|
|
|
}
|
2016-08-01 17:39:42 +00:00
|
|
|
|
|
|
|
func (s *Server) hasSandbox(name string) bool {
|
|
|
|
_, ok := s.state.sandboxes[name]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) addContainer(c *oci.Container) {
|
|
|
|
s.containers = append(s.containers, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) addContainer(c *oci.Container) {
|
|
|
|
sandbox := s.state.sandboxes[c.Sandbox()]
|
|
|
|
sandbox.addContainer(c)
|
|
|
|
s.state.containers[c.Name()] = c
|
|
|
|
}
|