Use container:path format in register
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Tue, 30 Jul 2013 12:00:36 +0000 (15:00 +0300)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Tue, 30 Jul 2013 12:00:36 +0000 (15:00 +0300)
Refs: #3778

docs/collection_of_examples/imageregister.rst
kamaki/cli/commands/image.py

index 2a52325..cfec9de 100644 (file)
@@ -14,6 +14,8 @@ The image location format::
 
     pithos://s0m3-u53r-1d/pithos/debian_base3.diskdump
 
+The crussial element in an image location is the container (e.g. `pithos`) and
+the image object path (e.g. `debian_base3.diskdump`).
 
 Register an image
 -----------------
@@ -34,7 +36,7 @@ Register the image object with the name 'Debian Base Alpha'
 
 .. code-block:: console
 
-    [kamaki]: image register 'Debian Base Alpha' pithos://s0m3-u53r-1d/pithos/debian_base3.diskdump
+    [kamaki]: image register 'Debian Base Alpha' pithos:debian_base3.diskdump
     checksum:         3cb03556ec971f...e8dd6190443b560cb7
     container-format: bare
     created-at:       2013-06-19 08:00:22
@@ -52,7 +54,7 @@ Register the image object with the name 'Debian Base Alpha'
     Metadata file uploaded as pithos:debian_base3.diskdump.meta (version 1352)
     [kamaki]:
 
-.. note:: The `image register` command automatically create a meta file and
+.. note:: The `image register` command automatically creates a meta file and
     uploads it to the same location as the image. The meta file can be
     downloaded and reused for more image registrations.
 
@@ -157,7 +159,7 @@ Attempt to register with properties
 
 .. code-block:: console
 
-    [kamaki]: image register 'Debian Base Gama' pithos://s0m3-u53r-1d/pithos/debian_base3.diskdump -p OS=Linux -p user=someuser
+    [kamaki]: image register 'Debian Base Gama' pithos:debian_base3.diskdump -p OS=Linux -p user=someuser
     Metadata file pithos:debian_base3.diskdump.meta already exists
     [kamaki]:
 
@@ -165,7 +167,7 @@ It's true that the metafile is already there, but we can override it (**-f**)
 
 .. code-block:: console
 
-    [kamaki]: image register -f 'Debian Base Gama' pithos://s0m3-u53r-1d/pithos/debian_base3.diskdump -p OS=Linux -p user=someuser
+    [kamaki]: image register -f 'Debian Base Gama' pithos:debian_base3.diskdump -p OS=Linux -p user=someuser
     [kamaki]:
 
 Register with a meta file
