github.com/rightscale/docker@v1.9.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/httputils"
     9  	"github.com/docker/docker/pkg/progressreader"
    10  	"github.com/docker/docker/pkg/streamformatter"
    11  	"github.com/docker/docker/runconfig"
    12  	"github.com/docker/docker/utils"
    13  )
    14  
    15  // Import imports an image, getting the archived layer data either from
    16  // inConfig (if src is "-"), or from a URI specified in src. Progress output is
    17  // written to outStream. Repository and tag names can optionally be given in
    18  // the repo and tag arguments, respectively.
    19  func (s *TagStore) Import(src string, repo string, tag string, msg string, inConfig io.ReadCloser, outStream io.Writer, containerConfig *runconfig.Config) error {
    20  	var (
    21  		sf      = streamformatter.NewJSONStreamFormatter()
    22  		archive io.ReadCloser
    23  		resp    *http.Response
    24  	)
    25  
    26  	if src == "-" {
    27  		archive = inConfig
    28  	} else {
    29  		inConfig.Close()
    30  		u, err := url.Parse(src)
    31  		if err != nil {
    32  			return err
    33  		}
    34  		if u.Scheme == "" {
    35  			u.Scheme = "http"
    36  			u.Host = src
    37  			u.Path = ""
    38  		}
    39  		outStream.Write(sf.FormatStatus("", "Downloading from %s", u))
    40  		resp, err = httputils.Download(u.String())
    41  		if err != nil {
    42  			return err
    43  		}
    44  		progressReader := progressreader.New(progressreader.Config{
    45  			In:        resp.Body,
    46  			Out:       outStream,
    47  			Formatter: sf,
    48  			Size:      resp.ContentLength,
    49  			NewLines:  true,
    50  			ID:        "",
    51  			Action:    "Importing",
    52  		})
    53  		archive = progressReader
    54  	}
    55  
    56  	defer archive.Close()
    57  	if len(msg) == 0 {
    58  		msg = "Imported from " + src
    59  	}
    60  
    61  	img, err := s.graph.Create(archive, "", "", msg, "", nil, containerConfig)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	// Optionally register the image at REPO/TAG
    66  	if repo != "" {
    67  		if err := s.Tag(repo, tag, img.ID, true); err != nil {
    68  			return err
    69  		}
    70  	}
    71  	outStream.Write(sf.FormatStatus("", img.ID))
    72  	logID := img.ID
    73  	if tag != "" {
    74  		logID = utils.ImageReference(logID, tag)
    75  	}
    76  
    77  	s.eventsService.Log("import", logID, "")
    78  	return nil
    79  }