import argparse import os import sys from getpass import getpass from .config import ( 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 interactive_plan(): panel = {"hostname": prompt("Panel hostname")} access = panel["access"] = prompt("Panel access (tls or proxy)", "tls") if access == "tls": panel["certificate"] = prompt("Panel certificate path") panel["key"] = prompt("Panel private key path") elif access == "proxy": panel["origin_ip"] = prompt("Private origin address") panel["proxy_ip"] = prompt("Trusted proxy address") panel["origin_port"] = number( prompt("Origin port", "8080"), "the origin" ) data = { "panel": panel, "admin": { "username": prompt("Administrator username", "operator"), "email": prompt("Administrator email"), }, } for name in ("dns", "mail"): mode = prompt(f"{name.upper()} role (off, local, or remote)", "off") target = data[name] = {"mode": mode} if mode == "remote": target["host"] = prompt( f"{name.upper()} node SSH hostname or address" ) target["port"] = number( prompt("SSH port", "22"), f"the {name} node" ) target["host_key"] = prompt("Verified SSH host public key file") elif name == "mail" and mode == "local": target["host"] = prompt("Local mail hostname") target["certificate"] = prompt("Mail certificate path") target["key"] = prompt("Mail private key path") 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." ) password = check_password(getpass("Administrator password: ")) if getpass("Repeat the administrator password: ") != password: raise InstallError("The administrator passwords differ.") return password 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 = interactive_plan() print(plan.description(), flush=True) if ( prompt("Install with these settings (yes or no)", "no").lower() != "yes" ): return 0 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) ) 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