github.com/samsalisbury/docker@v1.9.0/graph/service.go (about)

     1  package graph
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"runtime"
     7  	"time"
     8  
     9  	"github.com/Sirupsen/logrus"
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/utils"
    12  )
    13  
    14  // Lookup looks up an image by name in a TagStore and returns it as an
    15  // ImageInspect structure.
    16  func (s *TagStore) Lookup(name string) (*types.ImageInspect, error) {
    17  	image, err := s.LookupImage(name)
    18  	if err != nil || image == nil {
    19  		return nil, fmt.Errorf("No such image: %s", name)
    20  	}
    21  
    22  	var repoTags = make([]string, 0)
    23  	var repoDigests = make([]string, 0)
    24  
    25  	s.Lock()
    26  	for repoName, repository := range s.Repositories {
    27  		for ref, id := range repository {
    28  			if id == image.ID {
    29  				imgRef := utils.ImageReference(repoName, ref)
    30  				if utils.DigestReference(ref) {
    31  					repoDigests = append(repoDigests, imgRef)
    32  				} else {
    33  					repoTags = append(repoTags, imgRef)
    34  				}
    35  			}
    36  		}
    37  	}
    38  	s.Unlock()
    39  
    40  	imageInspect := &types.ImageInspect{
    41  		ID:              image.ID,
    42  		RepoTags:        repoTags,
    43  		RepoDigests:     repoDigests,
    44  		Parent:          image.Parent,
    45  		Comment:         image.Comment,
    46  		Created:         image.Created.Format(time.RFC3339Nano),
    47  		Container:       image.Container,
    48  		ContainerConfig: &image.ContainerConfig,
    49  		DockerVersion:   image.DockerVersion,
    50  		Author:          image.Author,
    51  		Config:          image.Config,
    52  		Architecture:    image.Architecture,
    53  		Os:              image.OS,
    54  		Size:            image.Size,
    55  		VirtualSize:     s.graph.GetParentsSize(image) + image.Size,
    56  	}
    57  
    58  	imageInspect.GraphDriver.Name = s.graph.driver.String()
    59  
    60  	graphDriverData, err := s.graph.driver.GetMetadata(image.ID)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	imageInspect.GraphDriver.Data = graphDriverData
    65  	return imageInspect, nil
    66  }
    67  
    68  // ImageTarLayer return the tarLayer of the image
    69  func (s *TagStore) ImageTarLayer(name string, dest io.Writer) error {
    70  	if image, err := s.LookupImage(name); err == nil && image != nil {
    71  		// On Windows, the base layer cannot be exported
    72  		if runtime.GOOS != "windows" || image.Parent != "" {
    73  
    74  			fs, err := s.graph.TarLayer(image)
    75  			if err != nil {
    76  				return err
    77  			}
    78  			defer fs.Close()
    79  
    80  			written, err := io.Copy(dest, fs)
    81  			if err != nil {
    82  				return err
    83  			}
    84  			logrus.Debugf("rendered layer for %s of [%d] size", image.ID, written)
    85  		}
    86  		return nil
    87  	}
    88  	return fmt.Errorf("No such image: %s", name)
    89  }