]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: fully replace agent files when writing them
authorAdam King <adking@redhat.com>
Wed, 19 Jan 2022 02:44:54 +0000 (21:44 -0500)
committerAdam King <adking@redhat.com>
Fri, 28 Jan 2022 16:30:20 +0000 (11:30 -0500)
otherwise, if the new file is shorter than the old one
we will end up with a malformed file retaining the end bit
of the old version

Signed-off-by: Adam King <adking@redhat.com>
src/cephadm/cephadm

index 98579a1ac3ba8ad065c911e7058df1fe888fd832..ca1d86063a770f8c351be9880c5fe8613caf1bb9 100755 (executable)
@@ -3720,8 +3720,10 @@ class MgrListener(Thread):
             config = data['config']
             for filename in config:
                 if filename in self.agent.required_files:
-                    with open(os.path.join(self.agent.daemon_dir, filename), 'w') as f:
+                    file_path = os.path.join(self.agent.daemon_dir, filename)
+                    with open(os.open(file_path + '.new', os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
                         f.write(config[filename])
+                        os.rename(file_path + '.new', file_path)
             self.agent.pull_conf_settings()
             self.agent.wakeup()
 
@@ -3781,17 +3783,23 @@ class CephadmAgent():
         # Create the required config files in the daemons dir, with restricted permissions
         for filename in config:
             if filename in self.required_files:
-                with open(os.open(os.path.join(self.daemon_dir, filename), os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
+                file_path = os.path.join(self.daemon_dir, filename)
+                with open(os.open(file_path + '.new', os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
                     f.write(config[filename])
+                    os.rename(file_path + '.new', file_path)
 
-        with open(os.open(os.path.join(self.daemon_dir, 'unit.run'), os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
+        unit_run_path = os.path.join(self.daemon_dir, 'unit.run')
+        with open(os.open(unit_run_path + '.new', os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
             f.write(self.unit_run())
+            os.rename(unit_run_path + '.new', unit_run_path)
 
         meta: Dict[str, Any] = {}
+        meta_file_path = os.path.join(self.daemon_dir, 'unit.meta')
         if 'meta_json' in self.ctx and self.ctx.meta_json:
             meta = json.loads(self.ctx.meta_json) or {}
-        with open(os.open(os.path.join(self.daemon_dir, 'unit.meta'), os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
+        with open(os.open(meta_file_path + '.new', os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
             f.write(json.dumps(meta, indent=4) + '\n')
+            os.rename(meta_file_path + '.new', meta_file_path)
 
         unit_file_path = os.path.join(self.ctx.unit_dir, self.unit_name())
         with open(os.open(unit_file_path + '.new', os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f: