2017-02-24 23:50:59 +00:00
|
|
|
package linux
|
2017-02-13 18:23:28 +00:00
|
|
|
|
|
|
|
import (
|
2017-04-03 20:14:15 +00:00
|
|
|
"github.com/containerd/containerd"
|
|
|
|
"github.com/containerd/containerd/api/services/shim"
|
|
|
|
"github.com/containerd/containerd/api/types/container"
|
2017-04-05 18:51:56 +00:00
|
|
|
protobuf "github.com/gogo/protobuf/types"
|
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2017-02-13 18:23:28 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type State struct {
|
|
|
|
pid uint32
|
2017-04-05 18:51:56 +00:00
|
|
|
status containerd.Status
|
2017-02-13 18:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) Pid() uint32 {
|
|
|
|
return s.pid
|
|
|
|
}
|
|
|
|
|
2017-04-05 18:51:56 +00:00
|
|
|
func (s State) Status() containerd.Status {
|
2017-02-13 18:23:28 +00:00
|
|
|
return s.status
|
|
|
|
}
|
|
|
|
|
|
|
|
type Container struct {
|
|
|
|
id string
|
|
|
|
|
|
|
|
shim shim.ShimClient
|
|
|
|
}
|
|
|
|
|
2017-02-16 00:59:58 +00:00
|
|
|
func (c *Container) Info() containerd.ContainerInfo {
|
|
|
|
return containerd.ContainerInfo{
|
|
|
|
ID: c.id,
|
|
|
|
Runtime: runtimeName,
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Start(ctx context.Context) error {
|
|
|
|
_, err := c.shim.Start(ctx, &shim.StartRequest{})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) State(ctx context.Context) (containerd.State, error) {
|
|
|
|
response, err := c.shim.State(ctx, &shim.StateRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-05 18:51:56 +00:00
|
|
|
var status containerd.Status
|
2017-03-02 09:42:55 +00:00
|
|
|
switch response.Status {
|
|
|
|
case container.Status_CREATED:
|
|
|
|
status = containerd.CreatedStatus
|
|
|
|
case container.Status_RUNNING:
|
|
|
|
status = containerd.RunningStatus
|
|
|
|
case container.Status_STOPPED:
|
|
|
|
status = containerd.StoppedStatus
|
|
|
|
case container.Status_PAUSED:
|
|
|
|
status = containerd.PausedStatus
|
|
|
|
// TODO: containerd.DeletedStatus
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
return &State{
|
2017-03-02 09:42:55 +00:00
|
|
|
pid: response.Pid,
|
|
|
|
status: status,
|
2017-02-13 18:23:28 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2017-03-21 20:08:49 +00:00
|
|
|
|
|
|
|
func (c *Container) Pause(ctx context.Context) error {
|
|
|
|
_, err := c.shim.Pause(ctx, &shim.PauseRequest{})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) Resume(ctx context.Context) error {
|
|
|
|
_, err := c.shim.Resume(ctx, &shim.ResumeRequest{})
|
|
|
|
return err
|
|
|
|
}
|
2017-04-05 01:03:07 +00:00
|
|
|
|
|
|
|
func (c *Container) Kill(ctx context.Context, signal uint32, all bool) error {
|
|
|
|
_, err := c.shim.Kill(ctx, &shim.KillRequest{
|
|
|
|
Signal: signal,
|
|
|
|
All: all,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2017-04-05 18:51:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|