github.com/crquan/docker@v1.8.1/graph/import.go (about)

     1  package graph
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	"github.com/docker/docker/pkg/archive"
     9  	"github.com/docker/docker/pkg/httputils"
    10  	"github.com/docker/docker/pkg/progressreader"
    11  	"github.com/docker/docker/pkg/streamformatter"
    12  	"github.com/docker/docker/runconfig"
    13  	"github.com/docker/docker/utils"
    14  )
    15  
    16  type ImageImportConfig struct {
    17  	Changes         []string
    18  	InConfig        io.ReadCloser
    19  	OutStream       io.Writer
    20  	ContainerConfig *runconfig.Config
    21  }
    22  
    23  func (s *TagStore) Import(src string, repo string, tag string, imageImportConfig *ImageImportConfig) error {
    24  	var (
    25  		sf      = streamformatter.NewJSONStreamFormatter()
    26  		archive archive.ArchiveReader
    27  		resp    *http.Response
    28  	)
    29  
    30  	if src == "-" {
    31  		archive = imageImportConfig.InConfig
    32  	} else {
    33  		u, err := url.Parse(src)
    34  		if err != nil {
    35  			return err
    36  		}
    37  		if u.Scheme == "" {
    38  			u.Scheme = "http"
    39  			u.Host = src
    40  			u.Path = ""
    41  		}
    42  		imageImportConfig.OutStream.Write(sf.FormatStatus("", "Downloading from %s", u))
    43  		resp, err = httputils.Download(u.String())
    44  		if err != nil {
    45  			return err
    46  		}
    47  		progressReader := progressreader.New(progressreader.Config{
    48  			In:        resp.Body,
    49  			Out:       imageImportConfig.OutStream,
    50  			Formatter: sf,
    51  			Size:      int(resp.ContentLength),
    52  			NewLines:  true,
    53  			ID:        "",
    54  			Action:    "Importing",
    55  		})
    56  		defer progressReader.Close()
    57  		archive = progressReader
    58  	}
    59  
    60  	img, err := s.graph.Create(archive, "", "", "Imported from "+src, "", nil, imageImportConfig.ContainerConfig)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	// Optionally register the image at REPO/TAG
    65  	if repo != "" {
    66  		if err := s.Tag(repo, tag, img.ID, true); err != nil {
    67  			return err
    68  		}
    69  	}
    70  	imageImportConfig.OutStream.Write(sf.FormatStatus("", img.ID))
    71  	logID := img.ID
    72  	if tag != "" {
    73  		logID = utils.ImageReference(logID, tag)
    74  	}
    75  
    76  	s.eventsService.Log("import", logID, "")
    77  	return nil
    78  }