Measure allocated website storage on the web node
This commit is contained in:
1 parent
710797df00
commit
9be1fa17b7
5 files changed
+118
-23
No files matched your search
@@ -11,6 +11,7 @@ def serve_apply_request(
|
|||||||
*,
|
*,
|
||||||
limit=MAX_REQUEST_BYTES,
|
limit=MAX_REQUEST_BYTES,
|
||||||
identity=None,
|
identity=None,
|
||||||
|
include_result=False,
|
||||||
):
|
):
|
||||||
report = {"version": manifest_version}
|
report = {"version": manifest_version}
|
||||||
try:
|
try:
|
||||||
@@ -23,7 +24,9 @@ def serve_apply_request(
|
|||||||
raise ValueError("Expected a UTF-8 JSON manifest.") from None
|
raise ValueError("Expected a UTF-8 JSON manifest.") from None
|
||||||
spec = parse_manifest(payload)
|
spec = parse_manifest(payload)
|
||||||
report = spec.manifest() if identity is None else identity(spec)
|
report = spec.manifest() if identity is None else identity(spec)
|
||||||
apply(spec)
|
result = apply(spec)
|
||||||
|
if include_result and result is not None:
|
||||||
|
report.update(result)
|
||||||
except (OSError, ValueError) as error:
|
except (OSError, ValueError) as error:
|
||||||
print(json.dumps({**report, "state": "failed", "problem": str(error)}))
|
print(json.dumps({**report, "state": "failed", "problem": str(error)}))
|
||||||
return 1
|
return 1
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ from .domain_names import canonical_domain
|
|||||||
from .node_requests import serve_apply_request
|
from .node_requests import serve_apply_request
|
||||||
|
|
||||||
MANIFEST_VERSION = 1
|
MANIFEST_VERSION = 1
|
||||||
|
MAX_STORAGE_BYTES = 2**63 - 1
|
||||||
CONFIGURATIONS = {
|
CONFIGURATIONS = {
|
||||||
"apache": "apache-vhost.conf",
|
"apache": "apache-vhost.conf",
|
||||||
"php-fpm": "php-fpm-pool.conf",
|
"php-fpm": "php-fpm-pool.conf",
|
||||||
@@ -75,6 +76,36 @@ class WebSite:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class WebStorageRequest:
|
||||||
|
site: WebSite
|
||||||
|
|
||||||
|
def manifest(self):
|
||||||
|
return {"operation": "measure_storage", "site": self.site.manifest()}
|
||||||
|
|
||||||
|
|
||||||
|
def parse_web_request(data):
|
||||||
|
if (
|
||||||
|
isinstance(data, dict)
|
||||||
|
and data.keys() == {"operation", "site"}
|
||||||
|
and data["operation"] == "measure_storage"
|
||||||
|
):
|
||||||
|
return WebStorageRequest(WebSite.from_manifest(data["site"]))
|
||||||
|
return WebSite.from_manifest(data)
|
||||||
|
|
||||||
|
|
||||||
|
def apply_web_request(request):
|
||||||
|
if isinstance(request, WebStorageRequest):
|
||||||
|
from .web_node_storage import measure_website_storage
|
||||||
|
|
||||||
|
return {"used_bytes": measure_website_storage(request.site)}
|
||||||
|
|
||||||
|
from .web_node_apply import apply_site
|
||||||
|
|
||||||
|
apply_site(request)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def main(argv=None):
|
def main(argv=None):
|
||||||
parser = argparse.ArgumentParser(prog="coulobre-web-node")
|
parser = argparse.ArgumentParser(prog="coulobre-web-node")
|
||||||
commands = parser.add_subparsers(dest="command", required=True)
|
commands = parser.add_subparsers(dest="command", required=True)
|
||||||
@@ -95,6 +126,10 @@ def main(argv=None):
|
|||||||
"status", help="Check a site's local configuration and services."
|
"status", help="Check a site's local configuration and services."
|
||||||
)
|
)
|
||||||
status.add_argument("manifest", type=Path)
|
status.add_argument("manifest", type=Path)
|
||||||
|
usage = commands.add_parser(
|
||||||
|
"storage-usage", help="Measure the allocated space in a document root."
|
||||||
|
)
|
||||||
|
usage.add_argument("manifest", type=Path)
|
||||||
https = commands.add_parser(
|
https = commands.add_parser(
|
||||||
"enable-https",
|
"enable-https",
|
||||||
help="Enable HTTPS with an existing Certbot certificate.",
|
help="Enable HTTPS with an existing Certbot certificate.",
|
||||||
@@ -144,10 +179,11 @@ def main(argv=None):
|
|||||||
parser.error("--replace requires --file.")
|
parser.error("--replace requires --file.")
|
||||||
|
|
||||||
if args.command == "apply-request":
|
if args.command == "apply-request":
|
||||||
from .web_node_apply import apply_site
|
|
||||||
|
|
||||||
return serve_apply_request(
|
return serve_apply_request(
|
||||||
MANIFEST_VERSION, WebSite.from_manifest, apply_site
|
MANIFEST_VERSION,
|
||||||
|
parse_web_request,
|
||||||
|
apply_web_request,
|
||||||
|
include_result=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
if args.command == "https-request":
|
if args.command == "https-request":
|
||||||
@@ -183,6 +219,13 @@ def main(argv=None):
|
|||||||
report = site_status(site)
|
report = site_status(site)
|
||||||
print(json.dumps(report, indent=2))
|
print(json.dumps(report, indent=2))
|
||||||
return 0 if report["state"] == "configured" else 2
|
return 0 if report["state"] == "configured" else 2
|
||||||
|
elif args.command == "storage-usage":
|
||||||
|
from .web_node_storage import measure_website_storage
|
||||||
|
|
||||||
|
report = site.manifest() | {
|
||||||
|
"used_bytes": measure_website_storage(site)
|
||||||
|
}
|
||||||
|
print(json.dumps(report, indent=2))
|
||||||
elif args.command == "enable-https":
|
elif args.command == "enable-https":
|
||||||
from .web_node_tls import enable_https
|
from .web_node_tls import enable_https
|
||||||
|
|
||||||
|
|||||||
@@ -131,12 +131,23 @@ def check_reservation(site):
|
|||||||
saved = json.loads(read_root_file(marker))
|
saved = json.loads(read_root_file(marker))
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Apply the site manifest before restoring website files."
|
"Apply the site manifest before using its document root."
|
||||||
) from None
|
) from None
|
||||||
if saved != site.manifest():
|
if saved != site.manifest():
|
||||||
raise ValueError("The account is reserved with a different manifest.")
|
raise ValueError("The account is reserved with a different manifest.")
|
||||||
|
|
||||||
|
|
||||||
|
def site_storage(site):
|
||||||
|
check_reservation(site)
|
||||||
|
paths = SitePaths.for_site(site)
|
||||||
|
require_root_directory(paths.account)
|
||||||
|
user = site.configuration()["service_user"]
|
||||||
|
uid, gid = check_system_account(user, paths.account)
|
||||||
|
public = paths.account / "public"
|
||||||
|
check_public_directory(public, uid, gid)
|
||||||
|
return public, uid, gid
|
||||||
|
|
||||||
|
|
||||||
def check_installation(site, paths):
|
def check_installation(site, paths):
|
||||||
for directory in (
|
for directory in (
|
||||||
paths.account,
|
paths.account,
|
||||||
|
|||||||
@@ -10,13 +10,7 @@ from tempfile import NamedTemporaryFile, TemporaryDirectory
|
|||||||
|
|
||||||
from .directadmin import backup_members, copy_member, member_path, open_backup
|
from .directadmin import backup_members, copy_member, member_path, open_backup
|
||||||
from .node_files import check_root_file, require_root_directory
|
from .node_files import check_root_file, require_root_directory
|
||||||
from .web_node_apply import (
|
from .web_node_apply import locked_web_node, site_storage
|
||||||
SitePaths,
|
|
||||||
check_public_directory,
|
|
||||||
check_reservation,
|
|
||||||
check_system_account,
|
|
||||||
locked_web_node,
|
|
||||||
)
|
|
||||||
from .web_node_restore import publish_empty_webroot
|
from .web_node_restore import publish_empty_webroot
|
||||||
|
|
||||||
BACKUP_VERSION = 1
|
BACKUP_VERSION = 1
|
||||||
@@ -83,17 +77,6 @@ def restore_native_website(site, backup, *, filename=None, replace=False):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def site_storage(site):
|
|
||||||
check_reservation(site)
|
|
||||||
paths = SitePaths.for_site(site)
|
|
||||||
require_root_directory(paths.account)
|
|
||||||
user = site.configuration()["service_user"]
|
|
||||||
uid, gid = check_system_account(user, paths.account)
|
|
||||||
public = paths.account / "public"
|
|
||||||
check_public_directory(public, uid, gid)
|
|
||||||
return public, uid, gid
|
|
||||||
|
|
||||||
|
|
||||||
def write_metadata(archive, site):
|
def write_metadata(archive, site):
|
||||||
metadata = {
|
metadata = {
|
||||||
"version": BACKUP_VERSION,
|
"version": BACKUP_VERSION,
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
import fcntl
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from .web_node import MAX_STORAGE_BYTES
|
||||||
|
from .web_node_apply import locked_web_node, site_storage
|
||||||
|
|
||||||
|
|
||||||
|
def measure_website_storage(site):
|
||||||
|
if os.geteuid() != 0:
|
||||||
|
raise ValueError("Run storage-usage as root on the web node.")
|
||||||
|
with locked_web_node(fcntl.LOCK_SH):
|
||||||
|
public, _, _ = site_storage(site)
|
||||||
|
user = site.configuration()["service_user"]
|
||||||
|
try:
|
||||||
|
result = subprocess.run(
|
||||||
|
[
|
||||||
|
"/usr/bin/du",
|
||||||
|
"--summarize",
|
||||||
|
"--block-size=1",
|
||||||
|
"--one-file-system",
|
||||||
|
"--no-dereference",
|
||||||
|
"--null",
|
||||||
|
"--",
|
||||||
|
".",
|
||||||
|
],
|
||||||
|
cwd=public,
|
||||||
|
user=user,
|
||||||
|
group=user,
|
||||||
|
extra_groups=[],
|
||||||
|
env={"PATH": "/usr/bin:/bin", "LC_ALL": "C"},
|
||||||
|
stdin=subprocess.DEVNULL,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.DEVNULL,
|
||||||
|
timeout=60,
|
||||||
|
)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
raise ValueError(
|
||||||
|
"Measuring the website's storage timed out."
|
||||||
|
) from None
|
||||||
|
if result.returncode:
|
||||||
|
raise ValueError(
|
||||||
|
"Could not measure all files in the document root."
|
||||||
|
)
|
||||||
|
size, _, entry = result.stdout.partition(b"\t")
|
||||||
|
if (
|
||||||
|
entry != b".\0"
|
||||||
|
or not size.isdigit()
|
||||||
|
or len(size) > 19
|
||||||
|
or int(size) > MAX_STORAGE_BYTES
|
||||||
|
):
|
||||||
|
raise ValueError(
|
||||||
|
"The disk usage command returned an invalid result."
|
||||||
|
)
|
||||||
|
return int(size)
|
||||||
Reference in new issue
Block a user