"""Collect a cluster inventory for the automated installer.""" from . import guidance from .cluster import SCOPE, ClusterInstaller, parse_cluster from .config import ( InstallError, comma_separated, file_path, hostname, read_password, ) from .interactive import ( InteractiveSetup, ask_checked, local_path, validated_list, ) def panel_name(value): if not SCOPE.fullmatch(value): raise InstallError("Use 1 to 12 lowercase letters, digits or hyphens.") return value def count(value): if value not in {str(number) for number in range(2, 13)}: raise InstallError("Enter a panel count from 2 to 12.") return int(value) def collect_receiver(terminal, name, previous): terminal.heading(f"Shared {name.upper()} receiver") selected = terminal.choose( "Configure a shared receiver", ("yes", "no"), "yes" if previous else "no", hint="Enter a dedicated host. DNS and mail can share the same host.", ) if selected == "no": return {} setup = InteractiveSetup(terminal) setup.data = { name: previous | {"mode": "remote-install"}, "panel": {"access": "acme"}, } setup.answers.update( ((name, field), value) for field, value in previous.items() ) setup.role_settings(name) result = setup.data[name] result.pop("mode") if name == "mail": def sending(value): addresses = validated_list("mail_sending_addresses", value) if not addresses: raise InstallError( "Enter at least one public sending address." ) return addresses result["sending_addresses"] = ask_checked( terminal, "Public SMTP sending IPs", ", ".join(previous.get("sending_addresses", ())), sending, hint=guidance.HOSTING_FIELDS[-1][2], ) return result def collect_panel(terminal, index, previous, receivers): terminal.heading( "Local panel" if index == 0 else f"Remote panel {index + 1}" ) name = ask_checked( terminal, "Panel identifier", previous.get("name", f"srv{index + 1}"), panel_name, hint="Use a stable name for this panel's receiver access.", ) setup = InteractiveSetup(terminal) setup.data = { key: previous[key] for key in ("panel", "admin") if key in previous } for section in ("panel", "admin"): setup.answers.update( ((section, key), value) for key, value in previous.get(section, {}).items() ) if transport := previous.get("ssh"): setup.answers.update( ((name, key), value) for key, value in transport.items() ) ssh = setup.ssh_settings(name, "remote-install") if index else None setup.panel(remote=bool(index)) setup.admin() terminal.heading(f"Hosting on {name}") dns = terminal.choose( "DNS on this panel", ("shared", "local", "off") if "dns" in receivers else ("local", "off"), previous.get("dns") if previous.get("dns") in {"local", "off"} or ("dns" in receivers and previous.get("dns") == "shared") else ("shared" if "dns" in receivers else "local"), hint=guidance.DNS_ROLE if "dns" not in receivers else "Shared DNS publishes zones on the receiver.", ) hosting = {} for field, label, hint in guidance.HOSTING_FIELDS[:2]: if field == "nameservers" and dns == "off": continue hosting[field] = ask_checked( terminal, label, ", ".join(previous.get("hosting", {}).get(field, ())) or "none", lambda value, field=field: validated_list(field, value), hint=hint, ) result = { "name": name, "panel": setup.data["panel"], "admin": setup.data["admin"], "hosting": hosting, "dns": dns, } if ssh: result["ssh"] = ssh if "mail" in receivers or dns == "shared": result["domains"] = ask_checked( terminal, "Assigned hosted domains", ", ".join(previous.get("domains", ())) or "none", assigned_domains, hint=( "Enter comma-separated domains or choose none to reserve " "receiver access without permitting domain changes." ), ) return result def assigned_domains(value): if value.lower() == "none": return [] domains = [ hostname(item, "hosted domain") for item in comma_separated(value) ] if not 1 <= len(domains) <= 200: raise InstallError("Enter up to 200 hosted domains or none.") return domains def edit_inventory(terminal, draft): terminal.heading("Edit cluster") changed = terminal.choose( "Section", ("dns", "mail", "panels", "back"), "back" ) if changed in {"dns", "mail"}: receiver = collect_receiver(terminal, changed, draft.get(changed, {})) if receiver: draft[changed] = receiver else: draft.pop(changed, None) if not receiver and changed == "dns": for entry in draft["panels"]: if entry["dns"] == "shared": entry["dns"] = "local" for panel in draft["panels"]: if ("mail" in draft or panel["dns"] == "shared") and not panel.get( "domains" ): terminal.heading(f"Receiver access for {panel['name']}") panel["domains"] = ask_checked( terminal, "Assigned hosted domains", "none", assigned_domains, ) elif changed == "panels": choices = tuple( str(index) for index in range(1, len(draft["panels"]) + 1) ) for prompt, entry in zip(choices, draft["panels"], strict=True): terminal.show( f"{prompt}. {entry['name']} ({entry['panel']['hostname']})" ) panel = terminal.choose("Panel", (*choices, "back"), "back") if panel != "back": index = int(panel) - 1 draft["panels"][index] = collect_panel( terminal, index, draft["panels"][index], draft ) def collect_cluster(terminal): terminal.step = None terminal.heading("Cluster topology") terminal.show( "Start on the first panel server. Provide SSH access from this server " "to the other panels and any shared receivers." ) size = ask_checked(terminal, "Number of panel servers", 3, count) draft = {} for name in ("dns", "mail"): receiver = collect_receiver(terminal, name, {}) if receiver: draft[name] = receiver draft["panels"] = [ collect_panel(terminal, index, {}, draft) for index in range(size) ] data = {"cluster": draft} while True: terminal.heading("Review cluster") try: cluster = parse_cluster(data) except InstallError as error: terminal.error(error) choices = ("edit", "cancel") else: for item in cluster.summary(): terminal.show(item) choices = ("install", "edit", "cancel") chosen = terminal.choose("Next step", choices, "cancel") if chosen == "cancel": return None if chosen == "install": terminal.heading("Checking cluster destinations") try: ClusterInstaller( cluster, dict.fromkeys((p.name for p in cluster.panels), ""), terminal.status, ).check() except (InstallError, OSError, ValueError) as error: terminal.error(error) chosen = terminal.choose( "Cluster check failed", ("edit", "review", "cancel"), "edit", ) else: return cluster, data if chosen == "cancel": return None if chosen == "edit": edit_inventory(terminal, draft) def collect_password(terminal, entry, *, default="", masked_password): terminal.heading(f"Administrator for {entry['name']}") candidate = terminal.prompt( "Administrator password file", default, hint=( "Use a root-owned 0600 file on this server or leave blank " "to type the password." ), ) while candidate: try: path = file_path( local_path(candidate), "the administrator password file" ) password = read_password(path) except InstallError as error: terminal.error(error) candidate = terminal.prompt("Administrator password file") else: entry["admin"]["password_file"] = path return password return masked_password(terminal)