2013-07-19 12:52:45 -07:00
|
|
|
from charmhelpers.contrib.storage.linux.utils import (
|
|
|
|
is_block_device,
|
|
|
|
zap_disk,
|
|
|
|
)
|
|
|
|
|
|
|
|
from charmhelpers.contrib.storage.linux.loopback import (
|
|
|
|
ensure_loopback_device,
|
|
|
|
)
|
|
|
|
|
|
|
|
from charmhelpers.contrib.storage.linux.lvm import (
|
|
|
|
deactivate_lvm_volume_group,
|
|
|
|
is_lvm_physical_volume,
|
|
|
|
remove_lvm_physical_volume,
|
|
|
|
)
|
|
|
|
|
|
|
|
from charmhelpers.core.host import (
|
|
|
|
mounts,
|
|
|
|
umount,
|
2015-08-28 11:21:17 +01:00
|
|
|
restart_on_change,
|
2013-07-19 12:52:45 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
from charmhelpers.core.hookenv import (
|
|
|
|
log,
|
|
|
|
INFO,
|
|
|
|
ERROR,
|
2015-08-28 11:21:17 +01:00
|
|
|
status_get,
|
2015-10-12 15:26:35 +00:00
|
|
|
status_set,
|
2013-07-19 12:52:45 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
DEFAULT_LOOPBACK_SIZE = '5G'
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_block_device(block_device):
|
|
|
|
'''
|
|
|
|
Confirm block_device, create as loopback if necessary.
|
|
|
|
|
|
|
|
:param block_device: str: Full path of block device to ensure.
|
|
|
|
|
|
|
|
:returns: str: Full path of ensured block device.
|
|
|
|
'''
|
|
|
|
_none = ['None', 'none', None]
|
|
|
|
if (block_device in _none):
|
|
|
|
log('prepare_storage(): Missing required input: '
|
|
|
|
'block_device=%s.' % block_device, level=ERROR)
|
|
|
|
raise
|
|
|
|
|
|
|
|
if block_device.startswith('/dev/'):
|
|
|
|
bdev = block_device
|
|
|
|
elif block_device.startswith('/'):
|
|
|
|
_bd = block_device.split('|')
|
|
|
|
if len(_bd) == 2:
|
|
|
|
bdev, size = _bd
|
|
|
|
else:
|
|
|
|
bdev = block_device
|
|
|
|
size = DEFAULT_LOOPBACK_SIZE
|
|
|
|
bdev = ensure_loopback_device(bdev, size)
|
|
|
|
else:
|
|
|
|
bdev = '/dev/%s' % block_device
|
|
|
|
|
|
|
|
if not is_block_device(bdev):
|
|
|
|
log('Failed to locate valid block device at %s' % bdev, level=ERROR)
|
2014-06-16 12:33:42 -05:00
|
|
|
# ignore missing block devices
|
|
|
|
return
|
2013-07-19 12:52:45 -07:00
|
|
|
|
|
|
|
return bdev
|
|
|
|
|
|
|
|
|
|
|
|
def clean_storage(block_device):
|
|
|
|
'''
|
|
|
|
Ensures a block device is clean. That is:
|
|
|
|
- unmounted
|
|
|
|
- any lvm volume groups are deactivated
|
|
|
|
- any lvm physical device signatures removed
|
|
|
|
- partition table wiped
|
|
|
|
|
|
|
|
:param block_device: str: Full path to block device to clean.
|
|
|
|
'''
|
|
|
|
for mp, d in mounts():
|
|
|
|
if d == block_device:
|
|
|
|
log('clean_storage(): Found %s mounted @ %s, unmounting.' %
|
|
|
|
(d, mp), level=INFO)
|
|
|
|
umount(mp, persist=True)
|
|
|
|
|
|
|
|
if is_lvm_physical_volume(block_device):
|
|
|
|
deactivate_lvm_volume_group(block_device)
|
|
|
|
remove_lvm_physical_volume(block_device)
|
|
|
|
else:
|
|
|
|
zap_disk(block_device)
|
2015-08-28 11:21:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def is_paused(status_get=status_get):
|
|
|
|
"""Is the unit paused?"""
|
|
|
|
status, message = status_get()
|
|
|
|
return status == "maintenance" and message.startswith("Paused")
|
|
|
|
|
|
|
|
|
|
|
|
def pause_aware_restart_on_change(restart_map):
|
|
|
|
"""Avoids restarting services if config changes when unit is paused."""
|
|
|
|
def wrapper(f):
|
|
|
|
if is_paused():
|
2015-10-12 13:24:12 +00:00
|
|
|
# save/restore paused status in case called function changes it
|
|
|
|
status, message = status_get()
|
|
|
|
ret = f
|
|
|
|
status_set(status, message)
|
|
|
|
return ret
|
2015-08-28 11:21:17 +01:00
|
|
|
else:
|
|
|
|
return restart_on_change(restart_map)(f)
|
|
|
|
return wrapper
|