Move global mount tracking into state
Keep track of the mount-point ordering in a state variable, rather than a global. This path is tested by existing unit tests. Note a prior change inserted the MountNode objects directly into a list in self.state, which makes sorting quite easy as it can just implement __lt__. Unfortunately we still json dump the state, and thus we can't have aribtrary objects in it (future work may be to check keys inserted into the status object...). So we have to do a bit of wrangling with tuple lists and comparision functions here, but it's not too bad. Change-Id: I0c51e0c53c4efdb7a65ab0efe09a6780cb1affa8
This commit is contained in:
parent
886f925b13
commit
09dee46579
@ -12,6 +12,7 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import functools
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@ -25,10 +26,6 @@ from diskimage_builder.block_device.utils import exec_sudo
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
# The order of mounting and unmounting is important.
|
|
||||||
sorted_mount_points = []
|
|
||||||
|
|
||||||
|
|
||||||
class MountPointNode(NodeBase):
|
class MountPointNode(NodeBase):
|
||||||
|
|
||||||
def __init__(self, mount_base, config, state):
|
def __init__(self, mount_base, config, state):
|
||||||
@ -43,12 +40,6 @@ class MountPointNode(NodeBase):
|
|||||||
setattr(self, pname, config[pname])
|
setattr(self, pname, config[pname])
|
||||||
logger.debug("MountPoint created [%s]", self)
|
logger.debug("MountPoint created [%s]", self)
|
||||||
|
|
||||||
def __lt__(self, other):
|
|
||||||
# in words: if the other mount-point has us as it's
|
|
||||||
# parent, we come before it (less than it). e.g.
|
|
||||||
# /var < /var/log < /var/log/foo
|
|
||||||
return other.mount_point.startswith(self.mount_point)
|
|
||||||
|
|
||||||
def get_edges(self):
|
def get_edges(self):
|
||||||
"""Insert all edges
|
"""Insert all edges
|
||||||
|
|
||||||
@ -62,12 +53,20 @@ class MountPointNode(NodeBase):
|
|||||||
edge_from = []
|
edge_from = []
|
||||||
edge_to = []
|
edge_to = []
|
||||||
|
|
||||||
|
# should have been added by __init__...
|
||||||
|
assert 'sorted_mount_points' in self.state
|
||||||
|
sorted_mount_points = self.state['sorted_mount_points']
|
||||||
|
|
||||||
# If we are not first, add our parent in the global dependency
|
# If we are not first, add our parent in the global dependency
|
||||||
# list
|
# list. sorted_mount_points is tuples (mount_point, node_name).
|
||||||
mpi = sorted_mount_points.index(self)
|
# find ourselves in the mount_points, and our parent node
|
||||||
|
# is one before us in node_name list.
|
||||||
|
mount_points = [x[0] for x in sorted_mount_points]
|
||||||
|
node_name = [x[1] for x in sorted_mount_points]
|
||||||
|
mpi = mount_points.index(self.mount_point)
|
||||||
if mpi > 0:
|
if mpi > 0:
|
||||||
dep = sorted_mount_points[mpi - 1]
|
dep = node_name[mpi - 1]
|
||||||
edge_from.append(dep.name)
|
edge_from.append(dep)
|
||||||
|
|
||||||
edge_from.append(self.base)
|
edge_from.append(self.base)
|
||||||
return (edge_from, edge_to)
|
return (edge_from, edge_to)
|
||||||
@ -102,6 +101,30 @@ class MountPointNode(NodeBase):
|
|||||||
self.umount()
|
self.umount()
|
||||||
|
|
||||||
|
|
||||||
|
def cmp_mount_order(this, other):
|
||||||
|
"""Sort comparision function for mount-point sorting
|
||||||
|
|
||||||
|
See if ``this`` comes before ``other`` in mount-order list. In
|
||||||
|
words: if the other mount-point has us as it's parent, we come
|
||||||
|
before it (are less than it). e.g. ``/var < /var/log <
|
||||||
|
/var/log/foo``
|
||||||
|
|
||||||
|
:param this: tuple of mount_point, node name
|
||||||
|
:param other: tuple of mount_point, node name
|
||||||
|
:returns int: cmp value
|
||||||
|
|
||||||
|
"""
|
||||||
|
# sort is only based on the mount_point.
|
||||||
|
this, _ = this
|
||||||
|
other, _ = other
|
||||||
|
if this == other:
|
||||||
|
return 0
|
||||||
|
if other.startswith(this):
|
||||||
|
return -1
|
||||||
|
else:
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
class Mount(PluginBase):
|
class Mount(PluginBase):
|
||||||
def __init__(self, config, defaults, state):
|
def __init__(self, config, defaults, state):
|
||||||
super(Mount, self).__init__()
|
super(Mount, self).__init__()
|
||||||
@ -112,15 +135,23 @@ class Mount(PluginBase):
|
|||||||
self.node = MountPointNode(defaults['mount-base'], config, state)
|
self.node = MountPointNode(defaults['mount-base'], config, state)
|
||||||
|
|
||||||
# save this new node to the global mount-point list and
|
# save this new node to the global mount-point list and
|
||||||
# re-order it.
|
# re-order it to keep it in mount-order. Used in get_edges()
|
||||||
global sorted_mount_points
|
# to ensure we build the mount graph in order
|
||||||
mount_points = [x.mount_point for x in sorted_mount_points]
|
#
|
||||||
|
# note we can't just put the MountPointNode into the state,
|
||||||
|
# because it's not json serialisable and we still dump the
|
||||||
|
# state to json. that's why we have this (mount_point, name)
|
||||||
|
# tuples and sorting trickery
|
||||||
|
sorted_mount_points = state.get('sorted_mount_points', [])
|
||||||
|
mount_points = [mp for mp, name in sorted_mount_points]
|
||||||
if self.node.mount_point in mount_points:
|
if self.node.mount_point in mount_points:
|
||||||
raise BlockDeviceSetupException(
|
raise BlockDeviceSetupException(
|
||||||
"Mount point [%s] specified more than once"
|
"Mount point [%s] specified more than once"
|
||||||
% self.node.mount_point)
|
% self.node.mount_point)
|
||||||
sorted_mount_points.append(self.node)
|
sorted_mount_points.append((self.node.mount_point, self.node.name))
|
||||||
sorted_mount_points.sort()
|
sorted(sorted_mount_points, key=functools.cmp_to_key(cmp_mount_order))
|
||||||
|
# reset the state key to the new list
|
||||||
|
state['sorted_mount_points'] = sorted_mount_points
|
||||||
logger.debug("Ordered mounts now: %s", sorted_mount_points)
|
logger.debug("Ordered mounts now: %s", sorted_mount_points)
|
||||||
|
|
||||||
def get_nodes(self):
|
def get_nodes(self):
|
||||||
|
@ -26,11 +26,7 @@ class TestConfig(TestBase):
|
|||||||
"""Helper for setting up and reading a config"""
|
"""Helper for setting up and reading a config"""
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestConfig, self).setUp()
|
super(TestConfig, self).setUp()
|
||||||
|
# previously we mocked some globals here ...
|
||||||
# reset all globals for each test.
|
|
||||||
# XXX: remove globals :/
|
|
||||||
import diskimage_builder.block_device.level3.mount
|
|
||||||
diskimage_builder.block_device.level3.mount.sorted_mount_points = []
|
|
||||||
|
|
||||||
|
|
||||||
class TestGraphGeneration(TestConfig):
|
class TestGraphGeneration(TestConfig):
|
||||||
|
Loading…
Reference in New Issue
Block a user