ef711962 |
package docker |
33f6a0aa |
import ( |
44b33b44 |
"encoding/json" |
33f6a0aa |
"fmt" |
9bb3dc98 |
"github.com/dotcloud/docker/registry" |
2e69e172 |
"github.com/dotcloud/docker/utils" |
09f1cbab |
"io" |
33f6a0aa |
"io/ioutil"
"os"
"path"
"path/filepath" |
004a5310 |
"strings" |
8ff17656 |
"sync" |
33f6a0aa |
"time"
)
|
13d2b086 |
// A Graph is a store for versioned filesystem images and the relationship between them. |
33f6a0aa |
type Graph struct { |
8ff17656 |
Root string |
2e69e172 |
idIndex *utils.TruncIndex |
8ff17656 |
checksumLock map[string]*sync.Mutex
lockSumFile *sync.Mutex
lockSumMap *sync.Mutex |
33f6a0aa |
}
|
13d2b086 |
// NewGraph instantiates a new graph at the given root path in the filesystem. |
99b36c2c |
// `root` will be created if it doesn't exist. |
ef711962 |
func NewGraph(root string) (*Graph, error) { |
33f6a0aa |
abspath, err := filepath.Abs(root)
if err != nil {
return nil, err
}
// Create the root directory if it doesn't exists |
0e23b4e1 |
if err := os.MkdirAll(root, 0700); err != nil && !os.IsExist(err) { |
33f6a0aa |
return nil, err
} |
1632566e |
graph := &Graph{ |
8ff17656 |
Root: abspath, |
2e69e172 |
idIndex: utils.NewTruncIndex(), |
8ff17656 |
checksumLock: make(map[string]*sync.Mutex),
lockSumFile: &sync.Mutex{},
lockSumMap: &sync.Mutex{}, |
1632566e |
}
if err := graph.restore(); err != nil {
return nil, err
}
return graph, nil
}
func (graph *Graph) restore() error {
dir, err := ioutil.ReadDir(graph.Root)
if err != nil {
return err
}
for _, v := range dir {
id := v.Name()
graph.idIndex.Add(id)
}
return nil |
33f6a0aa |
}
|
004a5310 |
// FIXME: Implement error subclass instead of looking at the error text
// Note: This is the way golang implements os.IsNotExists on Plan9
func (graph *Graph) IsNotExist(err error) bool { |
0a5d86d7 |
return err != nil && (strings.Contains(err.Error(), "does not exist") || strings.Contains(err.Error(), "No such")) |
004a5310 |
}
|
99b36c2c |
// Exists returns true if an image is registered at the given id.
// If the image doesn't exist or if an error is encountered, false is returned. |
33f6a0aa |
func (graph *Graph) Exists(id string) bool {
if _, err := graph.Get(id); err != nil {
return false
}
return true
}
|
99b36c2c |
// Get returns the image with the given id, or an error if the image doesn't exist. |
1632566e |
func (graph *Graph) Get(name string) (*Image, error) {
id, err := graph.idIndex.Get(name)
if err != nil {
return nil, err
} |
379d449c |
// FIXME: return nil when the image doesn't exist, instead of an error |
33f6a0aa |
img, err := LoadImage(graph.imageRoot(id))
if err != nil {
return nil, err
} |
fd224ee5 |
if img.ID != id {
return nil, fmt.Errorf("Image stored at '%s' has wrong id '%s'", id, img.ID) |
33f6a0aa |
} |
1c946ef0 |
img.graph = graph |
a91b7109 |
if img.Size == 0 {
root, err := img.root()
if err != nil {
return nil, err
}
if err := StoreSize(img, root); err != nil {
return nil, err
}
} |
8ff17656 |
graph.lockSumMap.Lock()
defer graph.lockSumMap.Unlock() |
fd224ee5 |
if _, exists := graph.checksumLock[img.ID]; !exists {
graph.checksumLock[img.ID] = &sync.Mutex{} |
8ff17656 |
} |
33f6a0aa |
return img, nil
}
|
99b36c2c |
// Create creates a new image and registers it in the graph. |
51d62282 |
func (graph *Graph) Create(layerData Archive, container *Container, comment, author string, config *Config) (*Image, error) { |
33f6a0aa |
img := &Image{ |
fd224ee5 |
ID: GenerateID(), |
8bfbdd7a |
Comment: comment,
Created: time.Now(),
DockerVersion: VERSION, |
4ef2d5c1 |
Author: author, |
51d62282 |
Config: config, |
5f69a53d |
Architecture: "x86_64", |
33f6a0aa |
} |
05ae69a6 |
if container != nil {
img.Parent = container.Image |
fd224ee5 |
img.Container = container.ID |
0146c80c |
img.ContainerConfig = *container.Config |
05ae69a6 |
} |
be9dd7b8 |
if err := graph.Register(layerData, layerData != nil, img); err != nil { |
33f6a0aa |
return nil, err
} |
8ff17656 |
go img.Checksum() |
33f6a0aa |
return img, nil
}
|
99b36c2c |
// Register imports a pre-existing image into the graph.
// FIXME: pass img as first argument |
aaaf3f07 |
func (graph *Graph) Register(layerData Archive, store bool, img *Image) error { |
fd224ee5 |
if err := ValidateID(img.ID); err != nil { |
33f6a0aa |
return err
}
// (This is a convenience to save time. Race conditions are taken care of by os.Rename) |
fd224ee5 |
if graph.Exists(img.ID) {
return fmt.Errorf("Image %s already exists", img.ID) |
33f6a0aa |
} |
13d9e26e |
tmp, err := graph.Mktemp("") |
33f6a0aa |
defer os.RemoveAll(tmp)
if err != nil {
return fmt.Errorf("Mktemp failed: %s", err)
} |
aaaf3f07 |
if err := StoreImage(img, layerData, tmp, store); err != nil { |
33f6a0aa |
return err
}
// Commit |
fd224ee5 |
if err := os.Rename(tmp, graph.imageRoot(img.ID)); err != nil { |
33f6a0aa |
return err
}
img.graph = graph |
fd224ee5 |
graph.idIndex.Add(img.ID)
graph.checksumLock[img.ID] = &sync.Mutex{} |
33f6a0aa |
return nil
}
|
baacae83 |
// TempLayerArchive creates a temporary archive of the given image's filesystem layer.
// The archive is stored on disk and will be automatically deleted as soon as has been read. |
965e8a02 |
// If output is not nil, a human-readable progress bar will be written to it.
// FIXME: does this belong in Graph? How about MktempFile, let the caller use it for archives?
func (graph *Graph) TempLayerArchive(id string, compression Compression, output io.Writer) (*TempArchive, error) { |
baacae83 |
image, err := graph.Get(id)
if err != nil {
return nil, err
}
tmp, err := graph.tmp()
if err != nil {
return nil, err
}
archive, err := image.TarLayer(compression)
if err != nil {
return nil, err
} |
5a36efb6 |
sf := utils.NewStreamFormatter(false)
return NewTempArchive(utils.ProgressReader(ioutil.NopCloser(archive), 0, output, sf.FormatProgress("Buffering to disk", "%v/%v (%v)"), sf), tmp.Root) |
baacae83 |
}
|
99b36c2c |
// Mktemp creates a temporary sub-directory inside the graph's filesystem. |
33f6a0aa |
func (graph *Graph) Mktemp(id string) (string, error) { |
5d3c0767 |
if id == "" { |
fd224ee5 |
id = GenerateID() |
5d3c0767 |
} |
baacae83 |
tmp, err := graph.tmp() |
33f6a0aa |
if err != nil {
return "", fmt.Errorf("Couldn't create temp: %s", err)
}
if tmp.Exists(id) {
return "", fmt.Errorf("Image %d already exists", id)
}
return tmp.imageRoot(id), nil
}
|
baacae83 |
func (graph *Graph) tmp() (*Graph, error) {
return NewGraph(path.Join(graph.Root, ":tmp:"))
}
|
13d2b086 |
// Check if given error is "not empty".
// Note: this is the way golang does it internally with os.IsNotExists. |
069918e1 |
func isNotEmpty(err error) bool {
switch pe := err.(type) {
case nil:
return false
case *os.PathError:
err = pe.Err
case *os.LinkError:
err = pe.Err
}
return strings.Contains(err.Error(), " not empty")
}
|
99b36c2c |
// Delete atomically removes an image from the graph. |
ff5cb8e8 |
func (graph *Graph) Delete(name string) error {
id, err := graph.idIndex.Get(name)
if err != nil {
return err
} |
5d3c0767 |
tmp, err := graph.Mktemp("") |
33f6a0aa |
if err != nil {
return err
} |
1632566e |
graph.idIndex.Delete(id) |
5d3c0767 |
err = os.Rename(graph.imageRoot(id), tmp) |
33f6a0aa |
if err != nil {
return err
} |
5d3c0767 |
return os.RemoveAll(tmp) |
33f6a0aa |
}
|
13d2b086 |
// Map returns a list of all images in the graph, addressable by ID. |
44faa07b |
func (graph *Graph) Map() (map[string]*Image, error) {
// FIXME: this should replace All()
all, err := graph.All()
if err != nil {
return nil, err
}
images := make(map[string]*Image, len(all))
for _, image := range all { |
fd224ee5 |
images[image.ID] = image |
44faa07b |
}
return images, nil
}
|
13d2b086 |
// All returns a list of all images in the graph. |
33f6a0aa |
func (graph *Graph) All() ([]*Image, error) { |
d301c7b9 |
var images []*Image
err := graph.WalkAll(func(image *Image) {
images = append(images, image)
})
return images, err
}
|
99b36c2c |
// WalkAll iterates over each image in the graph, and passes it to a handler.
// The walking order is undetermined. |
d301c7b9 |
func (graph *Graph) WalkAll(handler func(*Image)) error { |
33f6a0aa |
files, err := ioutil.ReadDir(graph.Root)
if err != nil { |
d301c7b9 |
return err |
33f6a0aa |
}
for _, st := range files {
if img, err := graph.Get(st.Name()); err != nil {
// Skip image
continue |
d301c7b9 |
} else if handler != nil {
handler(img)
}
}
return nil
}
|
99b36c2c |
// ByParent returns a lookup table of images by their parent.
// 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. |
d301c7b9 |
func (graph *Graph) ByParent() (map[string][]*Image, error) {
byParent := make(map[string][]*Image)
err := graph.WalkAll(func(image *Image) { |
23c5c130 |
parent, err := graph.Get(image.Parent) |
d301c7b9 |
if err != nil {
return
} |
fd224ee5 |
if children, exists := byParent[parent.ID]; exists {
byParent[parent.ID] = []*Image{image} |
33f6a0aa |
} else { |
fd224ee5 |
byParent[parent.ID] = append(children, image) |
33f6a0aa |
} |
d301c7b9 |
})
return byParent, err
}
|
99b36c2c |
// 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. |
d301c7b9 |
func (graph *Graph) Heads() (map[string]*Image, error) {
heads := make(map[string]*Image)
byParent, err := graph.ByParent()
if err != nil {
return nil, err |
33f6a0aa |
} |
d301c7b9 |
err = graph.WalkAll(func(image *Image) {
// If it's not in the byParent lookup table, then
// it's not a parent -> so it's a head! |
fd224ee5 |
if _, exists := byParent[image.ID]; !exists {
heads[image.ID] = image |
d301c7b9 |
}
})
return heads, err |
33f6a0aa |
}
func (graph *Graph) imageRoot(id string) string {
return path.Join(graph.Root, id)
} |
44b33b44 |
func (graph *Graph) getStoredChecksums() (map[string]string, error) {
checksums := make(map[string]string)
// FIXME: Store the checksum in memory
if checksumDict, err := ioutil.ReadFile(path.Join(graph.Root, "checksums")); err == nil {
if err := json.Unmarshal(checksumDict, &checksums); err != nil {
return nil, err
}
}
return checksums, nil
}
func (graph *Graph) storeChecksums(checksums map[string]string) error { |
fd224ee5 |
checksumJSON, err := json.Marshal(checksums) |
44b33b44 |
if err != nil {
return err
} |
fd224ee5 |
if err := ioutil.WriteFile(path.Join(graph.Root, "checksums"), checksumJSON, 0600); err != nil { |
44b33b44 |
return err
}
return nil
} |
9bb3dc98 |
|
95dd6d31 |
func (graph *Graph) UpdateChecksums(newChecksums map[string]*registry.ImgData) error { |
9bb3dc98 |
graph.lockSumFile.Lock()
defer graph.lockSumFile.Unlock()
localChecksums, err := graph.getStoredChecksums()
if err != nil {
return err
}
for id, elem := range newChecksums {
localChecksums[id] = elem.Checksum
}
return graph.storeChecksums(localChecksums)
} |