From 42c9b7a62545340eb08207a55bac39905bdd65b2 Mon Sep 17 00:00:00 2001 From: Edward Hope-Morley Date: Mon, 23 Jul 2018 12:46:07 +0100 Subject: [PATCH] Avoid unnecessary rewrites of ceph.conf Charm should avoid writing ceph.conf unless it absolutely needs to since this can clash with other processes that might be reading the file (such as ceph-disk called by udev). Change-Id: I3790b5b16fa1473f1c3271b795b3d32c5e8d2fad Closes-Bug: #1783113 --- hooks/ceph_hooks.py | 6 +++--- unit_tests/test_ceph_hooks.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/hooks/ceph_hooks.py b/hooks/ceph_hooks.py index a41586a4..d1b82d42 100755 --- a/hooks/ceph_hooks.py +++ b/hooks/ceph_hooks.py @@ -394,9 +394,9 @@ def emit_cephconf(upgrading=False): charm_ceph_conf = "/var/lib/charm/{}/ceph.conf".format(service_name()) mkdir(os.path.dirname(charm_ceph_conf), owner=ceph.ceph_user(), group=ceph.ceph_user()) - with open(charm_ceph_conf, 'w') as cephconf: - context = get_ceph_context(upgrading) - cephconf.write(render_template('ceph.conf', context)) + context = get_ceph_context(upgrading) + write_file(charm_ceph_conf, render_template('ceph.conf', context), + ceph.ceph_user(), ceph.ceph_user(), 0o644) install_alternative('ceph.conf', '/etc/ceph/ceph.conf', charm_ceph_conf, 90) diff --git a/unit_tests/test_ceph_hooks.py b/unit_tests/test_ceph_hooks.py index 543d3564..20e20895 100644 --- a/unit_tests/test_ceph_hooks.py +++ b/unit_tests/test_ceph_hooks.py @@ -522,6 +522,27 @@ class CephHooksTestCase(unittest.TestCase): config = {'osd-devices': '/srv/osd', 'osd-format': 'ext4'} self.assertTrue(ceph_hooks.use_short_objects()) + @patch.object(ceph_hooks, 'write_file') + @patch.object(ceph_hooks.ceph, 'ceph_user') + @patch.object(ceph_hooks, 'install_alternative') + @patch.object(ceph_hooks, 'render_template') + @patch.object(ceph_hooks, 'get_ceph_context') + @patch.object(ceph_hooks, 'service_name') + @patch.object(ceph_hooks, 'mkdir') + def test_emit_ceph_conf(self, mock_mkdir, mock_service_name, + mock_get_ceph_context, mock_render_template, + mock_install_alternative, mock_ceph_user, + mock_write_file): + mock_service_name.return_value = 'testsvc' + mock_ceph_user.return_value = 'ceph' + mock_get_ceph_context.return_value = {} + mock_render_template.return_value = "awesome ceph config" + + ceph_hooks.emit_cephconf() + + self.assertTrue(mock_write_file.called) + self.assertTrue(mock_install_alternative.called) + @patch.object(ceph_hooks, 'relation_get') @patch.object(ceph_hooks, 'relation_set')