178 lines
5.4 KiB
Python
178 lines
5.4 KiB
Python
from django import forms
|
|
from django.core.exceptions import ValidationError
|
|
from django.db import transaction
|
|
|
|
from coulobre.php_pool_settings import (
|
|
PHP_SETTING_BOUNDS,
|
|
PHP_UNLIMITED_SETTINGS,
|
|
PhpPoolSettings,
|
|
PhpPoolSettingsError,
|
|
)
|
|
|
|
from .models import SitePhpSettings, WebActivation
|
|
from .resource_requests import (
|
|
lock_hosting_account,
|
|
require_active_services,
|
|
require_open_account,
|
|
)
|
|
from .sftp import site_is_active
|
|
|
|
|
|
def php_setting_input(option, label, *, help_text=""):
|
|
minimum, maximum = PHP_SETTING_BOUNDS[option]
|
|
if option in PHP_UNLIMITED_SETTINGS:
|
|
minimum = 0
|
|
help_text = f"{help_text} Enter 0 for no PHP limit.".strip()
|
|
return forms.IntegerField(
|
|
label=label,
|
|
min_value=minimum,
|
|
max_value=maximum,
|
|
required=False,
|
|
help_text=help_text,
|
|
)
|
|
|
|
|
|
class SitePhpSettingsForm(forms.Form):
|
|
memory_limit_mib = php_setting_input(
|
|
"memory_limit_mib", "PHP memory limit (MiB)"
|
|
)
|
|
upload_limit_mib = php_setting_input(
|
|
"upload_limit_mib", "PHP upload limit (MiB)"
|
|
)
|
|
request_timeout_seconds = php_setting_input(
|
|
"request_timeout_seconds", "PHP request timeout (seconds)"
|
|
)
|
|
max_children = php_setting_input("max_children", "Maximum PHP-FPM workers")
|
|
use_plan = forms.BooleanField(
|
|
label="Apply the current hosting package defaults",
|
|
required=False,
|
|
help_text="Uses the plan's values in place of the limits above.",
|
|
)
|
|
|
|
def clean(self):
|
|
cleaned = super().clean()
|
|
if not self.errors and not cleaned.get("use_plan"):
|
|
try:
|
|
settings_from_form(cleaned)
|
|
except PhpPoolSettingsError as error:
|
|
self.add_error(error.field, str(error))
|
|
return cleaned
|
|
|
|
|
|
def settings_from_form(data):
|
|
return PhpPoolSettings(
|
|
**{option: data[option] for option in PHP_SETTING_BOUNDS}
|
|
)
|
|
|
|
|
|
def plan_php_settings(account):
|
|
plan = account.hosting_plan
|
|
return plan.get_php_settings() if plan is not None else PhpPoolSettings()
|
|
|
|
|
|
def last_applied_php_settings(account, job):
|
|
if job is not None and job.applied_at is not None:
|
|
return PhpPoolSettings.from_manifest(job.applied_settings)
|
|
initial = (
|
|
WebActivation.objects.filter(
|
|
plan__account=account,
|
|
plan__primary_domain=account.primary_domain,
|
|
finished_at__isnull=False,
|
|
problem="",
|
|
php_settings__isnull=False,
|
|
)
|
|
.order_by("-finished_at", "-pk")
|
|
.values_list("php_settings", flat=True)
|
|
.first()
|
|
)
|
|
return (
|
|
PhpPoolSettings.from_manifest(initial)
|
|
if initial is not None
|
|
else PhpPoolSettings()
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def queue_web_activation(plan, *, starter_page=True):
|
|
account = lock_hosting_account(plan.account_id, customer_id=None)
|
|
require_active_services(account)
|
|
activations = WebActivation.objects.filter(plan=plan)
|
|
activation = activations.first()
|
|
if activation is None:
|
|
options = plan_php_settings(account)
|
|
WebActivation.objects.create(
|
|
plan=plan,
|
|
php_settings=options.manifest() if options.has_overrides else None,
|
|
starter_page=starter_page,
|
|
)
|
|
elif activation.problem:
|
|
WebActivation.objects.requeue(activations)
|
|
|
|
|
|
def plan_php_versions(account):
|
|
plan = account.hosting_plan
|
|
return plan.allowed_php_versions() if plan is not None else ()
|
|
|
|
|
|
def limit_customer_php_settings(account, requested):
|
|
limits = plan_php_settings(account).manifest()
|
|
values = requested.manifest()
|
|
for key, limit in limits.items():
|
|
value = values[key]
|
|
if value is not None and (
|
|
limit is None or (limit != 0 and (value == 0 or value > limit))
|
|
):
|
|
raise ValidationError(
|
|
"Keep PHP limits within the account's hosting package."
|
|
)
|
|
if value is None:
|
|
values[key] = limit
|
|
try:
|
|
return PhpPoolSettings.from_manifest(values)
|
|
except PhpPoolSettingsError as error:
|
|
raise ValidationError(str(error)) from error
|
|
|
|
|
|
@transaction.atomic
|
|
def queue_site_php_settings(
|
|
account_id, php_settings, *, use_plan=False, customer_id=None
|
|
):
|
|
account = lock_hosting_account(account_id, customer_id=customer_id)
|
|
require_open_account(account)
|
|
if not site_is_active(account):
|
|
raise ValidationError(
|
|
"Activate the website before changing PHP settings."
|
|
)
|
|
if use_plan:
|
|
php_settings = plan_php_settings(account)
|
|
elif customer_id is not None:
|
|
require_active_services(account)
|
|
php_settings = limit_customer_php_settings(account, php_settings)
|
|
|
|
changes = {
|
|
"domain": account.primary_domain,
|
|
"requested_settings": php_settings.manifest(),
|
|
}
|
|
job, created = SitePhpSettings.objects.get_or_create(
|
|
account=account, defaults=changes
|
|
)
|
|
if created:
|
|
return
|
|
matches = (
|
|
job.domain == changes["domain"]
|
|
and job.requested_settings == changes["requested_settings"]
|
|
)
|
|
if job.finished_at is None:
|
|
if matches:
|
|
return
|
|
raise ValidationError(
|
|
"Wait for the current PHP settings request to finish."
|
|
)
|
|
if matches and not job.problem:
|
|
return
|
|
if job.domain != account.primary_domain:
|
|
changes.update(applied_settings=None, applied_at=None)
|
|
SitePhpSettings.objects.requeue(
|
|
SitePhpSettings.objects.filter(pk=job.pk), **changes
|
|
)
|