Browse code

Upload or download gzipped and bzipped images with put/pull -j/-b

Solomon Hykes authored on 2013/02/13 15:23:14
Showing 3 changed files
... ...
@@ -291,7 +291,20 @@ func (srv *Server) CmdKill(stdin io.ReadCloser, stdout io.Writer, args ...string
291 291
 }
292 292
 
293 293
 func (srv *Server) CmdPull(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
294
-	if len(args) < 1 {
294
+	cmd := rcli.Subcmd(stdout, "pull", "[OPTIONS] NAME", "Download a new image from a remote location")
295
+	fl_bzip2 := cmd.Bool("j", false, "Bzip2 compression")
296
+	fl_gzip := cmd.Bool("z", false, "Gzip compression")
297
+	if err := cmd.Parse(args); err != nil {
298
+		return nil
299
+	}
300
+	var compression image.Compression
301
+	if *fl_bzip2 {
302
+		compression = image.Bzip2
303
+	} else if *fl_gzip {
304
+		compression = image.Gzip
305
+	}
306
+	name := cmd.Arg(0)
307
+	if name == "" {
295 308
 		return errors.New("Not enough arguments")
296 309
 	}
297 310
 	resp, err := http.Get(args[0])
... ...
@@ -307,10 +320,23 @@ func (srv *Server) CmdPull(stdin io.ReadCloser, stdout io.Writer, args ...string
307 307
 }
308 308
 
309 309
 func (srv *Server) CmdPut(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
310
-	if len(args) < 1 {
310
+	cmd := rcli.Subcmd(stdout, "put", "[OPTIONS] NAME", "Import a new image from a local archive.")
311
+	fl_bzip2 := cmd.Bool("j", false, "Bzip2 compression")
312
+	fl_gzip := cmd.Bool("z", false, "Gzip compression")
313
+	if err := cmd.Parse(args); err != nil {
314
+		return nil
315
+	}
316
+	var compression image.Compression
317
+	if *fl_bzip2 {
318
+		compression = image.Bzip2
319
+	} else if *fl_gzip {
320
+		compression = image.Gzip
321
+	}
322
+	name := cmd.Arg(0)
323
+	if name == "" {
311 324
 		return errors.New("Not enough arguments")
312 325
 	}
313
-	img, err := srv.images.Import(args[0], stdin, stdout, nil)
326
+	img, err := srv.images.Import(name, stdin, stdout, nil, compression)
314 327
 	if err != nil {
315 328
 		return err
316 329
 	}
... ...
@@ -470,7 +496,7 @@ func (srv *Server) CmdCommit(stdin io.ReadCloser, stdout io.Writer, args ...stri
470 470
 		}
471 471
 		// Create a new image from the container's base layers + a new layer from container changes
472 472
 		parentImg := srv.images.Find(container.GetUserData("image"))
473
-		img, err := srv.images.Import(imgName, rwTar, stdout, parentImg)
473
+		img, err := srv.images.Import(imgName, rwTar, stdout, parentImg, image.Uncompressed)
474 474
 		if err != nil {
475 475
 			return err
476 476
 		}
... ...
@@ -41,9 +41,16 @@ func New(root string) (*Store, error) {
41 41
 	}, nil
42 42
 }
43 43
 
44
+type Compression uint32
44 45
 
45
-func (store *Store) Import(name string, archive io.Reader, stderr io.Writer, parent *Image) (*Image, error) {
46
-	layer, err := store.Layers.AddLayer(archive, stderr)
46
+const (
47
+	Uncompressed	Compression = iota
48
+	Bzip2
49
+	Gzip
50
+)
51
+
52
+func (store *Store) Import(name string, archive io.Reader, stderr io.Writer, parent *Image, compression Compression) (*Image, error) {
53
+	layer, err := store.Layers.AddLayer(archive, stderr, compression)
47 54
 	if err != nil {
48 55
 		return nil, err
49 56
 	}
... ...
@@ -82,13 +82,19 @@ func (store *LayerStore) layerPath(id string) string {
82 82
 }
83 83
 
84 84
 
85
-func (store *LayerStore) AddLayer(archive io.Reader, stderr io.Writer) (string, error) {
85
+func (store *LayerStore) AddLayer(archive io.Reader, stderr io.Writer, compression Compression) (string, error) {
86 86
 	tmp, err := store.Mktemp()
87 87
 	defer os.RemoveAll(tmp)
88 88
 	if err != nil {
89 89
 		return "", err
90 90
 	}
91
-	untarCmd := exec.Command("tar", "-C", tmp, "-x")
91
+	extractFlags := "-x"
92
+	if compression == Bzip2 {
93
+		extractFlags += "j"
94
+	} else if compression == Gzip {
95
+		extractFlags += "z"
96
+	}
97
+	untarCmd := exec.Command("tar", "-C", tmp, extractFlags)
92 98
 	untarW, err := untarCmd.StdinPipe()
93 99
 	if err != nil {
94 100
 		return "", err