Add logger to container and factory

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2014-10-30 22:08:28 +00:00 committed by Victor Marmol
parent d5b8418f75
commit 47b41a6f5d
5 changed files with 40 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import (
) )
type CgroupManager interface { type CgroupManager interface {
String() string
GetPids(*cgroups.Cgroup) ([]int, error) GetPids(*cgroups.Cgroup) ([]int, error)
GetStats(*cgroups.Cgroup) (*cgroups.Stats, error) GetStats(*cgroups.Cgroup) (*cgroups.Stats, error)
} }
@ -29,6 +30,10 @@ func (m *systemdCgroupManager) GetStats(config *cgroups.Cgroup) (*cgroups.Stats,
return systemd.GetStats(config) return systemd.GetStats(config)
} }
func (m *systemdCgroupManager) String() string {
return "systemd"
}
type fsCgroupsManager struct { type fsCgroupsManager struct {
} }
@ -39,3 +44,7 @@ func (m *fsCgroupsManager) GetPids(config *cgroups.Cgroup) ([]int, error) {
func (m *fsCgroupsManager) GetStats(config *cgroups.Cgroup) (*cgroups.Stats, error) { func (m *fsCgroupsManager) GetStats(config *cgroups.Cgroup) (*cgroups.Stats, error) {
return fs.GetStats(config) return fs.GetStats(config)
} }
func (m *fsCgroupsManager) String() string {
return "fs"
}

View File

@ -2,7 +2,10 @@
package libcontainer package libcontainer
import "github.com/docker/libcontainer/network" import (
"github.com/Sirupsen/logrus"
"github.com/docker/libcontainer/network"
)
type linuxContainer struct { type linuxContainer struct {
id string id string
@ -10,6 +13,7 @@ type linuxContainer struct {
config *Config config *Config
state *State state *State
cgroupManager CgroupManager cgroupManager CgroupManager
logger *logrus.Logger
} }
func (c *linuxContainer) ID() string { func (c *linuxContainer) ID() string {
@ -25,6 +29,7 @@ func (c *linuxContainer) RunState() (RunState, error) {
} }
func (c *linuxContainer) Processes() ([]int, error) { func (c *linuxContainer) Processes() ([]int, error) {
c.logger.Debug("fetch container processes")
pids, err := c.cgroupManager.GetPids(c.config.Cgroups) pids, err := c.cgroupManager.GetPids(c.config.Cgroups)
if err != nil { if err != nil {
return nil, newGenericError(err, SystemError) return nil, newGenericError(err, SystemError)
@ -33,6 +38,7 @@ func (c *linuxContainer) Processes() ([]int, error) {
} }
func (c *linuxContainer) Stats() (*ContainerStats, error) { func (c *linuxContainer) Stats() (*ContainerStats, error) {
c.logger.Debug("fetch container stats")
var ( var (
err error err error
stats = &ContainerStats{} stats = &ContainerStats{}
@ -48,29 +54,36 @@ func (c *linuxContainer) Stats() (*ContainerStats, error) {
} }
func (c *linuxContainer) StartProcess(config *ProcessConfig) (int, error) { func (c *linuxContainer) StartProcess(config *ProcessConfig) (int, error) {
c.logger.Debug("start new container process")
panic("not implemented") panic("not implemented")
} }
func (c *linuxContainer) Destroy() error { func (c *linuxContainer) Destroy() error {
c.logger.Debug("destroy container")
panic("not implemented") panic("not implemented")
} }
func (c *linuxContainer) Pause() error { func (c *linuxContainer) Pause() error {
c.logger.Debug("pause container")
panic("not implemented") panic("not implemented")
} }
func (c *linuxContainer) Resume() error { func (c *linuxContainer) Resume() error {
c.logger.Debug("resume container")
panic("not implemented") panic("not implemented")
} }
func (c *linuxContainer) Signal(pid, signal int) error { func (c *linuxContainer) Signal(pid, signal int) error {
c.logger.Debugf("sending signal %d to pid %d", signal, pid)
panic("not implemented") panic("not implemented")
} }
func (c *linuxContainer) Wait() (int, error) { func (c *linuxContainer) Wait() (int, error) {
c.logger.Debug("wait container")
panic("not implemented") panic("not implemented")
} }
func (c *linuxContainer) WaitProcess(pid int) (int, error) { func (c *linuxContainer) WaitProcess(pid int) (int, error) {
c.logger.Debugf("wait process %d", pid)
panic("not implemented") panic("not implemented")
} }

View File

@ -6,6 +6,8 @@ import (
"encoding/json" "encoding/json"
"os" "os"
"path/filepath" "path/filepath"
"github.com/Sirupsen/logrus"
) )
const ( const (
@ -14,13 +16,14 @@ const (
) )
// New returns a linux based container factory based in the root directory. // 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 { if err := os.MkdirAll(root, 0700); err != nil {
return nil, newGenericError(err, SystemError) return nil, newGenericError(err, SystemError)
} }
return &linuxFactory{ return &linuxFactory{
root: root, root: root,
logger: logger,
}, nil }, nil
} }
@ -28,6 +31,9 @@ func New(root string) (Factory, error) {
type linuxFactory struct { type linuxFactory struct {
// root is the root directory // root is the root directory
root string root string
// standard logger for all packages
logger *logrus.Logger
} }
func (l *linuxFactory) Create(id string, config *Config) (Container, error) { func (l *linuxFactory) Create(id string, config *Config) (Container, error) {
@ -36,22 +42,27 @@ func (l *linuxFactory) Create(id string, config *Config) (Container, error) {
func (l *linuxFactory) Load(id string) (Container, error) { func (l *linuxFactory) Load(id string) (Container, error) {
containerRoot := filepath.Join(l.root, id) containerRoot := filepath.Join(l.root, id)
l.logger.Debugf("loading container config from %s", containerRoot)
config, err := l.loadContainerConfig(containerRoot) config, err := l.loadContainerConfig(containerRoot)
if err != nil { if err != nil {
return nil, err return nil, err
} }
l.logger.Debugf("loading container state from %s", containerRoot)
state, err := l.loadContainerState(containerRoot) state, err := l.loadContainerState(containerRoot)
if err != nil { if err != nil {
return nil, err return nil, err
} }
cgroupManager := newCgroupsManager()
l.logger.Debugf("using %s as cgroup manager", cgroupManager)
return &linuxContainer{ return &linuxContainer{
id: id, id: id,
root: containerRoot, root: containerRoot,
config: config, config: config,
state: state, state: state,
cgroupManager: newCgroupsManager(), cgroupManager: cgroupManager,
logger: l.logger,
}, nil }, nil
} }

View File

@ -5,11 +5,13 @@ import (
"os" "os"
"strings" "strings"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
) )
var ( var (
logPath = os.Getenv("log") logPath = os.Getenv("log")
logger = logrus.New()
argvs = make(map[string]*rFunc) argvs = make(map[string]*rFunc)
) )

View File

@ -16,7 +16,7 @@ var statsCommand = cli.Command{
} }
func statsAction(context *cli.Context) { func statsAction(context *cli.Context) {
factory, err := libcontainer.New(context.GlobalString("root")) factory, err := libcontainer.New(context.GlobalString("root"), logger)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }