+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