337 lines
12 KiB
Python
337 lines
12 KiB
Python
import argparse
|
|
import json
|
|
import os
|
|
import sys
|
|
from getpass import getpass
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from .config import (
|
|
REMOTE_MODES,
|
|
InstallError,
|
|
check_password,
|
|
parse_plan,
|
|
platform_settings,
|
|
read_password,
|
|
read_plan,
|
|
)
|
|
from .engine import Installer
|
|
|
|
|
|
def prompt(label, default=""):
|
|
value = input(f"{label}{f' [{default}]' if default else ''}: ").strip()
|
|
return value or default
|
|
|
|
|
|
def number(value, label):
|
|
try:
|
|
return int(value)
|
|
except ValueError:
|
|
raise InstallError(f"Enter a numeric port for {label}.") from None
|
|
|
|
|
|
def comma_separated(values):
|
|
return [value.strip() for value in values.split(",") if value.strip()]
|
|
|
|
|
|
def interactive_plan(previous=None):
|
|
if previous is None:
|
|
preset = prompt(
|
|
"Installation profile (full-service or custom)", "full-service"
|
|
)
|
|
if preset not in {"full-service", "custom"}:
|
|
raise InstallError("Choose full-service or custom.")
|
|
defaults = {}
|
|
else:
|
|
preset = "custom"
|
|
defaults = export_parameters(previous, None)
|
|
|
|
def ask(label, section, name, fallback=""):
|
|
return prompt(
|
|
label, str(defaults.get(section, {}).get(name, fallback))
|
|
)
|
|
|
|
full_service = preset == "full-service"
|
|
panel = {"hostname": ask("Panel hostname", "panel", "hostname")}
|
|
access = panel["access"] = ask(
|
|
"Panel access (tls, acme, or proxy)",
|
|
"panel",
|
|
"access",
|
|
"acme" if full_service else "tls",
|
|
)
|
|
if access == "tls":
|
|
panel["certificate"] = ask(
|
|
"Panel certificate path", "panel", "certificate"
|
|
)
|
|
panel["key"] = ask("Panel private key path", "panel", "key")
|
|
elif access == "proxy":
|
|
panel["origin_ip"] = ask(
|
|
"Private origin address", "panel", "origin_ip"
|
|
)
|
|
panel["proxy_ip"] = ask("Trusted proxy address", "panel", "proxy_ip")
|
|
panel["origin_port"] = number(
|
|
ask("Origin port", "panel", "origin_port", "8080"), "the origin"
|
|
)
|
|
data = {
|
|
"panel": panel,
|
|
"admin": {
|
|
"username": ask(
|
|
"Administrator username", "admin", "username", "operator"
|
|
),
|
|
"email": ask("Administrator email", "admin", "email"),
|
|
},
|
|
}
|
|
for name in ("dns", "mail"):
|
|
mode = ask(
|
|
f"{name.upper()} role (off, local, remote, or remote-install)",
|
|
name,
|
|
"mode",
|
|
"local" if full_service else "off",
|
|
)
|
|
target = data[name] = {"mode": mode}
|
|
if mode in REMOTE_MODES:
|
|
target["host"] = ask(
|
|
f"{name.upper()} node SSH hostname or address", name, "host"
|
|
)
|
|
target["port"] = number(
|
|
ask("SSH port", name, "port", "22"), f"the {name} node"
|
|
)
|
|
target["host_key"] = ask(
|
|
"Verified SSH host public key file", name, "host_key"
|
|
)
|
|
if mode == "remote-install":
|
|
target["ssh_user"] = ask(
|
|
"SSH administrator username", name, "ssh_user"
|
|
)
|
|
target["ssh_identity"] = ask(
|
|
"SSH administrator private key file", name, "ssh_identity"
|
|
)
|
|
if name == "mail" and mode in {"local", "remote-install"}:
|
|
field = "host" if mode == "local" else "mail_hostname"
|
|
target[field] = ask(
|
|
"Public mail hostname",
|
|
name,
|
|
field,
|
|
panel["hostname"] if mode == "local" else "",
|
|
)
|
|
target["tls"] = ask(
|
|
"Mail TLS (provided or acme)",
|
|
name,
|
|
"tls",
|
|
"acme" if access == "acme" and mode == "local" else "provided",
|
|
)
|
|
if target["tls"] == "provided":
|
|
target["certificate"] = ask(
|
|
"Certificate path on the mail host", name, "certificate"
|
|
)
|
|
target["key"] = ask(
|
|
"Private key path on the mail host", name, "key"
|
|
)
|
|
hosting = data["hosting"] = {}
|
|
for name, label in (
|
|
("public_addresses", "IP addresses for hosted websites"),
|
|
("nameservers", "Nameservers for new DNS zones"),
|
|
("mail_sending_addresses", "Public sending IPs for hosted email"),
|
|
):
|
|
if name == "nameservers" and data["dns"]["mode"] == "off":
|
|
continue
|
|
if name == "mail_sending_addresses" and data["mail"]["mode"] not in {
|
|
"local",
|
|
"remote-install",
|
|
}:
|
|
continue
|
|
previous_values = defaults.get("hosting", {}).get(name, [])
|
|
value = prompt(
|
|
f"{label} (comma-separated, or none)",
|
|
", ".join(previous_values) or "none",
|
|
)
|
|
hosting[name] = [] if value == "none" else comma_separated(value)
|
|
return parse_plan(data)
|
|
|
|
|
|
def get_password(path):
|
|
if path:
|
|
return read_password(path)
|
|
if not sys.stdin.isatty():
|
|
raise InstallError(
|
|
"Use a terminal or provide a private password file."
|
|
)
|
|
while True:
|
|
try:
|
|
password = check_password(getpass("Administrator password: "))
|
|
if getpass("Repeat the administrator password: ") == password:
|
|
return password
|
|
raise InstallError("The administrator passwords differ.")
|
|
except InstallError as error:
|
|
print(error, file=sys.stderr)
|
|
|
|
|
|
def export_parameters(plan, password_path):
|
|
panel = {"hostname": plan.hostname, "access": plan.access}
|
|
if plan.access == "tls":
|
|
panel.update(certificate=plan.certificate, key=plan.key)
|
|
elif plan.access == "proxy":
|
|
panel.update(
|
|
origin_ip=plan.origin_ip,
|
|
origin_port=plan.origin_port,
|
|
proxy_ip=plan.proxy_ip,
|
|
)
|
|
admin = {"username": plan.admin_username, "email": plan.admin_email}
|
|
if password_path:
|
|
admin["password_file"] = password_path
|
|
parameters = {
|
|
"panel": panel,
|
|
"admin": admin,
|
|
"hosting": plan.hosting.parameters(),
|
|
}
|
|
for name, role in (("dns", plan.dns), ("mail", plan.mail)):
|
|
fields = parameters[name] = {"mode": role.mode}
|
|
if role.mode in REMOTE_MODES:
|
|
fields.update(
|
|
host=role.host, host_key=role.host_key, port=role.port
|
|
)
|
|
if role.mode == "remote-install":
|
|
fields.update(
|
|
ssh_user=role.ssh_user, ssh_identity=role.ssh_identity
|
|
)
|
|
if name == "mail" and role.mode in {"local", "remote-install"}:
|
|
host_key = "host" if role.mode == "local" else "mail_hostname"
|
|
fields[host_key] = role.mail_hostname
|
|
fields["tls"] = role.tls
|
|
if role.tls == "provided":
|
|
fields.update(certificate=role.certificate, key=role.key)
|
|
return parameters
|
|
|
|
|
|
def save_interactive_plan(plan, password_path):
|
|
destination = prompt("Save settings to a TOML path (Enter to skip)")
|
|
if not destination:
|
|
return
|
|
path = Path(destination).expanduser()
|
|
exists = os.path.lexists(path)
|
|
if exists and prompt(f"Replace {path} (yes or no)", "no") != "yes":
|
|
return
|
|
with TemporaryDirectory(dir=path.parent) as directory:
|
|
temporary = Path(directory) / "installation.toml"
|
|
with temporary.open("w", encoding="utf-8") as output:
|
|
temporary.chmod(0o600)
|
|
for section, fields in export_parameters(
|
|
plan, password_path
|
|
).items():
|
|
output.write(f"[{section}]\n")
|
|
for name, value in fields.items():
|
|
encoded = json.dumps(value, ensure_ascii=False).replace(
|
|
"\x7f", "\\u007f"
|
|
)
|
|
output.write(f"{name} = {encoded}\n")
|
|
output.write("\n")
|
|
if exists:
|
|
os.replace(temporary, path)
|
|
else:
|
|
os.link(temporary, path)
|
|
print(f"Saved installation settings to {path}.")
|
|
if not password_path:
|
|
print(
|
|
"Provide a private administrator password file "
|
|
"when reusing the plan."
|
|
)
|
|
|
|
|
|
def main(argv=None):
|
|
parser = argparse.ArgumentParser(
|
|
description="Install Coulobre Panel on a fresh server."
|
|
)
|
|
modes = parser.add_mutually_exclusive_group()
|
|
modes.add_argument(
|
|
"--interactive",
|
|
action="store_true",
|
|
help="Use guided prompts (the default).",
|
|
)
|
|
modes.add_argument(
|
|
"--config",
|
|
metavar="FILE",
|
|
help="Read installation parameters from TOML.",
|
|
)
|
|
modes.add_argument(
|
|
"--web", action="store_true", help="Open the loopback setup wizard."
|
|
)
|
|
parser.add_argument(
|
|
"--admin-password-file",
|
|
help="Read the administrator password from a private file.",
|
|
)
|
|
parser.add_argument(
|
|
"--port",
|
|
type=int,
|
|
default=8765,
|
|
help="Loopback port for the web wizard.",
|
|
)
|
|
parser.add_argument(
|
|
"--check",
|
|
action="store_true",
|
|
help="Validate parameters without installing.",
|
|
)
|
|
options = parser.parse_args(argv)
|
|
if options.check and not options.config:
|
|
parser.error("--check requires --config.")
|
|
if options.port != 8765 and not options.web:
|
|
parser.error("--port requires --web.")
|
|
try:
|
|
platform_settings()
|
|
if not options.check and os.geteuid() != 0:
|
|
raise InstallError("Run the installer as root.")
|
|
if options.web:
|
|
from .web import serve_wizard
|
|
|
|
if not options.admin_password_file:
|
|
parser.error("--web requires --admin-password-file.")
|
|
return serve_wizard(
|
|
read_password(options.admin_password_file), options.port
|
|
)
|
|
if options.config:
|
|
data = read_plan(options.config)
|
|
plan = parse_plan(data)
|
|
password_path = options.admin_password_file or data["admin"].get(
|
|
"password_file"
|
|
)
|
|
if not password_path:
|
|
raise InstallError(
|
|
"Specify the administrator password file in [admin]."
|
|
)
|
|
password = read_password(password_path)
|
|
else:
|
|
plan = None
|
|
while True:
|
|
plan = interactive_plan(plan)
|
|
print(plan.description(), flush=True)
|
|
if plan.access == "acme" or plan.mail.tls == "acme":
|
|
print(
|
|
"Let's Encrypt requires public DNS and inbound HTTP "
|
|
"for each selected hostname."
|
|
)
|
|
decision = prompt(
|
|
"Install with these settings (yes, edit, or no)", "no"
|
|
).lower()
|
|
if decision != "edit":
|
|
break
|
|
if decision != "yes":
|
|
return 0
|
|
save_interactive_plan(plan, options.admin_password_file)
|
|
password = get_password(options.admin_password_file)
|
|
if options.check:
|
|
print(plan.description())
|
|
return 0
|
|
installer = Installer(
|
|
plan,
|
|
password,
|
|
lambda message: print(message, flush=True),
|
|
heartbeat=sys.stdout.isatty(),
|
|
)
|
|
installer.install()
|
|
return 0
|
|
except (InstallError, OSError, ValueError) as error:
|
|
print(f"Coulobre installer: {error}", file=sys.stderr)
|
|
return 1
|
|
except (EOFError, KeyboardInterrupt):
|
|
print("\nInstallation input was interrupted.", file=sys.stderr)
|
|
return 1
|