2014-03-07 20:36:47 -05:00
|
|
|
package image
|
2013-03-18 03:15:35 -04:00
|
|
|
|
|
|
|
import (
|
2015-07-20 13:57:15 -04:00
|
|
|
"encoding/json"
|
2015-11-18 17:18:07 -05:00
|
|
|
"errors"
|
|
|
|
"io"
|
2015-07-20 13:57:15 -04:00
|
|
|
"time"
|
|
|
|
|
2015-08-26 17:58:56 -04:00
|
|
|
"github.com/docker/distribution/digest"
|
2015-12-18 13:36:17 -05:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2013-03-18 03:15:35 -04:00
|
|
|
)
|
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
// ID is the content-addressable ID of an image.
|
|
|
|
type ID digest.Digest
|
2015-05-13 14:42:45 -04:00
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
func (id ID) String() string {
|
|
|
|
return digest.Digest(id).String()
|
2015-08-26 17:58:56 -04:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
// V1Image stores the V1 image configuration.
|
|
|
|
type V1Image struct {
|
2015-07-21 01:49:27 -04:00
|
|
|
// ID a unique 64 character identifier of the image
|
2015-08-26 17:58:56 -04:00
|
|
|
ID string `json:"id,omitempty"`
|
2015-07-21 01:49:27 -04:00
|
|
|
// Parent id of the image
|
|
|
|
Parent string `json:"parent,omitempty"`
|
|
|
|
// Comment user added comment
|
|
|
|
Comment string `json:"comment,omitempty"`
|
|
|
|
// Created timestamp when image was created
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
// Container is the id of the container used to commit
|
|
|
|
Container string `json:"container,omitempty"`
|
|
|
|
// ContainerConfig is the configuration of the container that is committed into the image
|
2015-12-18 13:36:17 -05:00
|
|
|
ContainerConfig container.Config `json:"container_config,omitempty"`
|
2015-07-21 01:49:27 -04:00
|
|
|
// DockerVersion specifies version on which image is built
|
|
|
|
DockerVersion string `json:"docker_version,omitempty"`
|
|
|
|
// Author of the image
|
|
|
|
Author string `json:"author,omitempty"`
|
|
|
|
// Config is the configuration of the container received from the client
|
2015-12-18 13:36:17 -05:00
|
|
|
Config *container.Config `json:"config,omitempty"`
|
2015-07-21 01:49:27 -04:00
|
|
|
// Architecture is the hardware that the image is build and runs on
|
|
|
|
Architecture string `json:"architecture,omitempty"`
|
|
|
|
// OS is the operating system used to build and run the image
|
|
|
|
OS string `json:"os,omitempty"`
|
|
|
|
// Size is the total size of the image including all layers it is composed of
|
2015-11-18 17:18:07 -05:00
|
|
|
Size int64 `json:",omitempty"`
|
2015-07-20 13:57:15 -04:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
// Image stores the image configuration
|
|
|
|
type Image struct {
|
|
|
|
V1Image
|
|
|
|
Parent ID `json:"parent,omitempty"`
|
|
|
|
RootFS *RootFS `json:"rootfs,omitempty"`
|
|
|
|
History []History `json:"history,omitempty"`
|
2015-07-20 13:57:15 -04:00
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
// rawJSON caches the immutable JSON associated with this image.
|
|
|
|
rawJSON []byte
|
|
|
|
|
|
|
|
// computedID is the ID computed from the hash of the image config.
|
|
|
|
// Not to be confused with the legacy V1 ID in V1Image.
|
|
|
|
computedID ID
|
2015-07-20 13:57:15 -04:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
// RawJSON returns the immutable JSON associated with the image.
|
|
|
|
func (img *Image) RawJSON() []byte {
|
|
|
|
return img.rawJSON
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the image's content-addressable ID.
|
|
|
|
func (img *Image) ID() ID {
|
|
|
|
return img.computedID
|
2015-03-29 17:17:23 -04:00
|
|
|
}
|
2015-08-26 17:58:56 -04:00
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
// MarshalJSON serializes the image to JSON. It sorts the top-level keys so
|
|
|
|
// that JSON that's been manipulated by a push/pull cycle with a legacy
|
|
|
|
// registry won't end up with a different key order.
|
|
|
|
func (img *Image) MarshalJSON() ([]byte, error) {
|
|
|
|
type MarshalImage Image
|
2015-08-26 17:58:56 -04:00
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
pass1, err := json.Marshal(MarshalImage(*img))
|
2015-08-26 17:58:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var c map[string]*json.RawMessage
|
2015-11-18 17:18:07 -05:00
|
|
|
if err := json.Unmarshal(pass1, &c); err != nil {
|
2015-08-26 17:58:56 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.Marshal(c)
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
// History stores build commands that were used to create an image
|
|
|
|
type History struct {
|
|
|
|
// Created timestamp for build point
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
// Author of the build point
|
|
|
|
Author string `json:"author,omitempty"`
|
|
|
|
// CreatedBy keeps the Dockerfile command used while building image.
|
|
|
|
CreatedBy string `json:"created_by,omitempty"`
|
2015-12-13 11:00:39 -05:00
|
|
|
// Comment is custom message set by the user when creating the image.
|
2015-11-18 17:18:07 -05:00
|
|
|
Comment string `json:"comment,omitempty"`
|
|
|
|
// EmptyLayer is set to true if this history item did not generate a
|
|
|
|
// layer. Otherwise, the history item is associated with the next
|
|
|
|
// layer in the RootFS section.
|
|
|
|
EmptyLayer bool `json:"empty_layer,omitempty"`
|
2015-08-26 17:58:56 -04:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
// Exporter provides interface for exporting and importing images
|
|
|
|
type Exporter interface {
|
|
|
|
Load(io.ReadCloser, io.Writer) error
|
|
|
|
// TODO: Load(net.Context, io.ReadCloser, <- chan StatusMessage) error
|
|
|
|
Save([]string, io.Writer) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFromJSON creates an Image configuration from json.
|
|
|
|
func NewFromJSON(src []byte) (*Image, error) {
|
|
|
|
img := &Image{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(src, img); err != nil {
|
|
|
|
return nil, err
|
2015-08-26 17:58:56 -04:00
|
|
|
}
|
2015-11-18 17:18:07 -05:00
|
|
|
if img.RootFS == nil {
|
|
|
|
return nil, errors.New("Invalid image JSON, no RootFS key.")
|
|
|
|
}
|
|
|
|
|
|
|
|
img.rawJSON = src
|
|
|
|
|
|
|
|
return img, nil
|
2015-08-26 17:58:56 -04:00
|
|
|
}
|