Move shim service into top lvl package
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
fe280d2df0
commit
d619954a2b
7 changed files with 33 additions and 69 deletions
|
@ -1,38 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
type checkpoint struct {
|
||||
// Timestamp is the time that checkpoint happened
|
||||
Created time.Time `json:"created"`
|
||||
// Name is the name of the checkpoint
|
||||
Name string `json:"name"`
|
||||
// TCP checkpoints open tcp connections
|
||||
TCP bool `json:"tcp"`
|
||||
// UnixSockets persists unix sockets in the checkpoint
|
||||
UnixSockets bool `json:"unixSockets"`
|
||||
// Shell persists tty sessions in the checkpoint
|
||||
Shell bool `json:"shell"`
|
||||
// Exit exits the container after the checkpoint is finished
|
||||
Exit bool `json:"exit"`
|
||||
// EmptyNS tells CRIU not to restore a particular namespace
|
||||
EmptyNS []string `json:"emptyNS,omitempty"`
|
||||
}
|
||||
|
||||
func loadCheckpoint(checkpointPath string) (*checkpoint, error) {
|
||||
f, err := os.Open(filepath.Join(checkpointPath, "config.json"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
var cpt checkpoint
|
||||
if err := json.NewDecoder(f).Decode(&cpt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cpt, nil
|
||||
}
|
|
@ -10,7 +10,8 @@ import (
|
|||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/docker/containerd"
|
||||
"github.com/docker/containerd/api/shim"
|
||||
apishim "github.com/docker/containerd/api/shim"
|
||||
"github.com/docker/containerd/shim"
|
||||
"github.com/docker/containerd/sys"
|
||||
"github.com/docker/containerd/utils"
|
||||
"github.com/urfave/cli"
|
||||
|
@ -52,11 +53,9 @@ func main() {
|
|||
}
|
||||
var (
|
||||
server = grpc.NewServer()
|
||||
sv = &service{
|
||||
processes: make(map[int]process),
|
||||
}
|
||||
sv = shim.NewService()
|
||||
)
|
||||
shim.RegisterShimServer(server, sv)
|
||||
apishim.RegisterShimServer(server, sv)
|
||||
l, err := utils.CreateUnixSocket("shim.sock")
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -77,7 +76,7 @@ func main() {
|
|||
logrus.WithError(err).Error("reap exit status")
|
||||
}
|
||||
for _, e := range exits {
|
||||
if err := sv.processExited(e); err != nil {
|
||||
if err := sv.ProcessExit(e); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package shim
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -8,7 +8,7 @@ import (
|
|||
"sync"
|
||||
|
||||
runc "github.com/crosbymichael/go-runc"
|
||||
"github.com/docker/containerd/api/shim"
|
||||
apishim "github.com/docker/containerd/api/shim"
|
||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||
)
|
||||
|
||||
|
@ -24,7 +24,7 @@ type execProcess struct {
|
|||
parent *initProcess
|
||||
}
|
||||
|
||||
func newExecProcess(context context.Context, r *shim.ExecRequest, parent *initProcess) (process, error) {
|
||||
func newExecProcess(context context.Context, r *apishim.ExecRequest, parent *initProcess) (process, error) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -67,7 +67,7 @@ func newExecProcess(context context.Context, r *shim.ExecRequest, parent *initPr
|
|||
return e, nil
|
||||
}
|
||||
|
||||
func processFromRequest(r *shim.ExecRequest) specs.Process {
|
||||
func processFromRequest(r *apishim.ExecRequest) specs.Process {
|
||||
return specs.Process{
|
||||
Terminal: r.Terminal,
|
||||
User: specs.User{
|
||||
|
@ -86,7 +86,7 @@ func processFromRequest(r *shim.ExecRequest) specs.Process {
|
|||
}
|
||||
}
|
||||
|
||||
func rlimits(rr []*shim.Rlimit) (o []specs.LinuxRlimit) {
|
||||
func rlimits(rr []*apishim.Rlimit) (o []specs.LinuxRlimit) {
|
||||
for _, r := range rr {
|
||||
o = append(o, specs.LinuxRlimit{
|
||||
Type: r.Type,
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package shim
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -8,7 +8,7 @@ import (
|
|||
"syscall"
|
||||
|
||||
runc "github.com/crosbymichael/go-runc"
|
||||
"github.com/docker/containerd/api/shim"
|
||||
apishim "github.com/docker/containerd/api/shim"
|
||||
)
|
||||
|
||||
type initProcess struct {
|
||||
|
@ -23,7 +23,7 @@ type initProcess struct {
|
|||
pid int
|
||||
}
|
||||
|
||||
func newInitProcess(context context.Context, r *shim.CreateRequest) (process, error) {
|
||||
func newInitProcess(context context.Context, r *apishim.CreateRequest) (process, error) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package shim
|
||||
|
||||
import (
|
||||
"context"
|
|
@ -1,14 +1,11 @@
|
|||
package main
|
||||
package shim
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
runc "github.com/crosbymichael/go-runc"
|
||||
)
|
||||
|
||||
var errRuntime = errors.New("shim: runtime execution error")
|
||||
|
||||
type process interface {
|
||||
// Pid returns the pid for the process
|
||||
Pid() int
|
|
@ -1,11 +1,11 @@
|
|||
package main
|
||||
package shim
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
runc "github.com/crosbymichael/go-runc"
|
||||
"github.com/docker/containerd/api/shim"
|
||||
apishim "github.com/docker/containerd/api/shim"
|
||||
"github.com/docker/containerd/utils"
|
||||
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
||||
"golang.org/x/net/context"
|
||||
|
@ -13,13 +13,19 @@ import (
|
|||
|
||||
var emptyResponse = &google_protobuf.Empty{}
|
||||
|
||||
type service struct {
|
||||
func NewService() *Service {
|
||||
return &Service{
|
||||
processes: make(map[int]process),
|
||||
}
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
initPid int
|
||||
mu sync.Mutex
|
||||
processes map[int]process
|
||||
}
|
||||
|
||||
func (s *service) Create(ctx context.Context, r *shim.CreateRequest) (*shim.CreateResponse, error) {
|
||||
func (s *Service) Create(ctx context.Context, r *apishim.CreateRequest) (*apishim.CreateResponse, error) {
|
||||
process, err := newInitProcess(ctx, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -28,12 +34,12 @@ func (s *service) Create(ctx context.Context, r *shim.CreateRequest) (*shim.Crea
|
|||
pid := process.Pid()
|
||||
s.initPid, s.processes[pid] = pid, process
|
||||
s.mu.Unlock()
|
||||
return &shim.CreateResponse{
|
||||
return &apishim.CreateResponse{
|
||||
Pid: uint32(pid),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *service) Start(ctx context.Context, r *shim.StartRequest) (*google_protobuf.Empty, error) {
|
||||
func (s *Service) Start(ctx context.Context, r *apishim.StartRequest) (*google_protobuf.Empty, error) {
|
||||
s.mu.Lock()
|
||||
p := s.processes[s.initPid]
|
||||
s.mu.Unlock()
|
||||
|
@ -43,7 +49,7 @@ func (s *service) Start(ctx context.Context, r *shim.StartRequest) (*google_prot
|
|||
return emptyResponse, nil
|
||||
}
|
||||
|
||||
func (s *service) Delete(ctx context.Context, r *shim.DeleteRequest) (*shim.DeleteResponse, error) {
|
||||
func (s *Service) Delete(ctx context.Context, r *apishim.DeleteRequest) (*apishim.DeleteResponse, error) {
|
||||
s.mu.Lock()
|
||||
p, ok := s.processes[int(r.Pid)]
|
||||
s.mu.Unlock()
|
||||
|
@ -56,12 +62,12 @@ func (s *service) Delete(ctx context.Context, r *shim.DeleteRequest) (*shim.Dele
|
|||
s.mu.Lock()
|
||||
delete(s.processes, int(r.Pid))
|
||||
s.mu.Unlock()
|
||||
return &shim.DeleteResponse{
|
||||
return &apishim.DeleteResponse{
|
||||
ExitStatus: uint32(p.Status()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *service) Exec(ctx context.Context, r *shim.ExecRequest) (*shim.ExecResponse, error) {
|
||||
func (s *Service) Exec(ctx context.Context, r *apishim.ExecRequest) (*apishim.ExecResponse, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
process, err := newExecProcess(ctx, r, s.processes[s.initPid].(*initProcess))
|
||||
|
@ -70,12 +76,12 @@ func (s *service) Exec(ctx context.Context, r *shim.ExecRequest) (*shim.ExecResp
|
|||
}
|
||||
pid := process.Pid()
|
||||
s.processes[pid] = process
|
||||
return &shim.ExecResponse{
|
||||
return &apishim.ExecResponse{
|
||||
Pid: uint32(pid),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *service) Pty(ctx context.Context, r *shim.PtyRequest) (*google_protobuf.Empty, error) {
|
||||
func (s *Service) Pty(ctx context.Context, r *apishim.PtyRequest) (*google_protobuf.Empty, error) {
|
||||
ws := runc.WinSize{
|
||||
Width: uint16(r.Width),
|
||||
Height: uint16(r.Height),
|
||||
|
@ -92,7 +98,7 @@ func (s *service) Pty(ctx context.Context, r *shim.PtyRequest) (*google_protobuf
|
|||
return emptyResponse, nil
|
||||
}
|
||||
|
||||
func (s *service) processExited(e utils.Exit) error {
|
||||
func (s *Service) ProcessExit(e utils.Exit) error {
|
||||
s.mu.Lock()
|
||||
if p, ok := s.processes[e.Pid]; ok {
|
||||
p.Exited(e.Status)
|
Loading…
Reference in a new issue