зеркало из https://github.com/microsoft/docker.git
container.Name -> container.Id
This commit is contained in:
Родитель
13cd4650fd
Коммит
78c02daf47
20
container.go
20
container.go
|
@ -14,7 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Container struct {
|
type Container struct {
|
||||||
Name string
|
Id string
|
||||||
Root string
|
Root string
|
||||||
Path string
|
Path string
|
||||||
Args []string
|
Args []string
|
||||||
|
@ -34,9 +34,9 @@ type Config struct {
|
||||||
Ram int64
|
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{
|
container := &Container{
|
||||||
Name: name,
|
Id: id,
|
||||||
Root: root,
|
Root: root,
|
||||||
Path: command,
|
Path: command,
|
||||||
Args: args,
|
Args: args,
|
||||||
|
@ -110,7 +110,7 @@ func (container *Container) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
params := []string{
|
params := []string{
|
||||||
"-n", container.Name,
|
"-n", container.Id,
|
||||||
"-f", container.lxcConfigPath,
|
"-f", container.lxcConfigPath,
|
||||||
"--",
|
"--",
|
||||||
container.Path,
|
container.Path,
|
||||||
|
@ -134,7 +134,7 @@ func (container *Container) Start() error {
|
||||||
//
|
//
|
||||||
// This is a rare race condition that happens for short lived programs
|
// This is a rare race condition that happens for short lived programs
|
||||||
for retries := 0; retries < 3; retries++ {
|
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 {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -184,7 +184,7 @@ func (container *Container) monitor() {
|
||||||
container.stdout.Close()
|
container.stdout.Close()
|
||||||
container.stderr.Close()
|
container.stderr.Close()
|
||||||
if err := container.Filesystem.Umount(); err != nil {
|
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
|
// Report status back
|
||||||
|
@ -193,12 +193,12 @@ func (container *Container) monitor() {
|
||||||
|
|
||||||
func (container *Container) kill() error {
|
func (container *Container) kill() error {
|
||||||
// This will cause the main container process to receive a SIGKILL
|
// 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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for the container to be actually stopped
|
// 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 err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -217,13 +217,13 @@ func (container *Container) Stop() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Send a SIGTERM
|
// 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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Wait for the process to exit on its own
|
// 2. Wait for the process to exit on its own
|
||||||
if err := container.WaitTimeout(10 * time.Second); err != nil {
|
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
|
// 3. Force kill
|
||||||
|
|
26
docker.go
26
docker.go
|
@ -22,34 +22,34 @@ func (docker *Docker) List() []*Container {
|
||||||
return containers
|
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() {
|
for e := docker.containers.Front(); e != nil; e = e.Next() {
|
||||||
container := e.Value.(*Container)
|
container := e.Value.(*Container)
|
||||||
if container.Name == name {
|
if container.Id == id {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (docker *Docker) Get(name string) *Container {
|
func (docker *Docker) Get(id string) *Container {
|
||||||
e := docker.getContainerElement(name)
|
e := docker.getContainerElement(id)
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return e.Value.(*Container)
|
return e.Value.(*Container)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (docker *Docker) Exists(name string) bool {
|
func (docker *Docker) Exists(id string) bool {
|
||||||
return docker.Get(name) != nil
|
return docker.Get(id) != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (docker *Docker) Create(name string, command string, args []string, layers []string, config *Config) (*Container, error) {
|
func (docker *Docker) Create(id string, command string, args []string, layers []string, config *Config) (*Container, error) {
|
||||||
if docker.Exists(name) {
|
if docker.Exists(id) {
|
||||||
return nil, fmt.Errorf("Container %v already exists", name)
|
return nil, fmt.Errorf("Container %v already exists", id)
|
||||||
}
|
}
|
||||||
root := path.Join(docker.repository, name)
|
root := path.Join(docker.repository, id)
|
||||||
container, err := createContainer(name, root, command, args, layers, config)
|
container, err := createContainer(id, root, command, args, layers, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
func (docker *Docker) Destroy(container *Container) error {
|
||||||
element := docker.getContainerElement(container.Name)
|
element := docker.getContainerElement(container.Id)
|
||||||
if element == nil {
|
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 {
|
if err := container.Stop(); err != nil {
|
||||||
|
|
|
@ -51,7 +51,7 @@ func TestCreate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure the container List() returns is the right one
|
// 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])
|
t.Errorf("Unexpected container %v returned by List", docker.List()[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ const LxcTemplate = `
|
||||||
{{if .Config.Hostname}}
|
{{if .Config.Hostname}}
|
||||||
lxc.utsname = {{.Config.Hostname}}
|
lxc.utsname = {{.Config.Hostname}}
|
||||||
{{else}}
|
{{else}}
|
||||||
lxc.utsname = {{.Name}}
|
lxc.utsname = {{.Id}}
|
||||||
{{end}}
|
{{end}}
|
||||||
#lxc.aa_profile = unconfined
|
#lxc.aa_profile = unconfined
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче