Add option for passing user-defined image metadata
authorNikos Skalkotos <skalkoto@grnet.gr>
Fri, 18 May 2012 14:05:01 +0000 (17:05 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Fri, 18 May 2012 14:05:01 +0000 (17:05 +0300)
The metadata passed by the user will overwrite any automatically
collected metadata with the same key.

image_creator/main.py

index a18d6c7..cbbc42b 100644 (file)
@@ -90,6 +90,10 @@ def parse_options(input_args):
         help="Use this ACCOUNT when uploading/registring images [Default: %s]"\
         % account)
 
         help="Use this ACCOUNT when uploading/registring images [Default: %s]"\
         % account)
 
+    parser.add_option("-m", "--metadata", dest="metadata", default=[],
+        help="Add custom KEY=VALUE metadata to the image", action="append",
+        metavar="KEY=VALUE")
+
     parser.add_option("-t", "--token", dest="token", type="string",
         default=token,
         help="Use this token when uploading/registring images [Default: %s]"\
     parser.add_option("-t", "--token", dest="token", type="string",
         default=token,
         help="Use this token when uploading/registring images [Default: %s]"\
@@ -117,6 +121,7 @@ def parse_options(input_args):
 
     if len(args) != 1:
         parser.error('Wrong number of arguments')
 
     if len(args) != 1:
         parser.error('Wrong number of arguments')
+
     options.source = args[0]
     if not os.path.exists(options.source):
         raise FatalError("Input media `%s' is not accessible" % options.source)
     options.source = args[0]
     if not os.path.exists(options.source):
         raise FatalError("Input media `%s' is not accessible" % options.source)
@@ -132,6 +137,16 @@ def parse_options(input_args):
         raise FatalError("Image uploading cannot be performed. No ~okeanos "
         "token is specified. User -t to set a token.")
 
         raise FatalError("Image uploading cannot be performed. No ~okeanos "
         "token is specified. User -t to set a token.")
 
+    meta = {}
+    for m in options.metadata:
+        try:
+            key, value = m.split('=', 1)
+        except ValueError:
+            raise FatalError("Metadata option: `%s' is not in "\
+                                                    "KEY=VALUE format." % m)
+        meta[key] = value
+    options.metadata = meta
+
     return options
 
 
     return options
 
 
@@ -194,6 +209,9 @@ def image_creator():
         size = options.shrink and dev.shrink() or dev.size
         metadata.update(dev.meta)
 
         size = options.shrink and dev.shrink() or dev.size
         metadata.update(dev.meta)
 
+        # Add command line metadata to the collected ones...
+        metadata.update(options.metadata)
+
         checksum = md5(snapshot, size)
 
         metastring = '\n'.join(
         checksum = md5(snapshot, size)
 
         metastring = '\n'.join(