2013-01-28 03:42:42 +04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2013-02-27 05:26:46 +04:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"github.com/dotcloud/docker/future"
|
2013-01-28 03:42:42 +04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-02-27 05:26:46 +04:00
|
|
|
"os"
|
2013-01-28 03:42:42 +04:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2013-03-06 10:39:43 +04:00
|
|
|
"regexp"
|
2013-01-28 03:42:42 +04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2013-02-27 05:26:46 +04:00
|
|
|
"time"
|
2013-01-28 03:42:42 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Store struct {
|
|
|
|
*Index
|
2013-02-27 05:26:46 +04:00
|
|
|
Root string
|
|
|
|
Layers *LayerStore
|
2013-01-28 03:42:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(root string) (*Store, error) {
|
|
|
|
abspath, err := filepath.Abs(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-02-20 01:03:58 +04:00
|
|
|
if err := os.MkdirAll(abspath, 0700); err != nil && !os.IsExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-01-28 03:42:42 +04:00
|
|
|
layers, err := NewLayerStore(path.Join(root, "layers"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := layers.Init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Store{
|
2013-02-27 05:26:46 +04:00
|
|
|
Root: abspath,
|
|
|
|
Index: NewIndex(path.Join(root, "index.json")),
|
2013-01-28 03:42:42 +04:00
|
|
|
Layers: layers,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2013-02-23 00:28:25 +04:00
|
|
|
// Import creates a new image from the contents of `archive` and registers it in the store as `name`.
|
|
|
|
// If `parent` is not nil, it will registered as the parent of the new image.
|
|
|
|
func (store *Store) Import(name string, archive io.Reader, parent *Image) (*Image, error) {
|
|
|
|
layer, err := store.Layers.AddLayer(archive)
|
2013-01-28 03:42:42 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
layers := []string{layer}
|
|
|
|
if parent != nil {
|
2013-01-29 11:15:02 +04:00
|
|
|
layers = append(layers, parent.Layers...)
|
2013-01-28 03:42:42 +04:00
|
|
|
}
|
|
|
|
var parentId string
|
|
|
|
if parent != nil {
|
|
|
|
parentId = parent.Id
|
|
|
|
}
|
|
|
|
return store.Create(name, parentId, layers...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *Store) Create(name string, source string, layers ...string) (*Image, error) {
|
|
|
|
image, err := NewImage(name, layers, source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := store.Index.Add(name, image); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return image, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index
|
|
|
|
|
|
|
|
type Index struct {
|
2013-02-27 05:26:46 +04:00
|
|
|
Path string
|
|
|
|
ByName map[string]*History
|
|
|
|
ById map[string]*Image
|
2013-01-28 03:42:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIndex(path string) *Index {
|
|
|
|
return &Index{
|
2013-02-27 05:26:46 +04:00
|
|
|
Path: path,
|
2013-01-28 03:42:42 +04:00
|
|
|
ByName: make(map[string]*History),
|
2013-02-27 05:26:46 +04:00
|
|
|
ById: make(map[string]*Image),
|
2013-01-28 03:42:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *Index) Exists(id string) bool {
|
|
|
|
_, exists := index.ById[id]
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *Index) Find(idOrName string) *Image {
|
|
|
|
// Load
|
|
|
|
if err := index.load(); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Lookup by ID
|
|
|
|
if image, exists := index.ById[idOrName]; exists {
|
|
|
|
return image
|
|
|
|
}
|
|
|
|
// Lookup by name
|
|
|
|
if history, exists := index.ByName[idOrName]; exists && history.Len() > 0 {
|
2013-01-30 00:15:39 +04:00
|
|
|
return (*history)[0]
|
2013-01-28 03:42:42 +04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *Index) Add(name string, image *Image) error {
|
|
|
|
// Load
|
|
|
|
if err := index.load(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, exists := index.ByName[name]; !exists {
|
|
|
|
index.ByName[name] = new(History)
|
|
|
|
} else {
|
|
|
|
// If this image is already the latest version, don't add it.
|
2013-01-30 00:15:39 +04:00
|
|
|
if (*index.ByName[name])[0].Id == image.Id {
|
2013-01-28 03:42:42 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
index.ByName[name].Add(image)
|
|
|
|
index.ById[image.Id] = image
|
|
|
|
// Save
|
|
|
|
if err := index.save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *Index) Copy(srcNameOrId, dstName string) (*Image, error) {
|
|
|
|
if srcNameOrId == "" || dstName == "" {
|
|
|
|
return nil, errors.New("Illegal image name")
|
|
|
|
}
|
|
|
|
// Load
|
|
|
|
if err := index.load(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
src := index.Find(srcNameOrId)
|
|
|
|
if src == nil {
|
|
|
|
return nil, errors.New("No such image: " + srcNameOrId)
|
|
|
|
}
|
|
|
|
dst, err := NewImage(dstName, src.Layers, src.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := index.Add(dstName, dst); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Save
|
|
|
|
if err := index.save(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return dst, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *Index) Rename(oldName, newName string) error {
|
|
|
|
// Load
|
|
|
|
if err := index.load(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, exists := index.ByName[oldName]; !exists {
|
|
|
|
return errors.New("Can't rename " + oldName + ": no such image.")
|
|
|
|
}
|
|
|
|
if _, exists := index.ByName[newName]; exists {
|
|
|
|
return errors.New("Can't rename to " + newName + ": name is already in use.")
|
|
|
|
}
|
|
|
|
index.ByName[newName] = index.ByName[oldName]
|
|
|
|
delete(index.ByName, oldName)
|
|
|
|
// Change the ID of all images, since they include the name
|
2013-01-30 00:15:39 +04:00
|
|
|
for _, image := range *index.ByName[newName] {
|
2013-01-28 03:42:42 +04:00
|
|
|
if id, err := generateImageId(newName, image.Layers); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
oldId := image.Id
|
|
|
|
image.Id = id
|
|
|
|
index.ById[id] = image
|
|
|
|
delete(index.ById, oldId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Save
|
|
|
|
if err := index.save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-02-01 12:08:45 +04:00
|
|
|
// Delete deletes all images with the name `name`
|
|
|
|
func (index *Index) Delete(name string) error {
|
|
|
|
// Load
|
|
|
|
if err := index.load(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, exists := index.ByName[name]; !exists {
|
|
|
|
return errors.New("No such image: " + name)
|
|
|
|
}
|
|
|
|
// Remove from index lookup
|
|
|
|
for _, image := range *index.ByName[name] {
|
|
|
|
delete(index.ById, image.Id)
|
|
|
|
}
|
|
|
|
// Remove from name lookup
|
|
|
|
delete(index.ByName, name)
|
|
|
|
// Save
|
|
|
|
if err := index.save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-06 10:39:43 +04:00
|
|
|
// DeleteMatch deletes all images whose name matches `pattern`
|
|
|
|
func (index *Index) DeleteMatch(pattern string) error {
|
|
|
|
// Load
|
|
|
|
if err := index.load(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for name, history := range index.ByName {
|
|
|
|
if match, err := regexp.MatchString(pattern, name); err != nil {
|
|
|
|
return err
|
|
|
|
} else if match {
|
|
|
|
// Remove from index lookup
|
|
|
|
for _, image := range *history {
|
|
|
|
delete(index.ById, image.Id)
|
|
|
|
}
|
|
|
|
// Remove from name lookup
|
|
|
|
delete(index.ByName, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Save
|
|
|
|
if err := index.save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-28 03:42:42 +04:00
|
|
|
func (index *Index) Names() []string {
|
|
|
|
if err := index.load(); err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
2013-02-27 05:26:46 +04:00
|
|
|
var names []string
|
2013-01-28 03:42:42 +04:00
|
|
|
for name := range index.ByName {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *Index) load() error {
|
|
|
|
jsonData, err := ioutil.ReadFile(index.Path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
path := index.Path
|
|
|
|
if err := json.Unmarshal(jsonData, index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
index.Path = path
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (index *Index) save() error {
|
|
|
|
jsonData, err := json.Marshal(index)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(index.Path, jsonData, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// History wraps an array of images so they can be sorted by date (most recent first)
|
|
|
|
|
2013-01-30 00:15:39 +04:00
|
|
|
type History []*Image
|
2013-01-28 03:42:42 +04:00
|
|
|
|
|
|
|
func (history *History) Len() int {
|
|
|
|
return len(*history)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (history *History) Less(i, j int) bool {
|
2013-01-30 00:15:39 +04:00
|
|
|
images := *history
|
|
|
|
return images[j].Created.Before(images[i].Created)
|
2013-01-28 03:42:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (history *History) Swap(i, j int) {
|
2013-01-30 00:15:39 +04:00
|
|
|
images := *history
|
|
|
|
tmp := images[i]
|
|
|
|
images[i] = images[j]
|
|
|
|
images[j] = tmp
|
2013-01-28 03:42:42 +04:00
|
|
|
}
|
|
|
|
|
2013-01-30 00:15:39 +04:00
|
|
|
func (history *History) Add(image *Image) {
|
|
|
|
*history = append(*history, image)
|
2013-01-28 03:42:42 +04:00
|
|
|
sort.Sort(history)
|
|
|
|
}
|
|
|
|
|
2013-01-30 00:15:39 +04:00
|
|
|
func (history *History) Del(id string) {
|
|
|
|
for idx, image := range *history {
|
|
|
|
if image.Id == id {
|
2013-02-27 05:26:46 +04:00
|
|
|
*history = append((*history)[:idx], (*history)[idx+1:]...)
|
2013-01-30 00:15:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-28 03:42:42 +04:00
|
|
|
type Image struct {
|
2013-02-27 05:26:46 +04:00
|
|
|
Id string // Globally unique identifier
|
|
|
|
Layers []string // Absolute paths
|
|
|
|
Created time.Time
|
|
|
|
Parent string
|
2013-01-28 03:42:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (image *Image) IdParts() (string, string) {
|
|
|
|
if len(image.Id) < 8 {
|
|
|
|
return "", image.Id
|
|
|
|
}
|
2013-02-27 05:26:46 +04:00
|
|
|
hash := image.Id[len(image.Id)-8 : len(image.Id)]
|
2013-01-28 03:42:42 +04:00
|
|
|
name := image.Id[:len(image.Id)-9]
|
|
|
|
return name, hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (image *Image) IdIsFinal() bool {
|
|
|
|
return len(image.Layers) == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateImageId(name string, layers []string) (string, error) {
|
|
|
|
if len(layers) == 0 {
|
|
|
|
return "", errors.New("No layers provided.")
|
|
|
|
}
|
|
|
|
var hash string
|
|
|
|
if len(layers) == 1 {
|
|
|
|
hash = path.Base(layers[0])
|
|
|
|
} else {
|
|
|
|
var ids string
|
|
|
|
for _, layer := range layers {
|
|
|
|
ids += path.Base(layer)
|
|
|
|
}
|
2013-02-27 05:26:46 +04:00
|
|
|
if h, err := future.ComputeId(strings.NewReader(ids)); err != nil {
|
2013-01-28 03:42:42 +04:00
|
|
|
return "", err
|
|
|
|
} else {
|
|
|
|
hash = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return name + ":" + hash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewImage(name string, layers []string, parent string) (*Image, error) {
|
|
|
|
id, err := generateImageId(name, layers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Image{
|
2013-02-27 05:26:46 +04:00
|
|
|
Id: id,
|
|
|
|
Layers: layers,
|
|
|
|
Created: time.Now(),
|
|
|
|
Parent: parent,
|
2013-01-28 03:42:42 +04:00
|
|
|
}, nil
|
|
|
|
}
|