Getting ctr closer to compiling on Windows

Signed-off-by: John Howard <jhoward@microsoft.com>
This commit is contained in:
John Howard 2016-02-29 10:48:39 -08:00
parent 8d2051232a
commit d4ca79c978
18 changed files with 115 additions and 83 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/docker/containerd/api/grpc/types"
"github.com/docker/containerd/runtime"
"github.com/docker/containerd/specs"
"github.com/docker/containerd/supervisor"
"golang.org/x/net/context"
)
@ -66,7 +67,7 @@ func (s *apiServer) Signal(ctx context.Context, r *types.SignalRequest) (*types.
}
func (s *apiServer) AddProcess(ctx context.Context, r *types.AddProcessRequest) (*types.AddProcessResponse, error) {
process := &runtime.ProcessSpec{
process := &specs.ProcessSpec{
Terminal: r.Terminal,
Args: r.Args,
Env: r.Env,

View file

@ -10,11 +10,12 @@ import (
"github.com/docker/containerd/api/grpc/types"
"github.com/docker/containerd/runtime"
"github.com/docker/containerd/specs"
"github.com/docker/containerd/supervisor"
"github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/system"
"github.com/opencontainers/specs"
ocs "github.com/opencontainers/specs"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
@ -241,7 +242,7 @@ func (s *apiServer) Stats(ctx context.Context, r *types.StatsRequest) (*types.St
return t, nil
}
func setUserFieldsInProcess(p *types.Process, oldProc runtime.ProcessSpec) {
func setUserFieldsInProcess(p *types.Process, oldProc specs.ProcessSpec) {
p.User = &types.User{
Uid: oldProc.User.UID,
Gid: oldProc.User.GID,
@ -249,8 +250,8 @@ func setUserFieldsInProcess(p *types.Process, oldProc runtime.ProcessSpec) {
}
}
func setPlatformRuntimeProcessSpecUserFields(r *types.User, process *runtime.ProcessSpec) {
process.User = specs.User{
func setPlatformRuntimeProcessSpecUserFields(r *types.User, process *specs.ProcessSpec) {
process.User = ocs.User{
UID: r.Uid,
GID: r.Gid,
AdditionalGids: r.AdditionalGids,

View file

@ -4,7 +4,7 @@ import (
"errors"
"github.com/docker/containerd/api/grpc/types"
"github.com/docker/containerd/runtime"
"github.com/docker/containerd/specs"
"github.com/docker/containerd/supervisor"
"golang.org/x/net/context"
)
@ -32,8 +32,8 @@ func (s *apiServer) Stats(ctx context.Context, r *types.StatsRequest) (*types.St
return nil, errors.New("Stats() not supported on Windows")
}
func setUserFieldsInProcess(p *types.Process, oldProc runtime.ProcessSpec) {
func setUserFieldsInProcess(p *types.Process, oldProc specs.ProcessSpec) {
}
func setPlatformRuntimeProcessSpecUserFields(r *types.User, process *runtime.ProcessSpec) {
func setPlatformRuntimeProcessSpecUserFields(r *types.User, process *specs.ProcessSpec) {
}

View file

@ -17,8 +17,8 @@ import (
"github.com/codegangsta/cli"
"github.com/docker/containerd/api/grpc/types"
"github.com/docker/containerd/specs"
"github.com/docker/docker/pkg/term"
"github.com/opencontainers/specs"
netcontext "golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
@ -538,23 +538,3 @@ type stdio struct {
stdout string
stderr string
}
func createStdio() (s stdio, err error) {
tmp, err := ioutil.TempDir("", "ctr-")
if err != nil {
return s, err
}
// create fifo's for the process
for name, fd := range map[string]*string{
"stdin": &s.stdin,
"stdout": &s.stdout,
"stderr": &s.stderr,
} {
path := filepath.Join(tmp, name)
if err := syscall.Mkfifo(path, 0755); err != nil && !os.IsExist(err) {
return s, err
}
*fd = path
}
return s, nil
}

30
ctr/container_unix.go Normal file
View file

@ -0,0 +1,30 @@
// +build !windows
package main
import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
)
func createStdio() (s stdio, err error) {
tmp, err := ioutil.TempDir("", "ctr-")
if err != nil {
return s, err
}
// create fifo's for the process
for name, fd := range map[string]*string{
"stdin": &s.stdin,
"stdout": &s.stdout,
"stderr": &s.stderr,
} {
path := filepath.Join(tmp, name)
if err := syscall.Mkfifo(path, 0755); err != nil && !os.IsExist(err) {
return s, err
}
*fd = path
}
return s, nil
}

6
ctr/container_windows.go Normal file
View file

@ -0,0 +1,6 @@
package main
// TODO Windows: This will have a very different implementation
func createStdio() (s stdio, err error) {
return stdio{}, nil
}

View file

@ -5,9 +5,9 @@ import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
"github.com/Sirupsen/logrus"
"github.com/docker/containerd/specs"
)
type Container interface {
@ -18,7 +18,7 @@ type Container interface {
// Start starts the init process of the container
Start(checkpoint string, s Stdio) (Process, error)
// Exec starts another process in an existing container
Exec(string, ProcessSpec, Stdio) (Process, error)
Exec(string, specs.ProcessSpec, Stdio) (Process, error)
// Delete removes the container's state and any resources
Delete() error
// Processes returns all the containers processes that have been added
@ -175,8 +175,8 @@ func (c *container) Labels() []string {
return c.labels
}
func (c *container) readSpec() (*PlatformSpec, error) {
var spec PlatformSpec
func (c *container) readSpec() (*specs.PlatformSpec, error) {
var spec specs.PlatformSpec
f, err := os.Open(filepath.Join(c.bundle, "config.json"))
if err != nil {
return nil, err
@ -188,18 +188,6 @@ func (c *container) readSpec() (*PlatformSpec, error) {
return &spec, nil
}
func (c *container) State() State {
proc := c.processes["init"]
if proc == nil || proc.pid == 0 {
return Stopped
}
err := syscall.Kill(proc.pid, 0)
if err != nil && err == syscall.ESRCH {
return Stopped
}
return Running
}
func (c *container) Delete() error {
return os.RemoveAll(filepath.Join(c.root, c.id))
}

View file

@ -9,17 +9,18 @@ import (
"syscall"
"time"
"github.com/docker/containerd/specs"
"github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/specs"
ocs "github.com/opencontainers/specs"
)
func getRootIDs(s *PlatformSpec) (int, int, error) {
func getRootIDs(s *specs.PlatformSpec) (int, int, error) {
if s == nil {
return 0, 0, nil
}
var hasUserns bool
for _, ns := range s.Linux.Namespaces {
if ns.Type == specs.UserNamespace {
if ns.Type == ocs.UserNamespace {
hasUserns = true
break
}
@ -32,6 +33,18 @@ func getRootIDs(s *PlatformSpec) (int, int, error) {
return uid, gid, nil
}
func (c *container) State() State {
proc := c.processes["init"]
if proc == nil || proc.pid == 0 {
return Stopped
}
err := syscall.Kill(proc.pid, 0)
if err != nil && err == syscall.ESRCH {
return Stopped
}
return Running
}
func (c *container) Runtime() string {
return c.runtime
}
@ -136,7 +149,7 @@ func (c *container) Start(checkpoint string, s Stdio) (Process, error) {
c: c,
stdio: s,
spec: spec,
processSpec: ProcessSpec(spec.Process),
processSpec: specs.ProcessSpec(spec.Process),
}
p, err := newProcess(config)
if err != nil {
@ -152,7 +165,7 @@ func (c *container) Start(checkpoint string, s Stdio) (Process, error) {
return p, nil
}
func (c *container) Exec(pid string, spec ProcessSpec, s Stdio) (Process, error) {
func (c *container) Exec(pid string, spec specs.ProcessSpec, s Stdio) (Process, error) {
processRoot := filepath.Join(c.root, c.id, pid)
if err := os.Mkdir(processRoot, 0755); err != nil {
return nil, err
@ -187,14 +200,14 @@ func (c *container) Exec(pid string, spec ProcessSpec, s Stdio) (Process, error)
}
func (c *container) getLibctContainer() (libcontainer.Container, error) {
f, err := libcontainer.New(specs.LinuxStateDirectory, libcontainer.Cgroupfs)
f, err := libcontainer.New(ocs.LinuxStateDirectory, libcontainer.Cgroupfs)
if err != nil {
return nil, err
}
return f.Load(c.id)
}
func hostIDFromMap(id uint32, mp []specs.IDMapping) int {
func hostIDFromMap(id uint32, mp []ocs.IDMapping) int {
for _, m := range mp {
if (id >= m.ContainerID) && (id <= (m.ContainerID + m.Size - 1)) {
return int(m.HostID + (id - m.ContainerID))

View file

@ -1,11 +1,20 @@
package runtime
import "errors"
import (
"errors"
func getRootIDs(s *PlatformSpec) (int, int, error) {
"github.com/docker/containerd/specs"
)
func getRootIDs(s *specs.PlatformSpec) (int, int, error) {
return 0, 0, nil
}
// TODO Windows: This will have a different implementation
func (c *container) State() State {
return Running // HACK HACK HACK
}
func (c *container) Runtime() string {
return "windows"
}
@ -38,7 +47,7 @@ func (c *container) Start(checkpoint string, s Stdio) (Process, error) {
// TODO Windows: Implement me.
// This will have a very different implementation on Windows.
func (c *container) Exec(pid string, spec ProcessSpec, s Stdio) (Process, error) {
func (c *container) Exec(pid string, spec specs.ProcessSpec, s Stdio) (Process, error) {
return nil, errors.New("Exec not yet implemented on Windows")
}

View file

@ -8,6 +8,8 @@ import (
"os"
"path/filepath"
"strconv"
"github.com/docker/containerd/specs"
)
type Process interface {
@ -25,7 +27,7 @@ type Process interface {
// has not exited
ExitStatus() (int, error)
// Spec returns the process spec that created the process
Spec() ProcessSpec
Spec() specs.ProcessSpec
// Signal sends the provided signal to the process
Signal(os.Signal) error
// Container returns the container that the process belongs to
@ -39,8 +41,8 @@ type Process interface {
type processConfig struct {
id string
root string
processSpec ProcessSpec
spec *PlatformSpec
processSpec specs.ProcessSpec
spec *specs.PlatformSpec
c *container
stdio Stdio
exec bool
@ -118,7 +120,7 @@ type process struct {
exitPipe *os.File
controlPipe *os.File
container *container
spec ProcessSpec
spec specs.ProcessSpec
stdio Stdio
}
@ -163,7 +165,7 @@ func (p *process) ExitStatus() (int, error) {
return strconv.Atoi(string(data))
}
func (p *process) Spec() ProcessSpec {
func (p *process) Spec() specs.ProcessSpec {
return p.spec
}

View file

@ -3,6 +3,8 @@ package runtime
import (
"errors"
"time"
"github.com/docker/containerd/specs"
)
var (
@ -46,7 +48,7 @@ type state struct {
}
type ProcessState struct {
ProcessSpec
specs.ProcessSpec
Exec bool `json:"exec"`
Stdin string `json:"containerdStdin"`
Stdout string `json:"containerdStdout"`

View file

@ -1,8 +0,0 @@
package runtime
import "github.com/opencontainers/specs"
type (
PlatformSpec specs.LinuxSpec
ProcessSpec specs.Process
)

View file

@ -1,8 +0,0 @@
package runtime
// Temporary Windows version of the spec in lieu of opencontainers/specs having
// Windows support currently.
import "github.com/docker/containerd/specs"
type PlatformSpec specs.WindowsSpec
type ProcessSpec specs.Process

9
specs/spec_linux.go Normal file
View file

@ -0,0 +1,9 @@
package specs
import ocs "github.com/opencontainers/specs"
type (
PlatformSpec ocs.LinuxSpec
ProcessSpec ocs.Process
Spec ocs.Spec
)

View file

@ -1,5 +1,13 @@
package specs
// Temporary Windows version of the spec in lieu of opencontainers/specs having
// Windows support currently.
type (
PlatformSpec WindowsSpec
ProcessSpec Process
)
// This is a temporary module in lieu of opencontainers/specs being compatible
// currently on Windows.

View file

@ -1,3 +0,0 @@
// +build !windows
package specs

View file

@ -4,6 +4,7 @@ import (
"time"
"github.com/docker/containerd/runtime"
"github.com/docker/containerd/specs"
)
type AddProcessTask struct {
@ -13,7 +14,7 @@ type AddProcessTask struct {
Stdout string
Stderr string
Stdin string
ProcessSpec *runtime.ProcessSpec
ProcessSpec *specs.ProcessSpec
StartResponse chan StartResponse
}

View file

@ -6,6 +6,7 @@ import (
"testing"
"github.com/docker/containerd/runtime"
"github.com/docker/containerd/specs"
)
type testProcess struct {
@ -44,8 +45,8 @@ func (p *testProcess) Container() runtime.Container {
return nil
}
func (p *testProcess) Spec() runtime.ProcessSpec {
return runtime.ProcessSpec{}
func (p *testProcess) Spec() specs.ProcessSpec {
return specs.ProcessSpec{}
}
func (p *testProcess) Signal(os.Signal) error {