From 01b6b2be73a6f40e0179e0217385eea6b41100a5 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Fri, 7 Mar 2014 18:04:38 -0800 Subject: [PATCH] Move graph and tags to graph sub pkg Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- graph.go => graph/graph.go | 14 ++++----- tags.go => graph/tags.go | 2 +- tags_unit_test.go => graph/tags_unit_test.go | 23 ++++++++++++++- integration/graph_test.go | 24 ++++++++-------- runtime.go | 30 +++++++++++++------- server.go | 11 +++---- utils_test.go | 24 ---------------- volumes.go | 4 +-- 8 files changed, 70 insertions(+), 62 deletions(-) rename graph.go => graph/graph.go (97%) rename tags.go => graph/tags.go (99%) rename tags_unit_test.go => graph/tags_unit_test.go (76%) delete mode 100644 utils_test.go diff --git a/graph.go b/graph/graph.go similarity index 97% rename from graph.go rename to graph/graph.go index d164760d4c..01659b549f 100644 --- a/graph.go +++ b/graph/graph.go @@ -1,4 +1,4 @@ -package docker +package graph import ( "fmt" @@ -128,7 +128,7 @@ func (graph *Graph) Get(name string) (*image.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.Image, error) { +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, @@ -139,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 @@ -247,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", diff --git a/tags.go b/graph/tags.go similarity index 99% rename from tags.go rename to graph/tags.go index 27e19cd671..524e1a1f9d 100644 --- a/tags.go +++ b/graph/tags.go @@ -1,4 +1,4 @@ -package docker +package graph import ( "encoding/json" diff --git a/tags_unit_test.go b/graph/tags_unit_test.go similarity index 76% rename from tags_unit_test.go rename to graph/tags_unit_test.go index 8ee913f527..153f94db3d 100644 --- a/tags_unit_test.go +++ b/graph/tags_unit_test.go @@ -1,9 +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" @@ -14,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 { diff --git a/integration/graph_test.go b/integration/graph_test.go index 4fd612b5ac..e575a252f3 100644 --- a/integration/graph_test.go +++ b/integration/graph_test.go @@ -2,9 +2,9 @@ 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" @@ -25,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) } @@ -97,7 +97,7 @@ func TestGraphCreate(t *testing.T) { if err != nil { t.Fatal(err) } - img, err := graph.Create(archive, nil, "Testing", "", nil) + img, err := graph.Create(archive, "", "", "Testing", "", nil, nil) if err != nil { t.Fatal(err) } @@ -165,12 +165,12 @@ func TestDeletePrefix(t *testing.T) { assertNImages(graph, t, 0) } -func createTestImage(graph *docker.Graph, t *testing.T) *image.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) } @@ -185,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) } @@ -200,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) } @@ -208,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) @@ -280,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 { @@ -288,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) @@ -297,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/runtime.go b/runtime.go index 81bc9cbded..2608701b9b 100644 --- a/runtime.go +++ b/runtime.go @@ -10,6 +10,7 @@ import ( "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" @@ -48,11 +49,11 @@ 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 + volumes *graph.Graph srv *Server eng *engine.Engine config *daemonconfig.Config @@ -486,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 } @@ -555,7 +556,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 } @@ -654,13 +664,13 @@ func NewRuntimeFromDirectory(config *daemonconfig.Config, eng *engine.Engine) (* 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 } @@ -672,12 +682,12 @@ func NewRuntimeFromDirectory(config *daemonconfig.Config, eng *engine.Engine) (* 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) } @@ -878,7 +888,7 @@ 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 } diff --git a/server.go b/server.go index 37402ee502..5c28b262dc 100644 --- a/server.go +++ b/server.go @@ -8,6 +8,7 @@ import ( "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" @@ -334,7 +335,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) @@ -547,7 +548,7 @@ 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) } @@ -1617,7 +1618,7 @@ 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) } @@ -1664,7 +1665,7 @@ func (srv *Server) ContainerCreate(job *engine.Job) engine.Status { 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) } @@ -1837,7 +1838,7 @@ 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) 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 -} diff --git a/volumes.go b/volumes.go index 9f76e3698b..8684ff4e59 100644 --- a/volumes.go +++ b/volumes.go @@ -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 }