Skip to content

Commit fb48bf5

Browse files
committed
Move some container related methods and structs to smaller files
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent ce07eac commit fb48bf5

File tree

7 files changed

+279
-258
lines changed

7 files changed

+279
-258
lines changed

daemon/changes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ func (daemon *Daemon) ContainerChanges(name string) ([]archive.Change, error) {
1111

1212
container.Lock()
1313
defer container.Unlock()
14-
return daemon.changes(container)
14+
return container.RWLayer.Changes()
1515
}

daemon/container.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package daemon
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
"time"
7+
8+
"github.com/docker/docker/container"
9+
"github.com/docker/docker/daemon/network"
10+
"github.com/docker/docker/errors"
11+
"github.com/docker/docker/image"
12+
"github.com/docker/docker/pkg/truncindex"
13+
containertypes "github.com/docker/engine-api/types/container"
14+
)
15+
16+
// GetContainer looks for a container using the provided information, which could be
17+
// one of the following inputs from the caller:
18+
// - A full container ID, which will exact match a container in daemon's list
19+
// - A container name, which will only exact match via the GetByName() function
20+
// - A partial container ID prefix (e.g. short ID) of any length that is
21+
// unique enough to only return a single container object
22+
// If none of these searches succeed, an error is returned
23+
func (daemon *Daemon) GetContainer(prefixOrName string) (*container.Container, error) {
24+
if len(prefixOrName) == 0 {
25+
return nil, errors.NewBadRequestError(fmt.Errorf("No container name or ID supplied"))
26+
}
27+
28+
if containerByID := daemon.containers.Get(prefixOrName); containerByID != nil {
29+
// prefix is an exact match to a full container ID
30+
return containerByID, nil
31+
}
32+
33+
// GetByName will match only an exact name provided; we ignore errors
34+
if containerByName, _ := daemon.GetByName(prefixOrName); containerByName != nil {
35+
// prefix is an exact match to a full container Name
36+
return containerByName, nil
37+
}
38+
39+
containerID, indexError := daemon.idIndex.Get(prefixOrName)
40+
if indexError != nil {
41+
// When truncindex defines an error type, use that instead
42+
if indexError == truncindex.ErrNotExist {
43+
err := fmt.Errorf("No such container: %s", prefixOrName)
44+
return nil, errors.NewRequestNotFoundError(err)
45+
}
46+
return nil, indexError
47+
}
48+
return daemon.containers.Get(containerID), nil
49+
}
50+
51+
// Exists returns a true if a container of the specified ID or name exists,
52+
// false otherwise.
53+
func (daemon *Daemon) Exists(id string) bool {
54+
c, _ := daemon.GetContainer(id)
55+
return c != nil
56+
}
57+
58+
// IsPaused returns a bool indicating if the specified container is paused.
59+
func (daemon *Daemon) IsPaused(id string) bool {
60+
c, _ := daemon.GetContainer(id)
61+
return c.State.IsPaused()
62+
}
63+
64+
func (daemon *Daemon) containerRoot(id string) string {
65+
return filepath.Join(daemon.repository, id)
66+
}
67+
68+
// Load reads the contents of a container from disk
69+
// This is typically done at startup.
70+
func (daemon *Daemon) load(id string) (*container.Container, error) {
71+
container := daemon.newBaseContainer(id)
72+
73+
if err := container.FromDisk(); err != nil {
74+
return nil, err
75+
}
76+
77+
if container.ID != id {
78+
return container, fmt.Errorf("Container %s is stored at %s", container.ID, id)
79+
}
80+
81+
return container, nil
82+
}
83+
84+
// Register makes a container object usable by the daemon as <container.ID>
85+
func (daemon *Daemon) Register(c *container.Container) error {
86+
// Attach to stdout and stderr
87+
if c.Config.OpenStdin {
88+
c.NewInputPipes()
89+
} else {
90+
c.NewNopInputPipe()
91+
}
92+
93+
daemon.containers.Add(c.ID, c)
94+
daemon.idIndex.Add(c.ID)
95+
96+
return nil
97+
}
98+
99+
func (daemon *Daemon) newContainer(name string, config *containertypes.Config, imgID image.ID) (*container.Container, error) {
100+
var (
101+
id string
102+
err error
103+
noExplicitName = name == ""
104+
)
105+
id, name, err = daemon.generateIDAndName(name)
106+
if err != nil {
107+
return nil, err
108+
}
109+
110+
daemon.generateHostname(id, config)
111+
entrypoint, args := daemon.getEntrypointAndArgs(config.Entrypoint, config.Cmd)
112+
113+
base := daemon.newBaseContainer(id)
114+
base.Created = time.Now().UTC()
115+
base.Path = entrypoint
116+
base.Args = args //FIXME: de-duplicate from config
117+
base.Config = config
118+
base.HostConfig = &containertypes.HostConfig{}
119+
base.ImageID = imgID
120+
base.NetworkSettings = &network.Settings{IsAnonymousEndpoint: noExplicitName}
121+
base.Name = name
122+
base.Driver = daemon.GraphDriverName()
123+
124+
return base, err
125+
}
126+
127+
// GetByName returns a container given a name.
128+
func (daemon *Daemon) GetByName(name string) (*container.Container, error) {
129+
if len(name) == 0 {
130+
return nil, fmt.Errorf("No container name supplied")
131+
}
132+
fullName := name
133+
if name[0] != '/' {
134+
fullName = "/" + name
135+
}
136+
id, err := daemon.nameIndex.Get(fullName)
137+
if err != nil {
138+
return nil, fmt.Errorf("Could not find entity for %s", name)
139+
}
140+
e := daemon.containers.Get(id)
141+
if e == nil {
142+
return nil, fmt.Errorf("Could not find container for entity id %s", id)
143+
}
144+
return e, nil
145+
}

daemon/create.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,15 @@ func (daemon *Daemon) VolumeCreate(name, driverName string, opts, labels map[str
218218
apiV.Mountpoint = v.Path()
219219
return apiV, nil
220220
}
221+
222+
func (daemon *Daemon) mergeAndVerifyConfig(config *containertypes.Config, img *image.Image) error {
223+
if img != nil && img.Config != nil {
224+
if err := merge(config, img.Config); err != nil {
225+
return err
226+
}
227+
}
228+
if len(config.Entrypoint) == 0 && len(config.Cmd) == 0 {
229+
return fmt.Errorf("No command specified")
230+
}
231+
return nil
232+
}

0 commit comments

Comments
 (0)