diff --git a/sambacc/commands/initialize.py b/sambacc/commands/initialize.py index 6afceade..06cc034c 100644 --- a/sambacc/commands/initialize.py +++ b/sambacc/commands/initialize.py @@ -34,11 +34,19 @@ @setup_steps.command("nsswitch") def _import_nsswitch(ctx: Context) -> None: # should nsswitch validation/edit be conditional only on ads? - nss = nsswitch.NameServiceSwitchLoader("/etc/nsswitch.conf") - nss.read() - if not nss.winbind_enabled(): - nss.ensure_winbind_enabled() - nss.write() + paths = ["/etc/nsswitch.conf", "/usr/etc/nsswitch.conf"] + for path in paths: + nss = nsswitch.NameServiceSwitchLoader(path) + try: + nss.read() + if not nss.winbind_enabled(): + nss.ensure_winbind_enabled() + nss.write("/etc/nsswitch.conf") + return + except FileNotFoundError: + pass + + raise FileNotFoundError(f"Failed to open {' or '.join(paths)}") @setup_steps.command("smb_ctdb") diff --git a/sambacc/textfile.py b/sambacc/textfile.py index 1b67613b..b602cf20 100644 --- a/sambacc/textfile.py +++ b/sambacc/textfile.py @@ -28,11 +28,14 @@ def read(self) -> None: with open(self.path) as f: self.readfp(f) - def write(self) -> None: - tpath = self._tmp_path(self.path) + def write(self, alternate_path: typing.Optional[str] = None) -> None: + path = self.path + if alternate_path: + path = alternate_path + tpath = self._tmp_path(path) with open(tpath, "w") as f: self.writefp(f) - os.rename(tpath, self.path) + os.rename(tpath, path) def _tmp_path(self, path: str) -> str: # for later: make this smarter