]> mj.ucw.cz Git - pynsc.git/blobdiff - nsconfig/daemon/__init__.py
More daemon configuration
[pynsc.git] / nsconfig / daemon / __init__.py
index a981a0c22dbc676f5de931849d04b3d5ebc6fbdb..914056100d36340a051788049b2bd7696d78f785 100644 (file)
@@ -1,16 +1,63 @@
+from io import StringIO
 from pathlib import Path
 from typing import TextIO
+import subprocess
 import sys
 
-from nsconfig.core import Nsc
+from nsconfig.core import Nsc, NscZone
 
 
 class NscDaemon:
     nsc: Nsc
-    config_path: Path
+
+    def __init__(self) -> None:
+        pass
 
     def setup(self, nsc: Nsc) -> None:
         self.nsc = nsc
 
     def dump_config(self, file: TextIO = sys.stdout) -> None:
         pass
+
+    def write_config(self) -> None:
+        pass
+
+    def reload_zone(self, z: NscZone) -> None:
+        pass
+
+    def reload_daemon(self) -> None:
+        pass
+
+    def _install_config(self, path: Path, new_contents: str) -> bool:
+        try:
+            old_new_contents = path.read_text()
+        except FileNotFoundError:
+            old_new_contents = None
+        if new_contents == old_new_contents:
+            return False
+        else:
+            new_path = Path(str(path) + '.new')
+            with open(new_path, 'w') as f:
+                f.write(new_contents)
+            new_path.replace(path)
+            return True
+
+    def _write_config(self, config_path: Path) -> bool:
+        string_stream = StringIO()
+        self.dump_config(string_stream)
+        if self._install_config(config_path, string_stream.getvalue()):
+            print('Wrote new daemon configuration')
+            return True
+        else:
+            print('Daemon configuration not changed')
+            return False
+
+    def _run_command(self, argv, **kwargs) -> None:
+        res = subprocess.run(argv, **kwargs)
+        if res.returncode > 0:
+            print(f'Command failed: {argv}')
+            sys.exit(1)
+
+
+class NscDaemonNull(NscDaemon):
+    pass