Allow images to be saved and loaded by id and repository

This commit is contained in:
Michael Crosby 2013-11-20 17:28:19 -08:00
Родитель 2bc35287a0
Коммит fd7ab143bf
3 изменённых файлов: 108 добавлений и 80 удалений

12
api.go
Просмотреть файл

@ -539,19 +539,11 @@ func getImagesGet(srv *Server, version float64, w http.ResponseWriter, r *http.R
if version > 1.0 {
w.Header().Set("Content-Type", "application/x-tar")
}
err := srv.ImageExport(name, w)
if err != nil {
return err
}
return nil
return srv.ImageExport(name, w)
}
func postImagesLoad(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
err := srv.ImageLoad(r.Body)
if err != nil {
return err
}
return nil
return srv.ImageLoad(r.Body)
}
func postContainersCreate(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

Просмотреть файл

@ -1969,8 +1969,7 @@ func (cli *DockerCli) CmdCp(args ...string) error {
func (cli *DockerCli) CmdSave(args ...string) error {
cmd := Subcmd("save", "IMAGE DESTINATION", "Save an image to a tar archive")
if err := cmd.Parse(args); err != nil {
cmd.Usage()
return nil
return err
}
if cmd.NArg() != 1 {
@ -1979,7 +1978,6 @@ func (cli *DockerCli) CmdSave(args ...string) error {
}
image := cmd.Arg(0)
if err := cli.stream("GET", "/images/"+image+"/get", nil, cli.out, nil); err != nil {
return err
}
@ -1988,17 +1986,18 @@ func (cli *DockerCli) CmdSave(args ...string) error {
func (cli *DockerCli) CmdLoad(args ...string) error {
cmd := Subcmd("load", "SOURCE", "Load an image from a tar archive")
if err := cmd.Parse(args); err != nil {
return err
}
if cmd.NArg() != 0 {
cmd.Usage()
return nil
}
err := cli.stream("POST", "/images/load", cli.in, cli.out, nil)
if err != nil {
fmt.Println("Send failed", err)
if err := cli.stream("POST", "/images/load", cli.in, cli.out, nil); err != nil {
return err
}
return nil
}

Просмотреть файл

@ -212,16 +212,58 @@ func (srv *Server) ImageExport(name string, out io.Writer) error {
utils.Debugf("Serializing %s", name)
rootRepo := srv.runtime.repositories.Repositories[name]
for _, rootImage := range rootRepo {
image, _ := srv.ImageInspect(rootImage)
rootRepo, err := srv.runtime.repositories.Get(name)
if err != nil {
return err
}
if rootRepo != nil {
for _, id := range rootRepo {
image, err := srv.ImageInspect(id)
if err != nil {
return err
}
if err := srv.exportImage(image, tempdir); err != nil {
return err
}
}
// write repositories
rootRepoMap := map[string]Repository{}
rootRepoMap[name] = rootRepo
rootRepoJson, _ := json.Marshal(rootRepoMap)
if err := ioutil.WriteFile(path.Join(tempdir, "repositories"), rootRepoJson, os.ModeAppend); err != nil {
return err
}
} else {
image, err := srv.ImageInspect(name)
if err != nil {
return err
}
if err := srv.exportImage(image, tempdir); err != nil {
return err
}
}
fs, err := archive.Tar(tempdir, archive.Uncompressed)
if err != nil {
return err
}
if _, err := io.Copy(out, fs); err != nil {
return err
}
return nil
}
func (srv *Server) exportImage(image *Image, tempdir string) error {
for i := image; i != nil; {
// temporary directory
tmpImageDir := path.Join(tempdir, i.ID)
if err := os.Mkdir(tmpImageDir, os.ModeDir); err != nil {
return err
}
defer os.RemoveAll(tmpImageDir)
var version = "1.0"
var versionBuf = []byte(version)
@ -264,25 +306,6 @@ func (srv *Server) ImageExport(name string, out io.Writer) error {
i = nil
}
}
}
// write repositories
rootRepoMap := map[string]Repository{}
rootRepoMap[name] = rootRepo
rootRepoJson, _ := json.Marshal(rootRepoMap)
if err := ioutil.WriteFile(path.Join(tempdir, "repositories"), rootRepoJson, os.ModeAppend); err != nil {
return err
}
fs, err := archive.Tar(tempdir, archive.Uncompressed)
if err != nil {
return err
}
if _, err := io.Copy(out, fs); err != nil {
return err
}
return nil
}
@ -319,10 +342,22 @@ func (srv *Server) ImageLoad(in io.Reader) error {
if err := archive.Untar(repoFile, repoDir); err != nil {
return err
}
repositoriesJson, err := ioutil.ReadFile(path.Join(tmpImageDir, "repo", "repositories"))
dirs, err := ioutil.ReadDir(repoDir)
if err != nil {
return err
}
for _, d := range dirs {
if d.IsDir() {
if err := srv.recursiveLoad(d.Name(), tmpImageDir); err != nil {
return err
}
}
}
repositoriesJson, err := ioutil.ReadFile(path.Join(tmpImageDir, "repo", "repositories"))
if err == nil {
repositories := map[string]Repository{}
if err := json.Unmarshal(repositoriesJson, &repositories); err != nil {
return err
@ -330,14 +365,15 @@ func (srv *Server) ImageLoad(in io.Reader) error {
for imageName, tagMap := range repositories {
for tag, address := range tagMap {
if err := srv.recursiveLoad(address, tmpImageDir); err != nil {
return err
}
if err := srv.runtime.repositories.Set(imageName, tag, address, true); err != nil {
return err
}
}
}
} else if !os.IsNotExist(err) {
return err
}
return nil
}
@ -373,6 +409,7 @@ func (srv *Server) recursiveLoad(address, tmpImageDir string) error {
}
}
utils.Debugf("Completed processing %s", address)
return nil
}