Add exec APIs
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
cccf5d3723
commit
7715ddcefa
19 changed files with 759 additions and 126 deletions
|
@ -4,19 +4,21 @@ import (
|
|||
"github.com/containerd/containerd"
|
||||
"github.com/containerd/containerd/api/services/shim"
|
||||
"github.com/containerd/containerd/api/types/container"
|
||||
protobuf "github.com/gogo/protobuf/types"
|
||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type State struct {
|
||||
pid uint32
|
||||
status containerd.ContainerStatus
|
||||
status containerd.Status
|
||||
}
|
||||
|
||||
func (s State) Pid() uint32 {
|
||||
return s.pid
|
||||
}
|
||||
|
||||
func (s State) Status() containerd.ContainerStatus {
|
||||
func (s State) Status() containerd.Status {
|
||||
return s.status
|
||||
}
|
||||
|
||||
|
@ -43,7 +45,7 @@ func (c *Container) State(ctx context.Context) (containerd.State, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var status containerd.ContainerStatus
|
||||
var status containerd.Status
|
||||
switch response.Status {
|
||||
case container.Status_CREATED:
|
||||
status = containerd.CreatedStatus
|
||||
|
@ -78,3 +80,47 @@ func (c *Container) Kill(ctx context.Context, signal uint32, all bool) error {
|
|||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Container) Exec(ctx context.Context, opts containerd.ExecOpts) (containerd.Process, error) {
|
||||
request := &shim.ExecRequest{
|
||||
Stdin: opts.IO.Stdin,
|
||||
Stdout: opts.IO.Stdout,
|
||||
Stderr: opts.IO.Stderr,
|
||||
Terminal: opts.IO.Terminal,
|
||||
Spec: &protobuf.Any{
|
||||
TypeUrl: specs.Version,
|
||||
Value: opts.Spec,
|
||||
},
|
||||
}
|
||||
resp, err := c.shim.Exec(ctx, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Process{
|
||||
pid: int(resp.Pid),
|
||||
c: c,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type Process struct {
|
||||
pid int
|
||||
c *Container
|
||||
}
|
||||
|
||||
func (p *Process) Kill(ctx context.Context, signal uint32, _ bool) error {
|
||||
_, err := p.c.shim.Kill(ctx, &shim.KillRequest{
|
||||
Signal: signal,
|
||||
Pid: uint32(p.pid),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Process) State(ctx context.Context) (containerd.State, error) {
|
||||
// use the container status for the status of the process
|
||||
state, err := p.c.State(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state.(*State).pid = uint32(p.pid)
|
||||
return state, nil
|
||||
}
|
||||
|
|
|
@ -139,3 +139,7 @@ func (e *execProcess) Resize(ws console.WinSize) error {
|
|||
}
|
||||
return e.console.Resize(ws)
|
||||
}
|
||||
|
||||
func (e *execProcess) Signal(sig int) error {
|
||||
return syscall.Kill(e.pid, syscall.Signal(sig))
|
||||
}
|
||||
|
|
|
@ -169,3 +169,7 @@ func (p *initProcess) killAll(context context.Context) error {
|
|||
All: true,
|
||||
})
|
||||
}
|
||||
|
||||
func (p *initProcess) Signal(sig int) error {
|
||||
return syscall.Kill(p.pid, syscall.Signal(sig))
|
||||
}
|
||||
|
|
|
@ -15,5 +15,8 @@ type process interface {
|
|||
Exited(status int)
|
||||
// Status returns the exit status
|
||||
Status() int
|
||||
// Delete delets the process and its resourcess
|
||||
Delete(context.Context) error
|
||||
// Signal directly signals the process
|
||||
Signal(int) error
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package shim
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
@ -219,7 +220,17 @@ func (s *Service) Exit(ctx context.Context, r *shimapi.ExitRequest) (*google_pro
|
|||
}
|
||||
|
||||
func (s *Service) Kill(ctx context.Context, r *shimapi.KillRequest) (*google_protobuf.Empty, error) {
|
||||
if err := s.initProcess.Kill(ctx, r.Signal, r.All); err != nil {
|
||||
if r.Pid == 0 {
|
||||
if err := s.initProcess.Kill(ctx, r.Signal, r.All); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return empty, nil
|
||||
}
|
||||
proc, ok := s.processes[int(r.Pid)]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("process does not exist %d", r.Pid)
|
||||
}
|
||||
if err := proc.Signal(int(r.Signal)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return empty, nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue