From 1de57ab6f30fe774d119c6c396249a02f8ee663f Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Sun, 9 Feb 2020 20:42:56 -0500 Subject: [PATCH] Add CLI client interface to image upload Closes #68 --- client-cli/cli_lib/ceph.py | 23 +++++++++++++++++++++++ client-cli/cli_lib/common.py | 8 +++++--- client-cli/pvc.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/client-cli/cli_lib/ceph.py b/client-cli/cli_lib/ceph.py index db6498b5..f04bc54b 100644 --- a/client-cli/cli_lib/ceph.py +++ b/client-cli/cli_lib/ceph.py @@ -855,6 +855,29 @@ def ceph_volume_add(config, pool, volume, size): return retstatus, response.json()['message'] +def ceph_volume_upload(config, pool, volume, image_format, image_file): + """ + Upload a disk image to a Ceph volume + + API endpoint: POST /api/v1/storage/ceph/volume/{pool}/{volume}/upload + API arguments: image_format={image_format} + API schema: {"message":"{data}"} + """ + params = { + 'image_format': image_format + } + files = { + 'file': open(image_file,'rb') + } + response = call_api(config, 'post', '/storage/ceph/volume/{}/{}/upload'.format(pool, volume), params=params, files=files) + + if response.status_code == 200: + retstatus = True + else: + retstatus = False + + return retstatus, response.json()['message'] + def ceph_volume_remove(config, pool, volume): """ Remove Ceph volume diff --git a/client-cli/cli_lib/common.py b/client-cli/cli_lib/common.py index 35d850e8..06685ba4 100644 --- a/client-cli/cli_lib/common.py +++ b/client-cli/cli_lib/common.py @@ -23,7 +23,7 @@ import requests import click -def call_api(config, operation, request_uri, params=None, data=None): +def call_api(config, operation, request_uri, params=None, data=None, files=None): # Craft the URI uri = '{}://{}{}{}'.format( config['api_scheme'], @@ -52,14 +52,16 @@ def call_api(config, operation, request_uri, params=None, data=None): uri, headers=headers, params=params, - data=data + data=data, + files=files ) if operation == 'put': response = requests.put( uri, headers=headers, params=params, - data=data + data=data, + files=files ) if operation == 'patch': response = requests.patch( diff --git a/client-cli/pvc.py b/client-cli/pvc.py index 2cdd55f8..dcd390dc 100755 --- a/client-cli/pvc.py +++ b/client-cli/pvc.py @@ -1726,6 +1726,40 @@ def ceph_volume_add(pool, name, size): retcode, retmsg = pvc_ceph.ceph_volume_add(config, pool, name, size) cleanup(retcode, retmsg) +############################################################################### +# pvc storage volume upload +############################################################################### +@click.command(name='upload', short_help='Upload a local image file to RBD volume.') +@click.argument( + 'pool' +) +@click.argument( + 'name' +) +@click.argument( + 'image_file' +) +@click.option( + '-f', '--format', 'image_format', + default='raw', show_default=True, + help='The format of the source image.' +) +def ceph_volume_upload(pool, name, image_format, image_file): + """ + Upload a disk image file IMAGE_FILE to the RBD volume NAME in pool POOL. + + The volume NAME must exist in the pool before uploading to it, and must be large enough to fit the disk image in raw format. + + If the image format is "raw", the image is uploaded directly to the target volume without modification. Otherwise, it will be converted into raw format by "qemu-img convert" on the remote side before writing using a temporary volume. The image format must be a valid format recognized by "qemu-img", such as "vmdk" or "qcow2". + """ + + if not os.path.exists(image_file): + click.echo("ERROR: File '{}' does not exist!".format(image_file)) + exit(1) + + retcode, retmsg = pvc_ceph.ceph_volume_upload(config, pool, name, image_format, image_file) + cleanup(retcode, retmsg) + ############################################################################### # pvc storage volume remove ############################################################################### @@ -3271,6 +3305,7 @@ ceph_pool.add_command(ceph_pool_remove) ceph_pool.add_command(ceph_pool_list) ceph_volume.add_command(ceph_volume_add) +ceph_volume.add_command(ceph_volume_upload) ceph_volume.add_command(ceph_volume_resize) ceph_volume.add_command(ceph_volume_rename) ceph_volume.add_command(ceph_volume_clone)