forked from openstack/charm-swift-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc_utils.py
121 lines (98 loc) · 3.13 KB
/
misc_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
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,
restart_on_change,
)
from charmhelpers.core.hookenv import (
log,
INFO,
ERROR,
)
from charmhelpers.core.unitdata import (
HookData,
kv,
)
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.
'''
if block_device.startswith("/"):
# Resolve non-relative link(s) if device is a symlink to a real device.
# This fixes/prevents bug #1577408.
real_device = os.path.realpath(block_device)
if real_device != block_device:
log('Block device "{}" is a symlink to "{}". Using "{}".'.format(
block_device, real_device, real_device), level=INFO)
block_device = real_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)
# ignore missing block devices
return
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)
def is_paused():
"""Is the unit paused?"""
with HookData()():
if kv().get('unit-paused'):
return True
else:
return False
def pause_aware_restart_on_change(restart_map):
"""Avoids restarting services if config changes when unit is paused."""
def wrapper(f):
if is_paused():
return f
else:
return restart_on_change(restart_map)(f)
return wrapper