diff --git a/actions/add_disk.py b/actions/add_disk.py index bbe7914..b1b3620 100755 --- a/actions/add_disk.py +++ b/actions/add_disk.py @@ -59,9 +59,13 @@ def add_device(request, device_path, bucket=None): def get_devices(): - return [ - os.path.realpath(path) - for path in action_get('osd-devices').split(' ')] + devices = [] + for path in action_get('osd-devices').split(' '): + path = path.strip() + if os.path.isabs(path): + devices.append(path) + + return devices if __name__ == "__main__": diff --git a/hooks/ceph_hooks.py b/hooks/ceph_hooks.py index d71210d..f28ef15 100755 --- a/hooks/ceph_hooks.py +++ b/hooks/ceph_hooks.py @@ -365,12 +365,17 @@ def reformat_osd(): def get_devices(): + devices = [] + if config('osd-devices'): - devices = [ - os.path.realpath(path) - for path in config('osd-devices').split(' ')] - else: - devices = [] + for path in config('osd-devices').split(' '): + path = path.strip() + # Make sure its a device which is specified using an + # absolute path so that the current working directory + # or any relative path under this directory is not used + if os.path.isabs(path): + devices.append(os.path.realpath(path)) + # List storage instances for the 'osd-devices' # store declared for this charm too, and add # their block device paths to the list. diff --git a/unit_tests/test_config.py b/unit_tests/test_config.py index b31a6fa..66f2836 100644 --- a/unit_tests/test_config.py +++ b/unit_tests/test_config.py @@ -72,6 +72,25 @@ class GetDevicesTestCase(test_utils.CharmTestCase): self.test_config.set("osd-devices", "{} {}".format(device1, device2)) self.assertEqual([device1, device2], hooks.get_devices()) + def test_get_devices_extra_spaces(self): + """ + Multiple spaces do not result in additional devices. + """ + device1 = os.path.join(self.tmp_dir, "device1") + device2 = os.path.join(self.tmp_dir, "device2") + self.test_config.set("osd-devices", "{} {}".format(device1, device2)) + self.assertEqual([device1, device2], hooks.get_devices()) + + def test_get_devices_non_absolute_path(self): + """ + Charm does not allow relative paths as this may result in a path + on the root device/within the charm directory. + """ + device1 = os.path.join(self.tmp_dir, "device1") + device2 = "foo" + self.test_config.set("osd-devices", "{} {}".format(device1, device2)) + self.assertEqual([device1], hooks.get_devices()) + def test_get_devices_symlink(self): """ If a symlink is specified in osd-devices, get_devices() resolves