2013-03-22 00:47:23 +00:00
|
|
|
package docker
|
2013-03-18 07:15:35 +00:00
|
|
|
|
|
|
|
import (
|
2013-05-09 05:37:33 +00:00
|
|
|
"encoding/json"
|
2013-03-18 07:15:35 +00:00
|
|
|
"fmt"
|
2013-05-06 18:06:44 +00:00
|
|
|
"io"
|
2013-03-18 07:15:35 +00:00
|
|
|
"io/ioutil"
|
2013-04-18 15:34:43 +00:00
|
|
|
"net/http"
|
2013-03-18 07:15:35 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2013-03-26 12:28:17 +00:00
|
|
|
"strings"
|
2013-05-08 17:42:29 +00:00
|
|
|
"sync"
|
2013-03-18 07:15:35 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-03-30 07:22:24 +00:00
|
|
|
// A Graph is a store for versioned filesystem images and the relationship between them.
|
2013-03-18 07:15:35 +00:00
|
|
|
type Graph struct {
|
2013-05-08 17:42:29 +00:00
|
|
|
Root string
|
|
|
|
idIndex *TruncIndex
|
|
|
|
httpClient *http.Client
|
|
|
|
checksumLock map[string]*sync.Mutex
|
|
|
|
lockSumFile *sync.Mutex
|
|
|
|
lockSumMap *sync.Mutex
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
|
2013-03-30 07:22:24 +00:00
|
|
|
// NewGraph instantiates a new graph at the given root path in the filesystem.
|
2013-03-30 04:13:59 +00:00
|
|
|
// `root` will be created if it doesn't exist.
|
2013-03-22 00:47:23 +00:00
|
|
|
func NewGraph(root string) (*Graph, error) {
|
2013-03-18 07:15:35 +00:00
|
|
|
abspath, err := filepath.Abs(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Create the root directory if it doesn't exists
|
2013-05-08 23:22:12 +00:00
|
|
|
if err := os.MkdirAll(root, 0700); err != nil && !os.IsExist(err) {
|
2013-03-18 07:15:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-04-01 05:11:55 +00:00
|
|
|
graph := &Graph{
|
2013-05-08 17:42:29 +00:00
|
|
|
Root: abspath,
|
|
|
|
idIndex: NewTruncIndex(),
|
|
|
|
checksumLock: make(map[string]*sync.Mutex),
|
|
|
|
lockSumFile: &sync.Mutex{},
|
|
|
|
lockSumMap: &sync.Mutex{},
|
2013-04-01 05:11:55 +00:00
|
|
|
}
|
|
|
|
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
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
|
2013-03-26 12:28:17 +00:00
|
|
|
// 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 {
|
2013-05-09 16:57:47 +00:00
|
|
|
return err != nil && (strings.Contains(err.Error(), "does not exist") || strings.Contains(err.Error(), "No such"))
|
2013-03-26 12:28:17 +00:00
|
|
|
}
|
|
|
|
|
2013-03-30 04:13:59 +00:00
|
|
|
// 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.
|
2013-03-18 07:15:35 +00:00
|
|
|
func (graph *Graph) Exists(id string) bool {
|
|
|
|
if _, err := graph.Get(id); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-03-30 04:13:59 +00:00
|
|
|
// Get returns the image with the given id, or an error if the image doesn't exist.
|
2013-04-01 05:11:55 +00:00
|
|
|
func (graph *Graph) Get(name string) (*Image, error) {
|
|
|
|
id, err := graph.idIndex.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-03-22 02:01:55 +00:00
|
|
|
// FIXME: return nil when the image doesn't exist, instead of an error
|
2013-03-18 07:15:35 +00:00
|
|
|
img, err := 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)
|
|
|
|
}
|
2013-03-21 07:21:32 +00:00
|
|
|
img.graph = graph
|
2013-05-08 17:42:29 +00:00
|
|
|
graph.lockSumMap.Lock()
|
|
|
|
defer graph.lockSumMap.Unlock()
|
|
|
|
if _, exists := graph.checksumLock[img.Id]; !exists {
|
|
|
|
graph.checksumLock[img.Id] = &sync.Mutex{}
|
|
|
|
}
|
2013-03-18 07:15:35 +00:00
|
|
|
return img, nil
|
|
|
|
}
|
|
|
|
|
2013-03-30 04:13:59 +00:00
|
|
|
// Create creates a new image and registers it in the graph.
|
2013-04-25 23:48:31 +00:00
|
|
|
func (graph *Graph) Create(layerData Archive, container *Container, comment, author string, config *Config) (*Image, error) {
|
2013-03-18 07:15:35 +00:00
|
|
|
img := &Image{
|
2013-04-05 01:38:43 +00:00
|
|
|
Id: GenerateId(),
|
|
|
|
Comment: comment,
|
|
|
|
Created: time.Now(),
|
|
|
|
DockerVersion: VERSION,
|
2013-04-18 02:58:17 +00:00
|
|
|
Author: author,
|
2013-04-25 23:48:31 +00:00
|
|
|
Config: config,
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
2013-03-22 04:13:27 +00:00
|
|
|
if container != nil {
|
|
|
|
img.Parent = container.Image
|
2013-03-23 21:48:16 +00:00
|
|
|
img.Container = container.Id
|
|
|
|
img.ContainerConfig = *container.Config
|
2013-03-22 04:13:27 +00:00
|
|
|
}
|
2013-05-09 02:08:11 +00:00
|
|
|
if err := graph.Register(layerData, true, img); err != nil {
|
2013-03-18 07:15:35 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-05-08 17:42:29 +00:00
|
|
|
go img.Checksum()
|
2013-03-18 07:15:35 +00:00
|
|
|
return img, nil
|
|
|
|
}
|
|
|
|
|
2013-03-30 04:13:59 +00:00
|
|
|
// Register imports a pre-existing image into the graph.
|
|
|
|
// FIXME: pass img as first argument
|
2013-05-09 02:08:11 +00:00
|
|
|
func (graph *Graph) Register(layerData Archive, store bool, img *Image) error {
|
2013-03-18 07:15:35 +00:00
|
|
|
if err := ValidateId(img.Id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// (This is a convenience to save time. Race conditions are taken care of by os.Rename)
|
|
|
|
if graph.Exists(img.Id) {
|
|
|
|
return fmt.Errorf("Image %s already exists", img.Id)
|
|
|
|
}
|
2013-04-17 23:35:22 +00:00
|
|
|
tmp, err := graph.Mktemp("")
|
2013-03-18 07:15:35 +00:00
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Mktemp failed: %s", err)
|
|
|
|
}
|
2013-05-09 02:08:11 +00:00
|
|
|
if err := StoreImage(img, layerData, tmp, store); err != nil {
|
2013-03-18 07:15:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Commit
|
|
|
|
if err := os.Rename(tmp, graph.imageRoot(img.Id)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
img.graph = graph
|
2013-04-01 05:11:55 +00:00
|
|
|
graph.idIndex.Add(img.Id)
|
2013-05-08 17:42:29 +00:00
|
|
|
graph.checksumLock[img.Id] = &sync.Mutex{}
|
2013-03-18 07:15:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-04-21 21:23:55 +00:00
|
|
|
// 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.
|
2013-04-21 22:29:26 +00:00
|
|
|
// 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) {
|
2013-04-21 21:23:55 +00:00
|
|
|
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
|
|
|
|
}
|
2013-04-21 22:29:26 +00:00
|
|
|
return NewTempArchive(ProgressReader(ioutil.NopCloser(archive), 0, output, "Buffering to disk %v/%v (%v)"), tmp.Root)
|
2013-04-21 21:23:55 +00:00
|
|
|
}
|
|
|
|
|
2013-03-30 04:13:59 +00:00
|
|
|
// Mktemp creates a temporary sub-directory inside the graph's filesystem.
|
2013-03-18 07:15:35 +00:00
|
|
|
func (graph *Graph) Mktemp(id string) (string, error) {
|
2013-04-04 05:14:28 +00:00
|
|
|
if id == "" {
|
|
|
|
id = GenerateId()
|
|
|
|
}
|
2013-04-21 21:23:55 +00:00
|
|
|
tmp, err := graph.tmp()
|
2013-03-18 07:15:35 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-04-21 21:23:55 +00:00
|
|
|
func (graph *Graph) tmp() (*Graph, error) {
|
|
|
|
return NewGraph(path.Join(graph.Root, ":tmp:"))
|
|
|
|
}
|
|
|
|
|
2013-03-30 07:22:24 +00:00
|
|
|
// Check if given error is "not empty".
|
|
|
|
// Note: this is the way golang does it internally with os.IsNotExists.
|
2013-03-26 10:33:47 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2013-03-30 04:13:59 +00:00
|
|
|
// Delete atomically removes an image from the graph.
|
2013-04-01 23:04:44 +00:00
|
|
|
func (graph *Graph) Delete(name string) error {
|
|
|
|
id, err := graph.idIndex.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-04-04 05:14:28 +00:00
|
|
|
tmp, err := graph.Mktemp("")
|
2013-03-18 07:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-04-01 05:11:55 +00:00
|
|
|
graph.idIndex.Delete(id)
|
2013-04-04 05:14:28 +00:00
|
|
|
err = os.Rename(graph.imageRoot(id), tmp)
|
2013-03-18 07:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-04-04 05:14:28 +00:00
|
|
|
return os.RemoveAll(tmp)
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
|
2013-03-30 07:22:24 +00:00
|
|
|
// Map returns a list of all images in the graph, addressable by ID.
|
2013-03-22 00:35:49 +00:00
|
|
|
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 {
|
|
|
|
images[image.Id] = image
|
|
|
|
}
|
|
|
|
return images, nil
|
|
|
|
}
|
|
|
|
|
2013-03-30 07:22:24 +00:00
|
|
|
// All returns a list of all images in the graph.
|
2013-03-18 07:15:35 +00:00
|
|
|
func (graph *Graph) All() ([]*Image, error) {
|
2013-03-24 00:03:30 +00:00
|
|
|
var images []*Image
|
|
|
|
err := graph.WalkAll(func(image *Image) {
|
|
|
|
images = append(images, image)
|
|
|
|
})
|
|
|
|
return images, err
|
|
|
|
}
|
|
|
|
|
2013-03-30 04:13:59 +00:00
|
|
|
// WalkAll iterates over each image in the graph, and passes it to a handler.
|
|
|
|
// The walking order is undetermined.
|
2013-03-24 00:03:30 +00:00
|
|
|
func (graph *Graph) WalkAll(handler func(*Image)) error {
|
2013-03-18 07:15:35 +00:00
|
|
|
files, err := ioutil.ReadDir(graph.Root)
|
|
|
|
if err != nil {
|
2013-03-24 00:03:30 +00:00
|
|
|
return err
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
for _, st := range files {
|
|
|
|
if img, err := graph.Get(st.Name()); err != nil {
|
|
|
|
// Skip image
|
|
|
|
continue
|
2013-03-24 00:03:30 +00:00
|
|
|
} else if handler != nil {
|
|
|
|
handler(img)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-30 04:13:59 +00:00
|
|
|
// 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.
|
2013-03-24 00:03:30 +00:00
|
|
|
func (graph *Graph) ByParent() (map[string][]*Image, error) {
|
|
|
|
byParent := make(map[string][]*Image)
|
|
|
|
err := graph.WalkAll(func(image *Image) {
|
2013-05-07 04:31:59 +00:00
|
|
|
parent, err := graph.Get(image.Parent)
|
2013-03-24 00:03:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-05-07 04:31:59 +00:00
|
|
|
if children, exists := byParent[parent.Id]; exists {
|
|
|
|
byParent[parent.Id] = []*Image{image}
|
2013-03-18 07:15:35 +00:00
|
|
|
} else {
|
2013-05-07 04:31:59 +00:00
|
|
|
byParent[parent.Id] = append(children, image)
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
2013-03-24 00:03:30 +00:00
|
|
|
})
|
|
|
|
return byParent, err
|
|
|
|
}
|
|
|
|
|
2013-03-30 04:13:59 +00:00
|
|
|
// 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.
|
2013-03-24 00:03:30 +00:00
|
|
|
func (graph *Graph) Heads() (map[string]*Image, error) {
|
|
|
|
heads := make(map[string]*Image)
|
|
|
|
byParent, err := graph.ByParent()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
2013-03-24 00:03:30 +00:00
|
|
|
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!
|
|
|
|
if _, exists := byParent[image.Id]; !exists {
|
|
|
|
heads[image.Id] = image
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return heads, err
|
2013-03-18 07:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (graph *Graph) imageRoot(id string) string {
|
|
|
|
return path.Join(graph.Root, id)
|
|
|
|
}
|
2013-05-09 05:37:33 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
checksumJson, err := json.Marshal(checksums)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(path.Join(graph.Root, "checksums"), checksumJson, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|