Skip to content

Commit

Permalink
Merge pull request moby#3490 from vieux/image_export_job
Browse files Browse the repository at this point in the history
Move image_export to a job
  • Loading branch information
vieux committed Jan 10, 2014
2 parents 6dde20c + 5264914 commit b61570b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
10 changes: 8 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,17 @@ func postImagesPush(srv *Server, version float64, w http.ResponseWriter, r *http
}

func getImagesGet(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
name := vars["name"]
if vars == nil {
return fmt.Errorf("Missing parameter")
}
if version > 1.0 {
w.Header().Set("Content-Type", "application/x-tar")
}
return srv.ImageExport(name, w)
job := srv.Eng.Job("image_export", vars["name"])
if err := job.Stdout.Add(w); err != nil {
return err
}
return job.Run()
}

func postImagesLoad(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
Expand Down
42 changes: 30 additions & 12 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func jobInitApi(job *engine.Job) engine.Status {
job.Error(err)
return engine.StatusErr
}
if err := job.Eng.Register("image_export", srv.ImageExport); err != nil {
job.Error(err)
return engine.StatusErr
}
return engine.StatusOK
}

Expand Down Expand Up @@ -251,29 +255,38 @@ func (srv *Server) ContainerExport(job *engine.Job) engine.Status {
// uncompressed tar ball.
// name is the set of tags to export.
// out is the writer where the images are written to.
func (srv *Server) ImageExport(name string, out io.Writer) error {
func (srv *Server) ImageExport(job *engine.Job) engine.Status {
if len(job.Args) != 1 {
job.Errorf("Usage: %s CONTAINER\n", job.Name)
return engine.StatusErr
}
name := job.Args[0]
// get image json
tempdir, err := ioutil.TempDir("", "docker-export-")
if err != nil {
return err
job.Error(err)
return engine.StatusErr
}
defer os.RemoveAll(tempdir)

utils.Debugf("Serializing %s", name)

rootRepo, err := srv.runtime.repositories.Get(name)
if err != nil {
return err
job.Error(err)
return engine.StatusErr
}
if rootRepo != nil {
for _, id := range rootRepo {
image, err := srv.ImageInspect(id)
if err != nil {
return err
job.Error(err)
return engine.StatusErr
}

if err := srv.exportImage(image, tempdir); err != nil {
return err
job.Error(err)
return engine.StatusErr
}
}

Expand All @@ -283,27 +296,32 @@ func (srv *Server) ImageExport(name string, out io.Writer) error {
rootRepoJson, _ := json.Marshal(rootRepoMap)

if err := ioutil.WriteFile(path.Join(tempdir, "repositories"), rootRepoJson, os.ModeAppend); err != nil {
return err
job.Error(err)
return engine.StatusErr
}
} else {
image, err := srv.ImageInspect(name)
if err != nil {
return err
job.Error(err)
return engine.StatusErr
}
if err := srv.exportImage(image, tempdir); err != nil {
return err
job.Error(err)
return engine.StatusErr
}
}

fs, err := archive.Tar(tempdir, archive.Uncompressed)
if err != nil {
return err
job.Error(err)
return engine.StatusErr
}

if _, err := io.Copy(out, fs); err != nil {
return err
if _, err := io.Copy(job.Stdout, fs); err != nil {
job.Error(err)
return engine.StatusErr
}
return nil
return engine.StatusOK
}

func (srv *Server) exportImage(image *Image, tempdir string) error {
Expand Down

0 comments on commit b61570b

Please sign in to comment.