block_device: introduce helper function to check swap or ephemeral device
and move generic function, mappings_prepend_dev() from ec2utils to block_device
This commit is contained in:
parent
1f55e116ad
commit
ba6b6a20ee
@ -106,7 +106,7 @@ def _parse_block_device_mapping(bdm):
|
||||
|
||||
|
||||
def _properties_get_mappings(properties):
|
||||
return ec2utils.mappings_prepend_dev(properties.get('mappings', []))
|
||||
return block_device.mappings_prepend_dev(properties.get('mappings', []))
|
||||
|
||||
|
||||
def _format_block_device_mapping(bdm):
|
||||
@ -145,8 +145,7 @@ def _format_mappings(properties, result):
|
||||
"""Format multiple BlockDeviceMappingItemType"""
|
||||
mappings = [{'virtualName': m['virtual'], 'deviceName': m['device']}
|
||||
for m in _properties_get_mappings(properties)
|
||||
if (m['virtual'] == 'swap' or
|
||||
m['virtual'].startswith('ephemeral'))]
|
||||
if block_device.is_swap_or_ephemeral(m['virtual'])]
|
||||
|
||||
block_device_mapping = [_format_block_device_mapping(bdm) for bdm in
|
||||
properties.get('block_device_mapping', [])]
|
||||
@ -1447,8 +1446,7 @@ class CloudController(object):
|
||||
if virtual_name in ('ami', 'root'):
|
||||
continue
|
||||
|
||||
assert (virtual_name == 'swap' or
|
||||
virtual_name.startswith('ephemeral'))
|
||||
assert block_device.is_swap_or_ephemeral(virtual_name)
|
||||
device_name = m['device']
|
||||
if device_name in [b['device_name'] for b in mapping
|
||||
if not b.get('no_device', False)]:
|
||||
|
@ -135,13 +135,3 @@ def dict_from_dotted_str(items):
|
||||
args[key] = value
|
||||
|
||||
return args
|
||||
|
||||
|
||||
def mappings_prepend_dev(mappings):
|
||||
"""Prepend '/dev/' to 'device' entry of swap/ephemeral virtual type"""
|
||||
for m in mappings:
|
||||
virtual = m['virtual']
|
||||
if ((virtual == 'swap' or virtual.startswith('ephemeral')) and
|
||||
(not m['device'].startswith('/'))):
|
||||
m['device'] = '/dev/' + m['device']
|
||||
return mappings
|
||||
|
@ -15,6 +15,8 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import re
|
||||
|
||||
|
||||
def properties_root_device_name(properties):
|
||||
"""get root device name from image meta data.
|
||||
@ -33,3 +35,37 @@ def properties_root_device_name(properties):
|
||||
root_device_name = properties['root_device_name']
|
||||
|
||||
return root_device_name
|
||||
|
||||
|
||||
_ephemeral = re.compile('^ephemeral(\d|[1-9]\d+)$')
|
||||
|
||||
|
||||
def is_ephemeral(device_name):
|
||||
return _ephemeral.match(device_name)
|
||||
|
||||
|
||||
def ephemeral_num(ephemeral_name):
|
||||
assert is_ephemeral(ephemeral_name)
|
||||
return int(_ephemeral.sub('\\1', ephemeral_name))
|
||||
|
||||
|
||||
def is_swap_or_ephemeral(device_name):
|
||||
return device_name == 'swap' or is_ephemeral(device_name)
|
||||
|
||||
|
||||
def mappings_prepend_dev(mappings):
|
||||
"""Prepend '/dev/' to 'device' entry of swap/ephemeral virtual type"""
|
||||
for m in mappings:
|
||||
virtual = m['virtual']
|
||||
if (is_swap_or_ephemeral(virtual) and
|
||||
(not m['device'].startswith('/'))):
|
||||
m['device'] = '/dev/' + m['device']
|
||||
return mappings
|
||||
|
||||
|
||||
_dev = re.compile('^/dev/')
|
||||
|
||||
|
||||
def strip_dev(device_name):
|
||||
"""remove leading '/dev/'"""
|
||||
return _dev.sub('', device_name)
|
||||
|
@ -187,7 +187,7 @@ class Ec2utilsTestCase(test.TestCase):
|
||||
'device': '/dev/sdc1'},
|
||||
{'virtual': 'ephemeral1',
|
||||
'device': '/dev/sdc1'}]
|
||||
self.assertDictListMatch(ec2utils.mappings_prepend_dev(mappings),
|
||||
self.assertDictListMatch(block_device.mappings_prepend_dev(mappings),
|
||||
expected_result)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user