498 lines
16 KiB
Python
498 lines
16 KiB
Python
from pathlib import Path
|
|
|
|
from . import guidance
|
|
from .config import (
|
|
REMOTE_MODES,
|
|
InstallError,
|
|
address,
|
|
admin_email,
|
|
admin_username,
|
|
certificate,
|
|
comma_separated,
|
|
file_path,
|
|
hosting_defaults,
|
|
hostname,
|
|
number,
|
|
parse_plan,
|
|
port,
|
|
read_ssh_host_key,
|
|
ssh_endpoint,
|
|
ssh_username,
|
|
)
|
|
from .engine import root_file, run
|
|
from .terminal import PlainTerminal, PreviousStepError
|
|
|
|
|
|
def ask_checked(terminal, label, default, validate, *, hint=""):
|
|
while True:
|
|
value = terminal.prompt(label, str(default), hint=hint)
|
|
try:
|
|
return validate(value)
|
|
except (InstallError, OSError) as error:
|
|
terminal.error(error)
|
|
|
|
|
|
def typed_port(value, label, default):
|
|
return port(number(value, label), label, default)
|
|
|
|
|
|
class InteractiveSetup:
|
|
"""Collect and edit one panel and its optional receiver roles."""
|
|
|
|
PROFILES = {
|
|
"full-service": "local",
|
|
"custom": "off",
|
|
"distributed": "remote-install",
|
|
}
|
|
SECTIONS = ("topology", "panel", "admin", "dns", "mail", "hosting", "back")
|
|
|
|
def __init__(self, terminal):
|
|
self.terminal = terminal
|
|
self.answers = {}
|
|
self.profile = "full-service"
|
|
self.data = {}
|
|
|
|
def ask(self, section, field, label, default="", *, validate=str, hint=""):
|
|
answer_key = (section, field)
|
|
previous = self.answers.get(answer_key, default) or default
|
|
value = ask_checked(
|
|
self.terminal, label, previous, validate, hint=hint
|
|
)
|
|
self.answers[answer_key] = value
|
|
return value
|
|
|
|
def topology(self):
|
|
self.terminal.heading("Deployment topology")
|
|
self.terminal.show(
|
|
" Full-service runs the panel, DNS and email on one server.\n"
|
|
" Custom starts with the panel and lets you choose each role.\n"
|
|
" Distributed starts with separate DNS and mail nodes. "
|
|
"Run setup on each panel server."
|
|
)
|
|
profile = self.terminal.choose(
|
|
"Deployment profile", self.PROFILES, self.profile
|
|
)
|
|
for name, hint in (
|
|
("dns", guidance.DNS_ROLE),
|
|
("mail", guidance.MAIL_ROLE),
|
|
):
|
|
default = self.PROFILES[profile]
|
|
if profile == self.profile:
|
|
default = self.data.get(name, {}).get("mode", default)
|
|
mode = self.terminal.choose(
|
|
f"{name.upper()} role",
|
|
("off", "local", "remote", "remote-install"),
|
|
default,
|
|
hint=hint,
|
|
)
|
|
fields = self.data.get(name, {})
|
|
if mode != fields.get("mode"):
|
|
fields = {}
|
|
self.answers = {
|
|
key: value
|
|
for key, value in self.answers.items()
|
|
if key[0] != name
|
|
}
|
|
self.data[name] = fields | {"mode": mode}
|
|
self.profile = profile
|
|
|
|
def panel(self):
|
|
self.terminal.heading("Panel and HTTPS")
|
|
panel = {
|
|
"hostname": self.ask(
|
|
"panel",
|
|
"hostname",
|
|
"Panel hostname",
|
|
validate=lambda value: hostname(value, "panel"),
|
|
hint=guidance.PANEL_HOSTNAME,
|
|
),
|
|
"access": self.terminal.choose(
|
|
"Panel access",
|
|
("acme", "tls", "proxy"),
|
|
self.data.get("panel", {}).get("access", "acme"),
|
|
hint=guidance.PANEL_ACCESS,
|
|
),
|
|
}
|
|
if panel["access"] == "tls":
|
|
panel.update(self.certificates("panel", "panel", remote=False))
|
|
elif panel["access"] == "proxy":
|
|
for field, label, hint in (
|
|
(
|
|
"origin_ip",
|
|
"Private origin address",
|
|
guidance.ORIGIN_ADDRESS,
|
|
),
|
|
("proxy_ip", "Trusted proxy address", guidance.PROXY_ADDRESS),
|
|
):
|
|
panel[field] = self.ask(
|
|
"panel",
|
|
field,
|
|
label,
|
|
validate=lambda value, label=label: address(
|
|
value, label, private=True
|
|
),
|
|
hint=hint,
|
|
)
|
|
panel["origin_port"] = self.ask(
|
|
"panel",
|
|
"origin_port",
|
|
"Origin port",
|
|
8080,
|
|
validate=origin_port,
|
|
)
|
|
self.data["panel"] = panel
|
|
|
|
def admin(self):
|
|
self.terminal.heading("Administrator")
|
|
self.data["admin"] = {
|
|
"username": self.ask(
|
|
"admin",
|
|
"username",
|
|
"Administrator username",
|
|
"operator",
|
|
validate=admin_username,
|
|
),
|
|
"email": self.ask(
|
|
"admin",
|
|
"email",
|
|
"Administrator email",
|
|
validate=admin_email,
|
|
hint=guidance.ADMIN_EMAIL,
|
|
),
|
|
}
|
|
|
|
def certificates(self, section, service, *, remote):
|
|
fields = self.data.get(section, {})
|
|
where = " on the mail node" if remote else " on this server"
|
|
while True:
|
|
paths = {
|
|
field: ask_checked(
|
|
self.terminal,
|
|
label + where,
|
|
fields.get(field, ""),
|
|
lambda value, label=label: file_path(
|
|
value if remote else local_path(value),
|
|
label,
|
|
),
|
|
)
|
|
for field, label in (
|
|
("certificate", "TLS certificate path"),
|
|
("key", "TLS private key path"),
|
|
)
|
|
}
|
|
if remote:
|
|
return paths
|
|
try:
|
|
certificate(paths, service)
|
|
except InstallError as error:
|
|
self.terminal.error(error)
|
|
fields = paths
|
|
else:
|
|
return paths
|
|
|
|
def role_settings(self, name):
|
|
mode = self.data[name]["mode"]
|
|
if mode == "off" or (mode == "local" and name == "dns"):
|
|
return
|
|
self.terminal.heading(
|
|
"DNS receiver" if name == "dns" else "Email service"
|
|
)
|
|
target = {"mode": mode}
|
|
if mode in REMOTE_MODES:
|
|
target.update(self.ssh_settings(name, mode))
|
|
if name == "mail":
|
|
field = "host" if mode == "local" else "mail_hostname"
|
|
optional = mode == "remote"
|
|
default = self.data["panel"]["hostname"] if mode == "local" else ""
|
|
target[field] = self.ask(
|
|
name,
|
|
field,
|
|
"Public mail hostname",
|
|
"none" if optional else default,
|
|
validate=lambda value: (
|
|
""
|
|
if optional and value.lower() == "none"
|
|
else hostname(value, "mail")
|
|
),
|
|
hint=guidance.MAIL_HOSTNAME,
|
|
)
|
|
if not optional:
|
|
target["tls"] = self.terminal.choose(
|
|
"Mail TLS",
|
|
("acme", "provided"),
|
|
self.data[name].get(
|
|
"tls",
|
|
"acme"
|
|
if self.data["panel"]["access"] == "acme"
|
|
else "provided",
|
|
),
|
|
hint=guidance.MAIL_TLS,
|
|
)
|
|
if target["tls"] == "provided":
|
|
target.update(
|
|
self.certificates(
|
|
name, "mail", remote=mode == "remote-install"
|
|
)
|
|
)
|
|
self.data[name] = target
|
|
|
|
def ssh_settings(self, name, mode):
|
|
target = {
|
|
"host": self.ask(
|
|
name,
|
|
"host",
|
|
f"{name.upper()} node SSH address",
|
|
validate=lambda value: ssh_endpoint(value, name),
|
|
hint=guidance.SSH_ENDPOINT,
|
|
),
|
|
"port": self.ask(
|
|
name,
|
|
"port",
|
|
"SSH port",
|
|
22,
|
|
validate=lambda value: typed_port(
|
|
value, f"the {name} node", 22
|
|
),
|
|
),
|
|
"host_key": self.ask(
|
|
name,
|
|
"host_key",
|
|
"Verified destination SSH host public key file",
|
|
validate=lambda value: verified_key_path(value, name),
|
|
hint=guidance.SSH_HOST_KEY,
|
|
),
|
|
}
|
|
if mode == "remote-install":
|
|
target["ssh_user"] = self.ask(
|
|
name,
|
|
"ssh_user",
|
|
"SSH administrator username",
|
|
validate=lambda value: ssh_username(value, name),
|
|
)
|
|
target["ssh_identity"] = self.ask(
|
|
name,
|
|
"ssh_identity",
|
|
"SSH administrator private key file",
|
|
validate=private_key_path,
|
|
hint=guidance.SSH_IDENTITY,
|
|
)
|
|
return target
|
|
|
|
def hosting(self):
|
|
self.terminal.heading("Hosting and DNS defaults")
|
|
defaults = self.data.setdefault("hosting", {})
|
|
for field, label, hint in guidance.HOSTING_FIELDS:
|
|
if field == "nameservers" and self.data["dns"]["mode"] == "off":
|
|
defaults.pop(field, None)
|
|
continue
|
|
if (
|
|
field == "mail_sending_addresses"
|
|
and not self.mail_has_hostname()
|
|
):
|
|
defaults.pop(field, None)
|
|
continue
|
|
defaults[field] = ask_checked(
|
|
self.terminal,
|
|
f"{label} (comma-separated, or none)",
|
|
", ".join(defaults.get(field, [])) or "none",
|
|
lambda value, field=field: validated_list(field, value),
|
|
hint=hint,
|
|
)
|
|
|
|
def steps(self):
|
|
stages = [self.topology, self.panel, self.admin]
|
|
if self.data.get("dns", {}).get("mode") in REMOTE_MODES:
|
|
stages.append(self.dns)
|
|
if self.data.get("mail", {}).get("mode", "off") != "off":
|
|
stages.append(self.mail)
|
|
return [*stages, self.hosting]
|
|
|
|
def dns(self):
|
|
self.role_settings("dns")
|
|
|
|
def mail(self):
|
|
self.role_settings("mail")
|
|
|
|
def collect(self):
|
|
stages = self.steps()
|
|
index = 0
|
|
while index < len(stages):
|
|
self.terminal.step = (
|
|
(index + 1, len(stages) + 1) if index else None
|
|
)
|
|
self.terminal.can_go_back = index > 0
|
|
try:
|
|
stages[index]()
|
|
except PreviousStepError:
|
|
index -= 1
|
|
else:
|
|
if index == 0:
|
|
stages = self.steps()
|
|
index += 1
|
|
self.terminal.step = (len(stages) + 1, len(stages) + 1)
|
|
self.terminal.can_go_back = False
|
|
|
|
def mail_has_hostname(self):
|
|
mail = self.data["mail"]
|
|
return mail["mode"] == "local" or bool(mail.get("mail_hostname"))
|
|
|
|
def edit(self):
|
|
section = self.terminal.choose(
|
|
"Section to edit", self.SECTIONS, "back"
|
|
)
|
|
if section == "topology":
|
|
previous = (self.data["dns"]["mode"], self.data["mail"]["mode"])
|
|
self.topology()
|
|
for name, old_mode in zip(("dns", "mail"), previous, strict=True):
|
|
if self.data[name]["mode"] != old_mode:
|
|
self.role_settings(name)
|
|
self.hosting()
|
|
elif section in {"dns", "mail"}:
|
|
self.role_settings(section)
|
|
if section == "mail":
|
|
self.hosting()
|
|
elif section != "back":
|
|
getattr(self, section)()
|
|
|
|
|
|
def origin_port(value):
|
|
number = typed_port(value, "the origin", 8080)
|
|
if number in {80, 443}:
|
|
raise InstallError("Use a separate origin port for the reverse proxy.")
|
|
return number
|
|
|
|
|
|
def verified_key_path(value, name):
|
|
path = local_path(value)
|
|
read_ssh_host_key(path, name)
|
|
return path
|
|
|
|
|
|
def private_key_path(value):
|
|
path = file_path(local_path(value), "the SSH private key")
|
|
try:
|
|
root_file(Path(path), 0o600)
|
|
except OSError:
|
|
raise InstallError(
|
|
"Read an existing root-owned SSH private key with mode 0600."
|
|
) from None
|
|
return path
|
|
|
|
|
|
def local_path(value):
|
|
try:
|
|
return str(Path(value).expanduser())
|
|
except RuntimeError:
|
|
raise InstallError("Enter the absolute path on this server.") from None
|
|
|
|
|
|
def validated_list(field, raw):
|
|
values = [] if raw.lower() == "none" else comma_separated(raw)
|
|
return list(getattr(hosting_defaults({field: values}), field))
|
|
|
|
|
|
def review_plan(plan, terminal):
|
|
terminal.heading("Review installation")
|
|
terminal.show(f" Panel: https://{plan.hostname}/ ({plan.access})")
|
|
terminal.show(
|
|
f" Administrator: {plan.admin_username} ({plan.admin_email})"
|
|
)
|
|
if plan.access == "proxy":
|
|
terminal.show(
|
|
f" Private origin: {plan.origin_ip}, port {plan.origin_port}. "
|
|
f"Trusted proxy: {plan.proxy_ip}."
|
|
)
|
|
for name in ("dns", "mail"):
|
|
target = getattr(plan, name)
|
|
terminal.show(f" {name.upper()}: {target.mode}")
|
|
if target.mode in REMOTE_MODES:
|
|
terminal.show(
|
|
f" SSH destination: {target.host}, port {target.port}"
|
|
)
|
|
if target.mail_hostname:
|
|
terminal.show(f" Public mail hostname: {target.mail_hostname}")
|
|
if name == "mail" and target.mode in {"local", "remote-install"}:
|
|
terminal.show(f" TLS: {target.tls}")
|
|
for field, label, _ in guidance.HOSTING_FIELDS:
|
|
values = getattr(plan.hosting, field)
|
|
if values:
|
|
terminal.show(f" {label}: {', '.join(values)}")
|
|
if plan.access == "acme" or plan.mail.tls == "acme":
|
|
terminal.show(
|
|
" Let's Encrypt needs public DNS and inbound HTTP on port 80."
|
|
)
|
|
|
|
|
|
def check_remote_role(name, target, terminal):
|
|
if target.mode in REMOTE_MODES:
|
|
key = read_ssh_host_key(target.host_key, name)
|
|
run(
|
|
"ssh-keygen",
|
|
"-l",
|
|
"-f",
|
|
"-",
|
|
source=f"{key}\n".encode("ascii"),
|
|
label=f"Validate the pinned {name} SSH host public key",
|
|
)
|
|
if target.mode == "remote-install":
|
|
from .remote import ssh_bootstrap
|
|
|
|
terminal.status(
|
|
f"Checking {name.upper()} installation access via SSH."
|
|
)
|
|
try:
|
|
ssh_bootstrap(target, {}, check=True)
|
|
except (InstallError, OSError) as error:
|
|
raise InstallError(
|
|
f"{error} Verify the destination host public key, the "
|
|
"administrator's private key and permission for sudo -n."
|
|
) from error
|
|
|
|
|
|
def interactive_plan(terminal=None):
|
|
terminal = terminal or PlainTerminal()
|
|
setup = InteractiveSetup(terminal)
|
|
setup.collect()
|
|
while True:
|
|
try:
|
|
plan = parse_plan(setup.data)
|
|
except InstallError as error:
|
|
terminal.error(error)
|
|
if (
|
|
terminal.choose(
|
|
"Installation plan", ("edit", "cancel"), "edit"
|
|
)
|
|
== "cancel"
|
|
):
|
|
return None
|
|
setup.edit()
|
|
continue
|
|
review_plan(plan, terminal)
|
|
decision = terminal.choose(
|
|
"Next step", ("install", "edit", "cancel"), "cancel"
|
|
)
|
|
if decision == "cancel":
|
|
return None
|
|
if decision == "edit":
|
|
setup.edit()
|
|
continue
|
|
for name in ("dns", "mail"):
|
|
try:
|
|
check_remote_role(name, getattr(plan, name), terminal)
|
|
except InstallError as error:
|
|
terminal.error(error)
|
|
decision = terminal.choose(
|
|
f"{name.upper()} settings",
|
|
("edit", "review", "cancel"),
|
|
"edit",
|
|
)
|
|
if decision == "cancel":
|
|
return None
|
|
if decision == "edit":
|
|
setup.role_settings(name)
|
|
if name == "mail":
|
|
setup.hosting()
|
|
break
|
|
else:
|
|
return plan
|