-
Notifications
You must be signed in to change notification settings - Fork 18.7k
/
rename.go
129 lines (108 loc) · 3.44 KB
/
rename.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package daemon // import "github.com/docker/docker/daemon"
import (
"context"
"strings"
"github.com/containerd/log"
"github.com/docker/docker/api/types/events"
dockercontainer "github.com/docker/docker/container"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/libnetwork"
"github.com/pkg/errors"
)
// ContainerRename changes the name of a container, using the oldName
// to find the container. An error is returned if newName is already
// reserved.
func (daemon *Daemon) ContainerRename(oldName, newName string) (retErr error) {
var (
sid string
sb *libnetwork.Sandbox
)
if oldName == "" || newName == "" {
return errdefs.InvalidParameter(errors.New("Neither old nor new names may be empty"))
}
if newName[0] != '/' {
newName = "/" + newName
}
container, err := daemon.GetContainer(oldName)
if err != nil {
return err
}
container.Lock()
defer container.Unlock()
oldName = container.Name
oldIsAnonymousEndpoint := container.NetworkSettings.IsAnonymousEndpoint
if oldName == newName {
return errdefs.InvalidParameter(errors.New("Renaming a container with the same name as its current name"))
}
links := map[string]*dockercontainer.Container{}
for k, v := range daemon.linkIndex.children(container) {
if !strings.HasPrefix(k, oldName) {
return errdefs.InvalidParameter(errors.Errorf("Linked container %s does not match parent %s", k, oldName))
}
links[strings.TrimPrefix(k, oldName)] = v
}
if newName, err = daemon.reserveName(container.ID, newName); err != nil {
return errors.Wrap(err, "Error when allocating new name")
}
for k, v := range links {
daemon.containersReplica.ReserveName(newName+k, v.ID)
daemon.linkIndex.link(container, v, newName+k)
}
container.Name = newName
container.NetworkSettings.IsAnonymousEndpoint = false
defer func() {
if retErr != nil {
container.Name = oldName
container.NetworkSettings.IsAnonymousEndpoint = oldIsAnonymousEndpoint
daemon.reserveName(container.ID, oldName)
for k, v := range links {
daemon.containersReplica.ReserveName(oldName+k, v.ID)
daemon.linkIndex.link(container, v, oldName+k)
daemon.linkIndex.unlink(newName+k, v, container)
daemon.containersReplica.ReleaseName(newName + k)
}
daemon.releaseName(newName)
} else {
daemon.releaseName(oldName)
}
}()
for k, v := range links {
daemon.linkIndex.unlink(oldName+k, v, container)
daemon.containersReplica.ReleaseName(oldName + k)
}
if err = container.CheckpointTo(daemon.containersReplica); err != nil {
return err
}
attributes := map[string]string{
"oldName": oldName,
}
if !container.Running {
daemon.LogContainerEventWithAttributes(container, events.ActionRename, attributes)
return nil
}
defer func() {
if retErr != nil {
container.Name = oldName
container.NetworkSettings.IsAnonymousEndpoint = oldIsAnonymousEndpoint
if err := container.CheckpointTo(daemon.containersReplica); err != nil {
log.G(context.TODO()).WithFields(log.Fields{
"containerID": container.ID,
"error": err,
}).Error("failed to write container state to disk during rename")
}
}
}()
sid = container.NetworkSettings.SandboxID
if sid != "" && daemon.netController != nil {
sb, err = daemon.netController.SandboxByID(sid)
if err != nil {
return err
}
err = sb.Rename(strings.TrimPrefix(container.Name, "/"))
if err != nil {
return err
}
}
daemon.LogContainerEventWithAttributes(container, events.ActionRename, attributes)
return nil
}