Signed-off-by: Solomon Hykes <solomon@docker.com>
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,118 @@ |
| 0 |
+package graph |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "encoding/json" |
|
| 4 |
+ "io" |
|
| 5 |
+ "io/ioutil" |
|
| 6 |
+ "os" |
|
| 7 |
+ "path" |
|
| 8 |
+ |
|
| 9 |
+ "github.com/docker/docker/archive" |
|
| 10 |
+ "github.com/docker/docker/engine" |
|
| 11 |
+ "github.com/docker/docker/image" |
|
| 12 |
+ "github.com/docker/docker/utils" |
|
| 13 |
+) |
|
| 14 |
+ |
|
| 15 |
+// Loads a set of images into the repository. This is the complementary of ImageExport. |
|
| 16 |
+// The input stream is an uncompressed tar ball containing images and metadata. |
|
| 17 |
+func (s *TagStore) CmdLoad(job *engine.Job) engine.Status {
|
|
| 18 |
+ tmpImageDir, err := ioutil.TempDir("", "docker-import-")
|
|
| 19 |
+ if err != nil {
|
|
| 20 |
+ return job.Error(err) |
|
| 21 |
+ } |
|
| 22 |
+ defer os.RemoveAll(tmpImageDir) |
|
| 23 |
+ |
|
| 24 |
+ var ( |
|
| 25 |
+ repoTarFile = path.Join(tmpImageDir, "repo.tar") |
|
| 26 |
+ repoDir = path.Join(tmpImageDir, "repo") |
|
| 27 |
+ ) |
|
| 28 |
+ |
|
| 29 |
+ tarFile, err := os.Create(repoTarFile) |
|
| 30 |
+ if err != nil {
|
|
| 31 |
+ return job.Error(err) |
|
| 32 |
+ } |
|
| 33 |
+ if _, err := io.Copy(tarFile, job.Stdin); err != nil {
|
|
| 34 |
+ return job.Error(err) |
|
| 35 |
+ } |
|
| 36 |
+ tarFile.Close() |
|
| 37 |
+ |
|
| 38 |
+ repoFile, err := os.Open(repoTarFile) |
|
| 39 |
+ if err != nil {
|
|
| 40 |
+ return job.Error(err) |
|
| 41 |
+ } |
|
| 42 |
+ if err := os.Mkdir(repoDir, os.ModeDir); err != nil {
|
|
| 43 |
+ return job.Error(err) |
|
| 44 |
+ } |
|
| 45 |
+ if err := archive.Untar(repoFile, repoDir, nil); err != nil {
|
|
| 46 |
+ return job.Error(err) |
|
| 47 |
+ } |
|
| 48 |
+ |
|
| 49 |
+ dirs, err := ioutil.ReadDir(repoDir) |
|
| 50 |
+ if err != nil {
|
|
| 51 |
+ return job.Error(err) |
|
| 52 |
+ } |
|
| 53 |
+ |
|
| 54 |
+ for _, d := range dirs {
|
|
| 55 |
+ if d.IsDir() {
|
|
| 56 |
+ if err := s.recursiveLoad(job.Eng, d.Name(), tmpImageDir); err != nil {
|
|
| 57 |
+ return job.Error(err) |
|
| 58 |
+ } |
|
| 59 |
+ } |
|
| 60 |
+ } |
|
| 61 |
+ |
|
| 62 |
+ repositoriesJson, err := ioutil.ReadFile(path.Join(tmpImageDir, "repo", "repositories")) |
|
| 63 |
+ if err == nil {
|
|
| 64 |
+ repositories := map[string]Repository{}
|
|
| 65 |
+ if err := json.Unmarshal(repositoriesJson, &repositories); err != nil {
|
|
| 66 |
+ return job.Error(err) |
|
| 67 |
+ } |
|
| 68 |
+ |
|
| 69 |
+ for imageName, tagMap := range repositories {
|
|
| 70 |
+ for tag, address := range tagMap {
|
|
| 71 |
+ if err := s.Set(imageName, tag, address, true); err != nil {
|
|
| 72 |
+ return job.Error(err) |
|
| 73 |
+ } |
|
| 74 |
+ } |
|
| 75 |
+ } |
|
| 76 |
+ } else if !os.IsNotExist(err) {
|
|
| 77 |
+ return job.Error(err) |
|
| 78 |
+ } |
|
| 79 |
+ |
|
| 80 |
+ return engine.StatusOK |
|
| 81 |
+} |
|
| 82 |
+ |
|
| 83 |
+func (s *TagStore) recursiveLoad(eng *engine.Engine, address, tmpImageDir string) error {
|
|
| 84 |
+ if err := eng.Job("image_get", address).Run(); err != nil {
|
|
| 85 |
+ utils.Debugf("Loading %s", address)
|
|
| 86 |
+ |
|
| 87 |
+ imageJson, err := ioutil.ReadFile(path.Join(tmpImageDir, "repo", address, "json")) |
|
| 88 |
+ if err != nil {
|
|
| 89 |
+ utils.Debugf("Error reading json", err)
|
|
| 90 |
+ return err |
|
| 91 |
+ } |
|
| 92 |
+ |
|
| 93 |
+ layer, err := os.Open(path.Join(tmpImageDir, "repo", address, "layer.tar")) |
|
| 94 |
+ if err != nil {
|
|
| 95 |
+ utils.Debugf("Error reading embedded tar", err)
|
|
| 96 |
+ return err |
|
| 97 |
+ } |
|
| 98 |
+ img, err := image.NewImgJSON(imageJson) |
|
| 99 |
+ if err != nil {
|
|
| 100 |
+ utils.Debugf("Error unmarshalling json", err)
|
|
| 101 |
+ return err |
|
| 102 |
+ } |
|
| 103 |
+ if img.Parent != "" {
|
|
| 104 |
+ if !s.graph.Exists(img.Parent) {
|
|
| 105 |
+ if err := s.recursiveLoad(eng, img.Parent, tmpImageDir); err != nil {
|
|
| 106 |
+ return err |
|
| 107 |
+ } |
|
| 108 |
+ } |
|
| 109 |
+ } |
|
| 110 |
+ if err := s.graph.Register(imageJson, layer, img); err != nil {
|
|
| 111 |
+ return err |
|
| 112 |
+ } |
|
| 113 |
+ } |
|
| 114 |
+ utils.Debugf("Completed processing %s", address)
|
|
| 115 |
+ |
|
| 116 |
+ return nil |
|
| 117 |
+} |
| ... | ... |
@@ -5,7 +5,6 @@ |
| 5 | 5 |
package server |
| 6 | 6 |
|
| 7 | 7 |
import ( |
| 8 |
- "encoding/json" |
|
| 9 | 8 |
"fmt" |
| 10 | 9 |
"io" |
| 11 | 10 |
"io/ioutil" |
| ... | ... |
@@ -108,110 +107,6 @@ func (srv *Server) Build(job *engine.Job) engine.Status {
|
| 108 | 108 |
return engine.StatusOK |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 |
-// Loads a set of images into the repository. This is the complementary of ImageExport. |
|
| 112 |
-// The input stream is an uncompressed tar ball containing images and metadata. |
|
| 113 |
-func (srv *Server) ImageLoad(job *engine.Job) engine.Status {
|
|
| 114 |
- tmpImageDir, err := ioutil.TempDir("", "docker-import-")
|
|
| 115 |
- if err != nil {
|
|
| 116 |
- return job.Error(err) |
|
| 117 |
- } |
|
| 118 |
- defer os.RemoveAll(tmpImageDir) |
|
| 119 |
- |
|
| 120 |
- var ( |
|
| 121 |
- repoTarFile = path.Join(tmpImageDir, "repo.tar") |
|
| 122 |
- repoDir = path.Join(tmpImageDir, "repo") |
|
| 123 |
- ) |
|
| 124 |
- |
|
| 125 |
- tarFile, err := os.Create(repoTarFile) |
|
| 126 |
- if err != nil {
|
|
| 127 |
- return job.Error(err) |
|
| 128 |
- } |
|
| 129 |
- if _, err := io.Copy(tarFile, job.Stdin); err != nil {
|
|
| 130 |
- return job.Error(err) |
|
| 131 |
- } |
|
| 132 |
- tarFile.Close() |
|
| 133 |
- |
|
| 134 |
- repoFile, err := os.Open(repoTarFile) |
|
| 135 |
- if err != nil {
|
|
| 136 |
- return job.Error(err) |
|
| 137 |
- } |
|
| 138 |
- if err := os.Mkdir(repoDir, os.ModeDir); err != nil {
|
|
| 139 |
- return job.Error(err) |
|
| 140 |
- } |
|
| 141 |
- if err := archive.Untar(repoFile, repoDir, nil); err != nil {
|
|
| 142 |
- return job.Error(err) |
|
| 143 |
- } |
|
| 144 |
- |
|
| 145 |
- dirs, err := ioutil.ReadDir(repoDir) |
|
| 146 |
- if err != nil {
|
|
| 147 |
- return job.Error(err) |
|
| 148 |
- } |
|
| 149 |
- |
|
| 150 |
- for _, d := range dirs {
|
|
| 151 |
- if d.IsDir() {
|
|
| 152 |
- if err := srv.recursiveLoad(job.Eng, d.Name(), tmpImageDir); err != nil {
|
|
| 153 |
- return job.Error(err) |
|
| 154 |
- } |
|
| 155 |
- } |
|
| 156 |
- } |
|
| 157 |
- |
|
| 158 |
- repositoriesJson, err := ioutil.ReadFile(path.Join(tmpImageDir, "repo", "repositories")) |
|
| 159 |
- if err == nil {
|
|
| 160 |
- repositories := map[string]graph.Repository{}
|
|
| 161 |
- if err := json.Unmarshal(repositoriesJson, &repositories); err != nil {
|
|
| 162 |
- return job.Error(err) |
|
| 163 |
- } |
|
| 164 |
- |
|
| 165 |
- for imageName, tagMap := range repositories {
|
|
| 166 |
- for tag, address := range tagMap {
|
|
| 167 |
- if err := srv.daemon.Repositories().Set(imageName, tag, address, true); err != nil {
|
|
| 168 |
- return job.Error(err) |
|
| 169 |
- } |
|
| 170 |
- } |
|
| 171 |
- } |
|
| 172 |
- } else if !os.IsNotExist(err) {
|
|
| 173 |
- return job.Error(err) |
|
| 174 |
- } |
|
| 175 |
- |
|
| 176 |
- return engine.StatusOK |
|
| 177 |
-} |
|
| 178 |
- |
|
| 179 |
-func (srv *Server) recursiveLoad(eng *engine.Engine, address, tmpImageDir string) error {
|
|
| 180 |
- if err := eng.Job("image_get", address).Run(); err != nil {
|
|
| 181 |
- utils.Debugf("Loading %s", address)
|
|
| 182 |
- |
|
| 183 |
- imageJson, err := ioutil.ReadFile(path.Join(tmpImageDir, "repo", address, "json")) |
|
| 184 |
- if err != nil {
|
|
| 185 |
- utils.Debugf("Error reading json", err)
|
|
| 186 |
- return err |
|
| 187 |
- } |
|
| 188 |
- |
|
| 189 |
- layer, err := os.Open(path.Join(tmpImageDir, "repo", address, "layer.tar")) |
|
| 190 |
- if err != nil {
|
|
| 191 |
- utils.Debugf("Error reading embedded tar", err)
|
|
| 192 |
- return err |
|
| 193 |
- } |
|
| 194 |
- img, err := image.NewImgJSON(imageJson) |
|
| 195 |
- if err != nil {
|
|
| 196 |
- utils.Debugf("Error unmarshalling json", err)
|
|
| 197 |
- return err |
|
| 198 |
- } |
|
| 199 |
- if img.Parent != "" {
|
|
| 200 |
- if !srv.daemon.Graph().Exists(img.Parent) {
|
|
| 201 |
- if err := srv.recursiveLoad(eng, img.Parent, tmpImageDir); err != nil {
|
|
| 202 |
- return err |
|
| 203 |
- } |
|
| 204 |
- } |
|
| 205 |
- } |
|
| 206 |
- if err := srv.daemon.Graph().Register(imageJson, layer, img); err != nil {
|
|
| 207 |
- return err |
|
| 208 |
- } |
|
| 209 |
- } |
|
| 210 |
- utils.Debugf("Completed processing %s", address)
|
|
| 211 |
- |
|
| 212 |
- return nil |
|
| 213 |
-} |
|
| 214 |
- |
|
| 215 | 111 |
func (srv *Server) ImageTag(job *engine.Job) engine.Status {
|
| 216 | 112 |
if len(job.Args) != 2 && len(job.Args) != 3 {
|
| 217 | 113 |
return job.Errorf("Usage: %s IMAGE REPOSITORY [TAG]\n", job.Name)
|
| ... | ... |
@@ -89,7 +89,6 @@ func InitServer(job *engine.Job) engine.Status {
|
| 89 | 89 |
"tag": srv.ImageTag, // FIXME merge with "image_tag" |
| 90 | 90 |
"info": srv.DockerInfo, |
| 91 | 91 |
"log": srv.Log, |
| 92 |
- "load": srv.ImageLoad, |
|
| 93 | 92 |
"build": srv.Build, |
| 94 | 93 |
"pull": srv.ImagePull, |
| 95 | 94 |
"import": srv.ImageImport, |