1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Move graph and tags to graph sub pkg

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-03-07 18:04:38 -08:00
parent 82a5439835
commit 01b6b2be73
8 changed files with 70 additions and 62 deletions

View file

@ -1,4 +1,4 @@
package docker package graph
import ( import (
"fmt" "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. // 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{ img := &image.Image{
ID: utils.GenerateRandomID(), ID: utils.GenerateRandomID(),
Comment: comment, Comment: comment,
@ -139,10 +139,10 @@ func (graph *Graph) Create(layerData archive.ArchiveReader, container *Container
Architecture: runtime.GOARCH, Architecture: runtime.GOARCH,
OS: runtime.GOOS, OS: runtime.GOOS,
} }
if container != nil { if containerID != "" {
img.Parent = container.Image img.Parent = containerImage
img.Container = container.ID img.Container = containerID
img.ContainerConfig = *container.Config img.ContainerConfig = *containerConfig
} }
if err := graph.Register(nil, layerData, img); err != nil { if err := graph.Register(nil, layerData, img); err != nil {
return nil, err 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 // 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. // 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{ for pth, typ := range map[string]string{
"/dev/pts": "dir", "/dev/pts": "dir",
"/dev/shm": "dir", "/dev/shm": "dir",

View file

@ -1,4 +1,4 @@
package docker package graph
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,9 +1,13 @@
package docker package graph
import ( import (
"bytes"
"github.com/dotcloud/docker/graphdriver" "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/image"
"github.com/dotcloud/docker/utils" "github.com/dotcloud/docker/utils"
"github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
"io"
"os" "os"
"path" "path"
"testing" "testing"
@ -14,6 +18,23 @@ const (
testImageID = "foo" 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 { func mkTestTagStore(root string, t *testing.T) *TagStore {
driver, err := graphdriver.New(root) driver, err := graphdriver.New(root)
if err != nil { if err != nil {

View file

@ -2,9 +2,9 @@ package docker
import ( import (
"errors" "errors"
"github.com/dotcloud/docker"
"github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/archive"
"github.com/dotcloud/docker/dockerversion" "github.com/dotcloud/docker/dockerversion"
"github.com/dotcloud/docker/graph"
"github.com/dotcloud/docker/graphdriver" "github.com/dotcloud/docker/graphdriver"
"github.com/dotcloud/docker/image" "github.com/dotcloud/docker/image"
"github.com/dotcloud/docker/utils" "github.com/dotcloud/docker/utils"
@ -25,7 +25,7 @@ func TestMount(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
image, err := graph.Create(archive, nil, "Testing", "", nil) image, err := graph.Create(archive, "", "", "Testing", "", nil, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -97,7 +97,7 @@ func TestGraphCreate(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
img, err := graph.Create(archive, nil, "Testing", "", nil) img, err := graph.Create(archive, "", "", "Testing", "", nil, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -165,12 +165,12 @@ func TestDeletePrefix(t *testing.T) {
assertNImages(graph, t, 0) 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() archive, err := fakeTar()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
img, err := graph.Create(archive, nil, "Test image", "", nil) img, err := graph.Create(archive, "", "", "Test image", "", nil, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -185,7 +185,7 @@ func TestDelete(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
assertNImages(graph, t, 0) 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -200,7 +200,7 @@ func TestDelete(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
// Test 2 create (same name) / 1 delete // 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -208,7 +208,7 @@ func TestDelete(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) 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) t.Fatal(err)
} }
assertNImages(graph, t, 2) assertNImages(graph, t, 2)
@ -280,7 +280,7 @@ func TestByParent(t *testing.T) {
* HELPER FUNCTIONS * 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 { if images, err := graph.Map(); err != nil {
t.Fatal(err) t.Fatal(err)
} else if actualN := len(images); actualN != n { } 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-") tmp, err := ioutil.TempDir("", "docker-graph-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -297,14 +297,14 @@ func tempGraph(t *testing.T) (*docker.Graph, graphdriver.Driver) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
graph, err := docker.NewGraph(tmp, driver) graph, err := graph.NewGraph(tmp, driver)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
return graph, driver return graph, driver
} }
func nukeGraph(graph *docker.Graph) { func nukeGraph(graph *graph.Graph) {
graph.Driver().Cleanup() graph.Driver().Cleanup()
os.RemoveAll(graph.Root) os.RemoveAll(graph.Root)
} }

View file

@ -10,6 +10,7 @@ import (
"github.com/dotcloud/docker/execdriver" "github.com/dotcloud/docker/execdriver"
"github.com/dotcloud/docker/execdriver/lxc" "github.com/dotcloud/docker/execdriver/lxc"
"github.com/dotcloud/docker/execdriver/native" "github.com/dotcloud/docker/execdriver/native"
"github.com/dotcloud/docker/graph"
"github.com/dotcloud/docker/graphdriver" "github.com/dotcloud/docker/graphdriver"
"github.com/dotcloud/docker/graphdriver/aufs" "github.com/dotcloud/docker/graphdriver/aufs"
_ "github.com/dotcloud/docker/graphdriver/btrfs" _ "github.com/dotcloud/docker/graphdriver/btrfs"
@ -48,11 +49,11 @@ type Runtime struct {
repository string repository string
sysInitPath string sysInitPath string
containers *list.List containers *list.List
graph *Graph graph *graph.Graph
repositories *TagStore repositories *graph.TagStore
idIndex *utils.TruncIndex idIndex *utils.TruncIndex
sysInfo *sysinfo.SysInfo sysInfo *sysinfo.SysInfo
volumes *Graph volumes *graph.Graph
srv *Server srv *Server
eng *engine.Engine eng *engine.Engine
config *daemonconfig.Config config *daemonconfig.Config
@ -486,7 +487,7 @@ func (runtime *Runtime) Create(config *runconfig.Config, name string) (*Containe
} }
defer runtime.driver.Put(initID) defer runtime.driver.Put(initID)
if err := setupInitLayer(initPath); err != nil { if err := graph.SetupInitLayer(initPath); err != nil {
return nil, nil, err return nil, nil, err
} }
@ -555,7 +556,16 @@ func (runtime *Runtime) Commit(container *Container, repository, tag, comment, a
defer rwTar.Close() defer rwTar.Close()
// Create a new image from the container's base layers + a new layer from container changes // 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 { if err != nil {
return nil, err return nil, err
} }
@ -654,13 +664,13 @@ func NewRuntimeFromDirectory(config *daemonconfig.Config, eng *engine.Engine) (*
if ad, ok := driver.(*aufs.Driver); ok { if ad, ok := driver.(*aufs.Driver); ok {
utils.Debugf("Migrating existing containers") 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 return nil, err
} }
} }
utils.Debugf("Creating images graph") 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 { if err != nil {
return nil, err return nil, err
} }
@ -672,12 +682,12 @@ func NewRuntimeFromDirectory(config *daemonconfig.Config, eng *engine.Engine) (*
return nil, err return nil, err
} }
utils.Debugf("Creating volumes graph") 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 { if err != nil {
return nil, err return nil, err
} }
utils.Debugf("Creating repository list") 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 { if err != nil {
return nil, fmt.Errorf("Couldn't create Tag store: %s", err) 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. // which need direct access to runtime.graph.
// Once the tests switch to using engine and jobs, this method // Once the tests switch to using engine and jobs, this method
// can go away. // can go away.
func (runtime *Runtime) Graph() *Graph { func (runtime *Runtime) Graph() *graph.Graph {
return runtime.graph return runtime.graph
} }

View file

@ -8,6 +8,7 @@ import (
"github.com/dotcloud/docker/daemonconfig" "github.com/dotcloud/docker/daemonconfig"
"github.com/dotcloud/docker/dockerversion" "github.com/dotcloud/docker/dockerversion"
"github.com/dotcloud/docker/engine" "github.com/dotcloud/docker/engine"
"github.com/dotcloud/docker/graph"
"github.com/dotcloud/docker/image" "github.com/dotcloud/docker/image"
"github.com/dotcloud/docker/pkg/graphdb" "github.com/dotcloud/docker/pkg/graphdb"
"github.com/dotcloud/docker/registry" "github.com/dotcloud/docker/registry"
@ -334,7 +335,7 @@ func (srv *Server) ImageExport(job *engine.Job) engine.Status {
} }
// write repositories // write repositories
rootRepoMap := map[string]Repository{} rootRepoMap := map[string]graph.Repository{}
rootRepoMap[name] = rootRepo rootRepoMap[name] = rootRepo
rootRepoJson, _ := json.Marshal(rootRepoMap) 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")) repositoriesJson, err := ioutil.ReadFile(path.Join(tmpImageDir, "repo", "repositories"))
if err == nil { if err == nil {
repositories := map[string]Repository{} repositories := map[string]graph.Repository{}
if err := json.Unmarshal(repositoriesJson, &repositories); err != nil { if err := json.Unmarshal(repositoriesJson, &repositories); err != nil {
return job.Error(err) return job.Error(err)
} }
@ -1617,7 +1618,7 @@ func (srv *Server) ImageImport(job *engine.Job) engine.Status {
defer progressReader.Close() defer progressReader.Close()
archive = progressReader 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 { if err != nil {
return job.Error(err) return job.Error(err)
} }
@ -1664,7 +1665,7 @@ func (srv *Server) ContainerCreate(job *engine.Job) engine.Status {
if srv.runtime.graph.IsNotExist(err) { if srv.runtime.graph.IsNotExist(err) {
_, tag := utils.ParseRepositoryTag(config.Image) _, tag := utils.ParseRepositoryTag(config.Image)
if tag == "" { if tag == "" {
tag = DEFAULTTAG tag = graph.DEFAULTTAG
} }
return job.Errorf("No such image: %s (tag: %s)", config.Image, tag) 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) repoName, tag = utils.ParseRepositoryTag(name)
if tag == "" { if tag == "" {
tag = DEFAULTTAG tag = graph.DEFAULTTAG
} }
img, err := srv.runtime.repositories.LookupImage(name) img, err := srv.runtime.repositories.LookupImage(name)

View file

@ -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
}

View file

@ -216,7 +216,7 @@ func createVolumes(container *Container) error {
return err return err
} }
volumesDriver := container.runtime.volumes.driver volumesDriver := container.runtime.volumes.Driver()
// Create the requested volumes if they don't exist // Create the requested volumes if they don't exist
for volPath := range container.Config.Volumes { for volPath := range container.Config.Volumes {
volPath = filepath.Clean(volPath) 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. // Do not pass a container as the parameter for the volume creation.
// The graph driver using the container's information ( Image ) to // The graph driver using the container's information ( Image ) to
// create the parent. // create the parent.
c, err := container.runtime.volumes.Create(nil, nil, "", "", nil) c, err := container.runtime.volumes.Create(nil, "", "", "", "", nil, nil)
if err != nil { if err != nil {
return err return err
} }