Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.go → configs/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package libcontainer
package configs

import (
"github.com/docker/libcontainer/cgroups"
Expand Down
4 changes: 2 additions & 2 deletions config_test.go → configs/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package libcontainer
package configs

import (
"encoding/json"
Expand Down Expand Up @@ -34,7 +34,7 @@ func containsDevice(expected *devices.Device, values []*devices.Device) bool {
}

func loadConfig(name string) (*Config, error) {
f, err := os.Open(filepath.Join("sample_configs", name))
f, err := os.Open(filepath.Join("../sample_configs", name))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion state.go → configs/state.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package libcontainer
package configs

import (
"encoding/json"
Expand Down
8 changes: 6 additions & 2 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ NOTE: The API is in flux and mainly not implemented. Proceed with caution until
*/
package libcontainer

import (
"github.com/docker/libcontainer/configs"
)

// A libcontainer container object.
//
// Each container is thread-safe within the same process. Since a container can
Expand All @@ -17,10 +21,10 @@ type Container interface {
// errors:
// ContainerDestroyed - Container no longer exists,
// Systemerror - System error.
RunState() (RunState, error)
RunState() (configs.RunState, error)

// Returns the current config of the container.
Config() *Config
Config() *configs.Config

// Returns the PIDs inside this container. The PIDs are in the namespace of the calling process.
//
Expand Down
6 changes: 5 additions & 1 deletion factory.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package libcontainer

import (
"github.com/docker/libcontainer/configs"
)

type Factory interface {
// Creates a new container with the given id and starts the initial process inside it.
// id must be a string containing only letters, digits and underscores and must contain
Expand All @@ -17,7 +21,7 @@ type Factory interface {
// Systemerror - System error
//
// On error, any partially created container parts are cleaned up (the operation is atomic).
Create(id string, config *Config) (Container, error)
Create(id string, config *configs.Config) (Container, error)

// Load takes an ID for an existing container and returns the container information
// from the state. This presents a read only view of the container.
Expand Down
4 changes: 2 additions & 2 deletions integration/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"testing"

"github.com/docker/libcontainer"
"github.com/docker/libcontainer/configs"
)

func TestExecPS(t *testing.T) {
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestRlimit(t *testing.T) {
}
}

func getNamespaceIndex(config *libcontainer.Config, name string) int {
func getNamespaceIndex(config *configs.Config, name string) int {
for i, v := range config.Namespaces {
if v.Name == name {
return i
Expand Down
12 changes: 1 addition & 11 deletions integration/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@ func init() {
}
runtime.LockOSThread()

container, err := loadConfig()
if err != nil {
log.Fatal(err)
}

rootfs, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

if err := namespaces.Init(container, rootfs, "", os.NewFile(3, "pipe"), os.Args[3:]); err != nil {
if err := namespaces.Init(os.NewFile(3, "pipe")); err != nil {
log.Fatalf("unable to initialize for container: %s", err)
}
os.Exit(1)
Expand Down
14 changes: 7 additions & 7 deletions integration/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package integration
import (
"syscall"

"github.com/docker/libcontainer"
"github.com/docker/libcontainer/cgroups"
"github.com/docker/libcontainer/configs"
"github.com/docker/libcontainer/devices"
)

// newTemplateConfig returns a base template for running a container
//
// it uses a network strategy of just setting a loopback interface
// and the default setup for devices
func newTemplateConfig(rootfs string) *libcontainer.Config {
return &libcontainer.Config{
func newTemplateConfig(rootfs string) *configs.Config {
return &configs.Config{
RootFs: rootfs,
Tty: false,
Capabilities: []string{
Expand All @@ -32,7 +32,7 @@ func newTemplateConfig(rootfs string) *libcontainer.Config {
"KILL",
"AUDIT_WRITE",
},
Namespaces: []libcontainer.Namespace{
Namespaces: []configs.Namespace{
{Name: "NEWNS"},
{Name: "NEWUTS"},
{Name: "NEWIPC"},
Expand All @@ -45,7 +45,7 @@ func newTemplateConfig(rootfs string) *libcontainer.Config {
AllowedDevices: devices.DefaultAllowedDevices,
},

MountConfig: &libcontainer.MountConfig{
MountConfig: &configs.MountConfig{
DeviceNodes: devices.DefaultAutoCreatedDevices,
},
Hostname: "integration",
Expand All @@ -55,14 +55,14 @@ func newTemplateConfig(rootfs string) *libcontainer.Config {
"HOSTNAME=integration",
"TERM=xterm",
},
Networks: []*libcontainer.Network{
Networks: []*configs.Network{
{
Type: "loopback",
Address: "127.0.0.1/0",
Gateway: "localhost",
},
},
Rlimits: []libcontainer.Rlimit{
Rlimits: []configs.Rlimit{
{
Type: syscall.RLIMIT_NOFILE,
Hard: uint64(1024),
Expand Down
57 changes: 50 additions & 7 deletions integration/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"os"
"os/exec"
"path/filepath"
"syscall"

"github.com/docker/libcontainer"
"github.com/docker/libcontainer/namespaces"
"github.com/docker/libcontainer/configs"
)

func newStdBuffers() *stdBuffers {
Expand All @@ -27,7 +28,7 @@ type stdBuffers struct {
Stderr *bytes.Buffer
}

func writeConfig(config *libcontainer.Config) error {
func writeConfig(config *configs.Config) error {
f, err := os.OpenFile(filepath.Join(config.RootFs, "container.json"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
if err != nil {
return err
Expand All @@ -36,14 +37,14 @@ func writeConfig(config *libcontainer.Config) error {
return json.NewEncoder(f).Encode(config)
}

func loadConfig() (*libcontainer.Config, error) {
func loadConfig() (*configs.Config, error) {
f, err := os.Open(filepath.Join(os.Getenv("data_path"), "container.json"))
if err != nil {
return nil, err
}
defer f.Close()

var container *libcontainer.Config
var container *configs.Config
if err := json.NewDecoder(f).Decode(&container); err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,13 +84,55 @@ func copyBusybox(dest string) error {
//
// buffers are returned containing the STDOUT and STDERR output for the run
// along with the exit code and any go error
func runContainer(config *libcontainer.Config, console string, args ...string) (buffers *stdBuffers, exitCode int, err error) {
func runContainer(config *configs.Config, console string, args ...string) (buffers *stdBuffers, exitCode int, err error) {
if err := writeConfig(config); err != nil {
return nil, -1, err
}

buffers = newStdBuffers()
exitCode, err = namespaces.Exec(config, buffers.Stdin, buffers.Stdout, buffers.Stderr,
console, config.RootFs, args, namespaces.DefaultCreateCommand, nil)

process := &libcontainer.ProcessConfig{
Args: args,
Env: make([]string, 0),
Stdin: buffers.Stdin,
Stdout: buffers.Stdout,
Stderr: buffers.Stderr,
}

factory, err := libcontainer.New(".", []string{os.Args[0], "init", "--"})
if err != nil {
return nil, -1, err
}

container, err := factory.Create("testCT", config)
if err != nil {
return nil, -1, err
}
defer container.Destroy()

pid, err := container.StartProcess(process)
if err != nil {
return nil, -1, err
}

p, err := os.FindProcess(pid)
if err != nil {
return nil, -1, err
}

ps, err := p.Wait()
if err != nil {
return nil, -1, err
}

status := ps.Sys().(syscall.WaitStatus)
if status.Exited() {
exitCode = status.ExitStatus()
} else if status.Signaled() {
exitCode = -int(status.Signal())
} else {
return nil, -1, err
}

return
}
37 changes: 18 additions & 19 deletions linux_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (
"path/filepath"
"syscall"

"github.com/docker/libcontainer/configs"
"github.com/docker/libcontainer/namespaces"
"github.com/docker/libcontainer/network"
"github.com/golang/glog"
)

type linuxContainer struct {
id string
root string
config *Config
state *State
config *configs.Config
state *configs.State
cgroupManager CgroupManager
initArgs []string
}
Expand All @@ -27,12 +29,12 @@ func (c *linuxContainer) ID() string {
return c.id
}

func (c *linuxContainer) Config() *Config {
func (c *linuxContainer) Config() *configs.Config {
return c.config
}

func (c *linuxContainer) RunState() (RunState, error) {
return Destroyed, nil // FIXME return a real state
func (c *linuxContainer) RunState() (configs.RunState, error) {
return configs.Destroyed, nil // FIXME return a real state
}

func (c *linuxContainer) Processes() ([]int, error) {
Expand Down Expand Up @@ -60,39 +62,35 @@ func (c *linuxContainer) Stats() (*ContainerStats, error) {
return stats, nil
}

func (c *linuxContainer) StartProcess(config *ProcessConfig) (int, error) {
func (c *linuxContainer) StartProcess(pconfig *ProcessConfig) (int, error) {
state, err := c.RunState()
if err != nil {
return -1, err
}

if state != Destroyed {
if state != configs.Destroyed {
glog.Info("start new container process")
panic("not implemented")
}

if err := c.startInitProcess(config); err != nil {
if err := c.startInitProcess(pconfig); err != nil {
return -1, err
}

return c.state.InitPid, nil
}

func (c *linuxContainer) updateStateFile() error {
data, err := json.MarshalIndent(c.state, "", "\t")
if err != nil {
return newGenericError(err, SystemError)
}

fnew := filepath.Join(c.root, fmt.Sprintf("%s.new", stateFilename))
f, err := os.Create(fnew)
if err != nil {
return newGenericError(err, SystemError)
}

_, err = f.Write(data)
err = json.NewEncoder(f).Encode(c.state)
if err != nil {
f.Close()
os.Remove(fnew)
return newGenericError(err, SystemError)
}
f.Close()
Expand All @@ -118,16 +116,17 @@ func (c *linuxContainer) startInitProcess(config *ProcessConfig) error {
cmd.SysProcAttr = &syscall.SysProcAttr{}
}

cmd.SysProcAttr.Cloneflags = uintptr(namespaces.GetNamespaceFlags(c.config.Namespaces))
cmd.SysProcAttr.Pdeathsig = syscall.SIGKILL

//FIXME call namespaces.Exec()
if err := cmd.Start(); err != nil {
err := namespaces.Exec(config.Args, config.Env, cmd, c.config, c.state)
if err != nil {
return err
}

c.state.InitPid = cmd.Process.Pid
err := c.updateStateFile()
err = c.updateStateFile()
if err != nil {
// FIXME c.Kill()
return err
}

Expand All @@ -140,7 +139,7 @@ func (c *linuxContainer) Destroy() error {
return err
}

if state != Destroyed {
if state != configs.Destroyed {
return newGenericError(nil, ContainerNotStopped)
}

Expand Down
Loading