container.Name -> container.Id

This commit is contained in:
Andrea Luzzardi 2013-01-21 18:39:52 -08:00
parent 13cd4650fd
commit 78c02daf47
4 changed files with 25 additions and 25 deletions

View File

@ -14,7 +14,7 @@ import (
)
type Container struct {
Name string
Id string
Root string
Path string
Args []string
@ -34,9 +34,9 @@ type Config struct {
Ram int64
}
func createContainer(name string, root string, command string, args []string, layers []string, config *Config) (*Container, error) {
func createContainer(id string, root string, command string, args []string, layers []string, config *Config) (*Container, error) {
container := &Container{
Name: name,
Id: id,
Root: root,
Path: command,
Args: args,
@ -110,7 +110,7 @@ func (container *Container) Start() error {
}
params := []string{
"-n", container.Name,
"-n", container.Id,
"-f", container.lxcConfigPath,
"--",
container.Path,
@ -134,7 +134,7 @@ func (container *Container) Start() error {
//
// This is a rare race condition that happens for short lived programs
for retries := 0; retries < 3; retries++ {
err := exec.Command("/usr/bin/lxc-wait", "-n", container.Name, "-s", "RUNNING|STOPPED").Run()
err := exec.Command("/usr/bin/lxc-wait", "-n", container.Id, "-s", "RUNNING|STOPPED").Run()
if err == nil {
return nil
}
@ -184,7 +184,7 @@ func (container *Container) monitor() {
container.stdout.Close()
container.stderr.Close()
if err := container.Filesystem.Umount(); err != nil {
log.Printf("%v: Failed to umount filesystem: %v", container.Name, err)
log.Printf("%v: Failed to umount filesystem: %v", container.Id, err)
}
// Report status back
@ -193,12 +193,12 @@ func (container *Container) monitor() {
func (container *Container) kill() error {
// This will cause the main container process to receive a SIGKILL
if err := exec.Command("/usr/bin/lxc-stop", "-n", container.Name).Run(); err != nil {
if err := exec.Command("/usr/bin/lxc-stop", "-n", container.Id).Run(); err != nil {
return err
}
// Wait for the container to be actually stopped
if err := exec.Command("/usr/bin/lxc-wait", "-n", container.Name, "-s", "STOPPED").Run(); err != nil {
if err := exec.Command("/usr/bin/lxc-wait", "-n", container.Id, "-s", "STOPPED").Run(); err != nil {
return err
}
return nil
@ -217,13 +217,13 @@ func (container *Container) Stop() error {
}
// 1. Send a SIGTERM
if err := exec.Command("/usr/bin/lxc-kill", "-n", container.Name, "15").Run(); err != nil {
if err := exec.Command("/usr/bin/lxc-kill", "-n", container.Id, "15").Run(); err != nil {
return err
}
// 2. Wait for the process to exit on its own
if err := container.WaitTimeout(10 * time.Second); err != nil {
log.Printf("Container %v failed to exit within 10 seconds of SIGTERM", container.Name)
log.Printf("Container %v failed to exit within 10 seconds of SIGTERM", container.Id)
}
// 3. Force kill

View File

@ -22,34 +22,34 @@ func (docker *Docker) List() []*Container {
return containers
}
func (docker *Docker) getContainerElement(name string) *list.Element {
func (docker *Docker) getContainerElement(id string) *list.Element {
for e := docker.containers.Front(); e != nil; e = e.Next() {
container := e.Value.(*Container)
if container.Name == name {
if container.Id == id {
return e
}
}
return nil
}
func (docker *Docker) Get(name string) *Container {
e := docker.getContainerElement(name)
func (docker *Docker) Get(id string) *Container {
e := docker.getContainerElement(id)
if e == nil {
return nil
}
return e.Value.(*Container)
}
func (docker *Docker) Exists(name string) bool {
return docker.Get(name) != nil
func (docker *Docker) Exists(id string) bool {
return docker.Get(id) != nil
}
func (docker *Docker) Create(name string, command string, args []string, layers []string, config *Config) (*Container, error) {
if docker.Exists(name) {
return nil, fmt.Errorf("Container %v already exists", name)
func (docker *Docker) Create(id string, command string, args []string, layers []string, config *Config) (*Container, error) {
if docker.Exists(id) {
return nil, fmt.Errorf("Container %v already exists", id)
}
root := path.Join(docker.repository, name)
container, err := createContainer(name, root, command, args, layers, config)
root := path.Join(docker.repository, id)
container, err := createContainer(id, root, command, args, layers, config)
if err != nil {
return nil, err
}
@ -58,9 +58,9 @@ func (docker *Docker) Create(name string, command string, args []string, layers
}
func (docker *Docker) Destroy(container *Container) error {
element := docker.getContainerElement(container.Name)
element := docker.getContainerElement(container.Id)
if element == nil {
return fmt.Errorf("Container %v not found - maybe it was already destroyed?", container.Name)
return fmt.Errorf("Container %v not found - maybe it was already destroyed?", container.Id)
}
if err := container.Stop(); err != nil {

View File

@ -51,7 +51,7 @@ func TestCreate(t *testing.T) {
}
// Make sure the container List() returns is the right one
if docker.List()[0].Name != "test_create" {
if docker.List()[0].Id != "test_create" {
t.Errorf("Unexpected container %v returned by List", docker.List()[0])
}

View File

@ -9,7 +9,7 @@ const LxcTemplate = `
{{if .Config.Hostname}}
lxc.utsname = {{.Config.Hostname}}
{{else}}
lxc.utsname = {{.Name}}
lxc.utsname = {{.Id}}
{{end}}
#lxc.aa_profile = unconfined