diff --git a/buildfile.go b/buildfile.go index 6fae6a24a5..160db4d434 100644 --- a/buildfile.go +++ b/buildfile.go @@ -10,6 +10,7 @@ import ( "github.com/dotcloud/docker/auth" "github.com/dotcloud/docker/registry" "github.com/dotcloud/docker/runconfig" + "github.com/dotcloud/docker/runtime" "github.com/dotcloud/docker/utils" "io" "io/ioutil" @@ -34,7 +35,7 @@ type BuildFile interface { } type buildFile struct { - runtime *Runtime + runtime *runtime.Runtime srv *Server image string @@ -74,9 +75,9 @@ func (b *buildFile) clearTmp(containers map[string]struct{}) { } func (b *buildFile) CmdFrom(name string) error { - image, err := b.runtime.repositories.LookupImage(name) + image, err := b.runtime.Repositories().LookupImage(name) if err != nil { - if b.runtime.graph.IsNotExist(err) { + if b.runtime.Graph().IsNotExist(err) { remote, tag := utils.ParseRepositoryTag(name) pullRegistryAuth := b.authConfig if len(b.configFile.Configs) > 0 { @@ -96,7 +97,7 @@ func (b *buildFile) CmdFrom(name string) error { if err := job.Run(); err != nil { return err } - image, err = b.runtime.repositories.LookupImage(name) + image, err = b.runtime.Repositories().LookupImage(name) if err != nil { return err } @@ -110,7 +111,7 @@ func (b *buildFile) CmdFrom(name string) error { b.config = image.Config } if b.config.Env == nil || len(b.config.Env) == 0 { - b.config.Env = append(b.config.Env, "HOME=/", "PATH="+defaultPathEnv) + b.config.Env = append(b.config.Env, "HOME=/", "PATH="+runtime.DefaultPathEnv) } // Process ONBUILD triggers if they exist if nTriggers := len(b.config.OnBuild); nTriggers != 0 { @@ -371,7 +372,7 @@ func (b *buildFile) checkPathForAddition(orig string) error { return nil } -func (b *buildFile) addContext(container *Container, orig, dest string, remote bool) error { +func (b *buildFile) addContext(container *runtime.Container, orig, dest string, remote bool) error { var ( origPath = path.Join(b.contextPath, orig) destPath = path.Join(container.BasefsPath(), dest) @@ -604,7 +605,7 @@ func (sf *StderrFormater) Write(buf []byte) (int, error) { return len(buf), err } -func (b *buildFile) create() (*Container, error) { +func (b *buildFile) create() (*runtime.Container, error) { if b.image == "" { return nil, fmt.Errorf("Please provide a source image with `from` prior to run") } @@ -625,7 +626,7 @@ func (b *buildFile) create() (*Container, error) { return c, nil } -func (b *buildFile) run(c *Container) error { +func (b *buildFile) run(c *runtime.Container) error { var errCh chan error if b.verbose { diff --git a/config.go b/daemonconfig/config.go similarity index 93% rename from config.go rename to daemonconfig/config.go index 19aad9ed4a..0aee7e78ba 100644 --- a/config.go +++ b/daemonconfig/config.go @@ -1,4 +1,4 @@ -package docker +package daemonconfig import ( "net" @@ -13,7 +13,7 @@ const ( ) // FIXME: separate runtime configuration from http api configuration -type DaemonConfig struct { +type Config struct { Pidfile string Root string AutoRestart bool @@ -32,8 +32,8 @@ type DaemonConfig struct { // ConfigFromJob creates and returns a new DaemonConfig object // by parsing the contents of a job's environment. -func DaemonConfigFromJob(job *engine.Job) *DaemonConfig { - config := &DaemonConfig{ +func ConfigFromJob(job *engine.Job) *Config { + config := &Config{ Pidfile: job.Getenv("Pidfile"), Root: job.Getenv("Root"), AutoRestart: job.GetenvBool("AutoRestart"), diff --git a/graph.go b/graph/graph.go similarity index 85% rename from graph.go rename to graph/graph.go index 43af2c278a..01659b549f 100644 --- a/graph.go +++ b/graph/graph.go @@ -1,10 +1,11 @@ -package docker +package graph import ( "fmt" "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/dockerversion" "github.com/dotcloud/docker/graphdriver" + "github.com/dotcloud/docker/image" "github.com/dotcloud/docker/runconfig" "github.com/dotcloud/docker/utils" "io" @@ -79,20 +80,20 @@ func (graph *Graph) Exists(id string) bool { } // Get returns the image with the given id, or an error if the image doesn't exist. -func (graph *Graph) Get(name string) (*Image, error) { +func (graph *Graph) Get(name string) (*image.Image, error) { id, err := graph.idIndex.Get(name) if err != nil { return nil, err } // FIXME: return nil when the image doesn't exist, instead of an error - img, err := LoadImage(graph.imageRoot(id)) + img, err := image.LoadImage(graph.ImageRoot(id)) if err != nil { return nil, err } if img.ID != id { return nil, fmt.Errorf("Image stored at '%s' has wrong id '%s'", id, img.ID) } - img.graph = graph + img.SetGraph(graph) if img.Size < 0 { rootfs, err := graph.driver.Get(img.ID) @@ -119,7 +120,7 @@ func (graph *Graph) Get(name string) (*Image, error) { } img.Size = size - if err := img.SaveSize(graph.imageRoot(id)); err != nil { + if err := img.SaveSize(graph.ImageRoot(id)); err != nil { return nil, err } } @@ -127,9 +128,9 @@ func (graph *Graph) Get(name string) (*Image, error) { } // Create creates a new image and registers it in the graph. -func (graph *Graph) Create(layerData archive.ArchiveReader, container *Container, comment, author string, config *runconfig.Config) (*Image, error) { - img := &Image{ - ID: GenerateID(), +func (graph *Graph) Create(layerData archive.ArchiveReader, containerID, containerImage, comment, author string, containerConfig, config *runconfig.Config) (*image.Image, error) { + img := &image.Image{ + ID: utils.GenerateRandomID(), Comment: comment, Created: time.Now().UTC(), DockerVersion: dockerversion.VERSION, @@ -138,10 +139,10 @@ func (graph *Graph) Create(layerData archive.ArchiveReader, container *Container Architecture: runtime.GOARCH, OS: runtime.GOOS, } - if container != nil { - img.Parent = container.Image - img.Container = container.ID - img.ContainerConfig = *container.Config + if containerID != "" { + img.Parent = containerImage + img.Container = containerID + img.ContainerConfig = *containerConfig } if err := graph.Register(nil, layerData, img); err != nil { return nil, err @@ -151,7 +152,7 @@ func (graph *Graph) Create(layerData archive.ArchiveReader, container *Container // Register imports a pre-existing image into the graph. // FIXME: pass img as first argument -func (graph *Graph) Register(jsonData []byte, layerData archive.ArchiveReader, img *Image) (err error) { +func (graph *Graph) Register(jsonData []byte, layerData archive.ArchiveReader, img *image.Image) (err error) { defer func() { // If any error occurs, remove the new dir from the driver. // Don't check for errors since the dir might not have been created. @@ -160,7 +161,7 @@ func (graph *Graph) Register(jsonData []byte, layerData archive.ArchiveReader, i graph.driver.Remove(img.ID) } }() - if err := ValidateID(img.ID); err != nil { + if err := utils.ValidateID(img.ID); err != nil { return err } // (This is a convenience to save time. Race conditions are taken care of by os.Rename) @@ -171,7 +172,7 @@ func (graph *Graph) Register(jsonData []byte, layerData archive.ArchiveReader, i // Ensure that the image root does not exist on the filesystem // when it is not registered in the graph. // This is common when you switch from one graph driver to another - if err := os.RemoveAll(graph.imageRoot(img.ID)); err != nil && !os.IsNotExist(err) { + if err := os.RemoveAll(graph.ImageRoot(img.ID)); err != nil && !os.IsNotExist(err) { return err } @@ -197,12 +198,12 @@ func (graph *Graph) Register(jsonData []byte, layerData archive.ArchiveReader, i return fmt.Errorf("Driver %s failed to get image rootfs %s: %s", graph.driver, img.ID, err) } defer graph.driver.Put(img.ID) - img.graph = graph - if err := StoreImage(img, jsonData, layerData, tmp, rootfs); err != nil { + img.SetGraph(graph) + if err := image.StoreImage(img, jsonData, layerData, tmp, rootfs); err != nil { return err } // Commit - if err := os.Rename(tmp, graph.imageRoot(img.ID)); err != nil { + if err := os.Rename(tmp, graph.ImageRoot(img.ID)); err != nil { return err } graph.idIndex.Add(img.ID) @@ -233,7 +234,7 @@ func (graph *Graph) TempLayerArchive(id string, compression archive.Compression, // Mktemp creates a temporary sub-directory inside the graph's filesystem. func (graph *Graph) Mktemp(id string) (string, error) { - dir := path.Join(graph.Root, "_tmp", GenerateID()) + dir := path.Join(graph.Root, "_tmp", utils.GenerateRandomID()) if err := os.MkdirAll(dir, 0700); err != nil { return "", err } @@ -246,7 +247,7 @@ func (graph *Graph) Mktemp(id string) (string, error) { // // This extra layer is used by all containers as the top-most ro layer. It protects // the container from unwanted side-effects on the rw layer. -func setupInitLayer(initLayer string) error { +func SetupInitLayer(initLayer string) error { for pth, typ := range map[string]string{ "/dev/pts": "dir", "/dev/shm": "dir", @@ -320,7 +321,7 @@ func (graph *Graph) Delete(name string) error { return err } graph.idIndex.Delete(id) - err = os.Rename(graph.imageRoot(id), tmp) + err = os.Rename(graph.ImageRoot(id), tmp) if err != nil { return err } @@ -331,9 +332,9 @@ func (graph *Graph) Delete(name string) error { } // Map returns a list of all images in the graph, addressable by ID. -func (graph *Graph) Map() (map[string]*Image, error) { - images := make(map[string]*Image) - err := graph.walkAll(func(image *Image) { +func (graph *Graph) Map() (map[string]*image.Image, error) { + images := make(map[string]*image.Image) + err := graph.walkAll(func(image *image.Image) { images[image.ID] = image }) if err != nil { @@ -344,7 +345,7 @@ func (graph *Graph) Map() (map[string]*Image, error) { // walkAll iterates over each image in the graph, and passes it to a handler. // The walking order is undetermined. -func (graph *Graph) walkAll(handler func(*Image)) error { +func (graph *Graph) walkAll(handler func(*image.Image)) error { files, err := ioutil.ReadDir(graph.Root) if err != nil { return err @@ -364,17 +365,17 @@ func (graph *Graph) walkAll(handler func(*Image)) error { // If an image of id ID has 3 children images, then the value for key ID // will be a list of 3 images. // If an image has no children, it will not have an entry in the table. -func (graph *Graph) ByParent() (map[string][]*Image, error) { - byParent := make(map[string][]*Image) - err := graph.walkAll(func(image *Image) { - parent, err := graph.Get(image.Parent) +func (graph *Graph) ByParent() (map[string][]*image.Image, error) { + byParent := make(map[string][]*image.Image) + err := graph.walkAll(func(img *image.Image) { + parent, err := graph.Get(img.Parent) if err != nil { return } if children, exists := byParent[parent.ID]; exists { - byParent[parent.ID] = append(children, image) + byParent[parent.ID] = append(children, img) } else { - byParent[parent.ID] = []*Image{image} + byParent[parent.ID] = []*image.Image{img} } }) return byParent, err @@ -382,13 +383,13 @@ func (graph *Graph) ByParent() (map[string][]*Image, error) { // Heads returns all heads in the graph, keyed by id. // A head is an image which is not the parent of another image in the graph. -func (graph *Graph) Heads() (map[string]*Image, error) { - heads := make(map[string]*Image) +func (graph *Graph) Heads() (map[string]*image.Image, error) { + heads := make(map[string]*image.Image) byParent, err := graph.ByParent() if err != nil { return nil, err } - err = graph.walkAll(func(image *Image) { + err = graph.walkAll(func(image *image.Image) { // If it's not in the byParent lookup table, then // it's not a parent -> so it's a head! if _, exists := byParent[image.ID]; !exists { @@ -398,7 +399,7 @@ func (graph *Graph) Heads() (map[string]*Image, error) { return heads, err } -func (graph *Graph) imageRoot(id string) string { +func (graph *Graph) ImageRoot(id string) string { return path.Join(graph.Root, id) } diff --git a/tags.go b/graph/tags.go similarity index 96% rename from tags.go rename to graph/tags.go index 92c32b1ff5..524e1a1f9d 100644 --- a/tags.go +++ b/graph/tags.go @@ -1,8 +1,9 @@ -package docker +package graph import ( "encoding/json" "fmt" + "github.com/dotcloud/docker/image" "github.com/dotcloud/docker/utils" "io/ioutil" "os" @@ -65,7 +66,7 @@ func (store *TagStore) Reload() error { return nil } -func (store *TagStore) LookupImage(name string) (*Image, error) { +func (store *TagStore) LookupImage(name string) (*image.Image, error) { // FIXME: standardize on returning nil when the image doesn't exist, and err for everything else // (so we can pass all errors here) repos, tag := utils.ParseRepositoryTag(name) @@ -195,7 +196,7 @@ func (store *TagStore) Get(repoName string) (Repository, error) { return nil, nil } -func (store *TagStore) GetImage(repoName, tagOrID string) (*Image, error) { +func (store *TagStore) GetImage(repoName, tagOrID string) (*image.Image, error) { repo, err := store.Get(repoName) if err != nil { return nil, err diff --git a/tags_unit_test.go b/graph/tags_unit_test.go similarity index 74% rename from tags_unit_test.go rename to graph/tags_unit_test.go index b6236280a8..153f94db3d 100644 --- a/tags_unit_test.go +++ b/graph/tags_unit_test.go @@ -1,8 +1,13 @@ -package docker +package graph import ( + "bytes" "github.com/dotcloud/docker/graphdriver" + _ "github.com/dotcloud/docker/graphdriver/vfs" // import the vfs driver so it is used in the tests + "github.com/dotcloud/docker/image" "github.com/dotcloud/docker/utils" + "github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" + "io" "os" "path" "testing" @@ -13,6 +18,23 @@ const ( testImageID = "foo" ) +func fakeTar() (io.Reader, error) { + content := []byte("Hello world!\n") + buf := new(bytes.Buffer) + tw := tar.NewWriter(buf) + for _, name := range []string{"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} { + hdr := new(tar.Header) + hdr.Size = int64(len(content)) + hdr.Name = name + if err := tw.WriteHeader(hdr); err != nil { + return nil, err + } + tw.Write([]byte(content)) + } + tw.Close() + return buf, nil +} + func mkTestTagStore(root string, t *testing.T) *TagStore { driver, err := graphdriver.New(root) if err != nil { @@ -30,7 +52,7 @@ func mkTestTagStore(root string, t *testing.T) *TagStore { if err != nil { t.Fatal(err) } - img := &Image{ID: testImageID} + img := &image.Image{ID: testImageID} // FIXME: this fails on Darwin with: // tags_unit_test.go:36: mkdir /var/folders/7g/b3ydb5gx4t94ndr_cljffbt80000gq/T/docker-test569b-tRunner-075013689/vfs/dir/foo/etc/postgres: permission denied if err := graph.Register(nil, archive, img); err != nil { diff --git a/image/graph.go b/image/graph.go new file mode 100644 index 0000000000..857c09edd9 --- /dev/null +++ b/image/graph.go @@ -0,0 +1,11 @@ +package image + +import ( + "github.com/dotcloud/docker/graphdriver" +) + +type Graph interface { + Get(id string) (*Image, error) + ImageRoot(id string) string + Driver() graphdriver.Driver +} diff --git a/image.go b/image/image.go similarity index 86% rename from image.go rename to image/image.go index fa5b65787c..e091879049 100644 --- a/image.go +++ b/image/image.go @@ -1,20 +1,16 @@ -package docker +package image import ( - "crypto/rand" - "encoding/hex" "encoding/json" "fmt" "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/graphdriver" "github.com/dotcloud/docker/runconfig" "github.com/dotcloud/docker/utils" - "io" "io/ioutil" "os" "path" "strconv" - "strings" "time" ) @@ -30,8 +26,9 @@ type Image struct { Config *runconfig.Config `json:"config,omitempty"` Architecture string `json:"architecture,omitempty"` OS string `json:"os,omitempty"` - graph *Graph Size int64 + + graph Graph } func LoadImage(root string) (*Image, error) { @@ -45,7 +42,7 @@ func LoadImage(root string) (*Image, error) { if err := json.Unmarshal(jsonData, img); err != nil { return nil, err } - if err := ValidateID(img.ID); err != nil { + if err := utils.ValidateID(img.ID); err != nil { return nil, err } @@ -72,7 +69,7 @@ func StoreImage(img *Image, jsonData []byte, layerData archive.ArchiveReader, ro var ( size int64 err error - driver = img.graph.driver + driver = img.graph.Driver() ) if err := os.MkdirAll(layer, 0755); err != nil { return err @@ -136,6 +133,10 @@ func StoreImage(img *Image, jsonData []byte, layerData archive.ArchiveReader, ro return nil } +func (img *Image) SetGraph(graph Graph) { + img.graph = graph +} + // SaveSize stores the current `size` value of `img` in the directory `root`. func (img *Image) SaveSize(root string) error { if err := ioutil.WriteFile(path.Join(root, "layersize"), []byte(strconv.Itoa(int(img.Size))), 0600); err != nil { @@ -153,7 +154,7 @@ func (img *Image) TarLayer() (arch archive.Archive, err error) { if img.graph == nil { return nil, fmt.Errorf("Can't load storage driver for unregistered image %s", img.ID) } - driver := img.graph.driver + driver := img.graph.Driver() if differ, ok := driver.(graphdriver.Differ); ok { return differ.Diff(img.ID) } @@ -201,33 +202,6 @@ func (img *Image) TarLayer() (arch archive.Archive, err error) { }), nil } -func ValidateID(id string) error { - if id == "" { - return fmt.Errorf("Image id can't be empty") - } - if strings.Contains(id, ":") { - return fmt.Errorf("Invalid character in image id: ':'") - } - return nil -} - -func GenerateID() string { - for { - id := make([]byte, 32) - if _, err := io.ReadFull(rand.Reader, id); err != nil { - panic(err) // This shouldn't happen - } - value := hex.EncodeToString(id) - // if we try to parse the truncated for as an int and we don't have - // an error then the value is all numberic and causes issues when - // used as a hostname. ref #3869 - if _, err := strconv.Atoi(utils.TruncateID(value)); err == nil { - continue - } - return value - } -} - // Image includes convenience proxy functions to its graph // These functions will return an error if the image is not registered // (ie. if image.graph == nil) @@ -274,16 +248,16 @@ func (img *Image) root() (string, error) { if img.graph == nil { return "", fmt.Errorf("Can't lookup root of unregistered image") } - return img.graph.imageRoot(img.ID), nil + return img.graph.ImageRoot(img.ID), nil } -func (img *Image) getParentsSize(size int64) int64 { +func (img *Image) GetParentsSize(size int64) int64 { parentImage, err := img.GetParent() if err != nil || parentImage == nil { return size } size += parentImage.Size - return parentImage.getParentsSize(size) + return parentImage.GetParentsSize(size) } // Depth returns the number of parents for a diff --git a/integration/api_test.go b/integration/api_test.go index cb92d89858..bac4efea53 100644 --- a/integration/api_test.go +++ b/integration/api_test.go @@ -5,11 +5,12 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/dotcloud/docker" "github.com/dotcloud/docker/api" "github.com/dotcloud/docker/dockerversion" "github.com/dotcloud/docker/engine" + "github.com/dotcloud/docker/image" "github.com/dotcloud/docker/runconfig" + "github.com/dotcloud/docker/runtime" "github.com/dotcloud/docker/utils" "github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" "io" @@ -287,7 +288,7 @@ func TestGetImagesByName(t *testing.T) { } assertHttpNotError(r, t) - img := &docker.Image{} + img := &image.Image{} if err := json.Unmarshal(r.Body.Bytes(), img); err != nil { t.Fatal(err) } @@ -599,7 +600,7 @@ func TestGetContainersByName(t *testing.T) { t.Fatal(err) } assertHttpNotError(r, t) - outContainer := &docker.Container{} + outContainer := &runtime.Container{} if err := json.Unmarshal(r.Body.Bytes(), outContainer); err != nil { t.Fatal(err) } diff --git a/integration/buildfile_test.go b/integration/buildfile_test.go index efab9707ec..e5084d4355 100644 --- a/integration/buildfile_test.go +++ b/integration/buildfile_test.go @@ -5,6 +5,7 @@ import ( "github.com/dotcloud/docker" "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/engine" + "github.com/dotcloud/docker/image" "github.com/dotcloud/docker/utils" "io/ioutil" "net" @@ -350,7 +351,7 @@ func TestBuild(t *testing.T) { } } -func buildImage(context testContextTemplate, t *testing.T, eng *engine.Engine, useCache bool) (*docker.Image, error) { +func buildImage(context testContextTemplate, t *testing.T, eng *engine.Engine, useCache bool) (*image.Image, error) { if eng == nil { eng = NewTestEngine(t) runtime := mkRuntimeFromEngine(eng, t) diff --git a/integration/commands_test.go b/integration/commands_test.go index 9f7a41384c..46f623bedf 100644 --- a/integration/commands_test.go +++ b/integration/commands_test.go @@ -3,10 +3,11 @@ package docker import ( "bufio" "fmt" - "github.com/dotcloud/docker" "github.com/dotcloud/docker/api" "github.com/dotcloud/docker/engine" + "github.com/dotcloud/docker/image" "github.com/dotcloud/docker/pkg/term" + "github.com/dotcloud/docker/runtime" "github.com/dotcloud/docker/utils" "io" "io/ioutil" @@ -35,7 +36,7 @@ func closeWrap(args ...io.Closer) error { return nil } -func setRaw(t *testing.T, c *docker.Container) *term.State { +func setRaw(t *testing.T, c *runtime.Container) *term.State { pty, err := c.GetPtyMaster() if err != nil { t.Fatal(err) @@ -47,7 +48,7 @@ func setRaw(t *testing.T, c *docker.Container) *term.State { return state } -func unsetRaw(t *testing.T, c *docker.Container, state *term.State) { +func unsetRaw(t *testing.T, c *runtime.Container, state *term.State) { pty, err := c.GetPtyMaster() if err != nil { t.Fatal(err) @@ -55,8 +56,8 @@ func unsetRaw(t *testing.T, c *docker.Container, state *term.State) { term.RestoreTerminal(pty.Fd(), state) } -func waitContainerStart(t *testing.T, timeout time.Duration) *docker.Container { - var container *docker.Container +func waitContainerStart(t *testing.T, timeout time.Duration) *runtime.Container { + var container *runtime.Container setTimeout(t, "Waiting for the container to be started timed out", timeout, func() { for { @@ -902,7 +903,7 @@ func TestImagesTree(t *testing.T) { }) } -func buildTestImages(t *testing.T, eng *engine.Engine) *docker.Image { +func buildTestImages(t *testing.T, eng *engine.Engine) *image.Image { var testBuilder = testContextTemplate{ ` diff --git a/integration/graph_test.go b/integration/graph_test.go index ff1c0d9361..e575a252f3 100644 --- a/integration/graph_test.go +++ b/integration/graph_test.go @@ -2,10 +2,11 @@ package docker import ( "errors" - "github.com/dotcloud/docker" "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/dockerversion" + "github.com/dotcloud/docker/graph" "github.com/dotcloud/docker/graphdriver" + "github.com/dotcloud/docker/image" "github.com/dotcloud/docker/utils" "io" "io/ioutil" @@ -24,7 +25,7 @@ func TestMount(t *testing.T) { if err != nil { t.Fatal(err) } - image, err := graph.Create(archive, nil, "Testing", "", nil) + image, err := graph.Create(archive, "", "", "Testing", "", nil, nil) if err != nil { t.Fatal(err) } @@ -67,8 +68,8 @@ func TestInterruptedRegister(t *testing.T) { graph, _ := tempGraph(t) defer nukeGraph(graph) badArchive, w := io.Pipe() // Use a pipe reader as a fake archive which never yields data - image := &docker.Image{ - ID: docker.GenerateID(), + image := &image.Image{ + ID: utils.GenerateRandomID(), Comment: "testing", Created: time.Now(), } @@ -96,18 +97,18 @@ func TestGraphCreate(t *testing.T) { if err != nil { t.Fatal(err) } - image, err := graph.Create(archive, nil, "Testing", "", nil) + img, err := graph.Create(archive, "", "", "Testing", "", nil, nil) if err != nil { t.Fatal(err) } - if err := docker.ValidateID(image.ID); err != nil { + if err := utils.ValidateID(img.ID); err != nil { t.Fatal(err) } - if image.Comment != "Testing" { - t.Fatalf("Wrong comment: should be '%s', not '%s'", "Testing", image.Comment) + if img.Comment != "Testing" { + t.Fatalf("Wrong comment: should be '%s', not '%s'", "Testing", img.Comment) } - if image.DockerVersion != dockerversion.VERSION { - t.Fatalf("Wrong docker_version: should be '%s', not '%s'", dockerversion.VERSION, image.DockerVersion) + if img.DockerVersion != dockerversion.VERSION { + t.Fatalf("Wrong docker_version: should be '%s', not '%s'", dockerversion.VERSION, img.DockerVersion) } images, err := graph.Map() if err != nil { @@ -115,8 +116,8 @@ func TestGraphCreate(t *testing.T) { } else if l := len(images); l != 1 { t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l) } - if images[image.ID] == nil { - t.Fatalf("Could not find image with id %s", image.ID) + if images[img.ID] == nil { + t.Fatalf("Could not find image with id %s", img.ID) } } @@ -127,8 +128,8 @@ func TestRegister(t *testing.T) { if err != nil { t.Fatal(err) } - image := &docker.Image{ - ID: docker.GenerateID(), + image := &image.Image{ + ID: utils.GenerateRandomID(), Comment: "testing", Created: time.Now(), } @@ -164,12 +165,12 @@ func TestDeletePrefix(t *testing.T) { assertNImages(graph, t, 0) } -func createTestImage(graph *docker.Graph, t *testing.T) *docker.Image { +func createTestImage(graph *graph.Graph, t *testing.T) *image.Image { archive, err := fakeTar() if err != nil { t.Fatal(err) } - img, err := graph.Create(archive, nil, "Test image", "", nil) + img, err := graph.Create(archive, "", "", "Test image", "", nil, nil) if err != nil { t.Fatal(err) } @@ -184,7 +185,7 @@ func TestDelete(t *testing.T) { t.Fatal(err) } assertNImages(graph, t, 0) - img, err := graph.Create(archive, nil, "Bla bla", "", nil) + img, err := graph.Create(archive, "", "", "Bla bla", "", nil, nil) if err != nil { t.Fatal(err) } @@ -199,7 +200,7 @@ func TestDelete(t *testing.T) { t.Fatal(err) } // Test 2 create (same name) / 1 delete - img1, err := graph.Create(archive, nil, "Testing", "", nil) + img1, err := graph.Create(archive, "", "", "Testing", "", nil, nil) if err != nil { t.Fatal(err) } @@ -207,7 +208,7 @@ func TestDelete(t *testing.T) { if err != nil { t.Fatal(err) } - if _, err = graph.Create(archive, nil, "Testing", "", nil); err != nil { + if _, err = graph.Create(archive, "", "", "Testing", "", nil, nil); err != nil { t.Fatal(err) } assertNImages(graph, t, 2) @@ -243,20 +244,20 @@ func TestByParent(t *testing.T) { graph, _ := tempGraph(t) defer nukeGraph(graph) - parentImage := &docker.Image{ - ID: docker.GenerateID(), + parentImage := &image.Image{ + ID: utils.GenerateRandomID(), Comment: "parent", Created: time.Now(), Parent: "", } - childImage1 := &docker.Image{ - ID: docker.GenerateID(), + childImage1 := &image.Image{ + ID: utils.GenerateRandomID(), Comment: "child1", Created: time.Now(), Parent: parentImage.ID, } - childImage2 := &docker.Image{ - ID: docker.GenerateID(), + childImage2 := &image.Image{ + ID: utils.GenerateRandomID(), Comment: "child2", Created: time.Now(), Parent: parentImage.ID, @@ -279,7 +280,7 @@ func TestByParent(t *testing.T) { * HELPER FUNCTIONS */ -func assertNImages(graph *docker.Graph, t *testing.T, n int) { +func assertNImages(graph *graph.Graph, t *testing.T, n int) { if images, err := graph.Map(); err != nil { t.Fatal(err) } else if actualN := len(images); actualN != n { @@ -287,7 +288,7 @@ func assertNImages(graph *docker.Graph, t *testing.T, n int) { } } -func tempGraph(t *testing.T) (*docker.Graph, graphdriver.Driver) { +func tempGraph(t *testing.T) (*graph.Graph, graphdriver.Driver) { tmp, err := ioutil.TempDir("", "docker-graph-") if err != nil { t.Fatal(err) @@ -296,14 +297,14 @@ func tempGraph(t *testing.T) (*docker.Graph, graphdriver.Driver) { if err != nil { t.Fatal(err) } - graph, err := docker.NewGraph(tmp, driver) + graph, err := graph.NewGraph(tmp, driver) if err != nil { t.Fatal(err) } return graph, driver } -func nukeGraph(graph *docker.Graph) { +func nukeGraph(graph *graph.Graph) { graph.Driver().Cleanup() os.RemoveAll(graph.Root) } diff --git a/integration/runtime_test.go b/integration/runtime_test.go index 1e912c1bb4..dd478c289e 100644 --- a/integration/runtime_test.go +++ b/integration/runtime_test.go @@ -3,10 +3,11 @@ package docker import ( "bytes" "fmt" - "github.com/dotcloud/docker" "github.com/dotcloud/docker/engine" + "github.com/dotcloud/docker/image" "github.com/dotcloud/docker/nat" "github.com/dotcloud/docker/runconfig" + "github.com/dotcloud/docker/runtime" "github.com/dotcloud/docker/sysinit" "github.com/dotcloud/docker/utils" "io" @@ -15,7 +16,7 @@ import ( "net/url" "os" "path/filepath" - "runtime" + goruntime "runtime" "strconv" "strings" "syscall" @@ -35,14 +36,14 @@ const ( var ( // FIXME: globalRuntime is deprecated by globalEngine. All tests should be converted. - globalRuntime *docker.Runtime + globalRuntime *runtime.Runtime globalEngine *engine.Engine startFds int startGoroutines int ) // FIXME: nuke() is deprecated by Runtime.Nuke() -func nuke(runtime *docker.Runtime) error { +func nuke(runtime *runtime.Runtime) error { return runtime.Nuke() } @@ -119,7 +120,7 @@ func init() { // Create the "global runtime" with a long-running daemon for integration tests spawnGlobalDaemon() - startFds, startGoroutines = utils.GetTotalUsedFds(), runtime.NumGoroutine() + startFds, startGoroutines = utils.GetTotalUsedFds(), goruntime.NumGoroutine() } func setupBaseImage() { @@ -172,7 +173,7 @@ func spawnGlobalDaemon() { // FIXME: test that ImagePull(json=true) send correct json output -func GetTestImage(runtime *docker.Runtime) *docker.Image { +func GetTestImage(runtime *runtime.Runtime) *image.Image { imgs, err := runtime.Graph().Map() if err != nil { log.Fatalf("Unable to get the test image: %s", err) @@ -356,7 +357,7 @@ func TestGet(t *testing.T) { } -func startEchoServerContainer(t *testing.T, proto string) (*docker.Runtime, *docker.Container, string) { +func startEchoServerContainer(t *testing.T, proto string) (*runtime.Runtime, *runtime.Container, string) { var ( err error id string diff --git a/integration/utils_test.go b/integration/utils_test.go index 05d73df52a..88f2cc49c3 100644 --- a/integration/utils_test.go +++ b/integration/utils_test.go @@ -18,6 +18,7 @@ import ( "github.com/dotcloud/docker/builtins" "github.com/dotcloud/docker/engine" "github.com/dotcloud/docker/runconfig" + "github.com/dotcloud/docker/runtime" "github.com/dotcloud/docker/utils" ) @@ -27,7 +28,7 @@ import ( // Create a temporary runtime suitable for unit testing. // Call t.Fatal() at the first error. -func mkRuntime(f utils.Fataler) *docker.Runtime { +func mkRuntime(f utils.Fataler) *runtime.Runtime { eng := newTestEngine(f, false, "") return mkRuntimeFromEngine(eng, f) // FIXME: @@ -139,7 +140,7 @@ func assertHttpError(r *httptest.ResponseRecorder, t utils.Fataler) { } } -func getContainer(eng *engine.Engine, id string, t utils.Fataler) *docker.Container { +func getContainer(eng *engine.Engine, id string, t utils.Fataler) *runtime.Container { runtime := mkRuntimeFromEngine(eng, t) c := runtime.Get(id) if c == nil { @@ -160,14 +161,14 @@ func mkServerFromEngine(eng *engine.Engine, t utils.Fataler) *docker.Server { return srv } -func mkRuntimeFromEngine(eng *engine.Engine, t utils.Fataler) *docker.Runtime { +func mkRuntimeFromEngine(eng *engine.Engine, t utils.Fataler) *runtime.Runtime { iRuntime := eng.Hack_GetGlobalVar("httpapi.runtime") if iRuntime == nil { panic("Legacy runtime field not set in engine") } - runtime, ok := iRuntime.(*docker.Runtime) + runtime, ok := iRuntime.(*runtime.Runtime) if !ok { - panic("Legacy runtime field in engine does not cast to *docker.Runtime") + panic("Legacy runtime field in engine does not cast to *runtime.Runtime") } return runtime } @@ -249,7 +250,7 @@ func readFile(src string, t *testing.T) (content string) { // dynamically replaced by the current test image. // The caller is responsible for destroying the container. // Call t.Fatal() at the first error. -func mkContainer(r *docker.Runtime, args []string, t *testing.T) (*docker.Container, *runconfig.HostConfig, error) { +func mkContainer(r *runtime.Runtime, args []string, t *testing.T) (*runtime.Container, *runconfig.HostConfig, error) { config, hc, _, err := runconfig.Parse(args, nil) defer func() { if err != nil && t != nil { @@ -280,7 +281,7 @@ func mkContainer(r *docker.Runtime, args []string, t *testing.T) (*docker.Contai // and return its standard output as a string. // The image name (eg. the XXX in []string{"-i", "-t", "XXX", "bash"}, is dynamically replaced by the current test image. // If t is not nil, call t.Fatal() at the first error. Otherwise return errors normally. -func runContainer(eng *engine.Engine, r *docker.Runtime, args []string, t *testing.T) (output string, err error) { +func runContainer(eng *engine.Engine, r *runtime.Runtime, args []string, t *testing.T) (output string, err error) { defer func() { if err != nil && t != nil { t.Fatal(err) diff --git a/container.go b/runtime/container.go similarity index 96% rename from container.go rename to runtime/container.go index 50332f27de..813147e508 100644 --- a/container.go +++ b/runtime/container.go @@ -1,4 +1,4 @@ -package docker +package runtime import ( "encoding/json" @@ -8,6 +8,7 @@ import ( "github.com/dotcloud/docker/engine" "github.com/dotcloud/docker/execdriver" "github.com/dotcloud/docker/graphdriver" + "github.com/dotcloud/docker/image" "github.com/dotcloud/docker/links" "github.com/dotcloud/docker/nat" "github.com/dotcloud/docker/runconfig" @@ -23,7 +24,7 @@ import ( "time" ) -const defaultPathEnv = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" +const DefaultPathEnv = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" var ( ErrNotATTY = errors.New("The PTY is not a file") @@ -173,7 +174,7 @@ func (container *Container) ToDisk() (err error) { if err != nil { return } - return container.writeHostConfig() + return container.WriteHostConfig() } func (container *Container) readHostConfig() error { @@ -192,7 +193,7 @@ func (container *Container) readHostConfig() error { return json.Unmarshal(data, container.hostConfig) } -func (container *Container) writeHostConfig() (err error) { +func (container *Container) WriteHostConfig() (err error) { data, err := json.Marshal(container.hostConfig) if err != nil { return @@ -450,7 +451,7 @@ func (container *Container) Start() (err error) { // Setup environment env := []string{ "HOME=/", - "PATH=" + defaultPathEnv, + "PATH=" + DefaultPathEnv, "HOSTNAME=" + container.Config.Hostname, } @@ -692,7 +693,7 @@ func (container *Container) allocateNetwork() error { return err } container.Config.PortSpecs = nil - if err := container.writeHostConfig(); err != nil { + if err := container.WriteHostConfig(); err != nil { return err } } @@ -750,7 +751,7 @@ func (container *Container) allocateNetwork() error { } bindings[port] = binding } - container.writeHostConfig() + container.WriteHostConfig() container.NetworkSettings.Ports = bindings @@ -849,7 +850,7 @@ func (container *Container) cleanup() { } } -func (container *Container) kill(sig int) error { +func (container *Container) KillSig(sig int) error { container.Lock() defer container.Unlock() @@ -865,7 +866,7 @@ func (container *Container) Kill() error { } // 1. Send SIGKILL - if err := container.kill(9); err != nil { + if err := container.KillSig(9); err != nil { return err } @@ -890,10 +891,10 @@ func (container *Container) Stop(seconds int) error { } // 1. Send a SIGTERM - if err := container.kill(15); err != nil { + if err := container.KillSig(15); err != nil { utils.Debugf("Error sending kill SIGTERM: %s", err) log.Print("Failed to send SIGTERM to the process, force killing") - if err := container.kill(9); err != nil { + if err := container.KillSig(9); err != nil { return err } } @@ -992,7 +993,7 @@ func (container *Container) Changes() ([]archive.Change, error) { return container.runtime.Changes(container) } -func (container *Container) GetImage() (*Image, error) { +func (container *Container) GetImage() (*image.Image, error) { if container.runtime == nil { return nil, fmt.Errorf("Can't get image of unregistered container") } @@ -1140,3 +1141,21 @@ func (container *Container) GetPtyMaster() (*os.File, error) { } return ttyConsole.Master(), nil } + +func (container *Container) HostConfig() *runconfig.HostConfig { + return container.hostConfig +} + +func (container *Container) SetHostConfig(hostConfig *runconfig.HostConfig) { + container.hostConfig = hostConfig +} + +func (container *Container) DisableLink(name string) { + if container.activeLinks != nil { + if link, exists := container.activeLinks[name]; exists { + link.Disable() + } else { + utils.Debugf("Could not find active link for %s", name) + } + } +} diff --git a/container_unit_test.go b/runtime/container_unit_test.go similarity index 96% rename from container_unit_test.go rename to runtime/container_unit_test.go index 3877b7f0da..fba036ca50 100644 --- a/container_unit_test.go +++ b/runtime/container_unit_test.go @@ -1,4 +1,4 @@ -package docker +package runtime import ( "github.com/dotcloud/docker/nat" @@ -132,14 +132,14 @@ func TestParseNetworkOptsUdp(t *testing.T) { } func TestGetFullName(t *testing.T) { - name, err := getFullName("testing") + name, err := GetFullContainerName("testing") if err != nil { t.Fatal(err) } if name != "/testing" { t.Fatalf("Expected /testing got %s", name) } - if _, err := getFullName(""); err == nil { + if _, err := GetFullContainerName(""); err == nil { t.Fatal("Error should not be nil") } } diff --git a/runtime.go b/runtime/runtime.go similarity index 91% rename from runtime.go rename to runtime/runtime.go index 84f11e87b2..c11c309ad8 100644 --- a/runtime.go +++ b/runtime/runtime.go @@ -1,19 +1,22 @@ -package docker +package runtime import ( "container/list" "fmt" "github.com/dotcloud/docker/archive" + "github.com/dotcloud/docker/daemonconfig" "github.com/dotcloud/docker/dockerversion" "github.com/dotcloud/docker/engine" "github.com/dotcloud/docker/execdriver" "github.com/dotcloud/docker/execdriver/lxc" "github.com/dotcloud/docker/execdriver/native" + "github.com/dotcloud/docker/graph" "github.com/dotcloud/docker/graphdriver" "github.com/dotcloud/docker/graphdriver/aufs" _ "github.com/dotcloud/docker/graphdriver/btrfs" _ "github.com/dotcloud/docker/graphdriver/devmapper" _ "github.com/dotcloud/docker/graphdriver/vfs" + "github.com/dotcloud/docker/image" _ "github.com/dotcloud/docker/networkdriver/lxc" "github.com/dotcloud/docker/networkdriver/portallocator" "github.com/dotcloud/docker/pkg/graphdb" @@ -37,7 +40,7 @@ import ( const MaxImageDepth = 127 var ( - defaultDns = []string{"8.8.8.8", "8.8.4.4"} + DefaultDns = []string{"8.8.8.8", "8.8.4.4"} validContainerNameChars = `[a-zA-Z0-9_.-]` validContainerNamePattern = regexp.MustCompile(`^/?` + validContainerNameChars + `+$`) ) @@ -46,14 +49,14 @@ type Runtime struct { repository string sysInitPath string containers *list.List - graph *Graph - repositories *TagStore + graph *graph.Graph + repositories *graph.TagStore idIndex *utils.TruncIndex sysInfo *sysinfo.SysInfo - volumes *Graph - srv *Server + volumes *graph.Graph + srv Server eng *engine.Engine - config *DaemonConfig + config *daemonconfig.Config containerGraph *graphdb.Database driver graphdriver.Driver execDriver execdriver.Driver @@ -395,7 +398,7 @@ func (runtime *Runtime) Create(config *runconfig.Config, name string) (*Containe } // Generate id - id := GenerateID() + id := utils.GenerateRandomID() if name == "" { name, err = generateRandomName(runtime) @@ -484,7 +487,7 @@ func (runtime *Runtime) Create(config *runconfig.Config, name string) (*Containe } defer runtime.driver.Put(initID) - if err := setupInitLayer(initPath); err != nil { + if err := graph.SetupInitLayer(initPath); err != nil { return nil, nil, err } @@ -497,8 +500,7 @@ func (runtime *Runtime) Create(config *runconfig.Config, name string) (*Containe } if len(config.Dns) == 0 && len(runtime.config.Dns) == 0 && utils.CheckLocalDns(resolvConf) { - //"WARNING: Docker detected local DNS server on resolv.conf. Using default external servers: %v", defaultDns - runtime.config.Dns = defaultDns + runtime.config.Dns = DefaultDns } // If custom dns exists, then create a resolv.conf for the container @@ -538,7 +540,7 @@ func (runtime *Runtime) Create(config *runconfig.Config, name string) (*Containe // Commit creates a new filesystem image from the current state of a container. // The image can optionally be tagged into a repository -func (runtime *Runtime) Commit(container *Container, repository, tag, comment, author string, config *runconfig.Config) (*Image, error) { +func (runtime *Runtime) Commit(container *Container, repository, tag, comment, author string, config *runconfig.Config) (*image.Image, error) { // FIXME: freeze the container before copying it to avoid data corruption? // FIXME: this shouldn't be in commands. if err := container.Mount(); err != nil { @@ -553,7 +555,16 @@ func (runtime *Runtime) Commit(container *Container, repository, tag, comment, a defer rwTar.Close() // Create a new image from the container's base layers + a new layer from container changes - img, err := runtime.graph.Create(rwTar, container, comment, author, config) + var ( + containerID, containerImage string + containerConfig *runconfig.Config + ) + if container != nil { + containerID = container.ID + containerImage = container.Image + containerConfig = container.Config + } + img, err := runtime.graph.Create(rwTar, containerID, containerImage, comment, author, containerConfig, config) if err != nil { return nil, err } @@ -566,7 +577,7 @@ func (runtime *Runtime) Commit(container *Container, repository, tag, comment, a return img, nil } -func getFullName(name string) (string, error) { +func GetFullContainerName(name string) (string, error) { if name == "" { return "", fmt.Errorf("Container name cannot be empty") } @@ -577,7 +588,7 @@ func getFullName(name string) (string, error) { } func (runtime *Runtime) GetByName(name string) (*Container, error) { - fullName, err := getFullName(name) + fullName, err := GetFullContainerName(name) if err != nil { return nil, err } @@ -593,7 +604,7 @@ func (runtime *Runtime) GetByName(name string) (*Container, error) { } func (runtime *Runtime) Children(name string) (map[string]*Container, error) { - name, err := getFullName(name) + name, err := GetFullContainerName(name) if err != nil { return nil, err } @@ -624,7 +635,7 @@ func (runtime *Runtime) RegisterLink(parent, child *Container, alias string) err } // FIXME: harmonize with NewGraph() -func NewRuntime(config *DaemonConfig, eng *engine.Engine) (*Runtime, error) { +func NewRuntime(config *daemonconfig.Config, eng *engine.Engine) (*Runtime, error) { runtime, err := NewRuntimeFromDirectory(config, eng) if err != nil { return nil, err @@ -632,7 +643,7 @@ func NewRuntime(config *DaemonConfig, eng *engine.Engine) (*Runtime, error) { return runtime, nil } -func NewRuntimeFromDirectory(config *DaemonConfig, eng *engine.Engine) (*Runtime, error) { +func NewRuntimeFromDirectory(config *daemonconfig.Config, eng *engine.Engine) (*Runtime, error) { // Set the default driver graphdriver.DefaultDriver = config.GraphDriver @@ -652,13 +663,13 @@ func NewRuntimeFromDirectory(config *DaemonConfig, eng *engine.Engine) (*Runtime if ad, ok := driver.(*aufs.Driver); ok { utils.Debugf("Migrating existing containers") - if err := ad.Migrate(config.Root, setupInitLayer); err != nil { + if err := ad.Migrate(config.Root, graph.SetupInitLayer); err != nil { return nil, err } } utils.Debugf("Creating images graph") - g, err := NewGraph(path.Join(config.Root, "graph"), driver) + g, err := graph.NewGraph(path.Join(config.Root, "graph"), driver) if err != nil { return nil, err } @@ -670,12 +681,12 @@ func NewRuntimeFromDirectory(config *DaemonConfig, eng *engine.Engine) (*Runtime return nil, err } utils.Debugf("Creating volumes graph") - volumes, err := NewGraph(path.Join(config.Root, "volumes"), volumesDriver) + volumes, err := graph.NewGraph(path.Join(config.Root, "volumes"), volumesDriver) if err != nil { return nil, err } utils.Debugf("Creating repository list") - repositories, err := NewTagStore(path.Join(config.Root, "repositories-"+driver.String()), g) + repositories, err := graph.NewTagStore(path.Join(config.Root, "repositories-"+driver.String()), g) if err != nil { return nil, fmt.Errorf("Couldn't create Tag store: %s", err) } @@ -876,10 +887,46 @@ func (runtime *Runtime) Nuke() error { // which need direct access to runtime.graph. // Once the tests switch to using engine and jobs, this method // can go away. -func (runtime *Runtime) Graph() *Graph { +func (runtime *Runtime) Graph() *graph.Graph { return runtime.graph } +func (runtime *Runtime) Repositories() *graph.TagStore { + return runtime.repositories +} + +func (runtime *Runtime) Config() *daemonconfig.Config { + return runtime.config +} + +func (runtime *Runtime) SystemConfig() *sysinfo.SysInfo { + return runtime.sysInfo +} + +func (runtime *Runtime) SystemInitPath() string { + return runtime.sysInitPath +} + +func (runtime *Runtime) GraphDriver() graphdriver.Driver { + return runtime.driver +} + +func (runtime *Runtime) ExecutionDriver() execdriver.Driver { + return runtime.execDriver +} + +func (runtime *Runtime) Volumes() *graph.Graph { + return runtime.volumes +} + +func (runtime *Runtime) ContainerGraph() *graphdb.Database { + return runtime.containerGraph +} + +func (runtime *Runtime) SetServer(server Server) { + runtime.srv = server +} + // History is a convenience type for storing a list of containers, // ordered by creation date. type History []*Container diff --git a/runtime/server.go b/runtime/server.go new file mode 100644 index 0000000000..a74c4d1200 --- /dev/null +++ b/runtime/server.go @@ -0,0 +1,10 @@ +package runtime + +import ( + "github.com/dotcloud/docker/utils" +) + +type Server interface { + LogEvent(action, id, from string) *utils.JSONMessage + IsRunning() bool // returns true if the server is currently in operation +} diff --git a/sorter.go b/runtime/sorter.go similarity index 97% rename from sorter.go rename to runtime/sorter.go index b49ac58c24..c5af772dae 100644 --- a/sorter.go +++ b/runtime/sorter.go @@ -1,4 +1,4 @@ -package docker +package runtime import "sort" diff --git a/state.go b/runtime/state.go similarity index 98% rename from state.go rename to runtime/state.go index 1dc92af204..cce6912b46 100644 --- a/state.go +++ b/runtime/state.go @@ -1,4 +1,4 @@ -package docker +package runtime import ( "fmt" diff --git a/runtime/utils.go b/runtime/utils.go new file mode 100644 index 0000000000..b343b5b10e --- /dev/null +++ b/runtime/utils.go @@ -0,0 +1,44 @@ +package runtime + +import ( + "github.com/dotcloud/docker/nat" + "github.com/dotcloud/docker/pkg/namesgenerator" + "github.com/dotcloud/docker/runconfig" +) + +func migratePortMappings(config *runconfig.Config, hostConfig *runconfig.HostConfig) error { + if config.PortSpecs != nil { + ports, bindings, err := nat.ParsePortSpecs(config.PortSpecs) + if err != nil { + return err + } + config.PortSpecs = nil + if len(bindings) > 0 { + if hostConfig == nil { + hostConfig = &runconfig.HostConfig{} + } + hostConfig.PortBindings = bindings + } + + if config.ExposedPorts == nil { + config.ExposedPorts = make(nat.PortSet, len(ports)) + } + for k, v := range ports { + config.ExposedPorts[k] = v + } + } + return nil +} + +type checker struct { + runtime *Runtime +} + +func (c *checker) Exists(name string) bool { + return c.runtime.containerGraph.Exists("/" + name) +} + +// Generate a random and unique name +func generateRandomName(runtime *Runtime) (string, error) { + return namesgenerator.GenerateRandomName(&checker{runtime}) +} diff --git a/volumes.go b/runtime/volumes.go similarity index 98% rename from volumes.go rename to runtime/volumes.go index 9f76e3698b..1a548eca47 100644 --- a/volumes.go +++ b/runtime/volumes.go @@ -1,4 +1,4 @@ -package docker +package runtime import ( "fmt" @@ -216,7 +216,7 @@ func createVolumes(container *Container) error { return err } - volumesDriver := container.runtime.volumes.driver + volumesDriver := container.runtime.volumes.Driver() // Create the requested volumes if they don't exist for volPath := range container.Config.Volumes { volPath = filepath.Clean(volPath) @@ -246,7 +246,7 @@ func createVolumes(container *Container) error { // Do not pass a container as the parameter for the volume creation. // The graph driver using the container's information ( Image ) to // create the parent. - c, err := container.runtime.volumes.Create(nil, nil, "", "", nil) + c, err := container.runtime.volumes.Create(nil, "", "", "", "", nil, nil) if err != nil { return err } diff --git a/server.go b/server.go index d824d78d7a..85d56afdb6 100644 --- a/server.go +++ b/server.go @@ -5,11 +5,15 @@ import ( "fmt" "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/auth" + "github.com/dotcloud/docker/daemonconfig" "github.com/dotcloud/docker/dockerversion" "github.com/dotcloud/docker/engine" + "github.com/dotcloud/docker/graph" + "github.com/dotcloud/docker/image" "github.com/dotcloud/docker/pkg/graphdb" "github.com/dotcloud/docker/registry" "github.com/dotcloud/docker/runconfig" + "github.com/dotcloud/docker/runtime" "github.com/dotcloud/docker/utils" "io" "io/ioutil" @@ -21,7 +25,7 @@ import ( "os/signal" "path" "path/filepath" - "runtime" + goruntime "runtime" "strconv" "strings" "sync" @@ -34,13 +38,13 @@ import ( // The signals SIGINT, SIGQUIT and SIGTERM are intercepted for cleanup. func InitServer(job *engine.Job) engine.Status { job.Logf("Creating server") - srv, err := NewServer(job.Eng, DaemonConfigFromJob(job)) + srv, err := NewServer(job.Eng, daemonconfig.ConfigFromJob(job)) if err != nil { return job.Error(err) } - if srv.runtime.config.Pidfile != "" { + if srv.runtime.Config().Pidfile != "" { job.Logf("Creating pidfile") - if err := utils.CreatePidFile(srv.runtime.config.Pidfile); err != nil { + if err := utils.CreatePidFile(srv.runtime.Config().Pidfile); err != nil { // FIXME: do we need fatal here instead of returning a job error? log.Fatal(err) } @@ -51,7 +55,7 @@ func InitServer(job *engine.Job) engine.Status { go func() { sig := <-c log.Printf("Received signal '%v', exiting\n", sig) - utils.RemovePidFile(srv.runtime.config.Pidfile) + utils.RemovePidFile(srv.runtime.Config().Pidfile) srv.Close() os.Exit(0) }() @@ -178,10 +182,10 @@ func (srv *Server) ContainerKill(job *engine.Job) engine.Status { if err := container.Kill(); err != nil { return job.Errorf("Cannot kill container %s: %s", name, err) } - srv.LogEvent("kill", container.ID, srv.runtime.repositories.ImageName(container.Image)) + srv.LogEvent("kill", container.ID, srv.runtime.Repositories().ImageName(container.Image)) } else { // Otherwise, just send the requested signal - if err := container.kill(int(sig)); err != nil { + if err := container.KillSig(int(sig)); err != nil { return job.Errorf("Cannot kill container %s: %s", name, err) } // FIXME: Add event for signals @@ -290,7 +294,7 @@ func (srv *Server) ContainerExport(job *engine.Job) engine.Status { return job.Errorf("%s: %s", name, err) } // FIXME: factor job-specific LogEvent to engine.Job.Run() - srv.LogEvent("export", container.ID, srv.runtime.repositories.ImageName(container.Image)) + srv.LogEvent("export", container.ID, srv.runtime.Repositories().ImageName(container.Image)) return engine.StatusOK } return job.Errorf("No such container: %s", name) @@ -315,7 +319,7 @@ func (srv *Server) ImageExport(job *engine.Job) engine.Status { utils.Debugf("Serializing %s", name) - rootRepo, err := srv.runtime.repositories.Get(name) + rootRepo, err := srv.runtime.Repositories().Get(name) if err != nil { return job.Error(err) } @@ -332,7 +336,7 @@ func (srv *Server) ImageExport(job *engine.Job) engine.Status { } // write repositories - rootRepoMap := map[string]Repository{} + rootRepoMap := map[string]graph.Repository{} rootRepoMap[name] = rootRepo rootRepoJson, _ := json.Marshal(rootRepoMap) @@ -361,8 +365,8 @@ func (srv *Server) ImageExport(job *engine.Job) engine.Status { return engine.StatusOK } -func (srv *Server) exportImage(image *Image, tempdir string) error { - for i := image; i != nil; { +func (srv *Server) exportImage(img *image.Image, tempdir string) error { + for i := img; i != nil; { // temporary directory tmpImageDir := path.Join(tempdir, i.ID) if err := os.Mkdir(tmpImageDir, os.ModeDir); err != nil { @@ -491,7 +495,7 @@ func (srv *Server) Build(job *engine.Job) engine.Status { return job.Error(err) } if repoName != "" { - srv.runtime.repositories.Set(repoName, tag, id, false) + srv.runtime.Repositories().Set(repoName, tag, id, false) } return engine.StatusOK } @@ -545,14 +549,14 @@ func (srv *Server) ImageLoad(job *engine.Job) engine.Status { repositoriesJson, err := ioutil.ReadFile(path.Join(tmpImageDir, "repo", "repositories")) if err == nil { - repositories := map[string]Repository{} + repositories := map[string]graph.Repository{} if err := json.Unmarshal(repositoriesJson, &repositories); err != nil { return job.Error(err) } for imageName, tagMap := range repositories { for tag, address := range tagMap { - if err := srv.runtime.repositories.Set(imageName, tag, address, true); err != nil { + if err := srv.runtime.Repositories().Set(imageName, tag, address, true); err != nil { return job.Error(err) } } @@ -579,19 +583,19 @@ func (srv *Server) recursiveLoad(address, tmpImageDir string) error { utils.Debugf("Error reading embedded tar", err) return err } - img, err := NewImgJSON(imageJson) + img, err := image.NewImgJSON(imageJson) if err != nil { utils.Debugf("Error unmarshalling json", err) return err } if img.Parent != "" { - if !srv.runtime.graph.Exists(img.Parent) { + if !srv.runtime.Graph().Exists(img.Parent) { if err := srv.recursiveLoad(img.Parent, tmpImageDir); err != nil { return err } } } - if err := srv.runtime.graph.Register(imageJson, layer, img); err != nil { + if err := srv.runtime.Graph().Register(imageJson, layer, img); err != nil { return err } } @@ -647,7 +651,7 @@ func (srv *Server) ImageInsert(job *engine.Job) engine.Status { sf := utils.NewStreamFormatter(job.GetenvBool("json")) out := utils.NewWriteFlusher(job.Stdout) - img, err := srv.runtime.repositories.LookupImage(name) + img, err := srv.runtime.Repositories().LookupImage(name) if err != nil { return job.Error(err) } @@ -658,7 +662,7 @@ func (srv *Server) ImageInsert(job *engine.Job) engine.Status { } defer file.Body.Close() - config, _, _, err := runconfig.Parse([]string{img.ID, "echo", "insert", url, path}, srv.runtime.sysInfo) + config, _, _, err := runconfig.Parse([]string{img.ID, "echo", "insert", url, path}, srv.runtime.SystemConfig()) if err != nil { return job.Error(err) } @@ -682,14 +686,14 @@ func (srv *Server) ImageInsert(job *engine.Job) engine.Status { } func (srv *Server) ImagesViz(job *engine.Job) engine.Status { - images, _ := srv.runtime.graph.Map() + images, _ := srv.runtime.Graph().Map() if images == nil { return engine.StatusOK } job.Stdout.Write([]byte("digraph docker {\n")) var ( - parentImage *Image + parentImage *image.Image err error ) for _, image := range images { @@ -706,7 +710,7 @@ func (srv *Server) ImagesViz(job *engine.Job) engine.Status { reporefs := make(map[string][]string) - for name, repository := range srv.runtime.repositories.Repositories { + for name, repository := range srv.runtime.Repositories().Repositories { for tag, id := range repository { reporefs[utils.TruncateID(id)] = append(reporefs[utils.TruncateID(id)], fmt.Sprintf("%s:%s", name, tag)) } @@ -721,26 +725,26 @@ func (srv *Server) ImagesViz(job *engine.Job) engine.Status { func (srv *Server) Images(job *engine.Job) engine.Status { var ( - allImages map[string]*Image + allImages map[string]*image.Image err error ) if job.GetenvBool("all") { - allImages, err = srv.runtime.graph.Map() + allImages, err = srv.runtime.Graph().Map() } else { - allImages, err = srv.runtime.graph.Heads() + allImages, err = srv.runtime.Graph().Heads() } if err != nil { return job.Error(err) } lookup := make(map[string]*engine.Env) - for name, repository := range srv.runtime.repositories.Repositories { + for name, repository := range srv.runtime.Repositories().Repositories { if job.Getenv("filter") != "" { if match, _ := path.Match(job.Getenv("filter"), name); !match { continue } } for tag, id := range repository { - image, err := srv.runtime.graph.Get(id) + image, err := srv.runtime.Graph().Get(id) if err != nil { log.Printf("Warning: couldn't load %s from %s/%s: %s", id, name, tag, err) continue @@ -756,7 +760,7 @@ func (srv *Server) Images(job *engine.Job) engine.Status { out.Set("Id", image.ID) out.SetInt64("Created", image.Created.Unix()) out.SetInt64("Size", image.Size) - out.SetInt64("VirtualSize", image.getParentsSize(0)+image.Size) + out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size) lookup[id] = out } @@ -777,7 +781,7 @@ func (srv *Server) Images(job *engine.Job) engine.Status { out.Set("Id", image.ID) out.SetInt64("Created", image.Created.Unix()) out.SetInt64("Size", image.Size) - out.SetInt64("VirtualSize", image.getParentsSize(0)+image.Size) + out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size) outs.Add(out) } } @@ -790,7 +794,7 @@ func (srv *Server) Images(job *engine.Job) engine.Status { } func (srv *Server) DockerInfo(job *engine.Job) engine.Status { - images, _ := srv.runtime.graph.Map() + images, _ := srv.runtime.Graph().Map() var imgcount int if images == nil { imgcount = 0 @@ -806,21 +810,21 @@ func (srv *Server) DockerInfo(job *engine.Job) engine.Status { initPath := utils.DockerInitPath("") if initPath == "" { // if that fails, we'll just return the path from the runtime - initPath = srv.runtime.sysInitPath + initPath = srv.runtime.SystemInitPath() } v := &engine.Env{} v.SetInt("Containers", len(srv.runtime.List())) v.SetInt("Images", imgcount) - v.Set("Driver", srv.runtime.driver.String()) - v.SetJson("DriverStatus", srv.runtime.driver.Status()) - v.SetBool("MemoryLimit", srv.runtime.sysInfo.MemoryLimit) - v.SetBool("SwapLimit", srv.runtime.sysInfo.SwapLimit) - v.SetBool("IPv4Forwarding", !srv.runtime.sysInfo.IPv4ForwardingDisabled) + v.Set("Driver", srv.runtime.GraphDriver().String()) + v.SetJson("DriverStatus", srv.runtime.GraphDriver().Status()) + v.SetBool("MemoryLimit", srv.runtime.SystemConfig().MemoryLimit) + v.SetBool("SwapLimit", srv.runtime.SystemConfig().SwapLimit) + v.SetBool("IPv4Forwarding", !srv.runtime.SystemConfig().IPv4ForwardingDisabled) v.SetBool("Debug", os.Getenv("DEBUG") != "") v.SetInt("NFd", utils.GetTotalUsedFds()) - v.SetInt("NGoroutines", runtime.NumGoroutine()) - v.Set("ExecutionDriver", srv.runtime.execDriver.Name()) + v.SetInt("NGoroutines", goruntime.NumGoroutine()) + v.Set("ExecutionDriver", srv.runtime.ExecutionDriver().Name()) v.SetInt("NEventsListener", len(srv.listeners)) v.Set("KernelVersion", kernelVersion) v.Set("IndexServerAddress", auth.IndexServerAddress()) @@ -837,13 +841,13 @@ func (srv *Server) ImageHistory(job *engine.Job) engine.Status { return job.Errorf("Usage: %s IMAGE", job.Name) } name := job.Args[0] - image, err := srv.runtime.repositories.LookupImage(name) + foundImage, err := srv.runtime.Repositories().LookupImage(name) if err != nil { return job.Error(err) } lookupMap := make(map[string][]string) - for name, repository := range srv.runtime.repositories.Repositories { + for name, repository := range srv.runtime.Repositories().Repositories { for tag, id := range repository { // If the ID already has a reverse lookup, do not update it unless for "latest" if _, exists := lookupMap[id]; !exists { @@ -854,7 +858,7 @@ func (srv *Server) ImageHistory(job *engine.Job) engine.Status { } outs := engine.NewTable("Created", 0) - err = image.WalkHistory(func(img *Image) error { + err = foundImage.WalkHistory(func(img *image.Image) error { out := &engine.Env{} out.Set("Id", img.ID) out.SetInt64("Created", img.Created.Unix()) @@ -888,7 +892,7 @@ func (srv *Server) ContainerTop(job *engine.Job) engine.Status { if !container.State.IsRunning() { return job.Errorf("Container %s is not running", name) } - pids, err := srv.runtime.execDriver.GetPidsForContainer(container.ID) + pids, err := srv.runtime.ExecutionDriver().GetPidsForContainer(container.ID) if err != nil { return job.Error(err) } @@ -981,7 +985,7 @@ func (srv *Server) Containers(job *engine.Job) engine.Status { outs := engine.NewTable("Created", 0) names := map[string][]string{} - srv.runtime.containerGraph.Walk("/", func(p string, e *graphdb.Entity) error { + srv.runtime.ContainerGraph().Walk("/", func(p string, e *graphdb.Entity) error { names[e.ID()] = append(names[e.ID()], p) return nil }, -1) @@ -1006,7 +1010,7 @@ func (srv *Server) Containers(job *engine.Job) engine.Status { out := &engine.Env{} out.Set("Id", container.ID) out.SetList("Names", names[container.ID]) - out.Set("Image", srv.runtime.repositories.ImageName(container.Image)) + out.Set("Image", srv.runtime.Repositories().ImageName(container.Image)) if len(container.Args) > 0 { out.Set("Command", fmt.Sprintf("\"%s %s\"", container.Path, strings.Join(container.Args, " "))) } else { @@ -1064,7 +1068,7 @@ func (srv *Server) ImageTag(job *engine.Job) engine.Status { if len(job.Args) == 3 { tag = job.Args[2] } - if err := srv.runtime.repositories.Set(job.Args[1], tag, job.Args[0], job.GetenvBool("force")); err != nil { + if err := srv.runtime.Repositories().Set(job.Args[1], tag, job.Args[0], job.GetenvBool("force")); err != nil { return job.Error(err) } return engine.StatusOK @@ -1089,7 +1093,7 @@ func (srv *Server) pullImage(r *registry.Registry, out io.Writer, imgID, endpoin } defer srv.poolRemove("pull", "layer:"+id) - if !srv.runtime.graph.Exists(id) { + if !srv.runtime.Graph().Exists(id) { out.Write(sf.FormatProgress(utils.TruncateID(id), "Pulling metadata", nil)) imgJSON, imgSize, err := r.GetRemoteImageJSON(id, endpoint, token) if err != nil { @@ -1097,7 +1101,7 @@ func (srv *Server) pullImage(r *registry.Registry, out io.Writer, imgID, endpoin // FIXME: Keep going in case of error? return err } - img, err := NewImgJSON(imgJSON) + img, err := image.NewImgJSON(imgJSON) if err != nil { out.Write(sf.FormatProgress(utils.TruncateID(id), "Error pulling dependent layers", nil)) return fmt.Errorf("Failed to parse json: %s", err) @@ -1111,7 +1115,7 @@ func (srv *Server) pullImage(r *registry.Registry, out io.Writer, imgID, endpoin return err } defer layer.Close() - if err := srv.runtime.graph.Register(imgJSON, utils.ProgressReader(layer, imgSize, out, sf, false, utils.TruncateID(id), "Downloading"), img); err != nil { + if err := srv.runtime.Graph().Register(imgJSON, utils.ProgressReader(layer, imgSize, out, sf, false, utils.TruncateID(id), "Downloading"), img); err != nil { out.Write(sf.FormatProgress(utils.TruncateID(id), "Error downloading dependent layers", nil)) return err } @@ -1246,11 +1250,11 @@ func (srv *Server) pullRepository(r *registry.Registry, out io.Writer, localName if askedTag != "" && tag != askedTag { continue } - if err := srv.runtime.repositories.Set(localName, tag, id, true); err != nil { + if err := srv.runtime.Repositories().Set(localName, tag, id, true); err != nil { return err } } - if err := srv.runtime.repositories.Save(); err != nil { + if err := srv.runtime.Repositories().Save(); err != nil { return err } @@ -1371,7 +1375,7 @@ func (srv *Server) getImageList(localRepo map[string]string) ([]string, map[stri tagsByImage[id] = append(tagsByImage[id], tag) - for img, err := srv.runtime.graph.Get(id); img != nil; img, err = img.GetParent() { + for img, err := srv.runtime.Graph().Get(id); img != nil; img, err = img.GetParent() { if err != nil { return nil, nil, err } @@ -1478,7 +1482,7 @@ func (srv *Server) pushRepository(r *registry.Registry, out io.Writer, localName func (srv *Server) pushImage(r *registry.Registry, out io.Writer, remote, imgID, ep string, token []string, sf *utils.StreamFormatter) (checksum string, err error) { out = utils.NewWriteFlusher(out) - jsonRaw, err := ioutil.ReadFile(path.Join(srv.runtime.graph.Root, imgID, "json")) + jsonRaw, err := ioutil.ReadFile(path.Join(srv.runtime.Graph().Root, imgID, "json")) if err != nil { return "", fmt.Errorf("Cannot retrieve the path for {%s}: %s", imgID, err) } @@ -1497,7 +1501,7 @@ func (srv *Server) pushImage(r *registry.Registry, out io.Writer, remote, imgID, return "", err } - layerData, err := srv.runtime.graph.TempLayerArchive(imgID, archive.Uncompressed, sf, out) + layerData, err := srv.runtime.Graph().TempLayerArchive(imgID, archive.Uncompressed, sf, out) if err != nil { return "", fmt.Errorf("Failed to generate layer archive: %s", err) } @@ -1549,17 +1553,17 @@ func (srv *Server) ImagePush(job *engine.Job) engine.Status { return job.Error(err) } - img, err := srv.runtime.graph.Get(localName) + img, err := srv.runtime.Graph().Get(localName) r, err2 := registry.NewRegistry(authConfig, srv.HTTPRequestFactory(metaHeaders), endpoint) if err2 != nil { return job.Error(err2) } if err != nil { - reposLen := len(srv.runtime.repositories.Repositories[localName]) + reposLen := len(srv.runtime.Repositories().Repositories[localName]) job.Stdout.Write(sf.FormatStatus("", "The push refers to a repository [%s] (len: %d)", localName, reposLen)) // If it fails, try to get the repository - if localRepo, exists := srv.runtime.repositories.Repositories[localName]; exists { + if localRepo, exists := srv.runtime.Repositories().Repositories[localName]; exists { if err := srv.pushRepository(r, job.Stdout, localName, remoteName, localRepo, sf); err != nil { return job.Error(err) } @@ -1615,13 +1619,13 @@ func (srv *Server) ImageImport(job *engine.Job) engine.Status { defer progressReader.Close() archive = progressReader } - img, err := srv.runtime.graph.Create(archive, nil, "Imported from "+src, "", nil) + img, err := srv.runtime.Graph().Create(archive, "", "", "Imported from "+src, "", nil, nil) if err != nil { return job.Error(err) } // Optionally register the image at REPO/TAG if repo != "" { - if err := srv.runtime.repositories.Set(repo, tag, img.ID, true); err != nil { + if err := srv.runtime.Repositories().Set(repo, tag, img.ID, true); err != nil { return job.Error(err) } } @@ -1640,11 +1644,11 @@ func (srv *Server) ContainerCreate(job *engine.Job) engine.Status { if config.Memory != 0 && config.Memory < 524288 { return job.Errorf("Minimum memory limit allowed is 512k") } - if config.Memory > 0 && !srv.runtime.sysInfo.MemoryLimit { + if config.Memory > 0 && !srv.runtime.SystemConfig().MemoryLimit { job.Errorf("Your kernel does not support memory limit capabilities. Limitation discarded.\n") config.Memory = 0 } - if config.Memory > 0 && !srv.runtime.sysInfo.SwapLimit { + if config.Memory > 0 && !srv.runtime.SystemConfig().SwapLimit { job.Errorf("Your kernel does not support swap limit capabilities. Limitation discarded.\n") config.MemorySwap = -1 } @@ -1652,26 +1656,26 @@ func (srv *Server) ContainerCreate(job *engine.Job) engine.Status { if err != nil { return job.Error(err) } - if !config.NetworkDisabled && len(config.Dns) == 0 && len(srv.runtime.config.Dns) == 0 && utils.CheckLocalDns(resolvConf) { - job.Errorf("Local (127.0.0.1) DNS resolver found in resolv.conf and containers can't use it. Using default external servers : %v\n", defaultDns) - config.Dns = defaultDns + if !config.NetworkDisabled && len(config.Dns) == 0 && len(srv.runtime.Config().Dns) == 0 && utils.CheckLocalDns(resolvConf) { + job.Errorf("Local (127.0.0.1) DNS resolver found in resolv.conf and containers can't use it. Using default external servers : %v\n", runtime.DefaultDns) + config.Dns = runtime.DefaultDns } container, buildWarnings, err := srv.runtime.Create(config, name) if err != nil { - if srv.runtime.graph.IsNotExist(err) { + if srv.runtime.Graph().IsNotExist(err) { _, tag := utils.ParseRepositoryTag(config.Image) if tag == "" { - tag = DEFAULTTAG + tag = graph.DEFAULTTAG } return job.Errorf("No such image: %s (tag: %s)", config.Image, tag) } return job.Error(err) } - if !container.Config.NetworkDisabled && srv.runtime.sysInfo.IPv4ForwardingDisabled { + if !container.Config.NetworkDisabled && srv.runtime.SystemConfig().IPv4ForwardingDisabled { job.Errorf("IPv4 forwarding is disabled.\n") } - srv.LogEvent("create", container.ID, srv.runtime.repositories.ImageName(container.Image)) + srv.LogEvent("create", container.ID, srv.runtime.Repositories().ImageName(container.Image)) // FIXME: this is necessary because runtime.Create might return a nil container // with a non-nil error. This should not happen! Once it's fixed we // can remove this workaround. @@ -1699,7 +1703,7 @@ func (srv *Server) ContainerRestart(job *engine.Job) engine.Status { if err := container.Restart(int(t)); err != nil { return job.Errorf("Cannot restart container %s: %s\n", name, err) } - srv.LogEvent("restart", container.ID, srv.runtime.repositories.ImageName(container.Image)) + srv.LogEvent("restart", container.ID, srv.runtime.Repositories().ImageName(container.Image)) } else { return job.Errorf("No such container: %s\n", name) } @@ -1721,7 +1725,7 @@ func (srv *Server) ContainerDestroy(job *engine.Job) engine.Status { if container == nil { return job.Errorf("No such link: %s", name) } - name, err := getFullName(name) + name, err := runtime.GetFullContainerName(name) if err != nil { job.Error(err) } @@ -1729,21 +1733,17 @@ func (srv *Server) ContainerDestroy(job *engine.Job) engine.Status { if parent == "/" { return job.Errorf("Conflict, cannot remove the default name of the container") } - pe := srv.runtime.containerGraph.Get(parent) + pe := srv.runtime.ContainerGraph().Get(parent) if pe == nil { return job.Errorf("Cannot get parent %s for name %s", parent, name) } parentContainer := srv.runtime.Get(pe.ID()) - if parentContainer != nil && parentContainer.activeLinks != nil { - if link, exists := parentContainer.activeLinks[n]; exists { - link.Disable() - } else { - utils.Debugf("Could not find active link for %s", name) - } + if parentContainer != nil { + parentContainer.DisableLink(n) } - if err := srv.runtime.containerGraph.Delete(name); err != nil { + if err := srv.runtime.ContainerGraph().Delete(name); err != nil { return job.Error(err) } return engine.StatusOK @@ -1762,13 +1762,13 @@ func (srv *Server) ContainerDestroy(job *engine.Job) engine.Status { if err := srv.runtime.Destroy(container); err != nil { return job.Errorf("Cannot destroy container %s: %s", name, err) } - srv.LogEvent("destroy", container.ID, srv.runtime.repositories.ImageName(container.Image)) + srv.LogEvent("destroy", container.ID, srv.runtime.Repositories().ImageName(container.Image)) if removeVolume { var ( volumes = make(map[string]struct{}) binds = make(map[string]struct{}) - usedVolumes = make(map[string]*Container) + usedVolumes = make(map[string]*runtime.Container) ) // the volume id is always the base of the path @@ -1777,7 +1777,7 @@ func (srv *Server) ContainerDestroy(job *engine.Job) engine.Status { } // populate bind map so that they can be skipped and not removed - for _, bind := range container.hostConfig.Binds { + for _, bind := range container.HostConfig().Binds { source := strings.Split(bind, ":")[0] // TODO: refactor all volume stuff, all of it // this is very important that we eval the link @@ -1816,7 +1816,7 @@ func (srv *Server) ContainerDestroy(job *engine.Job) engine.Status { log.Printf("The volume %s is used by the container %s. Impossible to remove it. Skipping.\n", volumeId, c.ID) continue } - if err := srv.runtime.volumes.Delete(volumeId); err != nil { + if err := srv.runtime.Volumes().Delete(volumeId); err != nil { return job.Errorf("Error calling volumes.Delete(%q): %v", volumeId, err) } } @@ -1835,12 +1835,12 @@ func (srv *Server) DeleteImage(name string, imgs *engine.Table, first, force boo repoName, tag = utils.ParseRepositoryTag(name) if tag == "" { - tag = DEFAULTTAG + tag = graph.DEFAULTTAG } - img, err := srv.runtime.repositories.LookupImage(name) + img, err := srv.runtime.Repositories().LookupImage(name) if err != nil { - if r, _ := srv.runtime.repositories.Get(repoName); r != nil { + if r, _ := srv.runtime.Repositories().Get(repoName); r != nil { return fmt.Errorf("No such image: %s:%s", repoName, tag) } return fmt.Errorf("No such image: %s", name) @@ -1851,14 +1851,14 @@ func (srv *Server) DeleteImage(name string, imgs *engine.Table, first, force boo tag = "" } - byParents, err := srv.runtime.graph.ByParent() + byParents, err := srv.runtime.Graph().ByParent() if err != nil { return err } //If delete by id, see if the id belong only to one repository if repoName == "" { - for _, repoAndTag := range srv.runtime.repositories.ByID()[img.ID] { + for _, repoAndTag := range srv.runtime.Repositories().ByID()[img.ID] { parsedRepo, parsedTag := utils.ParseRepositoryTag(repoAndTag) if repoName == "" || repoName == parsedRepo { repoName = parsedRepo @@ -1881,7 +1881,7 @@ func (srv *Server) DeleteImage(name string, imgs *engine.Table, first, force boo //Untag the current image for _, tag := range tags { - tagDeleted, err := srv.runtime.repositories.Delete(repoName, tag) + tagDeleted, err := srv.runtime.Repositories().Delete(repoName, tag) if err != nil { return err } @@ -1892,16 +1892,16 @@ func (srv *Server) DeleteImage(name string, imgs *engine.Table, first, force boo srv.LogEvent("untag", img.ID, "") } } - tags = srv.runtime.repositories.ByID()[img.ID] + tags = srv.runtime.Repositories().ByID()[img.ID] if (len(tags) <= 1 && repoName == "") || len(tags) == 0 { if len(byParents[img.ID]) == 0 { if err := srv.canDeleteImage(img.ID); err != nil { return err } - if err := srv.runtime.repositories.DeleteAll(img.ID); err != nil { + if err := srv.runtime.Repositories().DeleteAll(img.ID); err != nil { return err } - if err := srv.runtime.graph.Delete(img.ID); err != nil { + if err := srv.runtime.Graph().Delete(img.ID); err != nil { return err } out := &engine.Env{} @@ -1940,12 +1940,12 @@ func (srv *Server) ImageDelete(job *engine.Job) engine.Status { func (srv *Server) canDeleteImage(imgID string) error { for _, container := range srv.runtime.List() { - parent, err := srv.runtime.repositories.LookupImage(container.Image) + parent, err := srv.runtime.Repositories().LookupImage(container.Image) if err != nil { return err } - if err := parent.WalkHistory(func(p *Image) error { + if err := parent.WalkHistory(func(p *image.Image) error { if imgID == p.ID { return fmt.Errorf("Conflict, cannot delete %s because the container %s is using it", utils.TruncateID(imgID), utils.TruncateID(container.ID)) } @@ -1957,10 +1957,10 @@ func (srv *Server) canDeleteImage(imgID string) error { return nil } -func (srv *Server) ImageGetCached(imgID string, config *runconfig.Config) (*Image, error) { +func (srv *Server) ImageGetCached(imgID string, config *runconfig.Config) (*image.Image, error) { // Retrieve all images - images, err := srv.runtime.graph.Map() + images, err := srv.runtime.Graph().Map() if err != nil { return nil, err } @@ -1975,9 +1975,9 @@ func (srv *Server) ImageGetCached(imgID string, config *runconfig.Config) (*Imag } // Loop on the children of the given image and check the config - var match *Image + var match *image.Image for elem := range imageMap[imgID] { - img, err := srv.runtime.graph.Get(elem) + img, err := srv.runtime.Graph().Get(elem) if err != nil { return nil, err } @@ -1990,7 +1990,7 @@ func (srv *Server) ImageGetCached(imgID string, config *runconfig.Config) (*Imag return match, nil } -func (srv *Server) RegisterLinks(container *Container, hostConfig *runconfig.HostConfig) error { +func (srv *Server) RegisterLinks(container *runtime.Container, hostConfig *runconfig.HostConfig) error { runtime := srv.runtime if hostConfig != nil && hostConfig.Links != nil { @@ -2014,7 +2014,7 @@ func (srv *Server) RegisterLinks(container *Container, hostConfig *runconfig.Hos // After we load all the links into the runtime // set them to nil on the hostconfig hostConfig.Links = nil - if err := container.writeHostConfig(); err != nil { + if err := container.WriteHostConfig(); err != nil { return err } } @@ -2062,13 +2062,13 @@ func (srv *Server) ContainerStart(job *engine.Job) engine.Status { if err := srv.RegisterLinks(container, hostConfig); err != nil { return job.Error(err) } - container.hostConfig = hostConfig + container.SetHostConfig(hostConfig) container.ToDisk() } if err := container.Start(); err != nil { return job.Errorf("Cannot start container %s: %s", name, err) } - srv.LogEvent("start", container.ID, runtime.repositories.ImageName(container.Image)) + srv.LogEvent("start", container.ID, runtime.Repositories().ImageName(container.Image)) return engine.StatusOK } @@ -2088,7 +2088,7 @@ func (srv *Server) ContainerStop(job *engine.Job) engine.Status { if err := container.Stop(int(t)); err != nil { return job.Errorf("Cannot stop container %s: %s\n", name, err) } - srv.LogEvent("stop", container.ID, srv.runtime.repositories.ImageName(container.Image)) + srv.LogEvent("stop", container.ID, srv.runtime.Repositories().ImageName(container.Image)) } else { return job.Errorf("No such container: %s\n", name) } @@ -2234,15 +2234,15 @@ func (srv *Server) ContainerAttach(job *engine.Job) engine.Status { return engine.StatusOK } -func (srv *Server) ContainerInspect(name string) (*Container, error) { +func (srv *Server) ContainerInspect(name string) (*runtime.Container, error) { if container := srv.runtime.Get(name); container != nil { return container, nil } return nil, fmt.Errorf("No such container: %s", name) } -func (srv *Server) ImageInspect(name string) (*Image, error) { - if image, err := srv.runtime.repositories.LookupImage(name); err == nil && image != nil { +func (srv *Server) ImageInspect(name string) (*image.Image, error) { + if image, err := srv.runtime.Repositories().LookupImage(name); err == nil && image != nil { return image, nil } return nil, fmt.Errorf("No such image: %s", name) @@ -2277,9 +2277,9 @@ func (srv *Server) JobInspect(job *engine.Job) engine.Status { return job.Error(errContainer) } object = &struct { - *Container + *runtime.Container HostConfig *runconfig.HostConfig - }{container, container.hostConfig} + }{container, container.HostConfig()} default: return job.Errorf("Unknown kind: %s", kind) } @@ -2318,8 +2318,8 @@ func (srv *Server) ContainerCopy(job *engine.Job) engine.Status { return job.Errorf("No such container: %s", name) } -func NewServer(eng *engine.Engine, config *DaemonConfig) (*Server, error) { - runtime, err := NewRuntime(config, eng) +func NewServer(eng *engine.Engine, config *daemonconfig.Config) (*Server, error) { + runtime, err := runtime.NewRuntime(config, eng) if err != nil { return nil, err } @@ -2332,7 +2332,7 @@ func NewServer(eng *engine.Engine, config *DaemonConfig) (*Server, error) { listeners: make(map[string]chan utils.JSONMessage), running: true, } - runtime.srv = srv + runtime.SetServer(srv) return srv, nil } @@ -2400,7 +2400,7 @@ func (srv *Server) Close() error { type Server struct { sync.RWMutex - runtime *Runtime + runtime *runtime.Runtime pullingPool map[string]chan struct{} pushingPool map[string]chan struct{} events []utils.JSONMessage diff --git a/utils.go b/utils.go index ef666b0de1..0fda006860 100644 --- a/utils.go +++ b/utils.go @@ -2,9 +2,6 @@ package docker import ( "github.com/dotcloud/docker/archive" - "github.com/dotcloud/docker/nat" - "github.com/dotcloud/docker/pkg/namesgenerator" - "github.com/dotcloud/docker/runconfig" "github.com/dotcloud/docker/utils" ) @@ -12,45 +9,8 @@ type Change struct { archive.Change } -func migratePortMappings(config *runconfig.Config, hostConfig *runconfig.HostConfig) error { - if config.PortSpecs != nil { - ports, bindings, err := nat.ParsePortSpecs(config.PortSpecs) - if err != nil { - return err - } - config.PortSpecs = nil - if len(bindings) > 0 { - if hostConfig == nil { - hostConfig = &runconfig.HostConfig{} - } - hostConfig.PortBindings = bindings - } - - if config.ExposedPorts == nil { - config.ExposedPorts = make(nat.PortSet, len(ports)) - } - for k, v := range ports { - config.ExposedPorts[k] = v - } - } - return nil -} - // Links come in the format of // name:alias func parseLink(rawLink string) (map[string]string, error) { return utils.PartParser("name:alias", rawLink) } - -type checker struct { - runtime *Runtime -} - -func (c *checker) Exists(name string) bool { - return c.runtime.containerGraph.Exists("/" + name) -} - -// Generate a random and unique name -func generateRandomName(runtime *Runtime) (string, error) { - return namesgenerator.GenerateRandomName(&checker{runtime}) -} diff --git a/utils/utils.go b/utils/utils.go index 07b8f6a3d0..e4cb04f39c 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,6 +2,7 @@ package utils import ( "bytes" + "crypto/rand" "crypto/sha1" "crypto/sha256" "encoding/hex" @@ -493,6 +494,34 @@ func TruncateID(id string) string { return id[:shortLen] } +// GenerateRandomID returns an unique id +func GenerateRandomID() string { + for { + id := make([]byte, 32) + if _, err := io.ReadFull(rand.Reader, id); err != nil { + panic(err) // This shouldn't happen + } + value := hex.EncodeToString(id) + // if we try to parse the truncated for as an int and we don't have + // an error then the value is all numberic and causes issues when + // used as a hostname. ref #3869 + if _, err := strconv.Atoi(TruncateID(value)); err == nil { + continue + } + return value + } +} + +func ValidateID(id string) error { + if id == "" { + return fmt.Errorf("Id can't be empty") + } + if strings.Contains(id, ":") { + return fmt.Errorf("Invalid character in id: ':'") + } + return nil +} + // Code c/c from io.Copy() modified to handle escape sequence func CopyEscapable(dst io.Writer, src io.ReadCloser) (written int64, err error) { buf := make([]byte, 32*1024) diff --git a/utils_test.go b/utils_test.go deleted file mode 100644 index 31fa12b6ad..0000000000 --- a/utils_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package docker - -import ( - "bytes" - "github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" - "io" -) - -func fakeTar() (io.Reader, error) { - content := []byte("Hello world!\n") - buf := new(bytes.Buffer) - tw := tar.NewWriter(buf) - for _, name := range []string{"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} { - hdr := new(tar.Header) - hdr.Size = int64(len(content)) - hdr.Name = name - if err := tw.WriteHeader(hdr); err != nil { - return nil, err - } - tw.Write([]byte(content)) - } - tw.Close() - return buf, nil -}