2014-04-28 02:59:46 -04:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
2014-08-06 16:09:29 -04:00
|
|
|
"fmt"
|
2014-05-20 15:36:15 -04:00
|
|
|
"io"
|
|
|
|
|
2014-10-24 18:11:48 -04:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
"github.com/docker/docker/image"
|
2014-04-28 02:59:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *TagStore) Install(eng *engine.Engine) error {
|
2014-08-06 16:09:29 -04:00
|
|
|
for name, handler := range map[string]engine.Handler{
|
|
|
|
"image_set": s.CmdSet,
|
2015-01-23 17:15:35 -05:00
|
|
|
"tag": s.CmdTag,
|
2014-08-06 16:09:29 -04:00
|
|
|
"image_get": s.CmdGet,
|
|
|
|
"image_inspect": s.CmdLookup,
|
|
|
|
"image_tarlayer": s.CmdTarLayer,
|
|
|
|
"image_export": s.CmdImageExport,
|
|
|
|
"history": s.CmdHistory,
|
|
|
|
"images": s.CmdImages,
|
|
|
|
"viz": s.CmdViz,
|
|
|
|
"load": s.CmdLoad,
|
|
|
|
"import": s.CmdImport,
|
2014-08-08 02:58:58 -04:00
|
|
|
"pull": s.CmdPull,
|
|
|
|
"push": s.CmdPush,
|
2014-08-06 16:09:29 -04:00
|
|
|
} {
|
|
|
|
if err := eng.Register(name, handler); err != nil {
|
|
|
|
return fmt.Errorf("Could not register %q: %v", name, err)
|
|
|
|
}
|
|
|
|
}
|
2014-04-28 02:59:46 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CmdSet stores a new image in the graph.
|
|
|
|
// Images are stored in the graph using 4 elements:
|
|
|
|
// - A user-defined ID
|
|
|
|
// - A collection of metadata describing the image
|
|
|
|
// - A directory tree stored as a tar archive (also called the "layer")
|
|
|
|
// - A reference to a "parent" ID on top of which the layer should be applied
|
|
|
|
//
|
|
|
|
// NOTE: even though the parent ID is only useful in relation to the layer and how
|
|
|
|
// to apply it (ie you could represent the full directory tree as 'parent_layer + layer',
|
|
|
|
// it is treated as a top-level property of the image. This is an artifact of early
|
|
|
|
// design and should probably be cleaned up in the future to simplify the design.
|
|
|
|
//
|
|
|
|
// Syntax: image_set ID
|
|
|
|
// Input:
|
|
|
|
// - Layer content must be streamed in tar format on stdin. An empty input is
|
|
|
|
// valid and represents a nil layer.
|
|
|
|
//
|
|
|
|
// - Image metadata must be passed in the command environment.
|
|
|
|
// 'json': a json-encoded object with all image metadata.
|
|
|
|
// It will be stored as-is, without any encoding/decoding artifacts.
|
|
|
|
// That is a requirement of the current registry client implementation,
|
|
|
|
// because a re-encoded json might invalidate the image checksum at
|
|
|
|
// the next upload, even with functionaly identical content.
|
2015-03-25 03:44:12 -04:00
|
|
|
func (s *TagStore) CmdSet(job *engine.Job) error {
|
2014-04-28 02:59:46 -04:00
|
|
|
if len(job.Args) != 1 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("usage: %s NAME", job.Name)
|
2014-04-28 02:59:46 -04:00
|
|
|
}
|
|
|
|
var (
|
|
|
|
imgJSON = []byte(job.Getenv("json"))
|
|
|
|
layer = job.Stdin
|
|
|
|
)
|
|
|
|
if len(imgJSON) == 0 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("mandatory key 'json' is not set")
|
2014-04-28 02:59:46 -04:00
|
|
|
}
|
|
|
|
// We have to pass an *image.Image object, even though it will be completely
|
|
|
|
// ignored in favor of the redundant json data.
|
|
|
|
// FIXME: the current prototype of Graph.Register is stupid and redundant.
|
|
|
|
img, err := image.NewImgJSON(imgJSON)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-04-28 02:59:46 -04:00
|
|
|
}
|
2014-10-27 14:00:29 -04:00
|
|
|
if err := s.graph.Register(img, layer); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-04-28 02:59:46 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-04-28 02:59:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CmdGet returns information about an image.
|
|
|
|
// If the image doesn't exist, an empty object is returned, to allow
|
|
|
|
// checking for an image's existence.
|
2015-03-25 03:44:12 -04:00
|
|
|
func (s *TagStore) CmdGet(job *engine.Job) error {
|
2014-04-28 02:59:46 -04:00
|
|
|
if len(job.Args) != 1 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("usage: %s NAME", job.Name)
|
2014-04-28 02:59:46 -04:00
|
|
|
}
|
|
|
|
name := job.Args[0]
|
|
|
|
res := &engine.Env{}
|
|
|
|
img, err := s.LookupImage(name)
|
|
|
|
// Note: if the image doesn't exist, LookupImage returns
|
|
|
|
// nil, nil.
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-04-28 02:59:46 -04:00
|
|
|
}
|
|
|
|
if img != nil {
|
|
|
|
// We don't directly expose all fields of the Image objects,
|
|
|
|
// to maintain a clean public API which we can maintain over
|
|
|
|
// time even if the underlying structure changes.
|
|
|
|
// We should have done this with the Image object to begin with...
|
|
|
|
// but we didn't, so now we're doing it here.
|
|
|
|
//
|
|
|
|
// Fields that we're probably better off not including:
|
|
|
|
// - Config/ContainerConfig. Those structs have the same sprawl problem,
|
|
|
|
// so we shouldn't include them wholesale either.
|
|
|
|
// - Comment: initially created to fulfill the "every image is a git commit"
|
|
|
|
// metaphor, in practice people either ignore it or use it as a
|
|
|
|
// generic description field which it isn't. On deprecation shortlist.
|
2014-05-30 21:13:37 -04:00
|
|
|
res.SetAuto("Created", img.Created)
|
2014-12-11 07:56:21 -05:00
|
|
|
res.SetJson("Author", img.Author)
|
2014-05-30 21:13:37 -04:00
|
|
|
res.Set("Os", img.OS)
|
|
|
|
res.Set("Architecture", img.Architecture)
|
|
|
|
res.Set("DockerVersion", img.DockerVersion)
|
2014-12-11 07:56:21 -05:00
|
|
|
res.SetJson("Id", img.ID)
|
|
|
|
res.SetJson("Parent", img.Parent)
|
2014-04-28 02:59:46 -04:00
|
|
|
}
|
|
|
|
res.WriteTo(job.Stdout)
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-04-28 02:59:46 -04:00
|
|
|
}
|
2014-05-20 15:36:15 -04:00
|
|
|
|
|
|
|
// CmdLookup return an image encoded in JSON
|
2015-03-25 03:44:12 -04:00
|
|
|
func (s *TagStore) CmdLookup(job *engine.Job) error {
|
2014-05-20 15:36:15 -04:00
|
|
|
if len(job.Args) != 1 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("usage: %s NAME", job.Name)
|
2014-05-20 15:36:15 -04:00
|
|
|
}
|
|
|
|
name := job.Args[0]
|
|
|
|
if image, err := s.LookupImage(name); err == nil && image != nil {
|
2014-06-16 20:06:21 -04:00
|
|
|
if job.GetenvBool("raw") {
|
2014-06-17 09:05:29 -04:00
|
|
|
b, err := image.RawJson()
|
2014-05-30 21:13:37 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-05-30 21:13:37 -04:00
|
|
|
}
|
|
|
|
job.Stdout.Write(b)
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-05-30 21:13:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
out := &engine.Env{}
|
2014-12-11 07:56:21 -05:00
|
|
|
out.SetJson("Id", image.ID)
|
|
|
|
out.SetJson("Parent", image.Parent)
|
|
|
|
out.SetJson("Comment", image.Comment)
|
2014-05-30 21:13:37 -04:00
|
|
|
out.SetAuto("Created", image.Created)
|
2014-12-11 07:56:21 -05:00
|
|
|
out.SetJson("Container", image.Container)
|
2014-05-30 21:13:37 -04:00
|
|
|
out.SetJson("ContainerConfig", image.ContainerConfig)
|
|
|
|
out.Set("DockerVersion", image.DockerVersion)
|
2014-12-11 07:56:21 -05:00
|
|
|
out.SetJson("Author", image.Author)
|
2014-05-30 21:13:37 -04:00
|
|
|
out.SetJson("Config", image.Config)
|
|
|
|
out.Set("Architecture", image.Architecture)
|
|
|
|
out.Set("Os", image.OS)
|
|
|
|
out.SetInt64("Size", image.Size)
|
2014-09-22 20:42:32 -04:00
|
|
|
out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size)
|
2014-05-30 21:13:37 -04:00
|
|
|
if _, err = out.WriteTo(job.Stdout); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-05-20 15:36:15 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-05-20 15:36:15 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("No such image: %s", name)
|
2014-05-20 15:36:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CmdTarLayer return the tarLayer of the image
|
2015-03-25 03:44:12 -04:00
|
|
|
func (s *TagStore) CmdTarLayer(job *engine.Job) error {
|
2014-05-20 15:36:15 -04:00
|
|
|
if len(job.Args) != 1 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("usage: %s NAME", job.Name)
|
2014-05-20 15:36:15 -04:00
|
|
|
}
|
|
|
|
name := job.Args[0]
|
|
|
|
if image, err := s.LookupImage(name); err == nil && image != nil {
|
|
|
|
fs, err := image.TarLayer()
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-05-20 15:36:15 -04:00
|
|
|
}
|
|
|
|
defer fs.Close()
|
|
|
|
|
2014-08-29 07:21:28 -04:00
|
|
|
written, err := io.Copy(job.Stdout, fs)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-05-20 15:36:15 -04:00
|
|
|
}
|
2014-08-29 07:21:28 -04:00
|
|
|
log.Debugf("rendered layer for %s of [%d] size", image.ID, written)
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-05-20 15:36:15 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("No such image: %s", name)
|
2014-05-20 15:36:15 -04:00
|
|
|
}
|