Add pool size check when resizing volumes

Closes #140
This commit is contained in:
Joshua Boniface 2021-09-12 19:54:51 -04:00
parent a8e9a56924
commit e71a6c90bf
1 changed files with 15 additions and 9 deletions

View File

@ -480,16 +480,16 @@ def getVolumeInformation(zkhandler, pool, volume):
def add_volume(zkhandler, pool, name, size): def add_volume(zkhandler, pool, name, size):
# Add 'B' if the volume is in bytes
if re.match(r'^[0-9]+$', size):
size = '{}B'.format(size)
# 1. Verify the size of the volume # 1. Verify the size of the volume
pool_information = getPoolInformation(zkhandler, pool) pool_information = getPoolInformation(zkhandler, pool)
size_bytes = format_bytes_fromhuman(size) size_bytes = format_bytes_fromhuman(size)
if size_bytes >= int(pool_information['stats']['free_bytes']): if size_bytes >= int(pool_information['stats']['free_bytes']):
return False, 'ERROR: Requested volume size is greater than the available free space in the pool' return False, 'ERROR: Requested volume size is greater than the available free space in the pool'
# Add 'B' if the volume is in bytes
if re.match(r'^[0-9]+$', size):
size = '{}B'.format(size)
# 2. Create the volume # 2. Create the volume
retcode, stdout, stderr = common.run_os_command('rbd create --size {} {}/{}'.format(size, pool, name)) retcode, stdout, stderr = common.run_os_command('rbd create --size {} {}/{}'.format(size, pool, name))
if retcode: if retcode:
@ -540,12 +540,18 @@ def resize_volume(zkhandler, pool, name, size):
if re.match(r'^[0-9]+$', size): if re.match(r'^[0-9]+$', size):
size = '{}B'.format(size) size = '{}B'.format(size)
# 1. Resize the volume # 1. Verify the size of the volume
pool_information = getPoolInformation(zkhandler, pool)
size_bytes = format_bytes_fromhuman(size)
if size_bytes >= int(pool_information['stats']['free_bytes']):
return False, 'ERROR: Requested volume size is greater than the available free space in the pool'
# 2. Resize the volume
retcode, stdout, stderr = common.run_os_command('rbd resize --size {} {}/{}'.format(size, pool, name)) retcode, stdout, stderr = common.run_os_command('rbd resize --size {} {}/{}'.format(size, pool, name))
if retcode: if retcode:
return False, 'ERROR: Failed to resize RBD volume "{}" to size "{}" in pool "{}": {}'.format(name, size, pool, stderr) return False, 'ERROR: Failed to resize RBD volume "{}" to size "{}" in pool "{}": {}'.format(name, size, pool, stderr)
# 2a. Determine the node running this VM if applicable # 3a. Determine the node running this VM if applicable
active_node = None active_node = None
volume_vm_name = name.split('_')[0] volume_vm_name = name.split('_')[0]
retcode, vm_info = vm.get_info(zkhandler, volume_vm_name) retcode, vm_info = vm.get_info(zkhandler, volume_vm_name)
@ -555,7 +561,7 @@ def resize_volume(zkhandler, pool, name, size):
if disk['name'] == '{}/{}'.format(pool, name): if disk['name'] == '{}/{}'.format(pool, name):
active_node = vm_info['node'] active_node = vm_info['node']
volume_id = disk['dev'] volume_id = disk['dev']
# 2b. Perform a live resize in libvirt if the VM is running # 3b. Perform a live resize in libvirt if the VM is running
if active_node is not None and vm_info.get('state', '') == 'start': if active_node is not None and vm_info.get('state', '') == 'start':
import libvirt import libvirt
# Run the libvirt command against the target host # Run the libvirt command against the target host
@ -569,11 +575,11 @@ def resize_volume(zkhandler, pool, name, size):
except Exception: except Exception:
pass pass
# 2. Get volume stats # 4. Get volume stats
retcode, stdout, stderr = common.run_os_command('rbd info --format json {}/{}'.format(pool, name)) retcode, stdout, stderr = common.run_os_command('rbd info --format json {}/{}'.format(pool, name))
volstats = stdout volstats = stdout
# 3. Add the new volume to Zookeeper # 5. Update the volume in Zookeeper
zkhandler.write([ zkhandler.write([
(('volume', f'{pool}/{name}'), ''), (('volume', f'{pool}/{name}'), ''),
(('volume.stats', f'{pool}/{name}'), volstats), (('volume.stats', f'{pool}/{name}'), volstats),