From: Thanh Ha Date: Fri, 7 Sep 2018 17:42:09 +0000 (-0400) Subject: Add cmd to upload openstack images X-Git-Tag: v0.18.0~9^2 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F98%2F12498%2F4;p=releng%2Flftools.git Add cmd to upload openstack images Issue: RELENG-1201 Change-Id: I1f6072fbfff003a4602d3765a6a5ce7c94fe4c58 Signed-off-by: Thanh Ha --- diff --git a/.coafile b/.coafile index 20374125..f4a09b99 100644 --- 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 diff --git a/lftools/openstack/cmd.py b/lftools/openstack/cmd.py index 9cafdf3d..495e9e7e 100644 --- a/lftools/openstack/cmd.py +++ b/lftools/openstack/cmd.py @@ -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() diff --git a/lftools/openstack/image.py b/lftools/openstack/image.py index 4757a001..6c842c78 100644 --- a/lftools/openstack/image.py +++ b/lftools/openstack/image.py @@ -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 index 00000000..612852cf --- /dev/null +++ b/releasenotes/notes/upload-openstack-images-99d86c78044850b0.yaml @@ -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...``