2014-03-07 20:36:47 -05:00
|
|
|
package image
|
2013-03-18 03:15:35 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2013-10-31 19:57:45 -04:00
|
|
|
"github.com/dotcloud/docker/archive"
|
2014-04-17 17:43:01 -04:00
|
|
|
"github.com/dotcloud/docker/daemon/graphdriver"
|
2014-02-11 23:04:39 -05:00
|
|
|
"github.com/dotcloud/docker/runconfig"
|
2013-05-14 18:37:35 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-03-18 03:15:35 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2013-07-22 18:40:33 -04:00
|
|
|
"strconv"
|
2013-03-18 03:15:35 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Image struct {
|
2014-02-11 23:04:39 -05:00
|
|
|
ID string `json:"id"`
|
|
|
|
Parent string `json:"parent,omitempty"`
|
|
|
|
Comment string `json:"comment,omitempty"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Container string `json:"container,omitempty"`
|
|
|
|
ContainerConfig runconfig.Config `json:"container_config,omitempty"`
|
|
|
|
DockerVersion string `json:"docker_version,omitempty"`
|
|
|
|
Author string `json:"author,omitempty"`
|
|
|
|
Config *runconfig.Config `json:"config,omitempty"`
|
|
|
|
Architecture string `json:"architecture,omitempty"`
|
|
|
|
OS string `json:"os,omitempty"`
|
2013-05-13 09:10:26 -04:00
|
|
|
Size int64
|
2014-03-07 20:36:47 -05:00
|
|
|
|
|
|
|
graph Graph
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadImage(root string) (*Image, error) {
|
|
|
|
// Load the json data
|
|
|
|
jsonData, err := ioutil.ReadFile(jsonPath(root))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-05-08 13:42:29 -04:00
|
|
|
img := &Image{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(jsonData, img); err != nil {
|
2013-03-18 03:15:35 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-07 20:36:47 -05:00
|
|
|
if err := utils.ValidateID(img.ID); err != nil {
|
2013-03-18 03:15:35 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-22 18:40:33 -04:00
|
|
|
|
|
|
|
if buf, err := ioutil.ReadFile(path.Join(root, "layersize")); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-05 17:03:23 -05:00
|
|
|
// If the layersize file does not exist then set the size to a negative number
|
|
|
|
// because a layer size of 0 (zero) is valid
|
|
|
|
img.Size = -1
|
2013-07-22 18:40:33 -04:00
|
|
|
} else {
|
2013-11-18 18:35:56 -05:00
|
|
|
size, err := strconv.Atoi(string(buf))
|
|
|
|
if err != nil {
|
2013-07-22 18:40:33 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-11-18 18:35:56 -05:00
|
|
|
img.Size = int64(size)
|
2013-07-22 18:40:33 -04:00
|
|
|
}
|
|
|
|
|
2013-05-08 13:42:29 -04:00
|
|
|
return img, nil
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
|
2014-02-14 06:41:46 -05:00
|
|
|
func StoreImage(img *Image, jsonData []byte, layerData archive.ArchiveReader, root, layer string) error {
|
2013-03-18 03:15:35 -04:00
|
|
|
// Store the layer
|
2013-11-20 17:51:04 -05:00
|
|
|
var (
|
|
|
|
size int64
|
|
|
|
err error
|
2014-03-07 20:36:47 -05:00
|
|
|
driver = img.graph.Driver()
|
2013-11-20 17:51:04 -05:00
|
|
|
)
|
2013-07-25 11:45:15 -04:00
|
|
|
if err := os.MkdirAll(layer, 0755); err != nil {
|
2013-03-18 03:15:35 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-05-08 22:08:11 -04:00
|
|
|
|
2013-06-22 15:29:42 -04:00
|
|
|
// If layerData is not nil, unpack it into the new layer
|
|
|
|
if layerData != nil {
|
2013-11-20 17:51:04 -05:00
|
|
|
if differ, ok := driver.(graphdriver.Differ); ok {
|
2013-11-11 20:17:38 -05:00
|
|
|
if err := differ.ApplyDiff(img.ID, layerData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-20 17:51:04 -05:00
|
|
|
|
|
|
|
if size, err = differ.DiffSize(img.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-11 20:17:38 -05:00
|
|
|
} else {
|
2013-11-25 22:01:13 -05:00
|
|
|
start := time.Now().UTC()
|
2013-11-11 20:17:38 -05:00
|
|
|
utils.Debugf("Start untar layer")
|
|
|
|
if err := archive.ApplyLayer(layer, layerData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-25 22:01:13 -05:00
|
|
|
utils.Debugf("Untar time: %vs", time.Now().UTC().Sub(start).Seconds())
|
2013-11-20 17:51:04 -05:00
|
|
|
|
|
|
|
if img.Parent == "" {
|
|
|
|
if size, err = utils.TreeSize(layer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-17 19:47:27 -04:00
|
|
|
parent, err := driver.Get(img.Parent, "")
|
2013-11-20 17:51:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-05 16:18:02 -05:00
|
|
|
defer driver.Put(img.Parent)
|
2013-11-20 17:51:04 -05:00
|
|
|
changes, err := archive.ChangesDirs(layer, parent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-05 17:03:23 -05:00
|
|
|
size = archive.ChangesSize(layer, changes)
|
2013-11-20 17:51:04 -05:00
|
|
|
}
|
2013-06-22 15:29:42 -04:00
|
|
|
}
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
2013-05-13 09:10:26 -04:00
|
|
|
|
2013-11-07 15:34:01 -05:00
|
|
|
img.Size = size
|
|
|
|
if err := img.SaveSize(root); err != nil {
|
|
|
|
return err
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
2013-07-22 18:40:33 -04:00
|
|
|
|
2013-12-05 17:03:23 -05:00
|
|
|
// If raw json is provided, then use it
|
|
|
|
if jsonData != nil {
|
|
|
|
if err := ioutil.WriteFile(jsonPath(root), jsonData, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if jsonData, err = json.Marshal(img); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(jsonPath(root), jsonData, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-03-18 03:15:35 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-07 20:36:47 -05:00
|
|
|
func (img *Image) SetGraph(graph Graph) {
|
|
|
|
img.graph = graph
|
|
|
|
}
|
|
|
|
|
2013-11-07 15:34:01 -05:00
|
|
|
// SaveSize stores the current `size` value of `img` in the directory `root`.
|
|
|
|
func (img *Image) SaveSize(root string) error {
|
|
|
|
if err := ioutil.WriteFile(path.Join(root, "layersize"), []byte(strconv.Itoa(int(img.Size))), 0600); err != nil {
|
|
|
|
return fmt.Errorf("Error storing image size in %s/layersize: %s", root, err)
|
|
|
|
}
|
|
|
|
return nil
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func jsonPath(root string) string {
|
|
|
|
return path.Join(root, "json")
|
|
|
|
}
|
|
|
|
|
2014-06-17 09:05:29 -04:00
|
|
|
func (img *Image) RawJson() ([]byte, error) {
|
|
|
|
root, err := img.root()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to get root for image %s: %s", img.ID, err)
|
|
|
|
}
|
|
|
|
fh, err := os.Open(jsonPath(root))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to open json for image %s: %s", img.ID, err)
|
|
|
|
}
|
|
|
|
buf, err := ioutil.ReadAll(fh)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to read json for image %s: %s", img.ID, err)
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2013-04-21 17:23:55 -04:00
|
|
|
// TarLayer returns a tar archive of the image's filesystem layer.
|
2013-12-05 16:18:02 -05:00
|
|
|
func (img *Image) TarLayer() (arch archive.Archive, err error) {
|
2013-11-19 17:57:53 -05:00
|
|
|
if img.graph == nil {
|
|
|
|
return nil, fmt.Errorf("Can't load storage driver for unregistered image %s", img.ID)
|
2013-03-21 01:13:28 -04:00
|
|
|
}
|
2014-03-07 20:36:47 -05:00
|
|
|
driver := img.graph.Driver()
|
2013-11-20 18:37:26 -05:00
|
|
|
if differ, ok := driver.(graphdriver.Differ); ok {
|
|
|
|
return differ.Diff(img.ID)
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
|
2014-04-17 19:47:27 -04:00
|
|
|
imgFs, err := driver.Get(img.ID, "")
|
2013-03-21 01:12:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-05 16:18:02 -05:00
|
|
|
|
|
|
|
defer func() {
|
2014-02-03 18:36:06 -05:00
|
|
|
if err != nil {
|
2013-12-05 16:18:02 -05:00
|
|
|
driver.Put(img.ID)
|
2013-11-20 18:37:26 -05:00
|
|
|
}
|
2013-12-05 16:18:02 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
if img.Parent == "" {
|
|
|
|
archive, err := archive.Tar(imgFs, archive.Uncompressed)
|
2013-11-20 18:37:26 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-02-14 06:41:46 -05:00
|
|
|
return utils.NewReadCloserWrapper(archive, func() error {
|
|
|
|
err := archive.Close()
|
|
|
|
driver.Put(img.ID)
|
|
|
|
return err
|
|
|
|
}), nil
|
2013-12-05 16:18:02 -05:00
|
|
|
}
|
|
|
|
|
2014-04-17 19:47:27 -04:00
|
|
|
parentFs, err := driver.Get(img.Parent, "")
|
2013-12-05 16:18:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer driver.Put(img.Parent)
|
|
|
|
changes, err := archive.ChangesDirs(imgFs, parentFs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
archive, err := archive.ExportChanges(imgFs, changes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-11-20 18:37:26 -05:00
|
|
|
}
|
2014-02-14 06:41:46 -05:00
|
|
|
return utils.NewReadCloserWrapper(archive, func() error {
|
|
|
|
err := archive.Close()
|
|
|
|
driver.Put(img.ID)
|
|
|
|
return err
|
|
|
|
}), nil
|
2013-03-21 01:12:38 -04:00
|
|
|
}
|
|
|
|
|
2013-03-18 03:15:35 -04:00
|
|
|
// Image includes convenience proxy functions to its graph
|
|
|
|
// These functions will return an error if the image is not registered
|
|
|
|
// (ie. if image.graph == nil)
|
|
|
|
func (img *Image) History() ([]*Image, error) {
|
|
|
|
var parents []*Image
|
|
|
|
if err := img.WalkHistory(
|
2013-03-21 13:10:14 -04:00
|
|
|
func(img *Image) error {
|
2013-03-18 03:15:35 -04:00
|
|
|
parents = append(parents, img)
|
2013-03-21 13:10:14 -04:00
|
|
|
return nil
|
2013-03-18 03:15:35 -04:00
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return parents, nil
|
|
|
|
}
|
|
|
|
|
2013-03-21 13:10:14 -04:00
|
|
|
func (img *Image) WalkHistory(handler func(*Image) error) (err error) {
|
2013-03-18 03:15:35 -04:00
|
|
|
currentImg := img
|
|
|
|
for currentImg != nil {
|
|
|
|
if handler != nil {
|
2013-03-21 13:10:14 -04:00
|
|
|
if err := handler(currentImg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
currentImg, err = currentImg.GetParent()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error while getting parent image: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (img *Image) GetParent() (*Image, error) {
|
|
|
|
if img.Parent == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if img.graph == nil {
|
|
|
|
return nil, fmt.Errorf("Can't lookup parent of unregistered image")
|
|
|
|
}
|
|
|
|
return img.graph.Get(img.Parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (img *Image) root() (string, error) {
|
|
|
|
if img.graph == nil {
|
|
|
|
return "", fmt.Errorf("Can't lookup root of unregistered image")
|
|
|
|
}
|
2014-03-07 20:36:47 -05:00
|
|
|
return img.graph.ImageRoot(img.ID), nil
|
2013-03-18 03:15:35 -04:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:36:47 -05:00
|
|
|
func (img *Image) GetParentsSize(size int64) int64 {
|
2013-05-13 09:10:26 -04:00
|
|
|
parentImage, err := img.GetParent()
|
|
|
|
if err != nil || parentImage == nil {
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
size += parentImage.Size
|
2014-03-07 20:36:47 -05:00
|
|
|
return parentImage.GetParentsSize(size)
|
2013-05-13 09:10:26 -04:00
|
|
|
}
|
2013-05-22 09:41:29 -04:00
|
|
|
|
2013-11-19 03:51:16 -05:00
|
|
|
// Depth returns the number of parents for a
|
|
|
|
// current image
|
|
|
|
func (img *Image) Depth() (int, error) {
|
|
|
|
var (
|
|
|
|
count = 0
|
|
|
|
parent = img
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for parent != nil {
|
|
|
|
count++
|
|
|
|
parent, err = parent.GetParent()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:41:39 -04:00
|
|
|
// Build an Image object from raw json data
|
2013-06-04 14:00:22 -04:00
|
|
|
func NewImgJSON(src []byte) (*Image, error) {
|
2013-05-14 21:41:39 -04:00
|
|
|
ret := &Image{}
|
|
|
|
|
2013-10-25 20:50:40 -04:00
|
|
|
utils.Debugf("Json string: {%s}", src)
|
2013-05-14 21:41:39 -04:00
|
|
|
// FIXME: Is there a cleaner way to "purify" the input json?
|
|
|
|
if err := json.Unmarshal(src, ret); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|