github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/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  	Json            bool
    20  	OutStream       io.Writer
    21  	ContainerConfig *runconfig.Config
    22  }
    23  
    24  func (s *TagStore) Import(src string, repo string, tag string, imageImportConfig *ImageImportConfig) error {
    25  	var (
    26  		sf      = streamformatter.NewStreamFormatter(imageImportConfig.Json)
    27  		archive archive.ArchiveReader
    28  		resp    *http.Response
    29  	)
    30  
    31  	if src == "-" {
    32  		archive = imageImportConfig.InConfig
    33  	} else {
    34  		u, err := url.Parse(src)
    35  		if err != nil {
    36  			return err
    37  		}
    38  		if u.Scheme == "" {
    39  			u.Scheme = "http"
    40  			u.Host = src
    41  			u.Path = ""
    42  		}
    43  		imageImportConfig.OutStream.Write(sf.FormatStatus("", "Downloading from %s", u))
    44  		resp, err = httputils.Download(u.String())
    45  		if err != nil {
    46  			return err
    47  		}
    48  		progressReader := progressreader.New(progressreader.Config{
    49  			In:        resp.Body,
    50  			Out:       imageImportConfig.OutStream,
    51  			Formatter: sf,
    52  			Size:      int(resp.ContentLength),
    53  			NewLines:  true,
    54  			ID:        "",
    55  			Action:    "Importing",
    56  		})
    57  		defer progressReader.Close()
    58  		archive = progressReader
    59  	}
    60  
    61  	img, err := s.graph.Create(archive, "", "", "Imported from "+src, "", nil, imageImportConfig.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  	imageImportConfig.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  }