Fix subscription status via CLI; derive domain status + badges
This commit is contained in:
@@ -52,19 +52,13 @@ type Props = {
|
|||||||
function getDomainStatusBadge(status: string | null) {
|
function getDomainStatusBadge(status: string | null) {
|
||||||
const normalized = (status ?? "unknown").toLowerCase();
|
const normalized = (status ?? "unknown").toLowerCase();
|
||||||
|
|
||||||
if (
|
if (normalized.includes("suspend") || normalized.includes("disabled")) {
|
||||||
normalized.includes("active") ||
|
|
||||||
normalized.includes("ok") ||
|
|
||||||
normalized.includes("enabled")
|
|
||||||
) {
|
|
||||||
return "bg-emerald-100 text-emerald-700";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (normalized.includes("suspended") || normalized.includes("disabled")) {
|
|
||||||
return "bg-rose-100 text-rose-700";
|
return "bg-rose-100 text-rose-700";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (normalized.includes("inactive")) return "bg-amber-100 text-amber-700";
|
if (normalized.includes("active") || normalized.includes("enabled")) {
|
||||||
|
return "bg-emerald-100 text-emerald-700";
|
||||||
|
}
|
||||||
|
|
||||||
return "bg-slate-100 text-slate-600";
|
return "bg-slate-100 text-slate-600";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ export type PleskConnection = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type PleskSubscription = {
|
export type PleskSubscription = {
|
||||||
|
plesk_subscription_id?: string;
|
||||||
id?: string | number;
|
id?: string | number;
|
||||||
guid?: string;
|
guid?: string;
|
||||||
subscription_id?: string | number;
|
subscription_id?: string | number;
|
||||||
@@ -137,51 +138,50 @@ export async function testPleskConnection(connection: PleskConnection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function listPleskSubscriptions(connection: PleskConnection) {
|
export async function listPleskSubscriptions(connection: PleskConnection) {
|
||||||
try {
|
const listOutput = await pleskCliCall(connection, "subscription", ["--list"]);
|
||||||
const all: PleskSubscription[] = [];
|
|
||||||
|
|
||||||
for (let page = 1; page <= 20; page += 1) {
|
const subscriptionNames = listOutput
|
||||||
const payload = await pleskRequest<PleskSubscription[]>(
|
|
||||||
connection,
|
|
||||||
`/api/v2/subscriptions?page=${page}&per_page=100`,
|
|
||||||
);
|
|
||||||
const rows = Array.isArray(payload) ? payload : [];
|
|
||||||
all.push(...rows);
|
|
||||||
if (rows.length < 100) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return all;
|
|
||||||
} catch {
|
|
||||||
const cli = await pleskRequest<PleskCliResult>(
|
|
||||||
connection,
|
|
||||||
"/api/v2/cli/subscription/call",
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
body: JSON.stringify({ params: ["--list"] }),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const stdout = cli?.stdout ?? "";
|
|
||||||
if (!stdout.trim()) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return stdout
|
|
||||||
.split(/\r?\n/)
|
.split(/\r?\n/)
|
||||||
.map((line) => line.trim())
|
.map((line) => line.trim())
|
||||||
.filter(Boolean)
|
.filter(Boolean);
|
||||||
.map((line) => {
|
|
||||||
const columns = line.split(/\s+/);
|
const subscriptions: PleskSubscription[] = [];
|
||||||
const externalId = columns[0] ?? line;
|
|
||||||
return {
|
for (const subscriptionName of subscriptionNames) {
|
||||||
id: externalId,
|
let status = "unknown";
|
||||||
subscription_id: externalId,
|
|
||||||
external_id: externalId,
|
try {
|
||||||
name: columns.slice(1).join(" ") || externalId,
|
const infoOutput = await pleskCliCall(connection, "subscription", [
|
||||||
status: null,
|
"--info",
|
||||||
};
|
subscriptionName,
|
||||||
|
]);
|
||||||
|
const normalizedInfo = infoOutput.toLowerCase();
|
||||||
|
|
||||||
|
const hasSuspendedMarker =
|
||||||
|
normalizedInfo.includes("suspended") ||
|
||||||
|
normalizedInfo.includes("webspace-off") ||
|
||||||
|
normalizedInfo.includes("disabled");
|
||||||
|
|
||||||
|
const hasActiveMarker =
|
||||||
|
normalizedInfo.includes("active") ||
|
||||||
|
normalizedInfo.includes("enabled") ||
|
||||||
|
normalizedInfo.includes("ok");
|
||||||
|
|
||||||
|
if (hasSuspendedMarker) status = "suspended";
|
||||||
|
else if (hasActiveMarker || !hasSuspendedMarker) status = "active";
|
||||||
|
} catch {
|
||||||
|
status = "unknown";
|
||||||
|
}
|
||||||
|
|
||||||
|
subscriptions.push({
|
||||||
|
plesk_subscription_id: subscriptionName,
|
||||||
|
subscription_id: subscriptionName,
|
||||||
|
name: subscriptionName,
|
||||||
|
status,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return subscriptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function pleskCliCall(
|
export async function pleskCliCall(
|
||||||
@@ -208,7 +208,7 @@ export async function pleskCliCall(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { code, stdout, stderr };
|
return stdout;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function listPleskDomains(connection: PleskConnection) {
|
export async function listPleskDomains(connection: PleskConnection) {
|
||||||
|
|||||||
@@ -82,13 +82,18 @@ export async function syncPleskInstance(
|
|||||||
|
|
||||||
const subscriptionsForUpsert = subscriptionsRaw
|
const subscriptionsForUpsert = subscriptionsRaw
|
||||||
.map((item: any) => {
|
.map((item: any) => {
|
||||||
const sourceId = item?.id ?? item?.guid ?? item?.subscription_id;
|
const sourceId =
|
||||||
|
item?.plesk_subscription_id ??
|
||||||
|
item?.name ??
|
||||||
|
item?.id ??
|
||||||
|
item?.guid ??
|
||||||
|
item?.subscription_id;
|
||||||
if (!sourceId) return null;
|
if (!sourceId) return null;
|
||||||
return {
|
return {
|
||||||
agency_id: instance.agency_id,
|
agency_id: instance.agency_id,
|
||||||
plesk_instance_id: instance.id,
|
plesk_instance_id: instance.id,
|
||||||
plesk_subscription_id: String(sourceId),
|
plesk_subscription_id: String(sourceId),
|
||||||
name: item?.name ? String(item.name) : null,
|
name: item?.name ? String(item.name) : String(sourceId),
|
||||||
status: item?.status ? String(item.status) : null,
|
status: item?.status ? String(item.status) : null,
|
||||||
owner_login: item?.owner_login
|
owner_login: item?.owner_login
|
||||||
? String(item.owner_login)
|
? String(item.owner_login)
|
||||||
|
|||||||
@@ -3,4 +3,9 @@ update public.plesk_domains d
|
|||||||
set status = s.status
|
set status = s.status
|
||||||
from public.plesk_subscriptions s
|
from public.plesk_subscriptions s
|
||||||
where d.subscription_id = s.id
|
where d.subscription_id = s.id
|
||||||
and d.agency_id = s.agency_id;
|
and d.agency_id = s.agency_id
|
||||||
|
and (d.status is null or d.status = 'unknown');
|
||||||
|
|
||||||
|
update public.plesk_subscriptions
|
||||||
|
set status = 'unknown'
|
||||||
|
where status is null;
|
||||||
Reference in New Issue
Block a user