mirror of https://github.com/docker/docs.git
Merge pull request #2549 from dotcloud/ensure-container-name
Ensure container name on register
This commit is contained in:
commit
c065e564fb
43
runtime.go
43
runtime.go
|
@ -113,6 +113,9 @@ func (runtime *Runtime) Register(container *Container) error {
|
||||||
if err := validateID(container.ID); err != nil {
|
if err := validateID(container.ID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := runtime.ensureName(container); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// init the wait lock
|
// init the wait lock
|
||||||
container.waitLock = make(chan struct{})
|
container.waitLock = make(chan struct{})
|
||||||
|
@ -174,6 +177,26 @@ func (runtime *Runtime) Register(container *Container) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (runtime *Runtime) ensureName(container *Container) error {
|
||||||
|
if container.Name == "" {
|
||||||
|
name, err := generateRandomName(runtime)
|
||||||
|
if err != nil {
|
||||||
|
name = container.ShortID()
|
||||||
|
}
|
||||||
|
container.Name = name
|
||||||
|
|
||||||
|
if err := container.ToDisk(); err != nil {
|
||||||
|
utils.Debugf("Error saving container name %s", err)
|
||||||
|
}
|
||||||
|
if !runtime.containerGraph.Exists(name) {
|
||||||
|
if _, err := runtime.containerGraph.Set(name, container.ID); err != nil {
|
||||||
|
utils.Debugf("Setting default id - %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (runtime *Runtime) LogToDisk(src *utils.WriteBroadcaster, dst, stream string) error {
|
func (runtime *Runtime) LogToDisk(src *utils.WriteBroadcaster, dst, stream string) error {
|
||||||
log, err := os.OpenFile(dst, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600)
|
log, err := os.OpenFile(dst, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -518,15 +541,22 @@ func (runtime *Runtime) Commit(container *Container, repository, tag, comment, a
|
||||||
return img, nil
|
return img, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (runtime *Runtime) getFullName(name string) string {
|
func (runtime *Runtime) getFullName(name string) (string, error) {
|
||||||
|
if name == "" {
|
||||||
|
return "", fmt.Errorf("Container name cannot be empty")
|
||||||
|
}
|
||||||
if name[0] != '/' {
|
if name[0] != '/' {
|
||||||
name = "/" + name
|
name = "/" + name
|
||||||
}
|
}
|
||||||
return name
|
return name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (runtime *Runtime) GetByName(name string) (*Container, error) {
|
func (runtime *Runtime) GetByName(name string) (*Container, error) {
|
||||||
entity := runtime.containerGraph.Get(runtime.getFullName(name))
|
fullName, err := runtime.getFullName(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
entity := runtime.containerGraph.Get(fullName)
|
||||||
if entity == nil {
|
if entity == nil {
|
||||||
return nil, fmt.Errorf("Could not find entity for %s", name)
|
return nil, fmt.Errorf("Could not find entity for %s", name)
|
||||||
}
|
}
|
||||||
|
@ -538,10 +568,13 @@ func (runtime *Runtime) GetByName(name string) (*Container, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (runtime *Runtime) Children(name string) (map[string]*Container, error) {
|
func (runtime *Runtime) Children(name string) (map[string]*Container, error) {
|
||||||
name = runtime.getFullName(name)
|
name, err := runtime.getFullName(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
children := make(map[string]*Container)
|
children := make(map[string]*Container)
|
||||||
|
|
||||||
err := runtime.containerGraph.Walk(name, func(p string, e *gograph.Entity) error {
|
err = runtime.containerGraph.Walk(name, func(p string, e *gograph.Entity) error {
|
||||||
c := runtime.Get(e.ID())
|
c := runtime.Get(e.ID())
|
||||||
if c == nil {
|
if c == nil {
|
||||||
return fmt.Errorf("Could not get container for name %s and id %s", e.ID(), p)
|
return fmt.Errorf("Could not get container for name %s and id %s", e.ID(), p)
|
||||||
|
|
|
@ -119,7 +119,7 @@ func init() {
|
||||||
|
|
||||||
func setupBaseImage() {
|
func setupBaseImage() {
|
||||||
config := &DaemonConfig{
|
config := &DaemonConfig{
|
||||||
Root: unitTestStoreBase,
|
Root: unitTestStoreBase,
|
||||||
AutoRestart: false,
|
AutoRestart: false,
|
||||||
BridgeIface: unitTestNetworkBridge,
|
BridgeIface: unitTestNetworkBridge,
|
||||||
}
|
}
|
||||||
|
@ -828,3 +828,19 @@ func TestGetAllChildren(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetFullName(t *testing.T) {
|
||||||
|
runtime := mkRuntime(t)
|
||||||
|
defer nuke(runtime)
|
||||||
|
|
||||||
|
name, err := runtime.getFullName("testing")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if name != "/testing" {
|
||||||
|
t.Fatalf("Expected /testing got %s", name)
|
||||||
|
}
|
||||||
|
if _, err := runtime.getFullName(""); err == nil {
|
||||||
|
t.Fatal("Error should not be nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1055,7 +1055,10 @@ func (srv *Server) ContainerDestroy(name string, removeVolume, removeLink bool)
|
||||||
if container == nil {
|
if container == nil {
|
||||||
return fmt.Errorf("No such link: %s", name)
|
return fmt.Errorf("No such link: %s", name)
|
||||||
}
|
}
|
||||||
name = srv.runtime.getFullName(name)
|
name, err := srv.runtime.getFullName(name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
parent, n := path.Split(name)
|
parent, n := path.Split(name)
|
||||||
if parent == "/" {
|
if parent == "/" {
|
||||||
return fmt.Errorf("Conflict, cannot remove the default name of the container")
|
return fmt.Errorf("Conflict, cannot remove the default name of the container")
|
||||||
|
|
Loading…
Reference in New Issue