2026-11-22 16:39:27 +01:00
|
|
|
import argparse
|
2026-11-22 21:27:29 +01:00
|
|
|
import json
|
2026-11-22 16:39:27 +01:00
|
|
|
import os
|
|
|
|
|
import sys
|
2026-11-22 21:27:29 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
from tempfile import TemporaryDirectory
|
2026-11-22 16:39:27 +01:00
|
|
|
|
2026-11-26 22:10:34 +01:00
|
|
|
from .cluster import (
|
|
|
|
|
Cluster,
|
|
|
|
|
ClusterInstaller,
|
|
|
|
|
cluster_passwords,
|
|
|
|
|
parse_cluster,
|
|
|
|
|
)
|
|
|
|
|
from .cluster_wizard import collect_cluster, collect_password
|
2026-11-22 16:39:27 +01:00
|
|
|
from .config import (
|
2026-11-22 17:51:05 +01:00
|
|
|
REMOTE_MODES,
|
2026-11-22 16:39:27 +01:00
|
|
|
InstallError,
|
|
|
|
|
check_password,
|
|
|
|
|
parse_plan,
|
|
|
|
|
platform_settings,
|
|
|
|
|
read_password,
|
|
|
|
|
read_plan,
|
|
|
|
|
)
|
|
|
|
|
from .engine import Installer
|
2026-11-26 22:10:34 +01:00
|
|
|
from .interactive import ClusterRequestedError, interactive_plan, local_path
|
2026-11-26 21:53:51 +01:00
|
|
|
from .terminal import PlainTerminal, configure_terminal
|
2026-11-22 16:39:27 +01:00
|
|
|
|
|
|
|
|
|
2026-11-26 21:53:51 +01:00
|
|
|
def get_password(terminal):
|
2026-11-22 21:27:29 +01:00
|
|
|
while True:
|
|
|
|
|
try:
|
2026-11-26 21:53:51 +01:00
|
|
|
password = check_password(
|
|
|
|
|
terminal.password("Administrator password")
|
|
|
|
|
)
|
|
|
|
|
if (
|
|
|
|
|
terminal.password("Repeat the administrator password")
|
|
|
|
|
== password
|
|
|
|
|
):
|
2026-11-22 21:27:29 +01:00
|
|
|
return password
|
|
|
|
|
raise InstallError("The administrator passwords differ.")
|
|
|
|
|
except InstallError as error:
|
2026-11-26 21:53:51 +01:00
|
|
|
if not sys.stdin.isatty():
|
|
|
|
|
raise
|
|
|
|
|
terminal.error(error)
|
2026-11-22 21:27:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2026-11-22 21:43:37 +01:00
|
|
|
parameters = {
|
|
|
|
|
"panel": panel,
|
|
|
|
|
"admin": admin,
|
|
|
|
|
"hosting": plan.hosting.parameters(),
|
|
|
|
|
}
|
2026-12-04 13:56:46 +01:00
|
|
|
if plan.smtp:
|
|
|
|
|
parameters["smtp"] = plan.record()["smtp"]
|
2026-11-22 21:27:29 +01:00
|
|
|
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
|
|
|
|
|
)
|
2026-11-24 13:43:58 +01:00
|
|
|
if name == "mail" and role.mail_hostname:
|
2026-11-22 21:27:29 +01:00
|
|
|
host_key = "host" if role.mode == "local" else "mail_hostname"
|
|
|
|
|
fields[host_key] = role.mail_hostname
|
2026-11-24 13:43:58 +01:00
|
|
|
if name == "mail" and role.mode in {"local", "remote-install"}:
|
2026-11-22 21:27:29 +01:00
|
|
|
fields["tls"] = role.tls
|
|
|
|
|
if role.tls == "provided":
|
|
|
|
|
fields.update(certificate=role.certificate, key=role.key)
|
|
|
|
|
return parameters
|
|
|
|
|
|
|
|
|
|
|
2026-11-26 21:53:51 +01:00
|
|
|
def save_interactive_plan(plan, password_path, terminal=None):
|
|
|
|
|
terminal = terminal or PlainTerminal()
|
|
|
|
|
while True:
|
|
|
|
|
destination = terminal.prompt(
|
|
|
|
|
"TOML file path",
|
|
|
|
|
hint="Leave blank to install without saving a copy.",
|
|
|
|
|
)
|
|
|
|
|
if not destination:
|
|
|
|
|
return
|
|
|
|
|
try:
|
|
|
|
|
path = Path(local_path(destination))
|
|
|
|
|
exists = os.path.lexists(path)
|
|
|
|
|
if (
|
|
|
|
|
exists
|
|
|
|
|
and terminal.choose(f"Replace {path}", ("yes", "no"), "no")
|
|
|
|
|
!= "yes"
|
|
|
|
|
):
|
|
|
|
|
continue
|
|
|
|
|
write_interactive_plan(path, plan, password_path, replace=exists)
|
|
|
|
|
except (OSError, InstallError) as error:
|
|
|
|
|
terminal.error(f"Could not save the installation plan. {error}")
|
|
|
|
|
else:
|
|
|
|
|
break
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def interactive_configuration(terminal, password_path):
|
2026-11-26 22:10:34 +01:00
|
|
|
try:
|
|
|
|
|
plan = interactive_plan(terminal, offer_cluster=True)
|
|
|
|
|
except ClusterRequestedError:
|
|
|
|
|
return interactive_cluster(terminal, password_path)
|
2026-11-26 21:53:51 +01:00
|
|
|
if plan is None:
|
|
|
|
|
return None
|
|
|
|
|
terminal.step = None
|
|
|
|
|
terminal.heading("Administrator password")
|
|
|
|
|
if password_path:
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
password = read_password(password_path)
|
|
|
|
|
break
|
|
|
|
|
except InstallError as error:
|
|
|
|
|
terminal.error(error)
|
|
|
|
|
password_path = terminal.prompt(
|
|
|
|
|
"Administrator password file", password_path
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
password = get_password(terminal)
|
|
|
|
|
terminal.heading("Save installation settings")
|
|
|
|
|
if not password_path:
|
|
|
|
|
terminal.show(
|
|
|
|
|
"Provide a private administrator password file "
|
|
|
|
|
"when reusing the plan."
|
|
|
|
|
)
|
|
|
|
|
saved = save_interactive_plan(plan, password_path, terminal)
|
|
|
|
|
return plan, password, saved
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def write_interactive_plan(path, plan, password_path, *, replace):
|
2026-11-22 21:27:29 +01:00
|
|
|
with TemporaryDirectory(dir=path.parent) as directory:
|
|
|
|
|
temporary = Path(directory) / "installation.toml"
|
|
|
|
|
with temporary.open("w", encoding="utf-8") as output:
|
|
|
|
|
temporary.chmod(0o600)
|
2026-11-26 22:10:34 +01:00
|
|
|
parameters = (
|
|
|
|
|
plan
|
|
|
|
|
if isinstance(plan, dict)
|
|
|
|
|
else export_parameters(plan, password_path)
|
|
|
|
|
)
|
|
|
|
|
for section, fields in parameters.items():
|
|
|
|
|
write_toml_table(output, section, fields)
|
2026-11-26 21:53:51 +01:00
|
|
|
if replace:
|
2026-11-22 21:27:29 +01:00
|
|
|
os.replace(temporary, path)
|
|
|
|
|
else:
|
|
|
|
|
os.link(temporary, path)
|
2026-11-26 21:53:51 +01:00
|
|
|
|
|
|
|
|
|
2026-11-26 22:10:34 +01:00
|
|
|
def write_toml_table(output, section, fields, *, array=False):
|
|
|
|
|
output.write(f"[[{section}]]\n" if array else f"[{section}]\n")
|
|
|
|
|
for name, value in fields.items():
|
|
|
|
|
if isinstance(value, dict) or table_array(value):
|
|
|
|
|
continue
|
|
|
|
|
encoded = json.dumps(value, ensure_ascii=False).replace(
|
|
|
|
|
"\x7f", "\\u007f"
|
|
|
|
|
)
|
|
|
|
|
output.write(f"{name} = {encoded}\n")
|
|
|
|
|
output.write("\n")
|
|
|
|
|
for name, value in fields.items():
|
|
|
|
|
qualified = f"{section}.{name}"
|
|
|
|
|
if isinstance(value, dict):
|
|
|
|
|
write_toml_table(output, qualified, value)
|
|
|
|
|
elif table_array(value):
|
|
|
|
|
for entry in value:
|
|
|
|
|
write_toml_table(output, qualified, entry, array=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def table_array(value):
|
|
|
|
|
return (
|
|
|
|
|
isinstance(value, list) and bool(value) and isinstance(value[0], dict)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def interactive_cluster(terminal, password_path):
|
|
|
|
|
configured = collect_cluster(terminal)
|
|
|
|
|
if configured is None:
|
|
|
|
|
return None
|
|
|
|
|
cluster, data = configured
|
2026-11-26 22:50:40 +01:00
|
|
|
entries = data["cluster"]["panels"]
|
|
|
|
|
passwords = {}
|
|
|
|
|
for index, entry in enumerate(entries):
|
|
|
|
|
terminal.context = (
|
|
|
|
|
f"Cluster / Passwords / Panel {index + 1} of {len(entries)}"
|
|
|
|
|
)
|
|
|
|
|
passwords[entry["name"]] = collect_password(
|
2026-11-26 22:10:34 +01:00
|
|
|
terminal,
|
|
|
|
|
entry,
|
|
|
|
|
default=password_path or "" if index == 0 else "",
|
|
|
|
|
masked_password=get_password,
|
|
|
|
|
)
|
2026-11-26 22:50:40 +01:00
|
|
|
terminal.context = "Cluster / Save configuration"
|
2026-11-26 22:10:34 +01:00
|
|
|
terminal.heading("Save cluster settings")
|
|
|
|
|
terminal.show(
|
|
|
|
|
"For automated setup, supply a private password file for each panel."
|
|
|
|
|
)
|
|
|
|
|
saved = save_interactive_plan(data, None, terminal)
|
|
|
|
|
return cluster, passwords, saved
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def install_cluster(cluster, passwords, *, interactive=False, check=False):
|
|
|
|
|
terminal = PlainTerminal()
|
|
|
|
|
installer = ClusterInstaller(
|
|
|
|
|
cluster,
|
|
|
|
|
passwords,
|
|
|
|
|
terminal.show,
|
|
|
|
|
heartbeat=sys.stdout.isatty(),
|
|
|
|
|
)
|
|
|
|
|
if check:
|
|
|
|
|
installer.check()
|
|
|
|
|
for line in cluster.summary():
|
|
|
|
|
terminal.show(line)
|
|
|
|
|
return 0
|
|
|
|
|
terminal.heading("Installing Coulobre cluster")
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
installer.install()
|
|
|
|
|
except (InstallError, OSError, ValueError) as error:
|
|
|
|
|
if not interactive:
|
|
|
|
|
raise
|
|
|
|
|
terminal.error(error)
|
|
|
|
|
terminal.show(
|
|
|
|
|
"Fix the reported problem and retry this cluster plan."
|
|
|
|
|
)
|
|
|
|
|
if (
|
|
|
|
|
terminal.choose("Installation", ("retry", "exit"), "exit")
|
|
|
|
|
== "exit"
|
|
|
|
|
):
|
|
|
|
|
return 1
|
|
|
|
|
else:
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
2026-11-26 21:53:51 +01:00
|
|
|
def install(plan, password, *, interactive):
|
|
|
|
|
terminal = PlainTerminal()
|
|
|
|
|
if interactive:
|
|
|
|
|
terminal.heading("Installing Coulobre")
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
Installer(
|
|
|
|
|
plan,
|
|
|
|
|
password,
|
|
|
|
|
lambda message: print(message, flush=True),
|
|
|
|
|
heartbeat=sys.stdout.isatty(),
|
|
|
|
|
).install()
|
|
|
|
|
except (InstallError, OSError, ValueError) as error:
|
|
|
|
|
if not interactive:
|
|
|
|
|
raise
|
|
|
|
|
print(f"Coulobre installer: {error}", file=sys.stderr)
|
|
|
|
|
print(
|
|
|
|
|
"Fix the reported problem and retry with these settings. "
|
|
|
|
|
"Coulobre resumes the same installation."
|
|
|
|
|
)
|
|
|
|
|
if (
|
|
|
|
|
terminal.choose("Installation", ("retry", "exit"), "exit")
|
|
|
|
|
== "exit"
|
|
|
|
|
):
|
|
|
|
|
return 1
|
|
|
|
|
else:
|
|
|
|
|
return 0
|
2026-11-22 16:39:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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",
|
2026-11-26 21:53:51 +01:00
|
|
|
help="Open the guided terminal wizard (the default).",
|
2026-11-22 16:39:27 +01:00
|
|
|
)
|
|
|
|
|
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."
|
|
|
|
|
)
|
2026-11-26 22:10:34 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--cluster",
|
|
|
|
|
action="store_true",
|
|
|
|
|
help="Open the interactive multi-server installation wizard.",
|
|
|
|
|
)
|
2026-11-22 16:39:27 +01:00
|
|
|
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.")
|
2026-11-26 22:10:34 +01:00
|
|
|
if options.cluster and (options.web or options.config):
|
|
|
|
|
parser.error(
|
|
|
|
|
"--cluster opens the terminal wizard. "
|
|
|
|
|
"Use --config for a cluster TOML file."
|
|
|
|
|
)
|
2026-11-22 16:39:27 +01:00
|
|
|
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)
|
2026-11-26 22:10:34 +01:00
|
|
|
if "cluster" in data:
|
|
|
|
|
cluster = parse_cluster(data)
|
|
|
|
|
passwords = cluster_passwords(
|
|
|
|
|
cluster, options.admin_password_file
|
|
|
|
|
)
|
|
|
|
|
return install_cluster(cluster, passwords, check=options.check)
|
2026-11-22 16:39:27 +01:00
|
|
|
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:
|
2026-11-26 22:10:34 +01:00
|
|
|
wizard = (
|
|
|
|
|
interactive_cluster
|
|
|
|
|
if options.cluster
|
|
|
|
|
else interactive_configuration
|
|
|
|
|
)
|
2026-11-26 21:53:51 +01:00
|
|
|
configured = configure_terminal(
|
2026-11-26 22:10:34 +01:00
|
|
|
lambda terminal: wizard(terminal, options.admin_password_file)
|
2026-11-26 21:53:51 +01:00
|
|
|
)
|
|
|
|
|
if configured is None:
|
2026-11-22 16:39:27 +01:00
|
|
|
return 0
|
2026-11-26 21:53:51 +01:00
|
|
|
plan, password, saved = configured
|
|
|
|
|
if saved is not None:
|
|
|
|
|
print(f"Saved installation settings to {saved}.")
|
2026-11-26 22:10:34 +01:00
|
|
|
if isinstance(plan, Cluster):
|
|
|
|
|
return install_cluster(plan, password, interactive=True)
|
2026-11-22 16:39:27 +01:00
|
|
|
if options.check:
|
|
|
|
|
print(plan.description())
|
|
|
|
|
return 0
|
2026-11-26 21:53:51 +01:00
|
|
|
return install(plan, password, interactive=not options.config)
|
2026-11-22 16:39:27 +01:00
|
|
|
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
|