|
| 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 | +} |
0 commit comments