1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

move load to a job

Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor.vieux@docker.com> (github: vieux)
This commit is contained in:
Victor Vieux 2014-01-20 16:09:17 -08:00
parent 497e7d651a
commit f2f44b91a8
3 changed files with 30 additions and 16 deletions

View file

@ -24,7 +24,7 @@ ok "/commit": postCommit, 0
TODO "/build": postBuild, 0 yes
TODO "/images/create": postImagesCreate, N yes yes (pull)
ok "/images/{name:.*}/insert": postImagesInsert, N yes yes
... "/images/load": postImagesLoad, 1 yes (stdin)
ok "/images/load": postImagesLoad, 1 yes (stdin)
TODO "/images/{name:.*}/push": postImagesPush, N yes
ok "/images/{name:.*}/tag": postImagesTag, 0
ok "/containers/create": postContainersCreate, 0

4
api.go
View file

@ -608,7 +608,9 @@ func getImagesGet(srv *Server, version float64, w http.ResponseWriter, r *http.R
}
func postImagesLoad(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
return srv.ImageLoad(r.Body)
job := srv.Eng.Job("load")
job.Stdin.Add(r.Body)
return job.Run()
}
func postContainersCreate(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

View file

@ -95,6 +95,7 @@ func jobInitApi(job *engine.Job) engine.Status {
"search": srv.ImagesSearch,
"changes": srv.ContainerChanges,
"top": srv.ContainerTop,
"load": srv.ImageLoad,
} {
if err := job.Eng.Register(name, handler); err != nil {
job.Error(err)
@ -355,10 +356,11 @@ func (srv *Server) exportImage(image *Image, tempdir string) error {
// Loads a set of images into the repository. This is the complementary of ImageExport.
// The input stream is an uncompressed tar ball containing images and metadata.
func (srv *Server) ImageLoad(in io.Reader) error {
func (srv *Server) ImageLoad(job *engine.Job) engine.Status {
tmpImageDir, err := ioutil.TempDir("", "docker-import-")
if err != nil {
return err
job.Error(err)
return engine.StatusErr
}
defer os.RemoveAll(tmpImageDir)
@ -369,33 +371,40 @@ func (srv *Server) ImageLoad(in io.Reader) error {
tarFile, err := os.Create(repoTarFile)
if err != nil {
return err
job.Error(err)
return engine.StatusErr
}
if _, err := io.Copy(tarFile, in); err != nil {
return err
if _, err := io.Copy(tarFile, job.Stdin); err != nil {
job.Error(err)
return engine.StatusErr
}
tarFile.Close()
repoFile, err := os.Open(repoTarFile)
if err != nil {
return err
job.Error(err)
return engine.StatusErr
}
if err := os.Mkdir(repoDir, os.ModeDir); err != nil {
return err
job.Error(err)
return engine.StatusErr
}
if err := archive.Untar(repoFile, repoDir, nil); err != nil {
return err
job.Error(err)
return engine.StatusErr
}
dirs, err := ioutil.ReadDir(repoDir)
if err != nil {
return err
job.Error(err)
return engine.StatusErr
}
for _, d := range dirs {
if d.IsDir() {
if err := srv.recursiveLoad(d.Name(), tmpImageDir); err != nil {
return err
job.Error(err)
return engine.StatusErr
}
}
}
@ -404,21 +413,24 @@ func (srv *Server) ImageLoad(in io.Reader) error {
if err == nil {
repositories := map[string]Repository{}
if err := json.Unmarshal(repositoriesJson, &repositories); err != nil {
return err
job.Error(err)
return engine.StatusErr
}
for imageName, tagMap := range repositories {
for tag, address := range tagMap {
if err := srv.runtime.repositories.Set(imageName, tag, address, true); err != nil {
return err
job.Error(err)
return engine.StatusErr
}
}
}
} else if !os.IsNotExist(err) {
return err
job.Error(err)
return engine.StatusErr
}
return nil
return engine.StatusOK
}
func (srv *Server) recursiveLoad(address, tmpImageDir string) error {