Skip to content

Commit

Permalink
Add logger to container and factory
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
  • Loading branch information
crosbymichael authored and vmarmol committed Dec 5, 2014
1 parent d5b8418 commit 47b41a6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
9 changes: 9 additions & 0 deletions cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type CgroupManager interface {
String() string
GetPids(*cgroups.Cgroup) ([]int, error)
GetStats(*cgroups.Cgroup) (*cgroups.Stats, error)
}
Expand All @@ -29,6 +30,10 @@ func (m *systemdCgroupManager) GetStats(config *cgroups.Cgroup) (*cgroups.Stats,
return systemd.GetStats(config)
}

func (m *systemdCgroupManager) String() string {
return "systemd"
}

type fsCgroupsManager struct {
}

Expand All @@ -39,3 +44,7 @@ func (m *fsCgroupsManager) GetPids(config *cgroups.Cgroup) ([]int, error) {
func (m *fsCgroupsManager) GetStats(config *cgroups.Cgroup) (*cgroups.Stats, error) {
return fs.GetStats(config)
}

func (m *fsCgroupsManager) String() string {
return "fs"
}
15 changes: 14 additions & 1 deletion linux_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

package libcontainer

import "github.com/docker/libcontainer/network"
import (
"github.com/Sirupsen/logrus"
"github.com/docker/libcontainer/network"
)

type linuxContainer struct {
id string
root string
config *Config
state *State
cgroupManager CgroupManager
logger *logrus.Logger
}

func (c *linuxContainer) ID() string {
Expand All @@ -25,6 +29,7 @@ func (c *linuxContainer) RunState() (RunState, error) {
}

func (c *linuxContainer) Processes() ([]int, error) {
c.logger.Debug("fetch container processes")
pids, err := c.cgroupManager.GetPids(c.config.Cgroups)
if err != nil {
return nil, newGenericError(err, SystemError)
Expand All @@ -33,6 +38,7 @@ func (c *linuxContainer) Processes() ([]int, error) {
}

func (c *linuxContainer) Stats() (*ContainerStats, error) {
c.logger.Debug("fetch container stats")
var (
err error
stats = &ContainerStats{}
Expand All @@ -48,29 +54,36 @@ func (c *linuxContainer) Stats() (*ContainerStats, error) {
}

func (c *linuxContainer) StartProcess(config *ProcessConfig) (int, error) {
c.logger.Debug("start new container process")
panic("not implemented")
}

func (c *linuxContainer) Destroy() error {
c.logger.Debug("destroy container")
panic("not implemented")
}

func (c *linuxContainer) Pause() error {
c.logger.Debug("pause container")
panic("not implemented")
}

func (c *linuxContainer) Resume() error {
c.logger.Debug("resume container")
panic("not implemented")
}

func (c *linuxContainer) Signal(pid, signal int) error {
c.logger.Debugf("sending signal %d to pid %d", signal, pid)
panic("not implemented")
}

func (c *linuxContainer) Wait() (int, error) {
c.logger.Debug("wait container")
panic("not implemented")
}

func (c *linuxContainer) WaitProcess(pid int) (int, error) {
c.logger.Debugf("wait process %d", pid)
panic("not implemented")
}
17 changes: 14 additions & 3 deletions linux_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"os"
"path/filepath"

"github.com/Sirupsen/logrus"
)

const (
Expand All @@ -14,20 +16,24 @@ const (
)

// New returns a linux based container factory based in the root directory.
func New(root string) (Factory, error) {
func New(root string, logger *logrus.Logger) (Factory, error) {
if err := os.MkdirAll(root, 0700); err != nil {
return nil, newGenericError(err, SystemError)
}

return &linuxFactory{
root: root,
root: root,
logger: logger,
}, nil
}

// linuxFactory implements the default factory interface for linux based systems.
type linuxFactory struct {
// root is the root directory
root string

// standard logger for all packages
logger *logrus.Logger
}

func (l *linuxFactory) Create(id string, config *Config) (Container, error) {
Expand All @@ -36,22 +42,27 @@ func (l *linuxFactory) Create(id string, config *Config) (Container, error) {

func (l *linuxFactory) Load(id string) (Container, error) {
containerRoot := filepath.Join(l.root, id)
l.logger.Debugf("loading container config from %s", containerRoot)
config, err := l.loadContainerConfig(containerRoot)
if err != nil {
return nil, err
}

l.logger.Debugf("loading container state from %s", containerRoot)
state, err := l.loadContainerState(containerRoot)
if err != nil {
return nil, err
}

cgroupManager := newCgroupsManager()
l.logger.Debugf("using %s as cgroup manager", cgroupManager)
return &linuxContainer{
id: id,
root: containerRoot,
config: config,
state: state,
cgroupManager: newCgroupsManager(),
cgroupManager: cgroupManager,
logger: l.logger,
}, nil
}

Expand Down
2 changes: 2 additions & 0 deletions nsinit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"os"
"strings"

"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)

var (
logPath = os.Getenv("log")
logger = logrus.New()
argvs = make(map[string]*rFunc)
)

Expand Down
2 changes: 1 addition & 1 deletion nsinit/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var statsCommand = cli.Command{
}

func statsAction(context *cli.Context) {
factory, err := libcontainer.New(context.GlobalString("root"))
factory, err := libcontainer.New(context.GlobalString("root"), logger)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 47b41a6

Please sign in to comment.