Move functions into juju utilities.

This commit is contained in:
Luciano Lo Giudice
2021-11-29 12:08:28 -03:00
parent 4a52ba1dba
commit 97f1ea5693
2 changed files with 68 additions and 66 deletions

View File

@@ -38,70 +38,6 @@ import zaza.utilities.juju as juju_utils
import zaza.openstack.utilities.openstack as zaza_openstack
def add_storage(unit, label, pool, size):
"""Add storage to a Juju unit.
:param unit: The unit name (i.e: ceph-osd/0)
:type unit: str
:param label: The storage label (i.e: osd-devices)
:type label: str
:param pool: The pool on which to allocate the storage (i.e: cinder)
:type pool: str
:size: The size in GB of the storage to attach.
:type size: int
:returns: The name of the allocated storage.
"""
rv = subprocess.check_output(['juju', 'add-storage', unit,
'{}={},{}'.format(label, pool,
str(size) + 'GB')],
stderr=subprocess.STDOUT)
return rv.decode('UTF-8').replace('added storage ', '').split(' ')[0]
def detach_storage(storage_name):
"""Detach previously allocated Juju storage."""
subprocess.check_call(['juju', 'detach-storage', storage_name])
def remove_storage(storage_name, force=False):
"""Remove Juju storage.
:param storage_name: The name of the previously allocated Juju storage.
:type storage_name: str
:param force: If False (default), require that the storage be detached
before it can be removed.
:type force: bool
"""
cmd = ['juju', 'remove-storage', storage_name]
if force:
cmd.append('--force')
subprocess.check_call(cmd)
def add_loop_device(unit, size=10):
"""Add a loopback device to a Juju unit.
:param unit: The unit name on which to create the device.
:type unit: str
:param size: The size in GB of the device.
:type size: int
:returns: The device name.
"""
loop_name = '/home/ubuntu/loop.img'
truncate = 'truncate --size {}GB {}'.format(size, loop_name)
losetup = 'losetup --find {}'.format(loop_name)
lofind = 'losetup -a | grep {} | cut -f1 -d ":"'.format(loop_name)
cmd = "sudo sh -c '{} && {} && {}'".format(truncate, losetup, lofind)
return zaza_model.run_on_unit(unit, cmd)
class CephLowLevelTest(test_utils.OpenStackBaseTest):
"""Ceph Low Level Test Class."""
@@ -615,8 +551,8 @@ class CephTest(test_utils.OpenStackBaseTest):
logging.info('Running add-disk action with a caching device')
osds = [x.entity_id for x in zaza_model.get_units('ceph-osd')]
for unit in osds:
add_storage(unit, 'cache-devices', 'cinder', 10)
loop_dev = add_loop_device(unit, 10)
juju_utils.add_storage(unit, 'cache-devices', 'cinder', 10)
loop_dev = juju_utils.add_loop_device(unit, 10)
action_obj = zaza_model.run_action(
unit_name=unit,
action_name='add-disk',

View File

@@ -17,7 +17,9 @@
import logging
import functools
import subprocess
import zaza.model
import zaza.utilities.juju
@@ -310,3 +312,67 @@ def get_subordinate_units(unit_list, charm_name=None, status=None,
charm_name=charm_name,
status=status,
model_name=model_name)
def add_storage(unit, label, pool, size):
"""Add storage to a Juju unit.
:param unit: The unit name (i.e: ceph-osd/0)
:type unit: str
:param label: The storage label (i.e: osd-devices)
:type label: str
:param pool: The pool on which to allocate the storage (i.e: cinder)
:type pool: str
:size: The size in GB of the storage to attach.
:type size: int
:returns: The name of the allocated storage.
"""
rv = subprocess.check_output(['juju', 'add-storage', unit,
'{}={},{}'.format(label, pool,
str(size) + 'GB')],
stderr=subprocess.STDOUT)
return rv.decode('UTF-8').replace('added storage ', '').split(' ')[0]
def detach_storage(storage_name):
"""Detach previously allocated Juju storage."""
subprocess.check_call(['juju', 'detach-storage', storage_name])
def remove_storage(storage_name, force=False):
"""Remove Juju storage.
:param storage_name: The name of the previously allocated Juju storage.
:type storage_name: str
:param force: If False (default), require that the storage be detached
before it can be removed.
:type force: bool
"""
cmd = ['juju', 'remove-storage', storage_name]
if force:
cmd.append('--force')
subprocess.check_call(cmd)
def add_loop_device(unit, size=10):
"""Add a loopback device to a Juju unit.
:param unit: The unit name on which to create the device.
:type unit: str
:param size: The size in GB of the device.
:type size: int
:returns: The device name.
"""
loop_name = '/home/ubuntu/loop.img'
truncate = 'truncate --size {}GB {}'.format(size, loop_name)
losetup = 'losetup --find {}'.format(loop_name)
lofind = 'losetup -a | grep {} | cut -f1 -d ":"'.format(loop_name)
cmd = "sudo sh -c '{} && {} && {}'".format(truncate, losetup, lofind)
return zaza.model.run_on_unit(unit, cmd)