246 lines
8.6 KiB
Python
246 lines
8.6 KiB
Python
from django.conf import settings
|
|
from django.core.exceptions import PermissionDenied, ValidationError
|
|
from django.db import transaction
|
|
from django.utils import timezone
|
|
|
|
from coulobre.node_retirement import RetirementRequest
|
|
from coulobre.php_pool_settings import PhpPoolSettings
|
|
from coulobre.php_runtime import validate_php_version
|
|
from coulobre.service_access import ServiceAccessRequest
|
|
|
|
from .access import customer_for_user, is_tenant, manages_account
|
|
from .installation_features import panel_feature_allowed
|
|
from .models import IndependentWebsite, WebsiteDomain, WebsiteRestoreJob
|
|
from .node_client import (
|
|
activate_local_site,
|
|
apply_on_local_node,
|
|
enable_local_https,
|
|
set_local_site_storage_limit,
|
|
update_local_php_settings,
|
|
update_local_php_version,
|
|
)
|
|
from .php_settings import limit_customer_php_settings, plan_php_settings
|
|
from .resource_requests import lock_hosting_account, require_active_services
|
|
|
|
Operation = IndependentWebsite.Operation
|
|
|
|
|
|
def account_websites(account):
|
|
return (
|
|
IndependentWebsite.objects.filter(reservation__account=account)
|
|
.select_related("reservation")
|
|
.order_by("reservation__domain")
|
|
)
|
|
|
|
|
|
def active_websites(account):
|
|
return account_websites(account).filter(
|
|
activated=True, retired_at__isnull=True
|
|
)
|
|
|
|
|
|
def website_limit(account):
|
|
plan = account.hosting_plan
|
|
return plan.website_limit if plan is not None else 1
|
|
|
|
|
|
def allowed_versions(account):
|
|
plan = account.hosting_plan
|
|
return plan.allowed_php_versions() if plan is not None else ()
|
|
|
|
|
|
def website_actions(website):
|
|
if website.finished_at is None or website.retired_at is not None:
|
|
return ()
|
|
if not website.activated:
|
|
return (Operation.ACTIVATE, Operation.RETIRE)
|
|
if website.suspended:
|
|
return (Operation.RESUME, Operation.RETIRE)
|
|
actions = (Operation.PHP, Operation.SETTINGS, Operation.SUSPEND)
|
|
if panel_feature_allowed("LOCAL_WEB_HTTPS") and not website.https_enabled:
|
|
actions += (Operation.HTTPS,)
|
|
return actions
|
|
|
|
|
|
def check_actor(account, actor):
|
|
if is_tenant(actor) and (
|
|
customer_for_user(actor) is None or not manages_account(actor, account)
|
|
):
|
|
raise PermissionDenied
|
|
|
|
|
|
def check_version(account, version):
|
|
try:
|
|
validate_php_version(version)
|
|
except ValueError as error:
|
|
raise ValidationError({"php_version": str(error)}) from error
|
|
allowed = allowed_versions(account)
|
|
if allowed and version not in allowed:
|
|
raise ValidationError(
|
|
{"php_version": "This PHP series is outside the hosting package."}
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def create_website(account_id, domain, version, actor):
|
|
account = lock_hosting_account(account_id, customer_id=None)
|
|
check_actor(account, actor)
|
|
require_active_services(account)
|
|
limit = website_limit(account)
|
|
if (
|
|
limit is not None
|
|
and account_websites(account).filter(retired_at__isnull=True).count()
|
|
+ 1
|
|
>= limit
|
|
):
|
|
raise ValidationError(
|
|
"This hosting account has reached its website limit."
|
|
)
|
|
if version:
|
|
check_version(account, version)
|
|
if WebsiteDomain.objects.filter(domain=domain).exists():
|
|
raise ValidationError({"domain": "This domain is unavailable."})
|
|
reservation = WebsiteDomain.objects.create(account=account, domain=domain)
|
|
return IndependentWebsite.objects.create(
|
|
reservation=reservation, requested_php_version=version
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def queue_website_operation(
|
|
account_id,
|
|
site_id,
|
|
operation,
|
|
version,
|
|
actor,
|
|
*,
|
|
php_settings=None,
|
|
):
|
|
account = lock_hosting_account(account_id, customer_id=None)
|
|
check_actor(account, actor)
|
|
require_active_services(account)
|
|
website = (
|
|
account_websites(account)
|
|
.select_for_update()
|
|
.get(pk=site_id, retired_at__isnull=True)
|
|
)
|
|
if operation not in Operation.values:
|
|
raise ValidationError("Choose a website operation.")
|
|
if website.finished_at is None:
|
|
raise ValidationError("Wait for the current website operation.")
|
|
if operation not in website_actions(website):
|
|
raise ValidationError(
|
|
"This operation is unavailable for this website."
|
|
)
|
|
if operation == Operation.PHP or version:
|
|
check_version(account, version)
|
|
if operation == Operation.RETIRE and (
|
|
account.backup_jobs.filter(finished_at__isnull=True).exists()
|
|
or WebsiteRestoreJob.objects.filter(
|
|
backup__account=account, finished_at__isnull=True
|
|
).exists()
|
|
):
|
|
raise ValidationError("Wait for the backup or restore to finish.")
|
|
customer = customer_for_user(actor)
|
|
if operation == Operation.SETTINGS:
|
|
if php_settings is None:
|
|
php_settings = plan_php_settings(account)
|
|
elif customer is not None:
|
|
php_settings = limit_customer_php_settings(account, php_settings)
|
|
IndependentWebsite.objects.requeue(
|
|
IndependentWebsite.objects.filter(pk=website.pk),
|
|
operation=operation,
|
|
requested_customer_id=customer.pk if customer else None,
|
|
requested_php_settings=php_settings.manifest()
|
|
if php_settings
|
|
else None,
|
|
requested_php_version=(
|
|
version
|
|
if operation in (Operation.ACTIVATE, Operation.PHP)
|
|
else website.requested_php_version
|
|
),
|
|
)
|
|
return website
|
|
|
|
|
|
def apply_website_operation(website):
|
|
site = website.spec
|
|
operation = website.operation
|
|
account = website.reservation.account
|
|
if website.requested_customer_id is not None and (
|
|
account.customer_id != website.requested_customer_id
|
|
):
|
|
raise ValueError("The hosting account's customer assignment changed.")
|
|
try:
|
|
require_active_services(account)
|
|
except ValidationError as error:
|
|
raise ValueError(" ".join(error.messages)) from None
|
|
if operation in (Operation.ACTIVATE, Operation.PHP):
|
|
requested = website.requested_php_version
|
|
if requested:
|
|
try:
|
|
check_version(account, requested)
|
|
except ValidationError as error:
|
|
raise ValueError(" ".join(error.messages)) from None
|
|
if operation == Operation.ACTIVATE:
|
|
plan = account.hosting_plan
|
|
options, version = activate_local_site(
|
|
site,
|
|
plan.get_php_settings() if plan else PhpPoolSettings(),
|
|
(requested,) if requested else allowed_versions(account),
|
|
)
|
|
limit = plan.website_storage_limit_mib if plan else None
|
|
if limit is not None:
|
|
set_local_site_storage_limit(site, limit)
|
|
return {
|
|
"activated": True,
|
|
"php_version": version,
|
|
"applied_php_settings": options.manifest()
|
|
if isinstance(options, PhpPoolSettings)
|
|
else None,
|
|
"storage_limit_mib": limit,
|
|
}
|
|
if operation == Operation.PHP:
|
|
return {"php_version": update_local_php_version(site, requested)}
|
|
if operation == Operation.SETTINGS:
|
|
requested_settings = PhpPoolSettings.from_manifest(
|
|
website.requested_php_settings
|
|
)
|
|
if website.requested_customer_id is not None:
|
|
try:
|
|
requested_settings = limit_customer_php_settings(
|
|
account, requested_settings
|
|
)
|
|
except ValidationError as error:
|
|
raise ValueError(" ".join(error.messages)) from None
|
|
applied = update_local_php_settings(site, requested_settings)
|
|
return {"applied_php_settings": applied.manifest()}
|
|
if operation == Operation.HTTPS:
|
|
if not settings.LOCAL_WEB_HTTPS:
|
|
raise ValueError("Enable the website HTTPS worker first.")
|
|
enable_local_https(site)
|
|
return {"https_enabled": True}
|
|
if operation in (Operation.SUSPEND, Operation.RESUME):
|
|
suspended = operation == Operation.SUSPEND
|
|
apply_on_local_node(
|
|
ServiceAccessRequest(site, suspended),
|
|
"/run/coulobre-web-apply.sock",
|
|
"web node",
|
|
"website access",
|
|
)
|
|
return {"suspended": suspended}
|
|
if operation == Operation.RETIRE:
|
|
request = RetirementRequest(site, str(website.retirement_id))
|
|
apply_on_local_node(
|
|
request,
|
|
"/run/coulobre-web-apply.sock",
|
|
"web node",
|
|
"website retirement",
|
|
identity=request.identity(),
|
|
)
|
|
website.websitescheduledjob_set.update(
|
|
enabled=False, next_run_at=None, dispatched_at=None
|
|
)
|
|
return {"retired_at": timezone.now()}
|
|
raise ValueError("Unknown website operation.")
|