Add cmd to upload openstack images 98/12498/4
authorThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 7 Sep 2018 17:42:09 +0000 (13:42 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 7 Sep 2018 19:32:53 +0000 (15:32 -0400)
Issue: RELENG-1201
Change-Id: I1f6072fbfff003a4602d3765a6a5ce7c94fe4c58
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
.coafile
lftools/openstack/cmd.py
lftools/openstack/image.py
releasenotes/notes/upload-openstack-images-99d86c78044850b0.yaml [new file with mode: 0644]

index 2037412..f4a09b9 100644 (file)
--- a/.coafile
+++ b/.coafile
@@ -36,6 +36,7 @@ known_first_party_imports = lftools
 known_third_party_imports =
     pytest,
     six,
+    shade,
     ruamel.yaml,
     yaml
 pydocstyle_ignore = D203, D213, D301
index 9cafdf3..495e9e7 100644 (file)
@@ -90,9 +90,23 @@ def share(ctx, image, dest):
     os_image.share(ctx.obj['os_cloud'], image, dest)
 
 
+@click.command()
+@click.argument('image')
+@click.argument('name', nargs=-1, required=True)
+@click.option(
+    '--disk-format', type=str, default='qcow2',
+    help='Disk format of image. (default: qcow2)')
+@click.pass_context
+def upload(ctx, image, name, disk_format):
+    """Share image with another tenant."""
+    name = ' '.join(name)
+    os_image.upload(ctx.obj['os_cloud'], image, name, disk_format)
+
+
 image.add_command(cleanup)
 image.add_command(list)
 image.add_command(share)
+image.add_command(upload)
 
 
 @openstack.group()
index 4757a00..6c842c7 100644 (file)
@@ -15,10 +15,13 @@ __author__ = 'Thanh Ha'
 from datetime import datetime
 from datetime import timedelta
 import logging
+import re
 import subprocess
 import sys
+import tempfile
 
 import shade
+from six.moves import urllib
 
 log = logging.getLogger(__name__)
 
@@ -180,3 +183,23 @@ def share(os_cloud, image, clouds):
         log.info('Sharing to {}.'.format(cloud))
         _share_to_cloud(os_cloud, image_id, _get_token(cloud))
         _accept_shared_image(cloud, image_id)
+
+
+def upload(os_cloud, image, name, disk_format='qcow2'):
+    """Upload image to openstack."""
+    log.info('Uploading image {} with name "{}".'.format(image, name))
+    cloud = shade.openstack_cloud(cloud=os_cloud)
+
+    if re.match(r'^http[s]?://', image):
+        tmp = tempfile.NamedTemporaryFile(suffix='.img')
+        log.info('URL provided downloading image locally to {}.'.format(tmp.name))
+        urllib.request.urlretrieve(image, tmp.name)  # nosec
+        image = tmp.name
+
+    try:
+        cloud.create_image(name, image, disk_format=disk_format, wait=True)
+    except FileNotFoundError as e:
+        log.info(str(e))
+        sys.exit(1)
+
+    log.info('Upload complete.')
diff --git a/releasenotes/notes/upload-openstack-images-99d86c78044850b0.yaml b/releasenotes/notes/upload-openstack-images-99d86c78044850b0.yaml
new file mode 100644 (file)
index 0000000..612852c
--- /dev/null
@@ -0,0 +1,7 @@
+---
+features:
+  - |
+    Add an ``openstack image upload`` sub-command to handle uploading images
+    to openstack.
+
+    Usage: ``Usage: lftools openstack image upload [OPTIONS] IMAGE NAME...``