2014-08-05 01:29:14 -04:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
2015-02-05 06:26:05 -05:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-03-25 03:44:12 -04:00
|
|
|
"fmt"
|
2014-08-05 01:29:14 -04:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-08-05 01:29:14 -04:00
|
|
|
"github.com/docker/docker/engine"
|
2014-09-30 02:23:36 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-02-24 03:51:46 -05:00
|
|
|
"github.com/docker/docker/pkg/progressreader"
|
2015-03-17 22:18:41 -04:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2015-02-05 06:26:05 -05:00
|
|
|
"github.com/docker/docker/runconfig"
|
2014-08-05 01:29:14 -04:00
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
2015-03-25 03:44:12 -04:00
|
|
|
func (s *TagStore) CmdImport(job *engine.Job) error {
|
2014-08-05 01:29:14 -04:00
|
|
|
if n := len(job.Args); n != 2 && n != 3 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("Usage: %s SRC REPO [TAG]", job.Name)
|
2014-08-05 01:29:14 -04:00
|
|
|
}
|
|
|
|
var (
|
2015-02-05 06:26:05 -05:00
|
|
|
src = job.Args[0]
|
|
|
|
repo = job.Args[1]
|
|
|
|
tag string
|
2015-03-17 22:18:41 -04:00
|
|
|
sf = streamformatter.NewStreamFormatter(job.GetenvBool("json"))
|
2015-02-05 06:26:05 -05:00
|
|
|
archive archive.ArchiveReader
|
|
|
|
resp *http.Response
|
|
|
|
stdoutBuffer = bytes.NewBuffer(nil)
|
|
|
|
newConfig runconfig.Config
|
2014-08-05 01:29:14 -04:00
|
|
|
)
|
|
|
|
if len(job.Args) > 2 {
|
|
|
|
tag = job.Args[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
if src == "-" {
|
|
|
|
archive = job.Stdin
|
|
|
|
} else {
|
|
|
|
u, err := url.Parse(src)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:29:14 -04:00
|
|
|
}
|
|
|
|
if u.Scheme == "" {
|
|
|
|
u.Scheme = "http"
|
|
|
|
u.Host = src
|
|
|
|
u.Path = ""
|
|
|
|
}
|
|
|
|
job.Stdout.Write(sf.FormatStatus("", "Downloading from %s", u))
|
|
|
|
resp, err = utils.Download(u.String())
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:29:14 -04:00
|
|
|
}
|
2015-02-24 03:51:46 -05:00
|
|
|
progressReader := progressreader.New(progressreader.Config{
|
|
|
|
In: resp.Body,
|
|
|
|
Out: job.Stdout,
|
|
|
|
Formatter: sf,
|
|
|
|
Size: int(resp.ContentLength),
|
|
|
|
NewLines: true,
|
|
|
|
ID: "",
|
|
|
|
Action: "Importing",
|
|
|
|
})
|
2014-08-05 01:29:14 -04:00
|
|
|
defer progressReader.Close()
|
|
|
|
archive = progressReader
|
|
|
|
}
|
2015-02-05 06:26:05 -05:00
|
|
|
|
|
|
|
buildConfigJob := job.Eng.Job("build_config")
|
|
|
|
buildConfigJob.Stdout.Add(stdoutBuffer)
|
|
|
|
buildConfigJob.Setenv("changes", job.Getenv("changes"))
|
|
|
|
// FIXME this should be remove when we remove deprecated config param
|
|
|
|
buildConfigJob.Setenv("config", job.Getenv("config"))
|
|
|
|
|
|
|
|
if err := buildConfigJob.Run(); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2015-02-05 06:26:05 -05:00
|
|
|
}
|
|
|
|
if err := json.NewDecoder(stdoutBuffer).Decode(&newConfig); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2015-02-05 06:26:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
img, err := s.graph.Create(archive, "", "", "Imported from "+src, "", nil, &newConfig)
|
2014-08-05 01:29:14 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:29:14 -04:00
|
|
|
}
|
|
|
|
// Optionally register the image at REPO/TAG
|
|
|
|
if repo != "" {
|
|
|
|
if err := s.Set(repo, tag, img.ID, true); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-05 01:29:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
job.Stdout.Write(sf.FormatStatus("", img.ID))
|
2014-10-22 09:48:02 -04:00
|
|
|
logID := img.ID
|
|
|
|
if tag != "" {
|
2015-02-26 21:23:50 -05:00
|
|
|
logID = utils.ImageReference(logID, tag)
|
2014-10-22 09:48:02 -04:00
|
|
|
}
|
|
|
|
if err = job.Eng.Job("log", "import", logID, "").Run(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error logging event 'import' for %s: %s", logID, err)
|
2014-10-22 09:48:02 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-08-05 01:29:14 -04:00
|
|
|
}
|