mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #3490 from vieux/image_export_job
Move image_export to a job
This commit is contained in:
commit
b61570b521
2 changed files with 38 additions and 14 deletions
10
api.go
10
api.go
|
@ -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 {
|
||||
|
|
42
server.go
42
server.go
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -251,11 +255,17 @@ 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)
|
||||
|
||||
|
@ -263,17 +273,20 @@ func (srv *Server) ImageExport(name string, out io.Writer) error {
|
|||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue