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
|
||||
}
|
|
@ -1,127 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
runc "github.com/crosbymichael/go-runc"
|
||||
"github.com/docker/containerd/api/shim"
|
||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||
)
|
||||
|
||||
type execProcess struct {
|
||||
sync.WaitGroup
|
||||
|
||||
id string
|
||||
console *runc.Console
|
||||
io runc.IO
|
||||
status int
|
||||
pid int
|
||||
|
||||
parent *initProcess
|
||||
}
|
||||
|
||||
func newExecProcess(context context.Context, r *shim.ExecRequest, parent *initProcess) (process, error) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e := &execProcess{
|
||||
id: r.ID,
|
||||
parent: parent,
|
||||
}
|
||||
var (
|
||||
socket *runc.ConsoleSocket
|
||||
io runc.IO
|
||||
pidfile = filepath.Join(cwd, fmt.Sprintf("%s.pid", r.ID))
|
||||
)
|
||||
if r.Terminal {
|
||||
if socket, err = runc.NewConsoleSocket(filepath.Join(cwd, "pty.sock")); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// TODO: get uid/gid
|
||||
if io, err = runc.NewPipeIO(0, 0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e.io = io
|
||||
}
|
||||
opts := &runc.ExecOpts{
|
||||
PidFile: pidfile,
|
||||
ConsoleSocket: socket,
|
||||
IO: io,
|
||||
Detach: true,
|
||||
Tty: socket != nil,
|
||||
}
|
||||
if err := parent.runc.Exec(context, r.ID, processFromRequest(r), opts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pid, err := runc.ReadPidFile(opts.PidFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e.pid = pid
|
||||
return e, nil
|
||||
}
|
||||
|
||||
func processFromRequest(r *shim.ExecRequest) specs.Process {
|
||||
return specs.Process{
|
||||
Terminal: r.Terminal,
|
||||
User: specs.User{
|
||||
UID: r.User.Uid,
|
||||
GID: r.User.Gid,
|
||||
AdditionalGids: r.User.AdditionalGids,
|
||||
},
|
||||
Rlimits: rlimits(r.Rlimits),
|
||||
Args: r.Args,
|
||||
Env: r.Env,
|
||||
Cwd: r.Cwd,
|
||||
Capabilities: r.Capabilities,
|
||||
NoNewPrivileges: r.NoNewPrivileges,
|
||||
ApparmorProfile: r.ApparmorProfile,
|
||||
SelinuxLabel: r.SelinuxLabel,
|
||||
}
|
||||
}
|
||||
|
||||
func rlimits(rr []*shim.Rlimit) (o []specs.LinuxRlimit) {
|
||||
for _, r := range rr {
|
||||
o = append(o, specs.LinuxRlimit{
|
||||
Type: r.Type,
|
||||
Hard: r.Hard,
|
||||
Soft: r.Soft,
|
||||
})
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
func (e *execProcess) Pid() int {
|
||||
return e.pid
|
||||
}
|
||||
|
||||
func (e *execProcess) Status() int {
|
||||
return e.status
|
||||
}
|
||||
|
||||
func (e *execProcess) Exited(status int) {
|
||||
e.status = status
|
||||
}
|
||||
|
||||
func (e *execProcess) Start(_ context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *execProcess) Delete(context context.Context) error {
|
||||
e.Wait()
|
||||
e.io.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *execProcess) Resize(ws runc.WinSize) error {
|
||||
if e.console == nil {
|
||||
return nil
|
||||
}
|
||||
return e.console.Resize(ws)
|
||||
}
|
|
@ -1,123 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
runc "github.com/crosbymichael/go-runc"
|
||||
"github.com/docker/containerd/api/shim"
|
||||
)
|
||||
|
||||
type initProcess struct {
|
||||
sync.WaitGroup
|
||||
|
||||
id string
|
||||
bundle string
|
||||
console *runc.Console
|
||||
io runc.IO
|
||||
runc *runc.Runc
|
||||
status int
|
||||
pid int
|
||||
}
|
||||
|
||||
func newInitProcess(context context.Context, r *shim.CreateRequest) (process, error) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
runtime := &runc.Runc{
|
||||
Command: r.Runtime,
|
||||
Log: filepath.Join(cwd, "log.json"),
|
||||
LogFormat: runc.JSON,
|
||||
PdeathSignal: syscall.SIGKILL,
|
||||
}
|
||||
p := &initProcess{
|
||||
id: r.ID,
|
||||
bundle: r.Bundle,
|
||||
runc: runtime,
|
||||
}
|
||||
var (
|
||||
socket *runc.ConsoleSocket
|
||||
io runc.IO
|
||||
)
|
||||
if r.Terminal {
|
||||
if socket, err = runc.NewConsoleSocket(filepath.Join(cwd, "pty.sock")); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// TODO: get uid/gid
|
||||
if io, err = runc.NewPipeIO(0, 0); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.io = io
|
||||
}
|
||||
opts := &runc.CreateOpts{
|
||||
PidFile: filepath.Join(cwd, "init.pid"),
|
||||
ConsoleSocket: socket,
|
||||
IO: io,
|
||||
NoPivot: r.NoPivot,
|
||||
}
|
||||
if err := p.runc.Create(context, r.ID, r.Bundle, opts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if socket != nil {
|
||||
console, err := socket.ReceiveMaster()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.console = console
|
||||
if err := copyConsole(context, console, r.Stdin, r.Stdout, r.Stderr, &p.WaitGroup); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if err := copyPipes(context, io, r.Stdin, r.Stdout, r.Stderr, &p.WaitGroup); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
pid, err := runc.ReadPidFile(opts.PidFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.pid = pid
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (p *initProcess) Pid() int {
|
||||
return p.pid
|
||||
}
|
||||
|
||||
func (p *initProcess) Status() int {
|
||||
return p.status
|
||||
}
|
||||
|
||||
func (p *initProcess) Start(context context.Context) error {
|
||||
return p.runc.Start(context, p.id)
|
||||
}
|
||||
|
||||
func (p *initProcess) Exited(status int) {
|
||||
p.status = status
|
||||
}
|
||||
|
||||
func (p *initProcess) Delete(context context.Context) error {
|
||||
p.killAll(context)
|
||||
p.Wait()
|
||||
err := p.runc.Delete(context, p.id)
|
||||
p.io.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *initProcess) Resize(ws runc.WinSize) error {
|
||||
if p.console == nil {
|
||||
return nil
|
||||
}
|
||||
return p.console.Resize(ws)
|
||||
}
|
||||
|
||||
func (p *initProcess) killAll(context context.Context) error {
|
||||
return p.runc.Kill(context, p.id, int(syscall.SIGKILL), &runc.KillOpts{
|
||||
All: true,
|
||||
})
|
||||
}
|
|
@ -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,25 +0,0 @@
|
|||
package main
|
||||
|
||||
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
|
||||
// Start starts the user's defined process inside
|
||||
Start(context.Context) error
|
||||
// Delete deletes the process and closes all open pipes
|
||||
Delete(context.Context) error
|
||||
// Resize resizes the process console
|
||||
Resize(ws runc.WinSize) error
|
||||
// Exited sets the exit status for the process
|
||||
Exited(status int)
|
||||
// Status returns the exit status
|
||||
Status() int
|
||||
}
|
|
@ -1,81 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
runc "github.com/crosbymichael/go-runc"
|
||||
"github.com/tonistiigi/fifo"
|
||||
)
|
||||
|
||||
func copyConsole(ctx context.Context, console *runc.Console, stdin, stdout, stderr string, wg *sync.WaitGroup) error {
|
||||
in, err := fifo.OpenFifo(ctx, stdin, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go io.Copy(console, in)
|
||||
outw, err := fifo.OpenFifo(ctx, stdout, syscall.O_WRONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outr, err := fifo.OpenFifo(ctx, stdout, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
io.Copy(outw, console)
|
||||
console.Close()
|
||||
outr.Close()
|
||||
outw.Close()
|
||||
wg.Done()
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyPipes(ctx context.Context, rio runc.IO, stdin, stdout, stderr string, wg *sync.WaitGroup) error {
|
||||
for name, dest := range map[string]func(wc io.WriteCloser, rc io.Closer){
|
||||
stdout: func(wc io.WriteCloser, rc io.Closer) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
io.Copy(wc, rio.Stdout())
|
||||
wg.Done()
|
||||
wc.Close()
|
||||
rc.Close()
|
||||
}()
|
||||
},
|
||||
stderr: func(wc io.WriteCloser, rc io.Closer) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
io.Copy(wc, rio.Stderr())
|
||||
wg.Done()
|
||||
wc.Close()
|
||||
rc.Close()
|
||||
}()
|
||||
},
|
||||
} {
|
||||
fw, err := fifo.OpenFifo(ctx, name, syscall.O_WRONLY, 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("containerd-shim: opening %s failed: %s", name, err)
|
||||
}
|
||||
fr, err := fifo.OpenFifo(ctx, name, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("containerd-shim: opening %s failed: %s", name, err)
|
||||
}
|
||||
dest(fw, fr)
|
||||
}
|
||||
|
||||
f, err := fifo.OpenFifo(ctx, stdin, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("containerd-shim: opening %s failed: %s", stdin, err)
|
||||
}
|
||||
go func() {
|
||||
io.Copy(rio.Stdin(), f)
|
||||
rio.Stdin().Close()
|
||||
f.Close()
|
||||
}()
|
||||
return nil
|
||||
}
|
|
@ -1,102 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
runc "github.com/crosbymichael/go-runc"
|
||||
"github.com/docker/containerd/api/shim"
|
||||
"github.com/docker/containerd/utils"
|
||||
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
var emptyResponse = &google_protobuf.Empty{}
|
||||
|
||||
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) {
|
||||
process, err := newInitProcess(ctx, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.mu.Lock()
|
||||
pid := process.Pid()
|
||||
s.initPid, s.processes[pid] = pid, process
|
||||
s.mu.Unlock()
|
||||
return &shim.CreateResponse{
|
||||
Pid: uint32(pid),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *service) Start(ctx context.Context, r *shim.StartRequest) (*google_protobuf.Empty, error) {
|
||||
s.mu.Lock()
|
||||
p := s.processes[s.initPid]
|
||||
s.mu.Unlock()
|
||||
if err := p.Start(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return emptyResponse, nil
|
||||
}
|
||||
|
||||
func (s *service) Delete(ctx context.Context, r *shim.DeleteRequest) (*shim.DeleteResponse, error) {
|
||||
s.mu.Lock()
|
||||
p, ok := s.processes[int(r.Pid)]
|
||||
s.mu.Unlock()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("process does not exist %d", r.Pid)
|
||||
}
|
||||
if err := p.Delete(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.mu.Lock()
|
||||
delete(s.processes, int(r.Pid))
|
||||
s.mu.Unlock()
|
||||
return &shim.DeleteResponse{
|
||||
ExitStatus: uint32(p.Status()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *service) Exec(ctx context.Context, r *shim.ExecRequest) (*shim.ExecResponse, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
process, err := newExecProcess(ctx, r, s.processes[s.initPid].(*initProcess))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pid := process.Pid()
|
||||
s.processes[pid] = process
|
||||
return &shim.ExecResponse{
|
||||
Pid: uint32(pid),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *service) Pty(ctx context.Context, r *shim.PtyRequest) (*google_protobuf.Empty, error) {
|
||||
ws := runc.WinSize{
|
||||
Width: uint16(r.Width),
|
||||
Height: uint16(r.Height),
|
||||
}
|
||||
s.mu.Lock()
|
||||
p, ok := s.processes[int(r.Pid)]
|
||||
s.mu.Unlock()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("process does not exist %d", r.Pid)
|
||||
}
|
||||
if err := p.Resize(ws); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return emptyResponse, nil
|
||||
}
|
||||
|
||||
func (s *service) processExited(e utils.Exit) error {
|
||||
s.mu.Lock()
|
||||
if p, ok := s.processes[e.Pid]; ok {
|
||||
p.Exited(e.Status)
|
||||
}
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue