137 lines
5.6 KiB
Python
137 lines
5.6 KiB
Python
from django.core.management.base import BaseCommand, CommandError
|
|
from django.db import transaction
|
|
from django.utils import timezone
|
|
|
|
from control.dispatch import FRESHNESS, lock_vpn_ai_runtime
|
|
from control.models import ClientSession, Node, NodeDeployment, Subscription
|
|
|
|
|
|
TARGET_SLUG = "racknerd-newyork-004"
|
|
LEGACY_SLUGS = ("aliyun-tokyo-002", "aliyun-siliconvalley-003")
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Safely drain and retire legacy VPN nodes after migrating access to VPN004."
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument(
|
|
"--phase",
|
|
choices=("drain", "finalize"),
|
|
default="drain",
|
|
help="drain migrates access and revokes desired credentials; finalize disables acknowledged nodes.",
|
|
)
|
|
|
|
@transaction.atomic
|
|
def handle(self, *args, **options):
|
|
# choose_node takes this same lock before assigning sessions. Holding it
|
|
# through the retirement transaction prevents a concurrent reservation
|
|
# from writing a legacy node back after sessions have been migrated.
|
|
lock_vpn_ai_runtime()
|
|
target = Node.objects.select_for_update().filter(slug=TARGET_SLUG).first()
|
|
if not target or not (
|
|
target.active
|
|
and target.subscription_enabled
|
|
and target.status == Node.Status.ONLINE
|
|
and target.protocol == Node.Protocol.VLESS_REALITY
|
|
and not target.office_only
|
|
):
|
|
raise CommandError(
|
|
f"replacement node {TARGET_SLUG} must be an active, subscribed, online public VLESS node"
|
|
)
|
|
if not target.last_seen_at or target.last_seen_at < timezone.now() - FRESHNESS:
|
|
raise CommandError(
|
|
f"replacement node {TARGET_SLUG} must have a fresh heartbeat within {FRESHNESS}"
|
|
)
|
|
|
|
legacy_by_slug = {
|
|
node.slug: node
|
|
for node in Node.objects.select_for_update().filter(slug__in=LEGACY_SLUGS)
|
|
}
|
|
if set(legacy_by_slug) != set(LEGACY_SLUGS):
|
|
missing = sorted(set(LEGACY_SLUGS) - set(legacy_by_slug))
|
|
raise CommandError(
|
|
"both legacy node records are required; missing: " + ", ".join(missing)
|
|
)
|
|
legacy_nodes = [legacy_by_slug[slug] for slug in LEGACY_SLUGS]
|
|
if options["phase"] == "finalize":
|
|
not_drained = [node.slug for node in legacy_nodes if node.subscription_enabled]
|
|
if not_drained:
|
|
raise CommandError(
|
|
"legacy nodes must be drained before finalization: " + ", ".join(not_drained)
|
|
)
|
|
still_deployed = sorted(
|
|
{
|
|
deployment.node.slug
|
|
for deployment in NodeDeployment.objects.select_for_update()
|
|
.select_related("node")
|
|
.filter(node__in=legacy_nodes, deployed=True)
|
|
}
|
|
)
|
|
if still_deployed:
|
|
raise CommandError(
|
|
"legacy nodes still report deployed credentials: " + ", ".join(still_deployed)
|
|
)
|
|
missing_empty_ack = [
|
|
node.slug
|
|
for node in legacy_nodes
|
|
if node.retirement_drain_started_at is None
|
|
or node.retirement_empty_ack_at is None
|
|
or node.retirement_empty_ack_at < node.retirement_drain_started_at
|
|
]
|
|
if missing_empty_ack:
|
|
raise CommandError(
|
|
"post-drain empty authorization acknowledgement is required from: "
|
|
+ ", ".join(missing_empty_ack)
|
|
)
|
|
for node in legacy_nodes:
|
|
node.active = False
|
|
node.subscription_enabled = False
|
|
node.status = Node.Status.OFFLINE
|
|
node.save(
|
|
update_fields=["active", "subscription_enabled", "status", "updated_at"]
|
|
)
|
|
self.stdout.write(
|
|
self.style.SUCCESS(
|
|
f"Finalize complete: {len(legacy_nodes)} legacy nodes disabled; records retained."
|
|
)
|
|
)
|
|
return
|
|
|
|
affected_subscriptions = list(
|
|
Subscription.objects.select_for_update()
|
|
.filter(allowed_nodes__in=legacy_nodes)
|
|
.distinct()
|
|
)
|
|
for subscription in affected_subscriptions:
|
|
subscription.allowed_nodes.remove(*legacy_nodes)
|
|
subscription.allowed_nodes.add(target)
|
|
|
|
now = timezone.now()
|
|
active_sessions = ClientSession.objects.filter(
|
|
assigned_node__in=legacy_nodes,
|
|
revoked_at__isnull=True,
|
|
expires_at__gt=now,
|
|
).update(assigned_node=target, dispatch_decision={})
|
|
|
|
drain_started_at = timezone.now()
|
|
for node in legacy_nodes:
|
|
update_fields = ["subscription_enabled", "status", "updated_at"]
|
|
if node.subscription_enabled or node.retirement_drain_started_at is None:
|
|
node.retirement_drain_started_at = drain_started_at
|
|
node.retirement_empty_ack_at = None
|
|
update_fields.extend(
|
|
["retirement_drain_started_at", "retirement_empty_ack_at"]
|
|
)
|
|
node.subscription_enabled = False
|
|
node.status = Node.Status.MAINTENANCE
|
|
node.save(update_fields=update_fields)
|
|
|
|
self.stdout.write(
|
|
self.style.SUCCESS(
|
|
"Drain complete: "
|
|
f"{len(legacy_nodes)} legacy nodes, "
|
|
f"{len(affected_subscriptions)} subscriptions, "
|
|
f"{active_sessions} active sessions migrated to {target.slug}."
|
|
)
|
|
)
|