Compare commits
11 Commits
feature/do
...
feature/pl
| Author | SHA1 | Date | |
|---|---|---|---|
| f2ad5c9d7d | |||
| 3bc2cf09c3 | |||
| e619c12c35 | |||
| a64de5aba3 | |||
| b80b3797fe | |||
| fa43428153 | |||
| 37a70cafdd | |||
| 878ef7555d | |||
| 3599ccbb39 | |||
| 09d66ea601 | |||
| 698bd85c7d |
@@ -52,11 +52,17 @@ type Props = {
|
|||||||
function getDomainStatusBadge(status: string | null) {
|
function getDomainStatusBadge(status: string | null) {
|
||||||
const normalized = (status ?? "unknown").toLowerCase();
|
const normalized = (status ?? "unknown").toLowerCase();
|
||||||
|
|
||||||
if (normalized === "active") return "bg-emerald-100 text-emerald-700";
|
if (normalized.includes("suspend") || normalized.includes("disabled")) {
|
||||||
|
return "bg-rose-100 text-rose-700";
|
||||||
|
}
|
||||||
|
|
||||||
if (normalized === "suspended") return "bg-rose-100 text-rose-700";
|
if (normalized.includes("active") || normalized.includes("enabled")) {
|
||||||
|
return "bg-emerald-100 text-emerald-700";
|
||||||
|
}
|
||||||
|
|
||||||
if (normalized === "inactive") return "bg-amber-100 text-amber-700";
|
if (normalized.includes("ok")) {
|
||||||
|
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,60 @@ export async function testPleskConnection(connection: PleskConnection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function listPleskSubscriptions(connection: PleskConnection) {
|
export async function listPleskSubscriptions(connection: PleskConnection) {
|
||||||
try {
|
const listResult = await pleskCliCall(connection, "subscription", ["--list"]);
|
||||||
const all: PleskSubscription[] = [];
|
|
||||||
|
|
||||||
for (let page = 1; page <= 20; page += 1) {
|
const subscriptionNames = (listResult.stdout ?? "")
|
||||||
const payload = await pleskRequest<PleskSubscription[]>(
|
.split(/\r?\n/)
|
||||||
connection,
|
.map((line) => line.trim())
|
||||||
`/api/v2/subscriptions?page=${page}&per_page=100`,
|
.filter(Boolean);
|
||||||
);
|
|
||||||
const rows = Array.isArray(payload) ? payload : [];
|
const subscriptions: PleskSubscription[] = [];
|
||||||
all.push(...rows);
|
|
||||||
if (rows.length < 100) break;
|
for (const subscriptionName of subscriptionNames.slice(0, 500)) {
|
||||||
|
let status: "active" | "suspended" | "unknown" = "unknown";
|
||||||
|
|
||||||
|
try {
|
||||||
|
const infoResult = await pleskCliCall(connection, "subscription", [
|
||||||
|
"--info",
|
||||||
|
subscriptionName,
|
||||||
|
]);
|
||||||
|
status = parseSubscriptionStatusFromInfo(infoResult.stdout ?? "");
|
||||||
|
} catch {
|
||||||
|
status = "unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
return all;
|
subscriptions.push({
|
||||||
} catch {
|
plesk_subscription_id: subscriptionName,
|
||||||
const cli = await pleskRequest<PleskCliResult>(
|
subscription_id: subscriptionName,
|
||||||
connection,
|
name: subscriptionName,
|
||||||
"/api/v2/cli/subscription/call",
|
status,
|
||||||
{
|
});
|
||||||
method: "POST",
|
|
||||||
body: JSON.stringify({ params: ["--list"] }),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const stdout = cli?.stdout ?? "";
|
|
||||||
if (!stdout.trim()) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return stdout
|
|
||||||
.split(/\r?\n/)
|
|
||||||
.map((line) => line.trim())
|
|
||||||
.filter(Boolean)
|
|
||||||
.map((line) => {
|
|
||||||
const columns = line.split(/\s+/);
|
|
||||||
const externalId = columns[0] ?? line;
|
|
||||||
return {
|
|
||||||
id: externalId,
|
|
||||||
subscription_id: externalId,
|
|
||||||
external_id: externalId,
|
|
||||||
name: columns.slice(1).join(" ") || externalId,
|
|
||||||
status: null,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return subscriptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseSubscriptionStatusFromInfo(
|
||||||
|
stdout: string,
|
||||||
|
): "active" | "suspended" | "unknown" {
|
||||||
|
const line = stdout
|
||||||
|
.split(/\r?\n/)
|
||||||
|
.map((entry) => entry.trim())
|
||||||
|
.find((entry) => /^domain\s+status\s*:/i.test(entry));
|
||||||
|
|
||||||
|
if (!line) return "unknown";
|
||||||
|
|
||||||
|
const value = line
|
||||||
|
.replace(/^domain\s+status\s*:/i, "")
|
||||||
|
.trim()
|
||||||
|
.toLowerCase();
|
||||||
|
|
||||||
|
if (value.includes("ok")) return "active";
|
||||||
|
if (value.includes("suspend") || value.includes("disabled")) {
|
||||||
|
return "suspended";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function pleskCliCall(
|
export async function pleskCliCall(
|
||||||
|
|||||||
@@ -82,14 +82,19 @@ 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) : "unknown",
|
||||||
owner_login: item?.owner_login
|
owner_login: item?.owner_login
|
||||||
? String(item.owner_login)
|
? String(item.owner_login)
|
||||||
: item?.owner?.login
|
: item?.owner?.login
|
||||||
@@ -113,15 +118,44 @@ export async function syncPleskInstance(
|
|||||||
if (error) throw new Error(error.message);
|
if (error) throw new Error(error.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { data: localSubscriptions, error: localSubscriptionsError } =
|
||||||
|
await supabase
|
||||||
|
.from("plesk_subscriptions")
|
||||||
|
.select("id, plesk_subscription_id, status")
|
||||||
|
.eq("agency_id", instance.agency_id)
|
||||||
|
.eq("plesk_instance_id", instance.id);
|
||||||
|
|
||||||
|
if (localSubscriptionsError) {
|
||||||
|
throw new Error(localSubscriptionsError.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
const subscriptionByExternalId = new Map<
|
||||||
|
string,
|
||||||
|
{ id: string; status: string | null }
|
||||||
|
>(
|
||||||
|
(localSubscriptions ?? [])
|
||||||
|
.filter((row: any) => row?.plesk_subscription_id && row?.id)
|
||||||
|
.map((row: any) => [
|
||||||
|
String(row.plesk_subscription_id),
|
||||||
|
{
|
||||||
|
id: String(row.id),
|
||||||
|
status: row?.status ? String(row.status) : null,
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
|
||||||
const domainsForUpsert = domainsRaw
|
const domainsForUpsert = domainsRaw
|
||||||
.map((item: any) => {
|
.map((item: any) => {
|
||||||
const domainId = item?.id ?? item?.guid ?? item?.name;
|
const domainId = item?.id ?? item?.guid ?? item?.name;
|
||||||
const domainName = item?.name ?? item?.domain_name ?? item?.domain;
|
const domainName = item?.name ?? item?.domain_name ?? item?.domain;
|
||||||
const status =
|
const subExternalId =
|
||||||
item?.status ??
|
|
||||||
(item?.hosting_type === "virtual" ? "active" : "inactive");
|
|
||||||
const externalSubscriptionId =
|
|
||||||
item?.subscription?.id ?? item?.subscription_id ?? item?.webspace_id;
|
item?.subscription?.id ?? item?.subscription_id ?? item?.webspace_id;
|
||||||
|
const externalSubscriptionId = subExternalId
|
||||||
|
? String(subExternalId)
|
||||||
|
: null;
|
||||||
|
const localSubscription = externalSubscriptionId
|
||||||
|
? subscriptionByExternalId.get(externalSubscriptionId)
|
||||||
|
: undefined;
|
||||||
const rawCreated = item?.created_at ?? item?.created;
|
const rawCreated = item?.created_at ?? item?.created;
|
||||||
const sourceCreatedAt = rawCreated
|
const sourceCreatedAt = rawCreated
|
||||||
? new Date(String(rawCreated)).toISOString()
|
? new Date(String(rawCreated)).toISOString()
|
||||||
@@ -138,13 +172,11 @@ export async function syncPleskInstance(
|
|||||||
return {
|
return {
|
||||||
agency_id: instance.agency_id,
|
agency_id: instance.agency_id,
|
||||||
plesk_instance_id: instance.id,
|
plesk_instance_id: instance.id,
|
||||||
subscription_id: null,
|
subscription_id: localSubscription?.id ?? null,
|
||||||
external_subscription_id: externalSubscriptionId
|
external_subscription_id: externalSubscriptionId,
|
||||||
? String(externalSubscriptionId)
|
|
||||||
: null,
|
|
||||||
plesk_domain_id: String(domainId),
|
plesk_domain_id: String(domainId),
|
||||||
domain_name: String(domainName),
|
domain_name: String(domainName),
|
||||||
status: status ? String(status) : "unknown",
|
status: localSubscription?.status ?? "unknown",
|
||||||
hosting_type: item?.hosting_type ? String(item.hosting_type) : null,
|
hosting_type: item?.hosting_type ? String(item.hosting_type) : null,
|
||||||
source_created_at: sourceCreatedAt,
|
source_created_at: sourceCreatedAt,
|
||||||
aliases_count: aliasesCount,
|
aliases_count: aliasesCount,
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
-- One-time backfill: align existing domain status with linked subscription status
|
||||||
|
update public.plesk_domains d
|
||||||
|
set status = coalesce(s.status, 'unknown')
|
||||||
|
from public.plesk_subscriptions s
|
||||||
|
where d.subscription_id = s.id
|
||||||
|
and d.agency_id = s.agency_id;
|
||||||
|
|
||||||
|
update public.plesk_subscriptions
|
||||||
|
set status = 'unknown'
|
||||||
|
where status is null;
|
||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user