@@ -218,7 +220,7 @@ Register the image (don't forget the -f parameter, to override the metafile).
 
 .. code-block:: console
 
-    [kamaki]: image register -f 'Debian Base Delta' pithos://s0m3-u53r-1d/pithos/debian_base3.diskdump --metafile=debian_base3.diskdump.meta
+    [kamaki]: image register -f 'Debian Base Delta' pithos:debian_base3.diskdump --metafile=debian_base3.diskdump.meta
     checksum:         3cb03556ec971f...e8dd6190443b560cb7
     container-format: bare
     created-at:       2013-06-19 08:00:22
@@ -272,7 +274,7 @@ Let's compine the metafile with a command line attribute `user: admin`
 
 .. code-block:: console
 
-    [kamaki]: image register -f 'Debian Base Delta' pithos://s0m3-u53r-1d/pithos/debian_base3.diskdump --metafile=debian_base3.diskdump.meta
+    [kamaki]: image register -f 'Debian Base Delta' pithos:debian_base3.diskdump --metafile=debian_base3.diskdump.meta
     checksum:         3cb03556ec971f...e8dd6190443b560cb7
     container-format: bare
     created-at:       2013-06-19 08:00:22
@@ -326,7 +328,7 @@ Register the image without uploading a metafile
 
 .. code-block:: console
 
-    [kamaki]: image register 'Debian Base Delta' pithos://s0m3-u53r-1d/pithos/debian_base3.diskdump --metafile=debian_base3.diskdump.meta --no-metafile-upload
+    [kamaki]: image register 'Debian Base Delta' pithos:debian_base3.diskdump --metafile=debian_base3.diskdump.meta --no-metafile-upload
     checksum:         3cb03556ec971f...e8dd6190443b560cb7
     container-format: bare
     created-at:       2013-06-19 08:00:22
@@ -436,7 +438,7 @@ images:
     container=... # e.g. pithos
 
     for path in images/*.diskdump; do
-        location=pithos://$userid/$container/${path}
+        location=$container:${path}
         kamaki image register $path $location
     done
 
@@ -449,7 +451,7 @@ VMs) by adding the **--public** flag argument when calling `image register`.
 .. code-block:: console
 
     $ for path in images/*.diskdump; do
-        location=pithos://s0m3-u53r-1d/pithos/${path}
+        location=pithos:${path}
         echo "- - - Register ${path} - - -"
         kamaki image register $path $location --public
     done
index bccb766..ae6a5f0 100644 (file)
@@ -153,9 +153,9 @@ def _load_image_meta(filepath):
 
 def _validate_image_location(location):
     """
-    :param location: (str) pithos://<user-id>/<container>/<img-file-path>
+    :param location: (str) pithos://<user-id>/<container>/<image-path>
 
-    :returns: (<user-id>, <container>, <img-file-path>)
+    :returns: (<user-id>, <container>, <image-path>)
 
     :raises AssertionError: if location is invalid
     """
@@ -305,7 +305,10 @@ class image_register(_init_image, _optional_json):
         no_metafile_upload=FlagArgument(
             'Do not store metadata in remote meta file',
             ('--no-metafile-upload')),
-
+        container=ValueArgument(
+            'Pithos+ container containing the image file',
+            ('-C', '--container')),
+        uuid=ValueArgument('Custom user uuid', '--uuid')
     )
 
     def _get_user_id(self):
@@ -379,7 +382,7 @@ class image_register(_init_image, _optional_json):
                 importance=2, details=[
                     'An image location is needed. Image location format:',
                     '  pithos://<user-id>/<container>/<path>',
-                    ' an image file at the above location must exist.'
+                    ' where an image file at the above location must exist.'
                     ] + howto_image_file)
         try:
             return _validate_image_location(location)
@@ -391,11 +394,29 @@ class image_register(_init_image, _optional_json):
                     '  pithos://<user-id>/<container>/<img-file-path>'
                     ] + howto_image_file)
 
+    def _mine_location(self, container_path):
+        uuid = self['uuid'] or self._get_user_id()
+        if self['container']:
+            return uuid, self['container'], container_path
+        container, sep, path = container_path.partition(':')
+        if not (bool(container) and bool(path)):
+            raiseCLIError(
+                'Incorrect container-path format', importance=1, details=[
+                'Use : to seperate container form path',
+                '  <container>:<image-path>',
+                'OR',
+                'Use -C to specifiy a container',
+                '  -C <container> <image-path>'] + howto_image_file)
+
+        return uuid, container, path
+
     @errors.generic.all
     @errors.plankton.connection
-    def _run(self, name, location):
-        (params, properties, location) = self._load_params_from_file(location)
-        uuid, container, img_path = self._validate_location(location)
+    def _run(self, name, uuid, container, img_path):
+        location = 'pithos://%s/%s/%s' % (uuid, container, img_path)
+        (params, properties, new_loc) = self._load_params_from_file(location)
+        if location != new_loc:
+            uuid, container, img_path = self._validate_location(new_loc)
         self._load_params_from_args(params, properties)
         pclient = self._get_pithos_client(container)
 
@@ -440,9 +461,9 @@ class image_register(_init_image, _optional_json):
                 print('Metadata file uploaded as %s:%s (version %s)' % (
                     container, meta_path, meta_headers['x-object-version']))
 
-    def main(self, name, location):
+    def main(self, name, container___image_path):
         super(self.__class__, self)._run()
-        self._run(name, location)
+        self._run(name, *self._mine_location(container___image_path))
 
 
 @command(image_cmds)