203 lines
6.7 KiB
Python
203 lines
6.7 KiB
Python
import argparse
|
|
import json
|
|
import sys
|
|
from dataclasses import dataclass
|
|
from importlib.resources import files
|
|
from pathlib import Path
|
|
from string import Template
|
|
|
|
from .directadmin import DOCUMENT_ROOTS
|
|
from .domain_names import canonical_domain
|
|
from .node_requests import serve_apply_request
|
|
|
|
MANIFEST_VERSION = 1
|
|
CONFIGURATIONS = {
|
|
"apache": "apache-vhost.conf",
|
|
"php-fpm": "php-fpm-pool.conf",
|
|
}
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class WebSite:
|
|
account_id: int
|
|
domain: str
|
|
|
|
def __post_init__(self):
|
|
if (
|
|
type(self.account_id) is not int
|
|
or not 1 <= self.account_id <= 2**63 - 1
|
|
):
|
|
raise ValueError(
|
|
"The account ID must be a positive 64-bit integer."
|
|
)
|
|
if not isinstance(self.domain, str):
|
|
raise ValueError("The domain must be a string.")
|
|
object.__setattr__(self, "domain", canonical_domain(self.domain))
|
|
|
|
@classmethod
|
|
def from_manifest(cls, data):
|
|
if not isinstance(data, dict) or data.keys() != {
|
|
"version",
|
|
"account_id",
|
|
"domain",
|
|
}:
|
|
raise ValueError(
|
|
"Expected a manifest with version, account_id, and domain."
|
|
)
|
|
version = data["version"]
|
|
if type(version) is not int or version != MANIFEST_VERSION:
|
|
raise ValueError("Unsupported web deployment manifest version.")
|
|
return cls(data["account_id"], data["domain"])
|
|
|
|
def manifest(self):
|
|
return {
|
|
"version": MANIFEST_VERSION,
|
|
"account_id": self.account_id,
|
|
"domain": self.domain,
|
|
}
|
|
|
|
def configuration(self):
|
|
service_user = f"coulobre-{self.account_id}"
|
|
return {
|
|
"account_id": self.account_id,
|
|
"domain": self.domain,
|
|
"service_user": service_user,
|
|
"document_root": f"/srv/coulobre/web/{self.account_id}/public",
|
|
"socket_path": f"/run/php/{service_user}.sock",
|
|
}
|
|
|
|
def render(self, configuration, *, port=80, tls_settings=""):
|
|
resource = files("coulobre.hosting").joinpath(
|
|
"templates", "hosting", CONFIGURATIONS[configuration]
|
|
)
|
|
return Template(resource.read_text(encoding="utf-8")).substitute(
|
|
self.configuration(), port=port, tls_settings=tls_settings
|
|
)
|
|
|
|
|
|
def main(argv=None):
|
|
parser = argparse.ArgumentParser(prog="coulobre-web-node")
|
|
commands = parser.add_subparsers(dest="command", required=True)
|
|
render = commands.add_parser(
|
|
"render", help="Render configuration from a site manifest."
|
|
)
|
|
render.add_argument("manifest", type=Path)
|
|
render.add_argument("configuration", choices=CONFIGURATIONS)
|
|
apply = commands.add_parser(
|
|
"apply", help="Install a site on an Ubuntu web node."
|
|
)
|
|
apply.add_argument("manifest", type=Path)
|
|
commands.add_parser(
|
|
"apply-request",
|
|
help="Apply one site manifest from standard input and report JSON.",
|
|
)
|
|
status = commands.add_parser(
|
|
"status", help="Check a site's local configuration and services."
|
|
)
|
|
status.add_argument("manifest", type=Path)
|
|
https = commands.add_parser(
|
|
"enable-https",
|
|
help="Enable HTTPS with an existing Certbot certificate.",
|
|
)
|
|
https.add_argument("manifest", type=Path)
|
|
commands.add_parser(
|
|
"https-request",
|
|
help="Obtain and enable HTTPS from a site manifest on standard input.",
|
|
)
|
|
https_status = commands.add_parser(
|
|
"https-status", help="Check a site's local HTTPS configuration."
|
|
)
|
|
https_status.add_argument("manifest", type=Path)
|
|
sftp = commands.add_parser(
|
|
"set-sftp-keys",
|
|
help="Set the SSH public keys for a site's SFTP login.",
|
|
)
|
|
sftp.add_argument("manifest", type=Path)
|
|
sftp.add_argument("public_keys", type=Path)
|
|
commands.add_parser(
|
|
"sftp-keys-request",
|
|
help="Apply one SFTP key request from standard input.",
|
|
)
|
|
restore = commands.add_parser(
|
|
"restore-directadmin",
|
|
help="Restore a DirectAdmin website into an empty web root.",
|
|
)
|
|
restore.add_argument("manifest", type=Path)
|
|
restore.add_argument("backup", type=Path)
|
|
restore.add_argument(
|
|
"--source-root", choices=DOCUMENT_ROOTS, default="public_html"
|
|
)
|
|
args = parser.parse_args(argv)
|
|
|
|
if args.command == "apply-request":
|
|
from .web_node_apply import apply_site
|
|
|
|
return serve_apply_request(
|
|
MANIFEST_VERSION, WebSite.from_manifest, apply_site
|
|
)
|
|
|
|
if args.command == "https-request":
|
|
from .web_node_tls import provision_https
|
|
|
|
return serve_apply_request(
|
|
MANIFEST_VERSION, WebSite.from_manifest, provision_https
|
|
)
|
|
|
|
if args.command == "sftp-keys-request":
|
|
from .sftp_keys import MAX_KEYS_BYTES, SFTPKeys
|
|
from .web_node_sftp import apply_sftp_keys
|
|
|
|
return serve_apply_request(
|
|
MANIFEST_VERSION,
|
|
SFTPKeys.from_manifest,
|
|
apply_sftp_keys,
|
|
limit=MAX_KEYS_BYTES + 4096,
|
|
identity=SFTPKeys.identity,
|
|
)
|
|
|
|
try:
|
|
payload = json.loads(args.manifest.read_text(encoding="utf-8"))
|
|
site = WebSite.from_manifest(payload)
|
|
if args.command == "apply":
|
|
from .web_node_apply import apply_site
|
|
|
|
apply_site(site)
|
|
print(f"Applied web configuration for {site.domain}.")
|
|
elif args.command == "status":
|
|
from .web_node_apply import site_status
|
|
|
|
report = site_status(site)
|
|
print(json.dumps(report, indent=2))
|
|
return 0 if report["state"] == "configured" else 2
|
|
elif args.command == "enable-https":
|
|
from .web_node_tls import enable_https
|
|
|
|
enable_https(site)
|
|
print(f"Enabled HTTPS for {site.domain}.")
|
|
elif args.command == "https-status":
|
|
from .web_node_tls import https_status
|
|
|
|
report = https_status(site)
|
|
print(json.dumps(report, indent=2))
|
|
return 0 if report["state"] == "configured" else 2
|
|
elif args.command == "set-sftp-keys":
|
|
from .web_node_sftp import set_sftp_keys
|
|
|
|
user, count = set_sftp_keys(site, args.public_keys)
|
|
print(f"Installed {count} SFTP public key(s) for {user}.")
|
|
elif args.command == "restore-directadmin":
|
|
from .web_node_restore import restore_website
|
|
|
|
print(
|
|
json.dumps(
|
|
restore_website(site, args.backup, args.source_root),
|
|
indent=2,
|
|
)
|
|
)
|
|
else:
|
|
sys.stdout.write(site.render(args.configuration))
|
|
except (OSError, ValueError) as error:
|
|
parser.exit(1, f"coulobre-web-node: {error}\n")
|
|
|
|
return 0
|