diff --git a/actions/add_disk.py b/actions/add_disk.py index bbe79148..b1b36202 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 0b1b1718..e2bcaeb5 100755 --- a/hooks/ceph_hooks.py +++ b/hooks/ceph_hooks.py @@ -213,7 +213,7 @@ def use_short_objects(): if config('osd-format') in ('ext4'): return True for device in config('osd-devices'): - if not device.startswith('/dev'): + if device and not device.startswith('/dev'): # TODO: determine format of directory based # OSD location return True @@ -412,12 +412,15 @@ 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 diff --git a/unit_tests/test_config.py b/unit_tests/test_config.py index b4a85722..c3ae347e 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