postgresai 0.18.0-dev.16 → 0.18.0-dev.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -5
- package/dist/bin/postgres-ai.js +1704 -246
- package/package.json +1 -1
package/dist/bin/postgres-ai.js
CHANGED
|
@@ -13466,6 +13466,11 @@ function resolveBaseUrls2(opts, cfg, defaults = {}) {
|
|
|
13466
13466
|
storageBaseUrl: normalizeBaseUrl2(storageCandidate)
|
|
13467
13467
|
};
|
|
13468
13468
|
}
|
|
13469
|
+
function canOpenBrowser(env = process.env, platform = process.platform) {
|
|
13470
|
+
if (platform === "darwin" || platform === "win32")
|
|
13471
|
+
return !env.SSH_CONNECTION && !env.SSH_TTY;
|
|
13472
|
+
return !!(env.DISPLAY || env.WAYLAND_DISPLAY);
|
|
13473
|
+
}
|
|
13469
13474
|
var DEFAULT_HTTP_REQUEST_TIMEOUT_MS2 = 25000, SENSITIVE_KEY_SRC2 = "(?:password|passwd|db[-_]?pass|connstr|secret|token|api[-_]?key|private[-_]?key|access[-_]?key|cred(?:ential)?s?|dsn)", SENSITIVE_PAIR_SEPARATOR_SRC2 = `["']?\\s*[:=]`, CREDENTIAL_CHAR_SRC2 = "[A-Za-z0-9_+/=~.-]", TEXT_SENSITIVE_PAIR2, CREDENTIAL_RUN_ONLY2;
|
|
13470
13475
|
var init_util = __esm(() => {
|
|
13471
13476
|
TEXT_SENSITIVE_PAIR2 = new RegExp(`(${SENSITIVE_KEY_SRC2}${SENSITIVE_PAIR_SEPARATOR_SRC2}\\s*)` + `(?:("[^"]*"|'[^']*')` + `|([^\\s,;&]+)` + `((?:\\s+(?![^\\s,;&]*${SENSITIVE_KEY_SRC2}${SENSITIVE_PAIR_SEPARATOR_SRC2})[^\\s,;&]+)*)` + `)`, "gi");
|
|
@@ -13533,7 +13538,7 @@ var {
|
|
|
13533
13538
|
// package.json
|
|
13534
13539
|
var package_default = {
|
|
13535
13540
|
name: "postgresai",
|
|
13536
|
-
version: "0.18.0-dev.
|
|
13541
|
+
version: "0.18.0-dev.17",
|
|
13537
13542
|
description: "postgres_ai CLI",
|
|
13538
13543
|
license: "Apache-2.0",
|
|
13539
13544
|
private: false,
|
|
@@ -16873,8 +16878,8 @@ var {
|
|
|
16873
16878
|
} = yaml;
|
|
16874
16879
|
|
|
16875
16880
|
// bin/postgres-ai.ts
|
|
16876
|
-
import * as
|
|
16877
|
-
import * as
|
|
16881
|
+
import * as fs12 from "fs";
|
|
16882
|
+
import * as path10 from "path";
|
|
16878
16883
|
import * as os3 from "os";
|
|
16879
16884
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
16880
16885
|
import * as crypto2 from "crypto";
|
|
@@ -16893,7 +16898,7 @@ var Result = import_lib.default.Result;
|
|
|
16893
16898
|
var TypeOverrides = import_lib.default.TypeOverrides;
|
|
16894
16899
|
var defaults = import_lib.default.defaults;
|
|
16895
16900
|
// package.json
|
|
16896
|
-
var version = "0.18.0-dev.
|
|
16901
|
+
var version = "0.18.0-dev.17";
|
|
16897
16902
|
var package_default2 = {
|
|
16898
16903
|
name: "postgresai",
|
|
16899
16904
|
version,
|
|
@@ -17231,7 +17236,7 @@ function maskSecret(secret) {
|
|
|
17231
17236
|
}
|
|
17232
17237
|
function isSensitiveLogKey(key) {
|
|
17233
17238
|
const normalized = key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[-\s]+/g, "_").toLowerCase();
|
|
17234
|
-
return /^(?:password|passwd|db_(?:pass|password|url)|conn_?str|secret|(?:[a-z]+_)?key_secret|token|api_key|private_key|access_key|access_token|refresh_token|auth|auth_key|auth_token|authorization|credentials?|dsn)$/.test(normalized);
|
|
17239
|
+
return /^(?:password|passwd|db_(?:pass|password|url)|prepare_db_sql|conn_?str|secret|(?:[a-z]+_)?key_secret|token|api_key|private_key|access_key|access_token|refresh_token|auth|auth_key|auth_token|authorization|credentials?|dsn)$/.test(normalized);
|
|
17235
17240
|
}
|
|
17236
17241
|
function redactSecrets(value) {
|
|
17237
17242
|
if (Array.isArray(value)) {
|
|
@@ -18505,6 +18510,11 @@ function appendAttachmentsToContent(content, attachments) {
|
|
|
18505
18510
|
${links}`;
|
|
18506
18511
|
}
|
|
18507
18512
|
|
|
18513
|
+
// lib/connect.ts
|
|
18514
|
+
import { execFile as execFile2 } from "node:child_process";
|
|
18515
|
+
import * as fs6 from "node:fs";
|
|
18516
|
+
import * as path5 from "node:path";
|
|
18517
|
+
|
|
18508
18518
|
// node_modules/pg/esm/index.mjs
|
|
18509
18519
|
var import_lib2 = __toESM(require_lib2(), 1);
|
|
18510
18520
|
var Client2 = import_lib2.default.Client;
|
|
@@ -22829,6 +22839,178 @@ function isNumericProjectRef(ref) {
|
|
|
22829
22839
|
return /^[0-9]+$/.test(ref.trim());
|
|
22830
22840
|
}
|
|
22831
22841
|
|
|
22842
|
+
// lib/privatelink.ts
|
|
22843
|
+
import { execFile } from "node:child_process";
|
|
22844
|
+
var CFN_TEMPLATE_URL = "https://postgres-ai-cloudformation.s3.amazonaws.com/rds-privatelink-provider.yaml";
|
|
22845
|
+
var TERRAFORM_URL = "https://postgres-ai-cloudformation.s3.amazonaws.com/terraform/rds-privatelink-provider/main.tf";
|
|
22846
|
+
var PRINCIPAL_ARN = "arn:aws:iam::005923036815:user/pgai-rds-privatelink-monitoring";
|
|
22847
|
+
function rdsEndpoint(hostname) {
|
|
22848
|
+
const host = hostname.toLowerCase().replace(/\.$/, "");
|
|
22849
|
+
const m = /^([a-z][a-z0-9-]*)\.([a-z0-9-]+)\.([a-z]{2}(?:-[a-z]+)+-\d)\.rds\.amazonaws\.com(\.cn)?$/.exec(host);
|
|
22850
|
+
if (!m)
|
|
22851
|
+
return;
|
|
22852
|
+
const [, id, second, region, cn] = m;
|
|
22853
|
+
const kind = second.startsWith("cluster-ro-") ? "aurora_reader" : second.startsWith("cluster-custom-") ? "aurora_custom" : second.startsWith("cluster-") ? "aurora_writer" : second.startsWith("proxy-") ? "proxy" : "instance";
|
|
22854
|
+
const partition = cn ? "aws-cn" : region.startsWith("us-gov-") ? "aws-us-gov" : "aws";
|
|
22855
|
+
return { host, id, region, kind, partition };
|
|
22856
|
+
}
|
|
22857
|
+
var stackNameFor = (rdsId) => `postgresai-privatelink-${rdsId}`.slice(0, 128);
|
|
22858
|
+
var VPCE_SERVICE = /^com\.amazonaws\.vpce\.([a-z]{2}(?:-[a-z]+)+-\d)\.vpce-svc-[0-9a-f]{8,}$/;
|
|
22859
|
+
var ROLE_ARN = /^arn:aws:iam::\d{12}:role\/[\w+=,.@\/-]{1,512}$/;
|
|
22860
|
+
function isPrivateAddress(ip) {
|
|
22861
|
+
const v4 = /^(\d+)\.(\d+)\.\d+\.\d+$/.exec(ip);
|
|
22862
|
+
if (v4) {
|
|
22863
|
+
const [a, b] = [Number(v4[1]), Number(v4[2])];
|
|
22864
|
+
return a === 10 || a === 172 && b >= 16 && b <= 31 || a === 192 && b === 168 || a === 100 && b >= 64 && b <= 127 || a === 169 && b === 254;
|
|
22865
|
+
}
|
|
22866
|
+
return /^f[cd][0-9a-f]{2}:/i.test(ip) || /^fe80:/i.test(ip);
|
|
22867
|
+
}
|
|
22868
|
+
var DOH = ["https://cloudflare-dns.com/dns-query", "https://dns.google/resolve"];
|
|
22869
|
+
var DOH_TIMEOUT_MS = 3000;
|
|
22870
|
+
async function publicAddresses(host, fetchFn = fetch) {
|
|
22871
|
+
for (const base of DOH) {
|
|
22872
|
+
try {
|
|
22873
|
+
const r = await fetchFn(`${base}?name=${encodeURIComponent(host)}&type=A`, { headers: { accept: "application/dns-json" }, signal: AbortSignal.timeout(DOH_TIMEOUT_MS) });
|
|
22874
|
+
if (!r.ok)
|
|
22875
|
+
continue;
|
|
22876
|
+
const body = await r.json();
|
|
22877
|
+
if (body.Status !== 0)
|
|
22878
|
+
continue;
|
|
22879
|
+
return (body.Answer ?? []).filter((a) => a.type === 1).map((a) => a.data);
|
|
22880
|
+
} catch {}
|
|
22881
|
+
}
|
|
22882
|
+
return;
|
|
22883
|
+
}
|
|
22884
|
+
|
|
22885
|
+
class AwsError extends Error {
|
|
22886
|
+
kind;
|
|
22887
|
+
constructor(message, kind) {
|
|
22888
|
+
super(message);
|
|
22889
|
+
this.kind = kind;
|
|
22890
|
+
}
|
|
22891
|
+
}
|
|
22892
|
+
function awsRunner(profile) {
|
|
22893
|
+
return (args) => new Promise((resolve, reject) => {
|
|
22894
|
+
execFile("aws", [...args, "--output", "json", ...profile ? ["--profile", profile] : []], { timeout: 30000, maxBuffer: 16 * 1024 * 1024, env: { ...process.env, AWS_PAGER: "" } }, (err, stdout, stderr) => {
|
|
22895
|
+
if (!err) {
|
|
22896
|
+
try {
|
|
22897
|
+
return resolve(stdout.trim() ? JSON.parse(stdout) : null);
|
|
22898
|
+
} catch {
|
|
22899
|
+
return reject(new AwsError("the aws CLI returned no JSON", "other"));
|
|
22900
|
+
}
|
|
22901
|
+
}
|
|
22902
|
+
if (err.code === "ENOENT")
|
|
22903
|
+
return reject(new AwsError("the aws CLI is not installed", "missing"));
|
|
22904
|
+
const said = String(stderr || err.message).trim().split(`
|
|
22905
|
+
`).filter(Boolean).pop() ?? "";
|
|
22906
|
+
const kind = /DBInstanceNotFound|does not exist|NotFound/i.test(said) ? "not_found" : /expired|Unable to locate credentials|NoCredentialProviders|aws (sso )?login|profile .* could not be found|InvalidClientTokenId|SignatureDoesNotMatch/i.test(said) ? "credentials" : /AccessDenied|not authorized|UnauthorizedOperation/i.test(said) ? "denied" : "other";
|
|
22907
|
+
reject(new AwsError(said.replace(/^aws: \[ERROR\]: /, ""), kind));
|
|
22908
|
+
});
|
|
22909
|
+
});
|
|
22910
|
+
}
|
|
22911
|
+
async function describeDbInstance(run, id, region) {
|
|
22912
|
+
const d = await run(["rds", "describe-db-instances", "--db-instance-identifier", id, "--region", region, "--query", "DBInstances[0]"]);
|
|
22913
|
+
if (!d)
|
|
22914
|
+
throw new AwsError(`DB instance ${id} not found`, "not_found");
|
|
22915
|
+
return {
|
|
22916
|
+
publiclyAccessible: !!d.PubliclyAccessible,
|
|
22917
|
+
port: d.Endpoint?.Port ?? 5432,
|
|
22918
|
+
vpcId: d.DBSubnetGroup?.VpcId,
|
|
22919
|
+
subnets: (d.DBSubnetGroup?.Subnets ?? []).map((s) => ({ id: s.SubnetIdentifier, az: s.SubnetAvailabilityZone?.Name ?? "" })),
|
|
22920
|
+
securityGroupIds: (d.VpcSecurityGroups ?? []).filter((g) => g.Status !== "inactive").map((g) => g.VpcSecurityGroupId),
|
|
22921
|
+
instanceClass: d.DBInstanceClass,
|
|
22922
|
+
engine: d.Engine
|
|
22923
|
+
};
|
|
22924
|
+
}
|
|
22925
|
+
async function callerIdentity(run) {
|
|
22926
|
+
const r = await run(["sts", "get-caller-identity"]);
|
|
22927
|
+
return { account: r.Account, arn: r.Arn };
|
|
22928
|
+
}
|
|
22929
|
+
async function vcpusOfClass(run, instanceClass, region) {
|
|
22930
|
+
if (!instanceClass.startsWith("db."))
|
|
22931
|
+
return;
|
|
22932
|
+
try {
|
|
22933
|
+
const n = await run(["ec2", "describe-instance-types", "--instance-types", instanceClass.slice(3), "--region", region, "--query", "InstanceTypes[0].VCpuInfo.DefaultVCpus"]);
|
|
22934
|
+
return typeof n === "number" && n > 0 ? n : undefined;
|
|
22935
|
+
} catch {
|
|
22936
|
+
return;
|
|
22937
|
+
}
|
|
22938
|
+
}
|
|
22939
|
+
function oneSubnetPerAz(subnets) {
|
|
22940
|
+
const seen = new Map;
|
|
22941
|
+
for (const s of [...subnets].sort((a, b) => a.az.localeCompare(b.az) || a.id.localeCompare(b.id)))
|
|
22942
|
+
if (!seen.has(s.az))
|
|
22943
|
+
seen.set(s.az, s.id);
|
|
22944
|
+
return [...seen.values()];
|
|
22945
|
+
}
|
|
22946
|
+
var toStack = (s) => ({
|
|
22947
|
+
name: s.StackName,
|
|
22948
|
+
status: s.StackStatus,
|
|
22949
|
+
...s.StackStatusReason ? { reason: s.StackStatusReason } : {},
|
|
22950
|
+
...s.CreationTime ? { created: s.CreationTime } : {},
|
|
22951
|
+
outputs: Object.fromEntries((s.Outputs ?? []).map((o) => [o.OutputKey, o.OutputValue])),
|
|
22952
|
+
rdsId: s.Parameters?.find((p) => p.ParameterKey === "RdsInstanceIdentifier")?.ParameterValue
|
|
22953
|
+
});
|
|
22954
|
+
async function findStacks(run, rdsId, region) {
|
|
22955
|
+
const all = (await run(["cloudformation", "describe-stacks", "--region", region])).Stacks ?? [];
|
|
22956
|
+
const live = all.map(toStack).filter((s) => stackState(s.status) !== "deleting");
|
|
22957
|
+
const forThis = (s) => s.rdsId?.toLowerCase() === rdsId;
|
|
22958
|
+
const named = live.find((s) => s.name === stackNameFor(rdsId));
|
|
22959
|
+
const stacks = live.filter((s) => forThis(s) && (s === named || ("VpceServiceName" in s.outputs) || stackState(s.status) !== "ready"));
|
|
22960
|
+
return { stacks, ...named && !forThis(named) ? { misnamed: named } : {} };
|
|
22961
|
+
}
|
|
22962
|
+
async function stackFailure(run, name, region) {
|
|
22963
|
+
try {
|
|
22964
|
+
const r = await run(["cloudformation", "describe-stack-events", "--stack-name", name, "--region", region]);
|
|
22965
|
+
const failed = (r.StackEvents ?? []).filter((e) => /_FAILED$/.test(e.ResourceStatus) && e.ResourceStatusReason && !/cancelled/i.test(e.ResourceStatusReason));
|
|
22966
|
+
const first = failed[failed.length - 1];
|
|
22967
|
+
return first ? `${first.LogicalResourceId}: ${first.ResourceStatusReason}` : undefined;
|
|
22968
|
+
} catch {
|
|
22969
|
+
return;
|
|
22970
|
+
}
|
|
22971
|
+
}
|
|
22972
|
+
function stackState(status) {
|
|
22973
|
+
if (status === "DELETE_IN_PROGRESS" || status === "DELETE_COMPLETE")
|
|
22974
|
+
return "deleting";
|
|
22975
|
+
if (status === "ROLLBACK_IN_PROGRESS")
|
|
22976
|
+
return "in_progress";
|
|
22977
|
+
if (/^(CREATE_COMPLETE|UPDATE_COMPLETE|UPDATE_ROLLBACK_COMPLETE|IMPORT_COMPLETE|IMPORT_ROLLBACK_COMPLETE)$/.test(status))
|
|
22978
|
+
return "ready";
|
|
22979
|
+
if (/^(CREATE|UPDATE|UPDATE_ROLLBACK|IMPORT|IMPORT_ROLLBACK|REVIEW)_IN_PROGRESS$|^UPDATE(_ROLLBACK)?_COMPLETE_CLEANUP_IN_PROGRESS$/.test(status))
|
|
22980
|
+
return "in_progress";
|
|
22981
|
+
return "failed";
|
|
22982
|
+
}
|
|
22983
|
+
function quickCreateUrl(i) {
|
|
22984
|
+
const params = new URLSearchParams({ templateURL: CFN_TEMPLATE_URL, stackName: stackNameFor(i.rdsId) });
|
|
22985
|
+
params.set("param_RdsInstanceIdentifier", i.rdsId);
|
|
22986
|
+
params.set("param_RdsPort", String(i.port));
|
|
22987
|
+
if (i.vpcId)
|
|
22988
|
+
params.set("param_VpcId", i.vpcId);
|
|
22989
|
+
if (i.subnetIds?.length)
|
|
22990
|
+
params.set("param_SubnetIds", i.subnetIds.join(","));
|
|
22991
|
+
if (i.securityGroupId)
|
|
22992
|
+
params.set("param_RdsSecurityGroupId", i.securityGroupId);
|
|
22993
|
+
params.set("param_PostgresAiAccountArn", PRINCIPAL_ARN);
|
|
22994
|
+
if (i.externalId)
|
|
22995
|
+
params.set("param_PostgresAiExternalId", i.externalId);
|
|
22996
|
+
return `https://${i.region}.console.aws.amazon.com/cloudformation/home?region=${encodeURIComponent(i.region)}#/stacks/quickcreate?${params.toString()}`;
|
|
22997
|
+
}
|
|
22998
|
+
function terraformFiles(mainTf, i) {
|
|
22999
|
+
const q = JSON.stringify;
|
|
23000
|
+
const tfvars = [
|
|
23001
|
+
`aws_region = ${q(i.region)}`,
|
|
23002
|
+
...i.externalId ? [`postgres_ai_external_id = ${q(i.externalId)}`] : [],
|
|
23003
|
+
`rds_instance_identifier = ${q(i.rdsId)}`,
|
|
23004
|
+
`rds_port = ${i.port}`,
|
|
23005
|
+
i.vpcId ? `vpc_id = ${q(i.vpcId)}` : `# vpc_id = "vpc-..." (the RDS's VPC)`,
|
|
23006
|
+
i.subnetIds?.length ? `subnet_ids = [${i.subnetIds.map((s) => q(s)).join(", ")}]` : `# subnet_ids = ["subnet-...", "subnet-..."] (one per availability zone)`,
|
|
23007
|
+
i.securityGroupId ? `rds_security_group_id = ${q(i.securityGroupId)}` : `# rds_security_group_id = "sg-..." (the RDS's security group)`
|
|
23008
|
+
].join(`
|
|
23009
|
+
`);
|
|
23010
|
+
return { mainTf, tfvars: `${tfvars}
|
|
23011
|
+
` };
|
|
23012
|
+
}
|
|
23013
|
+
|
|
22832
23014
|
// lib/supabase.ts
|
|
22833
23015
|
var SUPABASE_API_BASE = "https://api.supabase.com";
|
|
22834
23016
|
function extractProjectRefFromUrl(dbUrl) {
|
|
@@ -23169,9 +23351,17 @@ async function connect(url, opts, deps) {
|
|
|
23169
23351
|
const key = parseClickhouseKey(opts.clickhouseKey, provider === "clickhouse" && !opts.agent ? process.env : {});
|
|
23170
23352
|
if (key && provider !== "clickhouse")
|
|
23171
23353
|
throw new Error("--clickhouse-key applies to ClickHouse Managed Postgres only");
|
|
23354
|
+
const plFlag = privateLinkFlag(opts);
|
|
23172
23355
|
if (!opts.selfHosted && provider === "rds") {
|
|
23173
|
-
const
|
|
23174
|
-
|
|
23356
|
+
const route = await rdsRoute(url, opts, deps, progress);
|
|
23357
|
+
if ("status" in route)
|
|
23358
|
+
return { ...route, provider, name };
|
|
23359
|
+
if (route.private)
|
|
23360
|
+
return deployPrivateLink(url, route, { ...opts, monitoringUser }, deps, progress);
|
|
23361
|
+
if (plFlag)
|
|
23362
|
+
return { status: "action_required", provider, name, next: `${plFlag} applies to AWS PrivateLink only, and ${name} is monitored directly${opts.privatelink === false ? ` (${opts.agent ? "privatelink: false" : "--no-privatelink"})` : " (it is publicly accessible)"}` };
|
|
23363
|
+
} else if (plFlag) {
|
|
23364
|
+
return { status: "action_required", provider, name, next: `${plFlag} applies to Amazon RDS monitored from PostgresAI Cloud only` };
|
|
23175
23365
|
}
|
|
23176
23366
|
if (opts.selfHosted && opts.resetPassword) {
|
|
23177
23367
|
return { status: "action_required", provider, name, next: "--reset-password works with PostgresAI Cloud only (it checks what else monitors this server): deploy without --self-hosted, or set PGAI_MON_PASSWORD" };
|
|
@@ -23878,6 +24068,540 @@ async function supabaseBox(ref, name, opts, deps, progress, stop, label) {
|
|
|
23878
24068
|
return { ...connectStatus(box, provider, true), ...b.billing };
|
|
23879
24069
|
return follow(box, { provider, name, fresh: true, billing: b.billing, billingUrl: b.billingUrl, note: "", place }, opts, deps, progress);
|
|
23880
24070
|
}
|
|
24071
|
+
var PL = "AWS PrivateLink";
|
|
24072
|
+
var STACK_WAIT_MS = 30 * 60000;
|
|
24073
|
+
var STACK_POLL_MS = 15000;
|
|
24074
|
+
function privateLinkFlag(opts) {
|
|
24075
|
+
const named = [
|
|
24076
|
+
[opts.privatelink === true ? true : undefined, "--privatelink", "privatelink"],
|
|
24077
|
+
[opts.vpceService, "--vpce-service", "vpce_service"],
|
|
24078
|
+
[opts.hostStats ? true : undefined, "--host-stats", "host_stats"],
|
|
24079
|
+
[opts.hostStatsRole, "--host-stats-role", "host_stats_role"],
|
|
24080
|
+
[opts.terraform ? true : undefined, "--terraform", "terraform"],
|
|
24081
|
+
[opts.agent ? opts.nonce : undefined, "PGAI_MON_NONCE", "nonce"]
|
|
24082
|
+
];
|
|
24083
|
+
const hit = named.find(([v]) => v !== undefined);
|
|
24084
|
+
return hit && (opts.agent ? hit[2] : hit[1]);
|
|
24085
|
+
}
|
|
24086
|
+
async function rdsRoute(url, opts, deps, progress) {
|
|
24087
|
+
const host = new URL(url).hostname.toLowerCase().replace(/\.$/, "");
|
|
24088
|
+
const ep = rdsEndpoint(host);
|
|
24089
|
+
const flag = (cli, agent) => opts.agent ? agent : cli;
|
|
24090
|
+
const stop = (next) => ({ status: "action_required", next });
|
|
24091
|
+
if (opts.privatelink === false)
|
|
24092
|
+
return { private: false };
|
|
24093
|
+
const asked = opts.privatelink === true;
|
|
24094
|
+
let read = {};
|
|
24095
|
+
if (ep?.kind === "instance" && ep.partition === "aws" && opts.aws !== false && deps.aws)
|
|
24096
|
+
read = await readAws(ep, opts, deps, progress);
|
|
24097
|
+
let isPublic = read.inst?.publiclyAccessible;
|
|
24098
|
+
if (isPublic === undefined && ep && deps.publicAddresses) {
|
|
24099
|
+
progress("preparing", `Looking up ${host} in public DNS (cloudflare-dns.com, dns.google): is it publicly accessible?`);
|
|
24100
|
+
const addresses = await deps.publicAddresses(host);
|
|
24101
|
+
if (addresses?.length) {
|
|
24102
|
+
isPublic = !addresses.every(isPrivateAddress);
|
|
24103
|
+
progress("preparing", `${host} resolves to a ${isPublic ? "public address in public DNS, so it is monitored directly" : `private address in public DNS, so it is monitored over ${PL}`}`);
|
|
24104
|
+
}
|
|
24105
|
+
}
|
|
24106
|
+
if (isPublic === true) {
|
|
24107
|
+
if (asked)
|
|
24108
|
+
return stop(`${host} is publicly accessible, so it is monitored directly: deploy without ${flag("--privatelink", "privatelink")}`);
|
|
24109
|
+
return { private: false };
|
|
24110
|
+
}
|
|
24111
|
+
if (isPublic === undefined && !asked) {
|
|
24112
|
+
if (!deps.ask || opts.yes)
|
|
24113
|
+
return stop(`Could not tell whether ${host} is publicly accessible (AWS ${opts.aws === false ? "not read" : "not readable here"}, and public DNS did not answer): re-run with ${flag("--privatelink", "privatelink: true")} if it is not, or ${flag("--no-privatelink", "privatelink: false")} if it is`);
|
|
24114
|
+
const a = (await deps.ask(`Is ${host} publicly accessible? [y]es, monitor it directly / [n]o, over ${PL} / Enter to stop: `)).trim().toLowerCase();
|
|
24115
|
+
if (a === "y" || a === "yes")
|
|
24116
|
+
return { private: false };
|
|
24117
|
+
if (a !== "n" && a !== "no")
|
|
24118
|
+
return stop("Stopped: nothing was created");
|
|
24119
|
+
}
|
|
24120
|
+
if (!ep)
|
|
24121
|
+
return stop(`${PL} needs the RDS instance endpoint (<instance>.<id>.<region>.rds.amazonaws.com) in the URL, not ${host}; or monitor from this network with --self-hosted`);
|
|
24122
|
+
if (ep.partition !== "aws")
|
|
24123
|
+
return stop(`${ep.partition === "aws-cn" ? "AWS China" : "AWS GovCloud"} can't reach PostgresAI over ${PL} (it does not cross AWS partitions): monitor from this network with --self-hosted`);
|
|
24124
|
+
if (ep.kind !== "instance" || read.inst?.engine?.startsWith("aurora"))
|
|
24125
|
+
return stop(`${ep.kind === "proxy" ? "An RDS Proxy endpoint" : "Aurora"} is not supported over ${PL} yet: monitor from this network with --self-hosted, or ask support@postgres.ai`);
|
|
24126
|
+
return { private: true, ep, ...read };
|
|
24127
|
+
}
|
|
24128
|
+
async function readAws(ep, opts, deps, progress) {
|
|
24129
|
+
let profile = opts.awsProfile?.trim() || undefined;
|
|
24130
|
+
for (let tries = 0;tries < 3; tries++) {
|
|
24131
|
+
const run = deps.aws(profile);
|
|
24132
|
+
const of = profile ? ` (profile ${profile})` : "";
|
|
24133
|
+
let why;
|
|
24134
|
+
let missing = false;
|
|
24135
|
+
try {
|
|
24136
|
+
const me = await callerIdentity(run);
|
|
24137
|
+
const inst = await describeDbInstance(run, ep.id, ep.region);
|
|
24138
|
+
progress("preparing", `AWS account ${me.account} (${me.arn.split(":").pop()}), read-only: ${ep.id} is ${inst.publiclyAccessible ? "publicly accessible, so it is monitored directly" : `not publicly accessible, so it is monitored over ${PL}`}`);
|
|
24139
|
+
return { aws: run, inst };
|
|
24140
|
+
} catch (err) {
|
|
24141
|
+
if (!(err instanceof AwsError))
|
|
24142
|
+
throw err;
|
|
24143
|
+
missing = err.kind === "missing";
|
|
24144
|
+
why = missing ? "the aws CLI is not installed here" : err.kind === "not_found" ? `${ep.id} is not in ${ep.region} of this AWS account${of}` : err.kind === "credentials" ? `the AWS credentials${of} do not work: ${err.message}` : `AWS refused${of}: ${err.message}`;
|
|
24145
|
+
}
|
|
24146
|
+
if (missing || !deps.ask || opts.yes) {
|
|
24147
|
+
progress("preparing", `Note: ${why}; going on without AWS${missing ? "" : ` (${opts.agent ? "aws_profile" : "--aws-profile"} picks a profile)`}`);
|
|
24148
|
+
return {};
|
|
24149
|
+
}
|
|
24150
|
+
const profiles = deps.awsProfiles ? await deps.awsProfiles().catch(() => []) : [];
|
|
24151
|
+
const a = (await deps.ask(`AWS profile to read ${ep.id} with${profiles.length ? ` (${profiles.join(", ")})` : ""}, or Enter to go on without AWS: `, [`${why}.`])).trim();
|
|
24152
|
+
if (!a)
|
|
24153
|
+
return {};
|
|
24154
|
+
profile = a;
|
|
24155
|
+
}
|
|
24156
|
+
return {};
|
|
24157
|
+
}
|
|
24158
|
+
var VERIFY_FAILED = {
|
|
24159
|
+
unreachable: "The monitoring box could not reach the database over PrivateLink: check that the stack finished, its load balancer's target is healthy, and the RDS is not publicly accessible.",
|
|
24160
|
+
auth_failed: `The monitoring box reached the database, but ${DEFAULT_MONITORING_USER} could not log in: its password changed after this deploy set it (another setup ran its SQL).`,
|
|
24161
|
+
nonce_mismatch: "A newer PostgresAI setup SQL ran in this database after this deploy's (another deploy, or the Console), so monitoring does not start."
|
|
24162
|
+
};
|
|
24163
|
+
var utc = (iso) => {
|
|
24164
|
+
const d = new Date(iso);
|
|
24165
|
+
return Number.isNaN(d.getTime()) ? iso : `${d.toISOString().slice(0, 19).replace("T", " ")} UTC`;
|
|
24166
|
+
};
|
|
24167
|
+
function inDatabase(url, db) {
|
|
24168
|
+
const u = new URL(url);
|
|
24169
|
+
if (u.pathname.length <= 1)
|
|
24170
|
+
u.pathname = `/${encodeURIComponent(db)}`;
|
|
24171
|
+
return u.toString();
|
|
24172
|
+
}
|
|
24173
|
+
function roleUrl(url, db, password) {
|
|
24174
|
+
const u = new URL(loginUrlFor(url, DEFAULT_MONITORING_USER, db, password));
|
|
24175
|
+
u.pathname = new URL(inDatabase(url, db)).pathname;
|
|
24176
|
+
return u.toString();
|
|
24177
|
+
}
|
|
24178
|
+
async function deployPrivateLink(url, route, opts, deps, progress) {
|
|
24179
|
+
const provider = "rds";
|
|
24180
|
+
const { ep, inst } = route;
|
|
24181
|
+
const name = databaseName2(url);
|
|
24182
|
+
const db = name.split("/").slice(1).join("/") || "postgres";
|
|
24183
|
+
const port = Number(new URL(url).port || inst?.port || 5432);
|
|
24184
|
+
const cluster = `${ep.host}:${port}`;
|
|
24185
|
+
const flag = (cli, agent) => opts.agent ? agent : cli;
|
|
24186
|
+
const stop = (next, more = {}) => ({ status: "action_required", provider, name, ...more, next });
|
|
24187
|
+
const notHere = [
|
|
24188
|
+
[opts.cloud, "--cloud", "cloud"],
|
|
24189
|
+
[opts.location, "--location", "location"],
|
|
24190
|
+
[opts.name, "--name", "name"],
|
|
24191
|
+
[opts.ipFirst || undefined, "--ip-first", "ip_first"],
|
|
24192
|
+
[opts.ipAllowed || undefined, "--ip-allowed", "ip_allowed"],
|
|
24193
|
+
[opts.resetPassword || undefined, "--reset-password", "reset_password"],
|
|
24194
|
+
[opts.monitoringUser, "--monitoring-user", "monitoring_user"]
|
|
24195
|
+
].find(([v]) => v !== undefined && v !== "");
|
|
24196
|
+
if (notHere)
|
|
24197
|
+
return stop(`${flag(notHere[1], notHere[2])} does not apply over ${PL}: the box runs on AWS in ${ep.region} (the RDS's region), as ${DEFAULT_MONITORING_USER}, in a project named ${ep.id}`);
|
|
24198
|
+
if (opts.monitoringPassword === undefined !== (opts.nonce === undefined)) {
|
|
24199
|
+
return stop(opts.agent ? "Pass monitoring_password and nonce together (both from the prepare_db result)" : "Set PGAI_MON_PASSWORD and PGAI_MON_NONCE together (both from the printed SQL's result), or neither");
|
|
24200
|
+
}
|
|
24201
|
+
const rows = await deps.list();
|
|
24202
|
+
const live = rows.filter((d) => !disconnecting(d.status));
|
|
24203
|
+
const onServer = live.filter((d) => d.cluster && serverOf(d.cluster) === cluster);
|
|
24204
|
+
const verify = async (row) => VERIFY_FAILED[(await deps.rdsStatus?.(row.id).catch(() => {
|
|
24205
|
+
return;
|
|
24206
|
+
}))?.verified_status ?? ""];
|
|
24207
|
+
const own = onServer.find((d) => (d.provider === "rds" || d.provider === "aurora") && d.database != null && decodeURIComponent(d.database) === db);
|
|
24208
|
+
if (own) {
|
|
24209
|
+
const note = opts.vpceService || opts.hostStatsRole ? "; the flags for a new box were not used: it exists" : "";
|
|
24210
|
+
return overPrivateLink(await follow(own, { provider, name, fresh: false, billing: {}, note, verify }, opts, deps, progress));
|
|
24211
|
+
}
|
|
24212
|
+
const already = (d, how) => {
|
|
24213
|
+
const instead = d.database != null && decodeURIComponent(d.database) !== db ? `To monitor ${db} instead` : "To set it up from here instead";
|
|
24214
|
+
return stop(`${ep.id} is already monitored ${how} (instance ${d.name}). Setting up another instance would change ${DEFAULT_MONITORING_USER}'s password, which that instance logs in with. ${instead}, delete that instance first (pgai mon instances delete ${deleteRef(d, rows)})`);
|
|
24215
|
+
};
|
|
24216
|
+
if (onServer.length)
|
|
24217
|
+
return already(onServer[0], onServer[0].provider === "rds" ? `over ${PL}, database ${decodeURIComponent(onServer[0].database ?? "?")}` : "directly");
|
|
24218
|
+
const candidates = live.filter((r) => !r.cluster || r.provider === "rds" || r.provider === "aurora");
|
|
24219
|
+
let routes;
|
|
24220
|
+
try {
|
|
24221
|
+
routes = await Promise.all(candidates.map((d) => deps.rdsStatus?.(d.id).catch((err) => {
|
|
24222
|
+
if (httpStatus(err) === 404)
|
|
24223
|
+
return;
|
|
24224
|
+
throw err;
|
|
24225
|
+
})));
|
|
24226
|
+
} catch (err) {
|
|
24227
|
+
return stop(`Could not check what already monitors ${ep.id} (${errorText(err)}): re-run pgai mon deploy. Nothing was changed`);
|
|
24228
|
+
}
|
|
24229
|
+
const viaConsole = candidates.find((_, i) => routes[i]?.rds_endpoint.toLowerCase().replace(/\.$/, "") === ep.host);
|
|
24230
|
+
if (viaConsole)
|
|
24231
|
+
return already(viaConsole, `over ${PL}, set up in the console`);
|
|
24232
|
+
const hostStats = !!opts.hostStats || !!opts.hostStatsRole?.trim();
|
|
24233
|
+
const place = { cloud: "aws", location: ep.region, location_reason: "privatelink", location_reason_text: `same region as your database, over ${PL}` };
|
|
24234
|
+
progress("preparing", `Monitoring box: AWS, ${ep.region} (${place.location_reason_text})`, { cloud: "aws", location: ep.region, location_reason: "privatelink" });
|
|
24235
|
+
let quote;
|
|
24236
|
+
try {
|
|
24237
|
+
quote = await deps.quote(opts.coupon);
|
|
24238
|
+
} catch (err) {
|
|
24239
|
+
if (err instanceof HttpStatusError && err.status === 403)
|
|
24240
|
+
return stop(notAdminNext);
|
|
24241
|
+
throw err;
|
|
24242
|
+
}
|
|
24243
|
+
let vcpus = opts.vcpus;
|
|
24244
|
+
if (vcpus === undefined && route.aws && inst?.instanceClass) {
|
|
24245
|
+
vcpus = await vcpusOfClass(route.aws, inst.instanceClass, ep.region);
|
|
24246
|
+
if (vcpus)
|
|
24247
|
+
progress("billing", `vCPUs: ${vcpus} (${inst.instanceClass})`);
|
|
24248
|
+
}
|
|
24249
|
+
const where = `AWS, ${ep.region}`;
|
|
24250
|
+
const b = await billingFor(quote, name, opts, deps, (price) => progress("billing", estimateLine(price)), async (price) => {
|
|
24251
|
+
if (opts.yes)
|
|
24252
|
+
return { go: true };
|
|
24253
|
+
if (deps.ask) {
|
|
24254
|
+
return await deps.confirm(`Deploy ${terminalText(name)} over ${PL} (monitoring box on ${where})? [y/N] `) ? { go: true } : { go: false, stop: { next: `Nothing was created: re-run with --yes to deploy over ${PL}` } };
|
|
24255
|
+
}
|
|
24256
|
+
return { go: false, stop: { next: opts.agent ? `Show the user the price (${price}); the monitoring box runs on ${where}, reached over ${PL}. Then call deploy_monitoring again with yes: true. Nothing was created.` : `The monitoring box would run on ${where} for ${price}, reached over ${PL}: re-run with --yes to deploy. Nothing was created.` } };
|
|
24257
|
+
});
|
|
24258
|
+
if (b.stop)
|
|
24259
|
+
return { ...stop(b.stop.next), ...place, ...b.stop };
|
|
24260
|
+
if (deps.aborted?.())
|
|
24261
|
+
throw new CancelledError;
|
|
24262
|
+
const more = { ...place, ...b.billing };
|
|
24263
|
+
if (vcpus === undefined) {
|
|
24264
|
+
vcpus = !opts.yes && !opts.agent && deps.askVcpus ? await deps.askVcpus(VCPUS_QUESTION) : undefined;
|
|
24265
|
+
if (deps.aborted?.())
|
|
24266
|
+
throw new CancelledError;
|
|
24267
|
+
if (vcpus === undefined)
|
|
24268
|
+
progress("billing", NO_VCPUS_NOTE);
|
|
24269
|
+
}
|
|
24270
|
+
const stack = await privateLinkStack({ ep, aws: route.aws, inst, port, hostStats, opts, deps, progress, stop: (next, m = {}) => stop(next, { ...more, ...m }) });
|
|
24271
|
+
if ("status" in stack)
|
|
24272
|
+
return stack;
|
|
24273
|
+
if (deps.aborted?.())
|
|
24274
|
+
throw new CancelledError;
|
|
24275
|
+
const role = await privateLinkRole(url, db, opts, deps, progress, (next, m = {}) => stop(next, { ...more, ...m }));
|
|
24276
|
+
if ("status" in role)
|
|
24277
|
+
return role;
|
|
24278
|
+
if (deps.aborted?.())
|
|
24279
|
+
throw new CancelledError;
|
|
24280
|
+
progress("provisioning", `Provisioning monitoring for ${name} over ${PL}`);
|
|
24281
|
+
const created = await deps.rdsProvision({
|
|
24282
|
+
vpce_service_name: stack.vpce,
|
|
24283
|
+
rds_endpoint: ep.host,
|
|
24284
|
+
db_password: role.password,
|
|
24285
|
+
rds_port: port,
|
|
24286
|
+
rds_database: db,
|
|
24287
|
+
region: ep.region,
|
|
24288
|
+
...b.accepted ? { accept_price: true } : {},
|
|
24289
|
+
...opts.coupon ? { promo_code: opts.coupon } : {},
|
|
24290
|
+
...stack.roleArn ? { host_stats_role_arn: stack.roleArn } : {},
|
|
24291
|
+
...vcpus ? { vcpus } : {}
|
|
24292
|
+
}).catch((err) => {
|
|
24293
|
+
if (err instanceof HttpStatusError && err.status === 402)
|
|
24294
|
+
return /declined/i.test(err.message) ? "declined" : "payment";
|
|
24295
|
+
if (err instanceof HttpStatusError && err.status === 412)
|
|
24296
|
+
return "price";
|
|
24297
|
+
if (err instanceof HttpStatusError && err.status < 500)
|
|
24298
|
+
return { refused: errorText(err) };
|
|
24299
|
+
if (mayHaveRun(err))
|
|
24300
|
+
return "unanswered";
|
|
24301
|
+
throw err;
|
|
24302
|
+
});
|
|
24303
|
+
if (typeof created === "object" && "refused" in created)
|
|
24304
|
+
return stop(`${created.refused.replace(/\.?$/, ".")} No monitoring box was created.`, more);
|
|
24305
|
+
if (created === "payment")
|
|
24306
|
+
return { status: "action_required", provider, name, ...place, ...b.billingPage };
|
|
24307
|
+
if (created === "declined")
|
|
24308
|
+
return { status: "action_required", provider, name, ...place, ...b.billingPage, next: b.billingPage.next.replace(/^Add a payment method at /, "The payment method on file was declined: update it at ") };
|
|
24309
|
+
if (created === "price")
|
|
24310
|
+
return stop("The price changed since it was shown: re-run pgai mon deploy to see it", more);
|
|
24311
|
+
let box;
|
|
24312
|
+
if (created === "unanswered") {
|
|
24313
|
+
progress("provisioning", STILL_CREATING);
|
|
24314
|
+
const known = new Set(rows.map((d) => d.id));
|
|
24315
|
+
const made = await lookForBox((list) => list.find((d) => !known.has(d.id) && !!d.cluster && serverOf(d.cluster) === cluster), opts, deps, deps.now());
|
|
24316
|
+
if (!made)
|
|
24317
|
+
return stop(deps.aborted?.() ? STOPPED_LOOKING : opts.agent ? NOT_CONFIRMED_AGENT : NOT_CONFIRMED, more);
|
|
24318
|
+
box = made;
|
|
24319
|
+
} else {
|
|
24320
|
+
box = { id: created.id, name: created.name, provider, status: created.status, dashboard_url: null, host_metrics: !!stack.roleArn };
|
|
24321
|
+
}
|
|
24322
|
+
if (deps.aborted?.())
|
|
24323
|
+
return { ...connectStatus(box, provider, true), ...more };
|
|
24324
|
+
return overPrivateLink(await follow(box, { provider, name, fresh: true, billing: b.billing, billingUrl: b.billingUrl, note: "", place, verify }, opts, deps, progress));
|
|
24325
|
+
}
|
|
24326
|
+
var PRIVATELINK_LAUNCH_FAILED = `The monitoring box could not connect to the stack's endpoint service: check that the stack finished, and that the endpoint service (VPC console, Endpoint services, Allow principals) lists ${PRINCIPAL_ARN}.`;
|
|
24327
|
+
function overPrivateLink(r, why) {
|
|
24328
|
+
const { egress_ip: _ip, ...rest } = r;
|
|
24329
|
+
if (rest.status === "ready")
|
|
24330
|
+
return rest;
|
|
24331
|
+
const said = Object.values(VERIFY_FAILED).some((t) => rest.next.includes(t));
|
|
24332
|
+
const first = said ? undefined : why ?? (rest.status === "failed" ? PRIVATELINK_LAUNCH_FAILED : undefined);
|
|
24333
|
+
return first ? { ...rest, next: `${first} ${rest.status === "failed" ? "Then: " : ""}${rest.next}` } : rest;
|
|
24334
|
+
}
|
|
24335
|
+
async function privateLinkStack(c) {
|
|
24336
|
+
const { ep, opts, deps, progress } = c;
|
|
24337
|
+
const flag = (cli, agent) => opts.agent ? agent : cli;
|
|
24338
|
+
const terminal = !!deps.ask;
|
|
24339
|
+
const ask = opts.yes ? undefined : deps.ask;
|
|
24340
|
+
let aws = opts.terraform ? undefined : c.aws;
|
|
24341
|
+
let vpce = opts.vpceService?.trim() || undefined;
|
|
24342
|
+
let roleArn = opts.hostStatsRole?.trim() || undefined;
|
|
24343
|
+
const badVpce = (v) => {
|
|
24344
|
+
const m = VPCE_SERVICE.exec(v);
|
|
24345
|
+
if (!m)
|
|
24346
|
+
return `${v} is not a VpceServiceName (com.amazonaws.vpce.<region>.vpce-svc-<id>, the stack's output)`;
|
|
24347
|
+
if (m[1] !== ep.region)
|
|
24348
|
+
return `${v} is in ${m[1]}, and ${ep.id} in ${ep.region}: ${PL} connects within one region, so use the stack made in ${ep.region}`;
|
|
24349
|
+
return;
|
|
24350
|
+
};
|
|
24351
|
+
if (roleArn && !ROLE_ARN.test(roleArn))
|
|
24352
|
+
return c.stop(`${roleArn} is not an IAM role ARN (arn:aws:iam::<account>:role/<name>, the stack's HostStatsRoleArn output)`);
|
|
24353
|
+
if (vpce && badVpce(vpce))
|
|
24354
|
+
return c.stop(badVpce(vpce));
|
|
24355
|
+
let externalId;
|
|
24356
|
+
const externalIdOf = async () => externalId ??= await deps.rdsExternalId();
|
|
24357
|
+
let shown;
|
|
24358
|
+
const show = async () => {
|
|
24359
|
+
const inputs = {
|
|
24360
|
+
region: ep.region,
|
|
24361
|
+
rdsId: ep.id,
|
|
24362
|
+
port: c.port,
|
|
24363
|
+
vpcId: c.inst?.vpcId,
|
|
24364
|
+
subnetIds: c.inst ? oneSubnetPerAz(c.inst.subnets) : undefined,
|
|
24365
|
+
securityGroupId: c.inst?.securityGroupIds.length === 1 ? c.inst.securityGroupIds[0] : undefined,
|
|
24366
|
+
...c.hostStats ? { externalId: await externalIdOf() } : {}
|
|
24367
|
+
};
|
|
24368
|
+
const cfn = quickCreateUrl(inputs);
|
|
24369
|
+
const [vpcKey, subnetsKey, sgKey] = opts.terraform ? ["vpc_id", "subnet_ids", "rds_security_group_id"] : ["VpcId", "SubnetIds", "RdsSecurityGroupId"];
|
|
24370
|
+
const w = sgKey.length + 2;
|
|
24371
|
+
const blanks = [
|
|
24372
|
+
!inputs.vpcId && ` ${vpcKey.padEnd(w)}the RDS's VPC`,
|
|
24373
|
+
!inputs.subnetIds?.length && ` ${subnetsKey.padEnd(w)}one subnet per availability zone of the RDS's subnet group`,
|
|
24374
|
+
!inputs.securityGroupId && ` ${sgKey.padEnd(w)}the RDS's security group`
|
|
24375
|
+
].filter((l) => !!l);
|
|
24376
|
+
const choose = blanks.length ? `
|
|
24377
|
+
${opts.terraform ? "In terraform.tfvars, fill in" : "In the form, choose (empty in the link)"}:
|
|
24378
|
+
${blanks.join(`
|
|
24379
|
+
`)}
|
|
24380
|
+
(the RDS console lists them: ${ep.id}, Connectivity & security)` : "";
|
|
24381
|
+
let dir;
|
|
24382
|
+
if (opts.terraform)
|
|
24383
|
+
dir = await deps.writeTerraform(stackNameFor(ep.id), terraformFiles(await deps.terraformModule(), inputs));
|
|
24384
|
+
const where = deps.openUrl ? "" : `
|
|
24385
|
+
Open it in a browser signed in to that AWS account.`;
|
|
24386
|
+
const tf = opts.agent ? "" : `
|
|
24387
|
+
Prefer Terraform? ${deps.ask ? "Stop (Ctrl-C) and re-run" : "Re-run"} with --terraform: it writes the same stack as a module.`;
|
|
24388
|
+
progress("privatelink", dir ? `Terraform for the PrivateLink stack written to ${dir}: terraform init && terraform apply there${choose}` : `Create the PrivateLink stack in your AWS account from this link; nothing becomes public:${choose}
|
|
24389
|
+
${cfn}${where}${tf}`, { cfn_url: cfn, stack_name: stackNameFor(ep.id) });
|
|
24390
|
+
return {
|
|
24391
|
+
step: "privatelink_stack",
|
|
24392
|
+
cfn_url: cfn,
|
|
24393
|
+
stack_name: stackNameFor(ep.id),
|
|
24394
|
+
region: ep.region,
|
|
24395
|
+
...inputs.externalId ? { external_id: inputs.externalId } : {},
|
|
24396
|
+
...dir ? { terraform_dir: dir } : {},
|
|
24397
|
+
...blanks.length ? { choose_in_form: blanks.map((l) => l.trim().split(/\s+/)[0]) } : {}
|
|
24398
|
+
};
|
|
24399
|
+
};
|
|
24400
|
+
const resume = () => {
|
|
24401
|
+
const output = opts.terraform ? "vpce_service_name output" : "VpceServiceName output";
|
|
24402
|
+
const role = c.hostStats && !roleArn ? ` and ${flag("--host-stats-role", "host_stats_role")} <its ${opts.terraform ? "host_stats_role_arn" : "HostStatsRoleArn"} output>` : "";
|
|
24403
|
+
return aws ? opts.agent ? "call deploy_monitoring again" : "re-run pgai mon deploy" : `${opts.agent ? "call deploy_monitoring again with" : "re-run with"} ${flag("--vpce-service", "vpce_service")} <its ${output}>${role}`;
|
|
24404
|
+
};
|
|
24405
|
+
const deadline = deps.now() + STACK_WAIT_MS;
|
|
24406
|
+
let refusedPaste;
|
|
24407
|
+
let saidMisnamed = false;
|
|
24408
|
+
let readStack = false;
|
|
24409
|
+
const seenMaking = new Set;
|
|
24410
|
+
const saidFailed = new Set;
|
|
24411
|
+
const startedAt = Date.now() - 60000;
|
|
24412
|
+
const thisRuns = (s) => s.name === stackNameFor(ep.id) || seenMaking.has(s.name) || !!s.created && Date.parse(s.created) >= startedAt;
|
|
24413
|
+
for (let pastes = 0, empties = 0;!vpce; ) {
|
|
24414
|
+
let found = [];
|
|
24415
|
+
if (aws) {
|
|
24416
|
+
try {
|
|
24417
|
+
const r = await findStacks(aws, ep.id, ep.region);
|
|
24418
|
+
found = r.stacks;
|
|
24419
|
+
if (r.misnamed && !saidMisnamed) {
|
|
24420
|
+
saidMisnamed = true;
|
|
24421
|
+
progress("privatelink", `Note: stack ${r.misnamed.name} is for RDS ${r.misnamed.rdsId ?? "(none)"}, not ${ep.id}: not used. Give the new stack another name in the form`);
|
|
24422
|
+
}
|
|
24423
|
+
} catch (err) {
|
|
24424
|
+
if (!(err instanceof AwsError))
|
|
24425
|
+
throw err;
|
|
24426
|
+
progress("privatelink", `Note: could not read CloudFormation (${err.message}); going on without AWS`);
|
|
24427
|
+
aws = undefined;
|
|
24428
|
+
}
|
|
24429
|
+
}
|
|
24430
|
+
const ready = found.filter((s) => stackState(s.status) === "ready" && s.outputs.VpceServiceName);
|
|
24431
|
+
let stack;
|
|
24432
|
+
if (ready.length === 1)
|
|
24433
|
+
stack = ready[0];
|
|
24434
|
+
else if (ready.length > 1) {
|
|
24435
|
+
const rows = ready.map((s, i) => ` ${i + 1}. ${s.name} ${s.status}${s.created ? ` created ${utc(s.created)}` : ""} ${s.outputs.VpceServiceName}`);
|
|
24436
|
+
const listed = {
|
|
24437
|
+
step: "privatelink_stack",
|
|
24438
|
+
region: ep.region,
|
|
24439
|
+
stacks: ready.map((s) => ({ name: s.name, status: s.status, created: s.created ?? null, vpce_service_name: s.outputs.VpceServiceName }))
|
|
24440
|
+
};
|
|
24441
|
+
if (!ask) {
|
|
24442
|
+
return c.stop(`Several stacks publish ${ep.id}: ${opts.agent ? "ask the user which one, then call deploy_monitoring again with vpce_service" : "re-run with --vpce-service"} <the VpceServiceName of the one to use>. Nothing was created on PostgresAI yet.
|
|
24443
|
+
${rows.join(`
|
|
24444
|
+
`)}`, listed);
|
|
24445
|
+
}
|
|
24446
|
+
for (let tries = 0;!stack && tries < 3; tries++) {
|
|
24447
|
+
const a = (await ask(`The stack to use (1-${ready.length}, Enter to stop): `, tries ? [`Type a number from 1 to ${ready.length}.`] : [`Several stacks publish ${ep.id}:`, ...rows])).trim();
|
|
24448
|
+
if (!a)
|
|
24449
|
+
return c.stop(`Stopped: re-run with --vpce-service <the VpceServiceName of the stack to use>. Nothing was created on PostgresAI yet.`, listed);
|
|
24450
|
+
const i = Number(a);
|
|
24451
|
+
if (Number.isInteger(i) && i >= 1 && i <= ready.length)
|
|
24452
|
+
stack = ready[i - 1];
|
|
24453
|
+
}
|
|
24454
|
+
if (!stack)
|
|
24455
|
+
return c.stop(`No stack picked: re-run with --vpce-service <the VpceServiceName of the stack to use>. Nothing was created on PostgresAI yet.`, listed);
|
|
24456
|
+
}
|
|
24457
|
+
if (stack) {
|
|
24458
|
+
vpce = stack.outputs.VpceServiceName;
|
|
24459
|
+
roleArn ??= stack.outputs.HostStatsRoleArn;
|
|
24460
|
+
readStack = true;
|
|
24461
|
+
progress("privatelink", `Using stack ${stack.name}${stack.created ? ` (created ${utc(stack.created)})` : ""}: ${vpce}`, { stack_name: stack.name });
|
|
24462
|
+
break;
|
|
24463
|
+
}
|
|
24464
|
+
for (const s of found)
|
|
24465
|
+
if (stackState(s.status) === "in_progress")
|
|
24466
|
+
seenMaking.add(s.name);
|
|
24467
|
+
const foreign = found.find((s) => s.name === stackNameFor(ep.id) && stackState(s.status) === "ready" && !s.outputs.VpceServiceName);
|
|
24468
|
+
if (foreign && !found.some((s) => stackState(s.status) === "in_progress")) {
|
|
24469
|
+
return c.stop(`Stack ${foreign.name} has no VpceServiceName output: it was not made from the PostgresAI template. Delete it, create the stack from the link deploy prints, then ${resume()}`, { step: "privatelink_stack", stack_name: foreign.name, region: ep.region });
|
|
24470
|
+
}
|
|
24471
|
+
const failedNow = found.filter((s) => stackState(s.status) === "failed").sort((a, b) => Date.parse(b.created ?? "") - Date.parse(a.created ?? "") || 0);
|
|
24472
|
+
const waits = terminal && !!aws;
|
|
24473
|
+
const failed = failedNow.find(thisRuns) ?? (waits ? undefined : failedNow[0]);
|
|
24474
|
+
for (const s of failedNow) {
|
|
24475
|
+
if (s === failed || saidFailed.has(s.name))
|
|
24476
|
+
continue;
|
|
24477
|
+
saidFailed.add(s.name);
|
|
24478
|
+
progress("privatelink", `Note: stack ${s.name} for ${ep.id} failed earlier (${s.status}): not used`);
|
|
24479
|
+
}
|
|
24480
|
+
if (failed && !found.some((s) => stackState(s.status) === "in_progress")) {
|
|
24481
|
+
const why = (aws && await stackFailure(aws, failed.name, ep.region) || failed.reason || failed.status).replace(/\s*\(RequestId: [^)]*\)/g, "");
|
|
24482
|
+
const fix = /^(CREATE_FAILED|ROLLBACK_|DELETE_FAILED)/.test(failed.status) ? `delete the stack, then re-run pgai mon deploy:
|
|
24483
|
+
aws cloudformation delete-stack --stack-name ${failed.name} --region ${ep.region}` : "repair the stack in the CloudFormation console (Stack actions, Continue update rollback), then re-run pgai mon deploy";
|
|
24484
|
+
return c.stop(`Stack ${failed.name} failed (${failed.status}): ${why}
|
|
24485
|
+
Fix that, ${fix}`, { step: "privatelink_stack", stack_name: failed.name, region: ep.region });
|
|
24486
|
+
}
|
|
24487
|
+
const being = found.find((s) => stackState(s.status) === "in_progress");
|
|
24488
|
+
const first = !shown;
|
|
24489
|
+
shown ??= await show();
|
|
24490
|
+
const making = being ? `Stack ${being.name} is ${/ROLLBACK/.test(being.status) ? "rolling back" : "being created"} (${being.status})` : undefined;
|
|
24491
|
+
if (!terminal || !aws && !ask) {
|
|
24492
|
+
const make = shown.terraform_dir ? `Run terraform init && terraform apply in ${shown.terraform_dir}` : opts.agent ? `Give the user cfn_url to open in the AWS account of ${ep.id}, to create the stack there${shown.choose_in_form ? ` (choosing ${shown.choose_in_form.join(", ")} in the form)` : ""}, or call again with terraform: true for the same as Terraform` : `Open the link below in the AWS account of ${ep.id} and create the stack there${shown.choose_in_form ? ` (choose ${shown.choose_in_form.join(", ")} in the form)` : ""}, or re-run with --terraform for the same as Terraform`;
|
|
24493
|
+
return c.stop(`${making ? `${making}: once it is CREATE_COMPLETE` : `${make}; once it is done`}, ${resume()}. Nothing was created on PostgresAI yet.`, shown);
|
|
24494
|
+
}
|
|
24495
|
+
const openIt = !shown.terraform_dir && deps.openUrl ? ", [o] to open the link in your browser" : "";
|
|
24496
|
+
if (aws) {
|
|
24497
|
+
if (first && openIt && ask) {
|
|
24498
|
+
const a = (await ask(`[o] opens the link in your browser; Enter waits for the stack: `)).trim().toLowerCase();
|
|
24499
|
+
if (a === "o")
|
|
24500
|
+
deps.openUrl(shown.cfn_url);
|
|
24501
|
+
}
|
|
24502
|
+
if (deps.aborted?.())
|
|
24503
|
+
throw new CancelledError;
|
|
24504
|
+
if (deps.now() >= deadline)
|
|
24505
|
+
return c.stop(`No stack for ${ep.id} was ready within ${STACK_WAIT_MS / 60000} minutes: re-run pgai mon deploy once it is. Nothing was created on PostgresAI yet.`, shown);
|
|
24506
|
+
progress("privatelink", making ?? `Waiting for you to create the stack from the link above (any name; deploy finds it by its RDS; up to ${STACK_WAIT_MS / 60000} min)`, { stack_name: being?.name ?? stackNameFor(ep.id) });
|
|
24507
|
+
await deps.sleep(STACK_POLL_MS);
|
|
24508
|
+
continue;
|
|
24509
|
+
}
|
|
24510
|
+
const output = opts.terraform ? "vpce_service_name output" : "VpceServiceName (CloudFormation, the stack, Outputs)";
|
|
24511
|
+
const a = (await ask(`Paste the stack's ${output}${openIt}, or [s] to stop: `, refusedPaste ? [refusedPaste] : [])).trim();
|
|
24512
|
+
if (a.toLowerCase() === "s")
|
|
24513
|
+
return c.stop(`Stopped: once the stack is done, ${resume()}. Nothing was created on PostgresAI yet.`, shown);
|
|
24514
|
+
if (!a) {
|
|
24515
|
+
if (++empties >= 3)
|
|
24516
|
+
return c.stop(`Stopped: once the stack is done, ${resume()}. Nothing was created on PostgresAI yet.`, shown);
|
|
24517
|
+
refusedPaste = `Paste the stack's ${opts.terraform ? "vpce_service_name output" : "VpceServiceName"} once it is created, or s to stop.`;
|
|
24518
|
+
continue;
|
|
24519
|
+
}
|
|
24520
|
+
empties = 0;
|
|
24521
|
+
if (openIt && a.toLowerCase() === "o") {
|
|
24522
|
+
deps.openUrl(shown.cfn_url);
|
|
24523
|
+
refusedPaste = undefined;
|
|
24524
|
+
continue;
|
|
24525
|
+
}
|
|
24526
|
+
refusedPaste = badVpce(a);
|
|
24527
|
+
if (!refusedPaste)
|
|
24528
|
+
vpce = a;
|
|
24529
|
+
else if (++pastes >= 3)
|
|
24530
|
+
return c.stop(refusedPaste, shown);
|
|
24531
|
+
}
|
|
24532
|
+
if (c.hostStats && !roleArn && !readStack) {
|
|
24533
|
+
const out = opts.terraform ? "host_stats_role_arn" : "HostStatsRoleArn";
|
|
24534
|
+
return c.stop(`Host metrics need the metrics role too: ${opts.agent ? "call deploy_monitoring again with" : "re-run with"} ${flag("--vpce-service", "vpce_service")} ${vpce} and ${flag("--host-stats-role", "host_stats_role")} <its ${out} output>. If it has none, it was made without this PostgresAiExternalId: ${opts.terraform ? "set postgres_ai_external_id in terraform.tfvars and apply again" : "update the stack with it"}; or deploy without ${flag("--host-stats", "host_stats")}.
|
|
24535
|
+
PostgresAiExternalId = ${await externalIdOf()}`, { external_id: externalId });
|
|
24536
|
+
}
|
|
24537
|
+
if (c.hostStats && !roleArn) {
|
|
24538
|
+
return c.stop(`Host metrics need the stack's metrics role (its HostStatsRoleArn output), and it has none: update the stack with this PostgresAiExternalId (it then creates a read-only role), then ${resume()}; or deploy without ${flag("--host-stats", "host_stats")}.
|
|
24539
|
+
PostgresAiExternalId = ${await externalIdOf()}`, { external_id: externalId });
|
|
24540
|
+
}
|
|
24541
|
+
return { vpce, ...roleArn ? { roleArn } : {} };
|
|
24542
|
+
}
|
|
24543
|
+
async function privateLinkRole(url, db, opts, deps, progress, stop) {
|
|
24544
|
+
let password = opts.monitoringPassword;
|
|
24545
|
+
let nonce = opts.nonce;
|
|
24546
|
+
const ask = opts.yes ? undefined : deps.ask;
|
|
24547
|
+
const warned = new Set;
|
|
24548
|
+
const warn = (m) => {
|
|
24549
|
+
if (!warned.has(m)) {
|
|
24550
|
+
warned.add(m);
|
|
24551
|
+
progress("preparing", m);
|
|
24552
|
+
}
|
|
24553
|
+
};
|
|
24554
|
+
const resumeWith = (n) => opts.agent ? `call deploy_monitoring again with monitoring_password and nonce from this result` : `re-run with PGAI_MON_NONCE=${n} and PGAI_MON_PASSWORD set to the v_password value near the top of the SQL`;
|
|
24555
|
+
if (password === undefined || nonce === undefined) {
|
|
24556
|
+
let p;
|
|
24557
|
+
try {
|
|
24558
|
+
p = await deps.rdsPrepareSql(db);
|
|
24559
|
+
} catch (err) {
|
|
24560
|
+
if (err instanceof HttpStatusError && err.status === 403)
|
|
24561
|
+
return stop(notAdminNext);
|
|
24562
|
+
if (err instanceof HttpStatusError && err.status < 500)
|
|
24563
|
+
return stop(errorText(err));
|
|
24564
|
+
throw err;
|
|
24565
|
+
}
|
|
24566
|
+
if (!/^[A-Za-z0-9_-]{1,128}$/.test(p.nonce))
|
|
24567
|
+
throw new Error("cloud_monitoring_rds_prepare_sql returned a malformed nonce: nothing was changed");
|
|
24568
|
+
password = p.db_password;
|
|
24569
|
+
nonce = p.nonce;
|
|
24570
|
+
progress("preparing", `Preparing ${maskConnectionString(url)}`);
|
|
24571
|
+
const failed = await deps.runSql(inDatabase(url, db), p.prepare_db_sql, warn).then(() => {
|
|
24572
|
+
return;
|
|
24573
|
+
}, (err) => err);
|
|
24574
|
+
if (failed) {
|
|
24575
|
+
const e = failed;
|
|
24576
|
+
const refusedLogin = typeof e !== "object" || !e || !("severity" in e) || e.severity === "FATAL" || /^(28|3D|08)/.test(String(e.code ?? ""));
|
|
24577
|
+
if (refusedLogin)
|
|
24578
|
+
return stop(`Could not log in to ${maskConnectionString(inDatabase(url, db))}: ${errorText(failed)}. Check the URL's user and password, and run pgai mon deploy where the database is reachable (in its VPC)`);
|
|
24579
|
+
const why = `The user in this URL (${decodeURIComponent(new URL(url).username)}) can't create ${DEFAULT_MONITORING_USER} (${errorText(failed)})`;
|
|
24580
|
+
const printed = { step: "prepare_db", sql: p.prepare_db_sql, nonce, monitoring_password: password };
|
|
24581
|
+
const run = `Run the SQL as the master user, connected to database ${db}`;
|
|
24582
|
+
const only = `A deploy of this database without ${opts.agent ? "them" : "PGAI_MON_PASSWORD and PGAI_MON_NONCE"} issues a new SQL and replaces this one`;
|
|
24583
|
+
if (!ask)
|
|
24584
|
+
return stop(`${why}. ${run}, then ${resumeWith(nonce)}. ${only}.`, printed);
|
|
24585
|
+
const a = (await ask(`${run}, then press Enter ([s] to stop): `, [`${why}. The SQL:`, "", p.prepare_db_sql, ""])).trim().toLowerCase();
|
|
24586
|
+
if (a === "s" || a === "stop")
|
|
24587
|
+
return stop(`Stopped. ${run}, then ${resumeWith(nonce)}.`, { ...printed, sql: undefined });
|
|
24588
|
+
}
|
|
24589
|
+
}
|
|
24590
|
+
progress("preparing", `Checking that ${DEFAULT_MONITORING_USER} can log in to ${db} with the password the SQL set`);
|
|
24591
|
+
let found;
|
|
24592
|
+
try {
|
|
24593
|
+
found = await deps.readNonce(roleUrl(url, db, password), warn);
|
|
24594
|
+
} catch (err) {
|
|
24595
|
+
const fresh = opts.agent ? "call again without monitoring_password and nonce for a new SQL" : "re-run without PGAI_MON_PASSWORD and PGAI_MON_NONCE for a new SQL";
|
|
24596
|
+
return stop(opts.monitoringPassword === undefined ? `${DEFAULT_MONITORING_USER} could not log in to ${db} with the password the SQL set (${errorText(err)}): run the SQL again, connected to database ${db}` : opts.agent ? `${DEFAULT_MONITORING_USER} could not log in to ${db} with monitoring_password (${errorText(err)}): pass the monitoring_password of the prepare_db result whose SQL ran, or ${fresh}` : `${DEFAULT_MONITORING_USER} could not log in to ${db} with PGAI_MON_PASSWORD (${errorText(err)}): set it to the v_password value near the top of the SQL you ran, or ${fresh}`);
|
|
24597
|
+
}
|
|
24598
|
+
const again = opts.monitoringPassword !== undefined ? `${opts.agent ? "call again without monitoring_password and nonce" : "re-run without PGAI_MON_PASSWORD and PGAI_MON_NONCE"} for a new SQL, and run only that one` : "re-run pgai mon deploy";
|
|
24599
|
+
if (found === undefined)
|
|
24600
|
+
return stop(`The SQL has not run in database ${db} (perhaps in another database): run it connected to database ${db}, then ${resumeWith(nonce)}`);
|
|
24601
|
+
if (found !== nonce)
|
|
24602
|
+
return stop(`A newer PostgresAI setup SQL ran in database ${db} after this one (another deploy, or the Console): ${again}`);
|
|
24603
|
+
return { password, nonce };
|
|
24604
|
+
}
|
|
23881
24605
|
var STILL_IN_PROGRESS = "Still in progress";
|
|
23882
24606
|
var CONNECT_TIMEOUT_MS = 55000;
|
|
23883
24607
|
var AGENT_CONNECT_TIMEOUT_MS = 25000;
|
|
@@ -23911,7 +24635,14 @@ async function follow(row, f, opts, deps, progress) {
|
|
|
23911
24635
|
const url = (_) => f.url ?? name;
|
|
23912
24636
|
const billing = { ...f.billing, ...f.place };
|
|
23913
24637
|
let shown;
|
|
24638
|
+
let unverified;
|
|
23914
24639
|
for (const deadline = deps.now() + opts.waitMs;; ) {
|
|
24640
|
+
if (f.verify) {
|
|
24641
|
+
const why = await f.verify(row);
|
|
24642
|
+
if (why && why !== unverified)
|
|
24643
|
+
progress("box", `Note: ${why}`, { id: row.id });
|
|
24644
|
+
unverified = why;
|
|
24645
|
+
}
|
|
23915
24646
|
if (row.billing_error && disconnecting(row.status)) {
|
|
23916
24647
|
if (undoRole)
|
|
23917
24648
|
await undoRole();
|
|
@@ -23941,15 +24672,16 @@ async function follow(row, f, opts, deps, progress) {
|
|
|
23941
24672
|
final.next += "; for CPU, memory and disk, delete the instance (pgai mon instances delete) and deploy again with --clickhouse-key <key-id>:<key-secret>";
|
|
23942
24673
|
}
|
|
23943
24674
|
const { next, ...rest } = final;
|
|
24675
|
+
const why = unverified && final.status !== "ready" ? `${unverified} ` : "";
|
|
23944
24676
|
if (final.status === "in_progress" && opts.waitMs > 0) {
|
|
23945
24677
|
return {
|
|
23946
24678
|
...rest,
|
|
23947
24679
|
status: "action_required",
|
|
23948
24680
|
...billing,
|
|
23949
|
-
next: `${STILL_IN_PROGRESS} (box: ${boxWords(boxState(row))}) when the wait ended: resume with ${next}${note}`
|
|
24681
|
+
next: `${STILL_IN_PROGRESS} (box: ${boxWords(boxState(row))}) when the wait ended: ${why}${why ? "Resume" : "resume"} with ${next}${note}`
|
|
23950
24682
|
};
|
|
23951
24683
|
}
|
|
23952
|
-
return { ...rest, ...billing, next: next + note };
|
|
24684
|
+
return { ...rest, ...billing, next: why + next + note };
|
|
23953
24685
|
}
|
|
23954
24686
|
await deps.sleep(POLL_MS);
|
|
23955
24687
|
const listed = await deps.list().catch(() => {
|
|
@@ -24030,7 +24762,8 @@ async function prepareDatabase(url, provider, opts = {}) {
|
|
|
24030
24762
|
or (pg_has_role(current_user, 'pg_monitor', 'USAGE WITH ADMIN OPTION')
|
|
24031
24763
|
and pg_has_role(current_user, 'pg_read_all_stats', 'USAGE WITH ADMIN OPTION')))
|
|
24032
24764
|
and has_database_privilege(current_user, current_database(), 'CREATE')
|
|
24033
|
-
and exists (select 1 from pg_extension where extname = 'pg_stat_statements')
|
|
24765
|
+
and (exists (select 1 from pg_extension where extname = 'pg_stat_statements')
|
|
24766
|
+
or exists (select 1 from pg_roles where rolname = 'rds_superuser' and pg_has_role(current_user, oid, 'MEMBER')))) as admin,
|
|
24034
24767
|
exists (select 1 from pg_roles where rolname = ${quoteLiteral(role)}) as mon_exists,
|
|
24035
24768
|
exists (select 1 from pg_roles where rolname = ${quoteLiteral(role)} and (rolsuper or rolcreatedb or rolcreaterole or rolreplication or rolbypassrls)) as mon_privileged,
|
|
24036
24769
|
(select string_agg(g.rolname::text, ', ' order by g.rolname::text collate "C") from pg_roles m join pg_roles g
|
|
@@ -24281,10 +25014,50 @@ function platformDeps(p) {
|
|
|
24281
25014
|
supabaseConnect: (body) => rpc("cloud_monitoring_supabase_connect", body),
|
|
24282
25015
|
supabaseProjects: () => rpc("cloud_monitoring_supabase_projects", {}),
|
|
24283
25016
|
deployOptions: (args) => rpc("monitoring_deploy_options", args),
|
|
24284
|
-
|
|
24285
|
-
|
|
24286
|
-
|
|
24287
|
-
|
|
25017
|
+
rdsExternalId: async () => (await rpc("cloud_monitoring_rds_external_id")).external_id,
|
|
25018
|
+
rdsPrepareSql: (dbName) => rpc("cloud_monitoring_rds_prepare_sql", { db_name: dbName }),
|
|
25019
|
+
rdsProvision: (body) => rpc("cloud_monitoring_rds_provision", body, p.agent ? AGENT_CONNECT_TIMEOUT_MS : CONNECT_TIMEOUT_MS),
|
|
25020
|
+
rdsStatus: (id) => rpc("cloud_monitoring_rds_status", { instance_id: id }),
|
|
25021
|
+
aws: awsRunner,
|
|
25022
|
+
awsProfiles: () => new Promise((resolve) => execFile2("aws", ["configure", "list-profiles"], { timeout: 1e4 }, (err, stdout) => resolve(err ? [] : stdout.split(`
|
|
25023
|
+
`).map((l) => l.trim()).filter(Boolean)))),
|
|
25024
|
+
publicAddresses: (host) => publicAddresses(host),
|
|
25025
|
+
runSql: async (url, sql, warn) => {
|
|
25026
|
+
const { client } = await openConnection(url, { agent: p.agent, warn });
|
|
25027
|
+
try {
|
|
25028
|
+
await client.query(sql);
|
|
25029
|
+
} finally {
|
|
25030
|
+
await client.end().catch(() => {});
|
|
25031
|
+
}
|
|
25032
|
+
},
|
|
25033
|
+
readNonce: async (url, warn) => {
|
|
25034
|
+
const { client } = await openConnection(url, { agent: p.agent, warn });
|
|
25035
|
+
try {
|
|
25036
|
+
const r = await client.query("select nonce from postgres_ai.monitoring_ownership order by issued_at desc limit 1");
|
|
25037
|
+
return r.rows[0]?.nonce;
|
|
25038
|
+
} catch (err) {
|
|
25039
|
+
if (["3F000", "42P01"].includes(err.code ?? ""))
|
|
25040
|
+
return;
|
|
25041
|
+
throw err;
|
|
25042
|
+
} finally {
|
|
25043
|
+
await client.end().catch(() => {});
|
|
25044
|
+
}
|
|
25045
|
+
},
|
|
25046
|
+
terraformModule: async () => {
|
|
25047
|
+
const r = await fetch(TERRAFORM_URL, { signal: AbortSignal.timeout(30000) });
|
|
25048
|
+
if (!r.ok)
|
|
25049
|
+
throw new Error(`Could not download the Terraform module (${TERRAFORM_URL}: HTTP ${r.status})`);
|
|
25050
|
+
return r.text();
|
|
25051
|
+
},
|
|
25052
|
+
writeTerraform: async (dirName, files) => {
|
|
25053
|
+
const dir = path5.resolve(dirName);
|
|
25054
|
+
await fs6.promises.mkdir(dir, { recursive: true });
|
|
25055
|
+
await fs6.promises.writeFile(path5.join(dir, "main.tf"), files.mainTf);
|
|
25056
|
+
await fs6.promises.writeFile(path5.join(dir, "terraform.tfvars"), files.tfvars, { flag: "wx" }).catch((err) => {
|
|
25057
|
+
if (err.code !== "EEXIST")
|
|
25058
|
+
throw err;
|
|
25059
|
+
});
|
|
25060
|
+
return dir;
|
|
24288
25061
|
},
|
|
24289
25062
|
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
24290
25063
|
now: () => Date.now()
|
|
@@ -30963,6 +31736,7 @@ async function handleToolCall(req, rootOpts, extra) {
|
|
|
30963
31736
|
}
|
|
30964
31737
|
try {
|
|
30965
31738
|
if (toolName === "deploy_monitoring") {
|
|
31739
|
+
const str = (v) => v === undefined || v === null ? undefined : String(v);
|
|
30966
31740
|
const scope = resolveMcpOrgScope(args, apiKey, cfg);
|
|
30967
31741
|
if (scope.error)
|
|
30968
31742
|
return scope.error;
|
|
@@ -30982,6 +31756,15 @@ async function handleToolCall(req, rootOpts, extra) {
|
|
|
30982
31756
|
location: args.location === undefined || args.location === null ? undefined : String(args.location),
|
|
30983
31757
|
ipFirst: args.ip_first === true,
|
|
30984
31758
|
ipAllowed: args.ip_allowed === true,
|
|
31759
|
+
privatelink: typeof args.privatelink === "boolean" ? args.privatelink : undefined,
|
|
31760
|
+
vpceService: str(args.vpce_service),
|
|
31761
|
+
hostStats: args.host_stats === true,
|
|
31762
|
+
hostStatsRole: str(args.host_stats_role),
|
|
31763
|
+
awsProfile: str(args.aws_profile),
|
|
31764
|
+
aws: args.no_aws === true ? false : undefined,
|
|
31765
|
+
terraform: args.terraform === true,
|
|
31766
|
+
monitoringPassword: str(args.monitoring_password),
|
|
31767
|
+
nonce: str(args.nonce),
|
|
30985
31768
|
waitMs: 0,
|
|
30986
31769
|
agent: true
|
|
30987
31770
|
}, {
|
|
@@ -31556,7 +32339,7 @@ async function startMcpServer(rootOpts, extra) {
|
|
|
31556
32339
|
},
|
|
31557
32340
|
{
|
|
31558
32341
|
name: "deploy_monitoring",
|
|
31559
|
-
description: "Put a Postgres database under PostgresAI Cloud monitoring (same as `pgai mon deploy`): prepares the monitoring role (an admin URL creates it; otherwise returns the SQL), provisions the monitoring box, and returns JSON with status (ready | in_progress | deleting | deleted | inactive | action_required | failed: the same words as pgai dblab deploy), health_url (the project's Health Matrix in the console), dashboard_url (Grafana, signing in with PostgresAI) and next (the exact next action). A billed box is provisioned only with yes: true: without it the result is action_required with the price (and requires_payment_method), so show the price to the user first. It does not wait for the box: call it again to see the status. Safe to call again. When PostgresAI can't reach the database the way an IP allowlist refuses (or with ip_first), the box's IP is reserved first and the result is action_required with step allow_ip (ip, cidr, host, port, reserved_until): give the user the cidr and port to allow on the database, then call again with ip_allowed: true. ClickHouse Managed Postgres, RDS and Supabase are detected from the host. Supabase needs no password (PostgresAI prepares the database through the Supabase Management API): the first time, the result is action_required with a consent link in next; give it to the user to open and approve, then call again. Elsewhere the URL must carry its password, and only these query parameters: sslmode, channel_binding, application_name. PGAI_MON_PASSWORD, PGPASSWORD and CLICKHOUSE_KEY_ID + CLICKHOUSE_KEY_SECRET of the server process are not used (pass clickhouse_key), and a TLS failure is not retried in plaintext (say sslmode=disable for a server without TLS). For another database on a server PostgresAI already monitors for the org, an admin URL with sslmode=require (or verify-*), each query parameter once, is enough: PostgresAI fills in the password of postgres_ai_mon it keeps, over TLS only (on a server without TLS, pass the postgres_ai_mon URL; if nobody has its password, the user runs in a terminal: pgai mon instances delete the server's other databases, then pgai mon deploy <admin-url> --reset-password with PGAI_MON_PASSWORD set to a new one, then deploys them again with it).",
|
|
32342
|
+
description: "Put a Postgres database under PostgresAI Cloud monitoring (same as `pgai mon deploy`): prepares the monitoring role (an admin URL creates it; otherwise returns the SQL), provisions the monitoring box, and returns JSON with status (ready | in_progress | deleting | deleted | inactive | action_required | failed: the same words as pgai dblab deploy), health_url (the project's Health Matrix in the console), dashboard_url (Grafana, signing in with PostgresAI) and next (the exact next action). A billed box is provisioned only with yes: true: without it the result is action_required with the price (and requires_payment_method), so show the price to the user first. It does not wait for the box: call it again to see the status. Safe to call again. When PostgresAI can't reach the database the way an IP allowlist refuses (or with ip_first), the box's IP is reserved first and the result is action_required with step allow_ip (ip, cidr, host, port, reserved_until): give the user the cidr and port to allow on the database, then call again with ip_allowed: true. ClickHouse Managed Postgres, RDS and Supabase are detected from the host. An RDS instance that is not publicly accessible is monitored over AWS PrivateLink (no public IP, nothing opened): the user creates a CloudFormation stack in their AWS account from a link. Until the stack exists the result is action_required with step privatelink_stack and cfn_url: give the user the link to open in the RDS's AWS account (the stack keeps its name); once they say it is created, call again. When this machine's aws CLI can read the RDS (read-only; aws_profile picks the profile, no_aws: true skips it), the link is filled in and the stack is found by itself; otherwise pass vpce_service, the stack's VpceServiceName output. When the URL's user can't create the monitoring role, the result is action_required with step prepare_db, sql, nonce and monitoring_password: the user runs the SQL as the master user in that database, then call again with monitoring_password and nonce. host_stats: true adds host CPU, memory, disk and IOPS through a read-only role the stack creates (host_stats_role: its HostStatsRoleArn output, when not read from AWS). Aurora and RDS Proxy endpoints are not supported over PrivateLink. Supabase needs no password (PostgresAI prepares the database through the Supabase Management API): the first time, the result is action_required with a consent link in next; give it to the user to open and approve, then call again. Elsewhere the URL must carry its password, and only these query parameters: sslmode, channel_binding, application_name. PGAI_MON_PASSWORD, PGPASSWORD and CLICKHOUSE_KEY_ID + CLICKHOUSE_KEY_SECRET of the server process are not used (pass clickhouse_key), and a TLS failure is not retried in plaintext (say sslmode=disable for a server without TLS). For another database on a server PostgresAI already monitors for the org, an admin URL with sslmode=require (or verify-*), each query parameter once, is enough: PostgresAI fills in the password of postgres_ai_mon it keeps, over TLS only (on a server without TLS, pass the postgres_ai_mon URL; if nobody has its password, the user runs in a terminal: pgai mon instances delete the server's other databases, then pgai mon deploy <admin-url> --reset-password with PGAI_MON_PASSWORD set to a new one, then deploys them again with it).",
|
|
31560
32343
|
inputSchema: {
|
|
31561
32344
|
type: "object",
|
|
31562
32345
|
properties: {
|
|
@@ -31569,9 +32352,18 @@ async function startMcpServer(rootOpts, extra) {
|
|
|
31569
32352
|
location: { type: "string", description: "A location of that cloud, from the result's choices (default: the database's region, else the cloud's default)" },
|
|
31570
32353
|
coupon: { type: "string", description: "Stripe promotion code for the organization's monitoring subscription (applies when it is first created)" },
|
|
31571
32354
|
vcpus: { type: "integer", minimum: 1, maximum: 1024, description: "vCPUs of the database server. Without it there is no AAS (database load) data for this database: ask the user for it" },
|
|
31572
|
-
monitoring_user: { type: "string", description: "Monitoring role to create or use (default postgres_ai_mon). A custom role always carries its own password: PostgresAI keeps only postgres_ai_mon's, so pass the role's own URL as database_url (an admin URL creates only postgres_ai_mon). Not for Supabase, where PostgresAI creates postgres_ai_mon" },
|
|
32355
|
+
monitoring_user: { type: "string", description: "Monitoring role to create or use (default postgres_ai_mon). A custom role always carries its own password: PostgresAI keeps only postgres_ai_mon's, so pass the role's own URL as database_url (an admin URL creates only postgres_ai_mon). Not for Supabase, where PostgresAI creates postgres_ai_mon, nor for a private RDS over AWS PrivateLink" },
|
|
31573
32356
|
ip_first: { type: "boolean", description: "Reserve the monitoring box's IP and stop before the box (even when PostgresAI can reach the database), so the user can allow it on the database first. The result is action_required with step allow_ip: ip, cidr, host, port, reserved_until" },
|
|
31574
32357
|
ip_allowed: { type: "boolean", description: "The database now allows the reserved IP (a previous result's step allow_ip): create the box with it. Give the user the cidr and port to allow first, and call with ip_allowed: true only once they confirm" },
|
|
32358
|
+
privatelink: { type: "boolean", description: "RDS: true monitors it over AWS PrivateLink (it is not publicly accessible), false directly (it is). Default: decided from AWS or public DNS; asked (action_required) when neither tells" },
|
|
32359
|
+
vpce_service: { type: "string", description: "RDS over PrivateLink: the stack's VpceServiceName output (com.amazonaws.vpce.<region>.vpce-svc-<id>), from the user, after step privatelink_stack" },
|
|
32360
|
+
host_stats: { type: "boolean", description: "RDS over PrivateLink: also host CPU, memory, disk and IOPS, through a read-only role the stack creates in the user's account" },
|
|
32361
|
+
host_stats_role: { type: "string", description: "RDS over PrivateLink with host_stats: the stack's HostStatsRoleArn output, when it is not read from AWS" },
|
|
32362
|
+
aws_profile: { type: "string", description: "RDS: the aws CLI profile that reads the RDS and its stack (read-only); default: the aws CLI's own (AWS_PROFILE, default, the instance role)" },
|
|
32363
|
+
no_aws: { type: "boolean", description: "RDS: do not use the aws CLI on this machine" },
|
|
32364
|
+
terraform: { type: "boolean", description: "RDS over PrivateLink: write the stack as a Terraform module (in the current directory) instead of the CloudFormation link" },
|
|
32365
|
+
monitoring_password: { type: "string", description: "RDS over PrivateLink: the monitoring_password of a prepare_db result whose SQL the user ran (with nonce)" },
|
|
32366
|
+
nonce: { type: "string", description: "RDS over PrivateLink: the nonce of that prepare_db result" },
|
|
31575
32367
|
debug: { type: "boolean", description: "Enable verbose debug logs" }
|
|
31576
32368
|
},
|
|
31577
32369
|
required: ["database_url"],
|
|
@@ -32474,6 +33266,9 @@ function renderMarkdownForTerminal(md) {
|
|
|
32474
33266
|
}
|
|
32475
33267
|
|
|
32476
33268
|
// lib/connect.ts
|
|
33269
|
+
import { execFile as execFile3 } from "node:child_process";
|
|
33270
|
+
import * as fs7 from "node:fs";
|
|
33271
|
+
import * as path6 from "node:path";
|
|
32477
33272
|
var clickhouseKeyHeld = (d) => d.host_metrics && (d.host_metrics_source === undefined || d.host_metrics_source === "clickhouse");
|
|
32478
33273
|
function httpStatus2(err) {
|
|
32479
33274
|
const status = err instanceof Error ? err.status : undefined;
|
|
@@ -32710,8 +33505,11 @@ function stepProgress(o) {
|
|
|
32710
33505
|
view.start();
|
|
32711
33506
|
}
|
|
32712
33507
|
view.advance(e.event);
|
|
32713
|
-
|
|
33508
|
+
const kept = e.event === "privatelink" && (!!e.cfn_url || e.message.startsWith("Using stack "));
|
|
33509
|
+
if (/^(Note|Warning):/.test(e.message) || kept)
|
|
32714
33510
|
view.log(e.message);
|
|
33511
|
+
if (e.event === "privatelink" && !kept && !/^(Note|Warning):/.test(e.message))
|
|
33512
|
+
view.setNote(e.message);
|
|
32715
33513
|
if (e.event === "box" && e.state) {
|
|
32716
33514
|
view.setNote(e.state === "waiting_for_access" && e.ip ? `box: waiting for database access from ${e.ip}` : `box: ${boxWords2(e.state)}`);
|
|
32717
33515
|
if (e.state === "waiting_for_access" && e.ip && e.error && e.error !== refused) {
|
|
@@ -32729,7 +33527,7 @@ function stepProgress(o) {
|
|
|
32729
33527
|
function stepViewEnd(r) {
|
|
32730
33528
|
if (r.status === "ready")
|
|
32731
33529
|
return "complete";
|
|
32732
|
-
if (r.status === "in_progress" || r.step
|
|
33530
|
+
if (r.status === "in_progress" || r.step || r.next.startsWith(STILL_IN_PROGRESS2))
|
|
32733
33531
|
return "stop";
|
|
32734
33532
|
return "fail";
|
|
32735
33533
|
}
|
|
@@ -32750,6 +33548,7 @@ function personLines(r) {
|
|
|
32750
33548
|
...r.price ? [` Price: ${r.price}`] : [],
|
|
32751
33549
|
...r.cloud ? [` Runs on: ${cloudWord2(r.cloud)}, ${r.location}`] : [],
|
|
32752
33550
|
...r.egress_ip ? [` Connects from: ${r.egress_ip}`] : [],
|
|
33551
|
+
...r.provider === "rds" && r.host_metrics ? [" Host metrics: on (CPU, memory, disk, IOPS)"] : [],
|
|
32753
33552
|
...!r.health_url && !r.dashboard_url && r.next && r.next !== "none" ? [`Next: ${r.next}`] : [],
|
|
32754
33553
|
...extra && (r.health_url || r.dashboard_url) ? [`Note: ${extra}`] : []
|
|
32755
33554
|
];
|
|
@@ -32770,6 +33569,8 @@ function personLines(r) {
|
|
|
32770
33569
|
}
|
|
32771
33570
|
if (r.projects?.length)
|
|
32772
33571
|
return [r.next, ...supabaseProjectRows(r.projects)];
|
|
33572
|
+
if (r.step === "privatelink_stack" && r.cfn_url && !r.terraform_dir)
|
|
33573
|
+
return [r.next, ` ${r.cfn_url}`];
|
|
32773
33574
|
return r.choices ? [r.next, ...Object.entries(r.choices).map(([cloud, codes]) => ` --cloud ${cloud} --location ${codes.join("|")}`)] : [r.next];
|
|
32774
33575
|
}
|
|
32775
33576
|
}
|
|
@@ -32887,9 +33688,17 @@ async function connect2(url, opts, deps) {
|
|
|
32887
33688
|
const key = parseClickhouseKey2(opts.clickhouseKey, provider === "clickhouse" && !opts.agent ? process.env : {});
|
|
32888
33689
|
if (key && provider !== "clickhouse")
|
|
32889
33690
|
throw new Error("--clickhouse-key applies to ClickHouse Managed Postgres only");
|
|
33691
|
+
const plFlag = privateLinkFlag2(opts);
|
|
32890
33692
|
if (!opts.selfHosted && provider === "rds") {
|
|
32891
|
-
const
|
|
32892
|
-
|
|
33693
|
+
const route = await rdsRoute2(url, opts, deps, progress);
|
|
33694
|
+
if ("status" in route)
|
|
33695
|
+
return { ...route, provider, name };
|
|
33696
|
+
if (route.private)
|
|
33697
|
+
return deployPrivateLink2(url, route, { ...opts, monitoringUser }, deps, progress);
|
|
33698
|
+
if (plFlag)
|
|
33699
|
+
return { status: "action_required", provider, name, next: `${plFlag} applies to AWS PrivateLink only, and ${name} is monitored directly${opts.privatelink === false ? ` (${opts.agent ? "privatelink: false" : "--no-privatelink"})` : " (it is publicly accessible)"}` };
|
|
33700
|
+
} else if (plFlag) {
|
|
33701
|
+
return { status: "action_required", provider, name, next: `${plFlag} applies to Amazon RDS monitored from PostgresAI Cloud only` };
|
|
32893
33702
|
}
|
|
32894
33703
|
if (opts.selfHosted && opts.resetPassword) {
|
|
32895
33704
|
return { status: "action_required", provider, name, next: "--reset-password works with PostgresAI Cloud only (it checks what else monitors this server): deploy without --self-hosted, or set PGAI_MON_PASSWORD" };
|
|
@@ -33664,6 +34473,559 @@ async function supabaseBox2(ref, name, opts, deps, progress, stop, label) {
|
|
|
33664
34473
|
return { ...connectStatus2(box, provider, true), ...b.billing };
|
|
33665
34474
|
return follow2(box, { provider, name, fresh: true, billing: b.billing, billingUrl: b.billingUrl, note: "", place }, opts, deps, progress);
|
|
33666
34475
|
}
|
|
34476
|
+
var PL2 = "AWS PrivateLink";
|
|
34477
|
+
var STACK_WAIT_MS2 = 30 * 60000;
|
|
34478
|
+
var STACK_POLL_MS2 = 15000;
|
|
34479
|
+
function privateLinkFlag2(opts) {
|
|
34480
|
+
const named = [
|
|
34481
|
+
[opts.privatelink === true ? true : undefined, "--privatelink", "privatelink"],
|
|
34482
|
+
[opts.vpceService, "--vpce-service", "vpce_service"],
|
|
34483
|
+
[opts.hostStats ? true : undefined, "--host-stats", "host_stats"],
|
|
34484
|
+
[opts.hostStatsRole, "--host-stats-role", "host_stats_role"],
|
|
34485
|
+
[opts.terraform ? true : undefined, "--terraform", "terraform"],
|
|
34486
|
+
[opts.agent ? opts.nonce : undefined, "PGAI_MON_NONCE", "nonce"]
|
|
34487
|
+
];
|
|
34488
|
+
const hit = named.find(([v]) => v !== undefined);
|
|
34489
|
+
return hit && (opts.agent ? hit[2] : hit[1]);
|
|
34490
|
+
}
|
|
34491
|
+
async function rdsRoute2(url, opts, deps, progress) {
|
|
34492
|
+
const host = new URL(url).hostname.toLowerCase().replace(/\.$/, "");
|
|
34493
|
+
const ep = rdsEndpoint(host);
|
|
34494
|
+
const flag = (cli, agent) => opts.agent ? agent : cli;
|
|
34495
|
+
const stop = (next) => ({ status: "action_required", next });
|
|
34496
|
+
if (opts.privatelink === false)
|
|
34497
|
+
return { private: false };
|
|
34498
|
+
const asked = opts.privatelink === true;
|
|
34499
|
+
let read = {};
|
|
34500
|
+
if (ep?.kind === "instance" && ep.partition === "aws" && opts.aws !== false && deps.aws)
|
|
34501
|
+
read = await readAws2(ep, opts, deps, progress);
|
|
34502
|
+
let isPublic = read.inst?.publiclyAccessible;
|
|
34503
|
+
if (isPublic === undefined && ep && deps.publicAddresses) {
|
|
34504
|
+
progress("preparing", `Looking up ${host} in public DNS (cloudflare-dns.com, dns.google): is it publicly accessible?`);
|
|
34505
|
+
const addresses = await deps.publicAddresses(host);
|
|
34506
|
+
if (addresses?.length) {
|
|
34507
|
+
isPublic = !addresses.every(isPrivateAddress);
|
|
34508
|
+
progress("preparing", `${host} resolves to a ${isPublic ? "public address in public DNS, so it is monitored directly" : `private address in public DNS, so it is monitored over ${PL2}`}`);
|
|
34509
|
+
}
|
|
34510
|
+
}
|
|
34511
|
+
if (isPublic === true) {
|
|
34512
|
+
if (asked)
|
|
34513
|
+
return stop(`${host} is publicly accessible, so it is monitored directly: deploy without ${flag("--privatelink", "privatelink")}`);
|
|
34514
|
+
return { private: false };
|
|
34515
|
+
}
|
|
34516
|
+
if (isPublic === undefined && !asked) {
|
|
34517
|
+
if (!deps.ask || opts.yes)
|
|
34518
|
+
return stop(`Could not tell whether ${host} is publicly accessible (AWS ${opts.aws === false ? "not read" : "not readable here"}, and public DNS did not answer): re-run with ${flag("--privatelink", "privatelink: true")} if it is not, or ${flag("--no-privatelink", "privatelink: false")} if it is`);
|
|
34519
|
+
const a = (await deps.ask(`Is ${host} publicly accessible? [y]es, monitor it directly / [n]o, over ${PL2} / Enter to stop: `)).trim().toLowerCase();
|
|
34520
|
+
if (a === "y" || a === "yes")
|
|
34521
|
+
return { private: false };
|
|
34522
|
+
if (a !== "n" && a !== "no")
|
|
34523
|
+
return stop("Stopped: nothing was created");
|
|
34524
|
+
}
|
|
34525
|
+
if (!ep)
|
|
34526
|
+
return stop(`${PL2} needs the RDS instance endpoint (<instance>.<id>.<region>.rds.amazonaws.com) in the URL, not ${host}; or monitor from this network with --self-hosted`);
|
|
34527
|
+
if (ep.partition !== "aws")
|
|
34528
|
+
return stop(`${ep.partition === "aws-cn" ? "AWS China" : "AWS GovCloud"} can't reach PostgresAI over ${PL2} (it does not cross AWS partitions): monitor from this network with --self-hosted`);
|
|
34529
|
+
if (ep.kind !== "instance" || read.inst?.engine?.startsWith("aurora"))
|
|
34530
|
+
return stop(`${ep.kind === "proxy" ? "An RDS Proxy endpoint" : "Aurora"} is not supported over ${PL2} yet: monitor from this network with --self-hosted, or ask support@postgres.ai`);
|
|
34531
|
+
return { private: true, ep, ...read };
|
|
34532
|
+
}
|
|
34533
|
+
async function readAws2(ep, opts, deps, progress) {
|
|
34534
|
+
let profile = opts.awsProfile?.trim() || undefined;
|
|
34535
|
+
for (let tries = 0;tries < 3; tries++) {
|
|
34536
|
+
const run = deps.aws(profile);
|
|
34537
|
+
const of = profile ? ` (profile ${profile})` : "";
|
|
34538
|
+
let why;
|
|
34539
|
+
let missing = false;
|
|
34540
|
+
try {
|
|
34541
|
+
const me = await callerIdentity(run);
|
|
34542
|
+
const inst = await describeDbInstance(run, ep.id, ep.region);
|
|
34543
|
+
progress("preparing", `AWS account ${me.account} (${me.arn.split(":").pop()}), read-only: ${ep.id} is ${inst.publiclyAccessible ? "publicly accessible, so it is monitored directly" : `not publicly accessible, so it is monitored over ${PL2}`}`);
|
|
34544
|
+
return { aws: run, inst };
|
|
34545
|
+
} catch (err) {
|
|
34546
|
+
if (!(err instanceof AwsError))
|
|
34547
|
+
throw err;
|
|
34548
|
+
missing = err.kind === "missing";
|
|
34549
|
+
why = missing ? "the aws CLI is not installed here" : err.kind === "not_found" ? `${ep.id} is not in ${ep.region} of this AWS account${of}` : err.kind === "credentials" ? `the AWS credentials${of} do not work: ${err.message}` : `AWS refused${of}: ${err.message}`;
|
|
34550
|
+
}
|
|
34551
|
+
if (missing || !deps.ask || opts.yes) {
|
|
34552
|
+
progress("preparing", `Note: ${why}; going on without AWS${missing ? "" : ` (${opts.agent ? "aws_profile" : "--aws-profile"} picks a profile)`}`);
|
|
34553
|
+
return {};
|
|
34554
|
+
}
|
|
34555
|
+
const profiles = deps.awsProfiles ? await deps.awsProfiles().catch(() => []) : [];
|
|
34556
|
+
const a = (await deps.ask(`AWS profile to read ${ep.id} with${profiles.length ? ` (${profiles.join(", ")})` : ""}, or Enter to go on without AWS: `, [`${why}.`])).trim();
|
|
34557
|
+
if (!a)
|
|
34558
|
+
return {};
|
|
34559
|
+
profile = a;
|
|
34560
|
+
}
|
|
34561
|
+
return {};
|
|
34562
|
+
}
|
|
34563
|
+
var VERIFY_FAILED2 = {
|
|
34564
|
+
unreachable: "The monitoring box could not reach the database over PrivateLink: check that the stack finished, its load balancer's target is healthy, and the RDS is not publicly accessible.",
|
|
34565
|
+
auth_failed: `The monitoring box reached the database, but ${DEFAULT_MONITORING_USER} could not log in: its password changed after this deploy set it (another setup ran its SQL).`,
|
|
34566
|
+
nonce_mismatch: "A newer PostgresAI setup SQL ran in this database after this deploy's (another deploy, or the Console), so monitoring does not start."
|
|
34567
|
+
};
|
|
34568
|
+
async function privateLinkOf(d, rdsStatus) {
|
|
34569
|
+
if (!rdsStatus || disconnecting2(d.status))
|
|
34570
|
+
return;
|
|
34571
|
+
if (d.cluster && d.provider !== "rds" && d.provider !== "aurora")
|
|
34572
|
+
return;
|
|
34573
|
+
return rdsStatus(d.id).catch(() => {
|
|
34574
|
+
return;
|
|
34575
|
+
});
|
|
34576
|
+
}
|
|
34577
|
+
function verifiedWords(verified) {
|
|
34578
|
+
return verified === "db_verified" ? "verified" : verified === "pending" ? "not verified yet" : VERIFY_FAILED2[verified] ?? verified;
|
|
34579
|
+
}
|
|
34580
|
+
var privateLinkFailure = (s) => s ? VERIFY_FAILED2[s.verified_status] : undefined;
|
|
34581
|
+
function stackTeardown(s, id) {
|
|
34582
|
+
const ep = rdsEndpoint(s.rds_endpoint);
|
|
34583
|
+
const made = ep ? ` If deploy's link made it:
|
|
34584
|
+
aws cloudformation delete-stack --stack-name ${stackNameFor(ep.id)} --region ${ep.region}` : "";
|
|
34585
|
+
return `Once it is deleted (pgai mon instances watch ${id}), and if nothing else is to monitor this RDS (a new deploy for it reuses the stack), delete the PrivateLink stack in your AWS account whose VpceServiceName is ${s.vpce_service_name} (terraform destroy, if it was made with Terraform).${made}`;
|
|
34586
|
+
}
|
|
34587
|
+
var utc2 = (iso) => {
|
|
34588
|
+
const d = new Date(iso);
|
|
34589
|
+
return Number.isNaN(d.getTime()) ? iso : `${d.toISOString().slice(0, 19).replace("T", " ")} UTC`;
|
|
34590
|
+
};
|
|
34591
|
+
function inDatabase2(url, db) {
|
|
34592
|
+
const u = new URL(url);
|
|
34593
|
+
if (u.pathname.length <= 1)
|
|
34594
|
+
u.pathname = `/${encodeURIComponent(db)}`;
|
|
34595
|
+
return u.toString();
|
|
34596
|
+
}
|
|
34597
|
+
function roleUrl2(url, db, password) {
|
|
34598
|
+
const u = new URL(loginUrlFor2(url, DEFAULT_MONITORING_USER, db, password));
|
|
34599
|
+
u.pathname = new URL(inDatabase2(url, db)).pathname;
|
|
34600
|
+
return u.toString();
|
|
34601
|
+
}
|
|
34602
|
+
async function deployPrivateLink2(url, route, opts, deps, progress) {
|
|
34603
|
+
const provider = "rds";
|
|
34604
|
+
const { ep, inst } = route;
|
|
34605
|
+
const name = databaseName3(url);
|
|
34606
|
+
const db = name.split("/").slice(1).join("/") || "postgres";
|
|
34607
|
+
const port = Number(new URL(url).port || inst?.port || 5432);
|
|
34608
|
+
const cluster = `${ep.host}:${port}`;
|
|
34609
|
+
const flag = (cli, agent) => opts.agent ? agent : cli;
|
|
34610
|
+
const stop = (next, more = {}) => ({ status: "action_required", provider, name, ...more, next });
|
|
34611
|
+
const notHere = [
|
|
34612
|
+
[opts.cloud, "--cloud", "cloud"],
|
|
34613
|
+
[opts.location, "--location", "location"],
|
|
34614
|
+
[opts.name, "--name", "name"],
|
|
34615
|
+
[opts.ipFirst || undefined, "--ip-first", "ip_first"],
|
|
34616
|
+
[opts.ipAllowed || undefined, "--ip-allowed", "ip_allowed"],
|
|
34617
|
+
[opts.resetPassword || undefined, "--reset-password", "reset_password"],
|
|
34618
|
+
[opts.monitoringUser, "--monitoring-user", "monitoring_user"]
|
|
34619
|
+
].find(([v]) => v !== undefined && v !== "");
|
|
34620
|
+
if (notHere)
|
|
34621
|
+
return stop(`${flag(notHere[1], notHere[2])} does not apply over ${PL2}: the box runs on AWS in ${ep.region} (the RDS's region), as ${DEFAULT_MONITORING_USER}, in a project named ${ep.id}`);
|
|
34622
|
+
if (opts.monitoringPassword === undefined !== (opts.nonce === undefined)) {
|
|
34623
|
+
return stop(opts.agent ? "Pass monitoring_password and nonce together (both from the prepare_db result)" : "Set PGAI_MON_PASSWORD and PGAI_MON_NONCE together (both from the printed SQL's result), or neither");
|
|
34624
|
+
}
|
|
34625
|
+
const rows = await deps.list();
|
|
34626
|
+
const live = rows.filter((d) => !disconnecting2(d.status));
|
|
34627
|
+
const onServer = live.filter((d) => d.cluster && serverOf2(d.cluster) === cluster);
|
|
34628
|
+
const verify = async (row) => VERIFY_FAILED2[(await deps.rdsStatus?.(row.id).catch(() => {
|
|
34629
|
+
return;
|
|
34630
|
+
}))?.verified_status ?? ""];
|
|
34631
|
+
const own = onServer.find((d) => (d.provider === "rds" || d.provider === "aurora") && d.database != null && decodeURIComponent(d.database) === db);
|
|
34632
|
+
if (own) {
|
|
34633
|
+
const note = opts.vpceService || opts.hostStatsRole ? "; the flags for a new box were not used: it exists" : "";
|
|
34634
|
+
return overPrivateLink2(await follow2(own, { provider, name, fresh: false, billing: {}, note, verify }, opts, deps, progress));
|
|
34635
|
+
}
|
|
34636
|
+
const already = (d, how) => {
|
|
34637
|
+
const instead = d.database != null && decodeURIComponent(d.database) !== db ? `To monitor ${db} instead` : "To set it up from here instead";
|
|
34638
|
+
return stop(`${ep.id} is already monitored ${how} (instance ${d.name}). Setting up another instance would change ${DEFAULT_MONITORING_USER}'s password, which that instance logs in with. ${instead}, delete that instance first (pgai mon instances delete ${deleteRef2(d, rows)})`);
|
|
34639
|
+
};
|
|
34640
|
+
if (onServer.length)
|
|
34641
|
+
return already(onServer[0], onServer[0].provider === "rds" ? `over ${PL2}, database ${decodeURIComponent(onServer[0].database ?? "?")}` : "directly");
|
|
34642
|
+
const candidates = live.filter((r) => !r.cluster || r.provider === "rds" || r.provider === "aurora");
|
|
34643
|
+
let routes;
|
|
34644
|
+
try {
|
|
34645
|
+
routes = await Promise.all(candidates.map((d) => deps.rdsStatus?.(d.id).catch((err) => {
|
|
34646
|
+
if (httpStatus2(err) === 404)
|
|
34647
|
+
return;
|
|
34648
|
+
throw err;
|
|
34649
|
+
})));
|
|
34650
|
+
} catch (err) {
|
|
34651
|
+
return stop(`Could not check what already monitors ${ep.id} (${errorText2(err)}): re-run pgai mon deploy. Nothing was changed`);
|
|
34652
|
+
}
|
|
34653
|
+
const viaConsole = candidates.find((_, i) => routes[i]?.rds_endpoint.toLowerCase().replace(/\.$/, "") === ep.host);
|
|
34654
|
+
if (viaConsole)
|
|
34655
|
+
return already(viaConsole, `over ${PL2}, set up in the console`);
|
|
34656
|
+
const hostStats = !!opts.hostStats || !!opts.hostStatsRole?.trim();
|
|
34657
|
+
const place = { cloud: "aws", location: ep.region, location_reason: "privatelink", location_reason_text: `same region as your database, over ${PL2}` };
|
|
34658
|
+
progress("preparing", `Monitoring box: AWS, ${ep.region} (${place.location_reason_text})`, { cloud: "aws", location: ep.region, location_reason: "privatelink" });
|
|
34659
|
+
let quote;
|
|
34660
|
+
try {
|
|
34661
|
+
quote = await deps.quote(opts.coupon);
|
|
34662
|
+
} catch (err) {
|
|
34663
|
+
if (err instanceof HttpStatusError && err.status === 403)
|
|
34664
|
+
return stop(notAdminNext);
|
|
34665
|
+
throw err;
|
|
34666
|
+
}
|
|
34667
|
+
let vcpus = opts.vcpus;
|
|
34668
|
+
if (vcpus === undefined && route.aws && inst?.instanceClass) {
|
|
34669
|
+
vcpus = await vcpusOfClass(route.aws, inst.instanceClass, ep.region);
|
|
34670
|
+
if (vcpus)
|
|
34671
|
+
progress("billing", `vCPUs: ${vcpus} (${inst.instanceClass})`);
|
|
34672
|
+
}
|
|
34673
|
+
const where = `AWS, ${ep.region}`;
|
|
34674
|
+
const b = await billingFor2(quote, name, opts, deps, (price) => progress("billing", estimateLine(price)), async (price) => {
|
|
34675
|
+
if (opts.yes)
|
|
34676
|
+
return { go: true };
|
|
34677
|
+
if (deps.ask) {
|
|
34678
|
+
return await deps.confirm(`Deploy ${terminalText2(name)} over ${PL2} (monitoring box on ${where})? [y/N] `) ? { go: true } : { go: false, stop: { next: `Nothing was created: re-run with --yes to deploy over ${PL2}` } };
|
|
34679
|
+
}
|
|
34680
|
+
return { go: false, stop: { next: opts.agent ? `Show the user the price (${price}); the monitoring box runs on ${where}, reached over ${PL2}. Then call deploy_monitoring again with yes: true. Nothing was created.` : `The monitoring box would run on ${where} for ${price}, reached over ${PL2}: re-run with --yes to deploy. Nothing was created.` } };
|
|
34681
|
+
});
|
|
34682
|
+
if (b.stop)
|
|
34683
|
+
return { ...stop(b.stop.next), ...place, ...b.stop };
|
|
34684
|
+
if (deps.aborted?.())
|
|
34685
|
+
throw new CancelledError2;
|
|
34686
|
+
const more = { ...place, ...b.billing };
|
|
34687
|
+
if (vcpus === undefined) {
|
|
34688
|
+
vcpus = !opts.yes && !opts.agent && deps.askVcpus ? await deps.askVcpus(VCPUS_QUESTION2) : undefined;
|
|
34689
|
+
if (deps.aborted?.())
|
|
34690
|
+
throw new CancelledError2;
|
|
34691
|
+
if (vcpus === undefined)
|
|
34692
|
+
progress("billing", NO_VCPUS_NOTE2);
|
|
34693
|
+
}
|
|
34694
|
+
const stack = await privateLinkStack2({ ep, aws: route.aws, inst, port, hostStats, opts, deps, progress, stop: (next, m = {}) => stop(next, { ...more, ...m }) });
|
|
34695
|
+
if ("status" in stack)
|
|
34696
|
+
return stack;
|
|
34697
|
+
if (deps.aborted?.())
|
|
34698
|
+
throw new CancelledError2;
|
|
34699
|
+
const role = await privateLinkRole2(url, db, opts, deps, progress, (next, m = {}) => stop(next, { ...more, ...m }));
|
|
34700
|
+
if ("status" in role)
|
|
34701
|
+
return role;
|
|
34702
|
+
if (deps.aborted?.())
|
|
34703
|
+
throw new CancelledError2;
|
|
34704
|
+
progress("provisioning", `Provisioning monitoring for ${name} over ${PL2}`);
|
|
34705
|
+
const created = await deps.rdsProvision({
|
|
34706
|
+
vpce_service_name: stack.vpce,
|
|
34707
|
+
rds_endpoint: ep.host,
|
|
34708
|
+
db_password: role.password,
|
|
34709
|
+
rds_port: port,
|
|
34710
|
+
rds_database: db,
|
|
34711
|
+
region: ep.region,
|
|
34712
|
+
...b.accepted ? { accept_price: true } : {},
|
|
34713
|
+
...opts.coupon ? { promo_code: opts.coupon } : {},
|
|
34714
|
+
...stack.roleArn ? { host_stats_role_arn: stack.roleArn } : {},
|
|
34715
|
+
...vcpus ? { vcpus } : {}
|
|
34716
|
+
}).catch((err) => {
|
|
34717
|
+
if (err instanceof HttpStatusError && err.status === 402)
|
|
34718
|
+
return /declined/i.test(err.message) ? "declined" : "payment";
|
|
34719
|
+
if (err instanceof HttpStatusError && err.status === 412)
|
|
34720
|
+
return "price";
|
|
34721
|
+
if (err instanceof HttpStatusError && err.status < 500)
|
|
34722
|
+
return { refused: errorText2(err) };
|
|
34723
|
+
if (mayHaveRun2(err))
|
|
34724
|
+
return "unanswered";
|
|
34725
|
+
throw err;
|
|
34726
|
+
});
|
|
34727
|
+
if (typeof created === "object" && "refused" in created)
|
|
34728
|
+
return stop(`${created.refused.replace(/\.?$/, ".")} No monitoring box was created.`, more);
|
|
34729
|
+
if (created === "payment")
|
|
34730
|
+
return { status: "action_required", provider, name, ...place, ...b.billingPage };
|
|
34731
|
+
if (created === "declined")
|
|
34732
|
+
return { status: "action_required", provider, name, ...place, ...b.billingPage, next: b.billingPage.next.replace(/^Add a payment method at /, "The payment method on file was declined: update it at ") };
|
|
34733
|
+
if (created === "price")
|
|
34734
|
+
return stop("The price changed since it was shown: re-run pgai mon deploy to see it", more);
|
|
34735
|
+
let box;
|
|
34736
|
+
if (created === "unanswered") {
|
|
34737
|
+
progress("provisioning", STILL_CREATING2);
|
|
34738
|
+
const known = new Set(rows.map((d) => d.id));
|
|
34739
|
+
const made = await lookForBox2((list) => list.find((d) => !known.has(d.id) && !!d.cluster && serverOf2(d.cluster) === cluster), opts, deps, deps.now());
|
|
34740
|
+
if (!made)
|
|
34741
|
+
return stop(deps.aborted?.() ? STOPPED_LOOKING2 : opts.agent ? NOT_CONFIRMED_AGENT2 : NOT_CONFIRMED2, more);
|
|
34742
|
+
box = made;
|
|
34743
|
+
} else {
|
|
34744
|
+
box = { id: created.id, name: created.name, provider, status: created.status, dashboard_url: null, host_metrics: !!stack.roleArn };
|
|
34745
|
+
}
|
|
34746
|
+
if (deps.aborted?.())
|
|
34747
|
+
return { ...connectStatus2(box, provider, true), ...more };
|
|
34748
|
+
return overPrivateLink2(await follow2(box, { provider, name, fresh: true, billing: b.billing, billingUrl: b.billingUrl, note: "", place, verify }, opts, deps, progress));
|
|
34749
|
+
}
|
|
34750
|
+
var PRIVATELINK_LAUNCH_FAILED2 = `The monitoring box could not connect to the stack's endpoint service: check that the stack finished, and that the endpoint service (VPC console, Endpoint services, Allow principals) lists ${PRINCIPAL_ARN}.`;
|
|
34751
|
+
function overPrivateLink2(r, why) {
|
|
34752
|
+
const { egress_ip: _ip, ...rest } = r;
|
|
34753
|
+
if (rest.status === "ready")
|
|
34754
|
+
return rest;
|
|
34755
|
+
const said = Object.values(VERIFY_FAILED2).some((t) => rest.next.includes(t));
|
|
34756
|
+
const first = said ? undefined : why ?? (rest.status === "failed" ? PRIVATELINK_LAUNCH_FAILED2 : undefined);
|
|
34757
|
+
return first ? { ...rest, next: `${first} ${rest.status === "failed" ? "Then: " : ""}${rest.next}` } : rest;
|
|
34758
|
+
}
|
|
34759
|
+
async function privateLinkStack2(c) {
|
|
34760
|
+
const { ep, opts, deps, progress } = c;
|
|
34761
|
+
const flag = (cli, agent) => opts.agent ? agent : cli;
|
|
34762
|
+
const terminal = !!deps.ask;
|
|
34763
|
+
const ask = opts.yes ? undefined : deps.ask;
|
|
34764
|
+
let aws = opts.terraform ? undefined : c.aws;
|
|
34765
|
+
let vpce = opts.vpceService?.trim() || undefined;
|
|
34766
|
+
let roleArn = opts.hostStatsRole?.trim() || undefined;
|
|
34767
|
+
const badVpce = (v) => {
|
|
34768
|
+
const m = VPCE_SERVICE.exec(v);
|
|
34769
|
+
if (!m)
|
|
34770
|
+
return `${v} is not a VpceServiceName (com.amazonaws.vpce.<region>.vpce-svc-<id>, the stack's output)`;
|
|
34771
|
+
if (m[1] !== ep.region)
|
|
34772
|
+
return `${v} is in ${m[1]}, and ${ep.id} in ${ep.region}: ${PL2} connects within one region, so use the stack made in ${ep.region}`;
|
|
34773
|
+
return;
|
|
34774
|
+
};
|
|
34775
|
+
if (roleArn && !ROLE_ARN.test(roleArn))
|
|
34776
|
+
return c.stop(`${roleArn} is not an IAM role ARN (arn:aws:iam::<account>:role/<name>, the stack's HostStatsRoleArn output)`);
|
|
34777
|
+
if (vpce && badVpce(vpce))
|
|
34778
|
+
return c.stop(badVpce(vpce));
|
|
34779
|
+
let externalId;
|
|
34780
|
+
const externalIdOf = async () => externalId ??= await deps.rdsExternalId();
|
|
34781
|
+
let shown;
|
|
34782
|
+
const show = async () => {
|
|
34783
|
+
const inputs = {
|
|
34784
|
+
region: ep.region,
|
|
34785
|
+
rdsId: ep.id,
|
|
34786
|
+
port: c.port,
|
|
34787
|
+
vpcId: c.inst?.vpcId,
|
|
34788
|
+
subnetIds: c.inst ? oneSubnetPerAz(c.inst.subnets) : undefined,
|
|
34789
|
+
securityGroupId: c.inst?.securityGroupIds.length === 1 ? c.inst.securityGroupIds[0] : undefined,
|
|
34790
|
+
...c.hostStats ? { externalId: await externalIdOf() } : {}
|
|
34791
|
+
};
|
|
34792
|
+
const cfn = quickCreateUrl(inputs);
|
|
34793
|
+
const [vpcKey, subnetsKey, sgKey] = opts.terraform ? ["vpc_id", "subnet_ids", "rds_security_group_id"] : ["VpcId", "SubnetIds", "RdsSecurityGroupId"];
|
|
34794
|
+
const w = sgKey.length + 2;
|
|
34795
|
+
const blanks = [
|
|
34796
|
+
!inputs.vpcId && ` ${vpcKey.padEnd(w)}the RDS's VPC`,
|
|
34797
|
+
!inputs.subnetIds?.length && ` ${subnetsKey.padEnd(w)}one subnet per availability zone of the RDS's subnet group`,
|
|
34798
|
+
!inputs.securityGroupId && ` ${sgKey.padEnd(w)}the RDS's security group`
|
|
34799
|
+
].filter((l) => !!l);
|
|
34800
|
+
const choose = blanks.length ? `
|
|
34801
|
+
${opts.terraform ? "In terraform.tfvars, fill in" : "In the form, choose (empty in the link)"}:
|
|
34802
|
+
${blanks.join(`
|
|
34803
|
+
`)}
|
|
34804
|
+
(the RDS console lists them: ${ep.id}, Connectivity & security)` : "";
|
|
34805
|
+
let dir;
|
|
34806
|
+
if (opts.terraform)
|
|
34807
|
+
dir = await deps.writeTerraform(stackNameFor(ep.id), terraformFiles(await deps.terraformModule(), inputs));
|
|
34808
|
+
const where = deps.openUrl ? "" : `
|
|
34809
|
+
Open it in a browser signed in to that AWS account.`;
|
|
34810
|
+
const tf = opts.agent ? "" : `
|
|
34811
|
+
Prefer Terraform? ${deps.ask ? "Stop (Ctrl-C) and re-run" : "Re-run"} with --terraform: it writes the same stack as a module.`;
|
|
34812
|
+
progress("privatelink", dir ? `Terraform for the PrivateLink stack written to ${dir}: terraform init && terraform apply there${choose}` : `Create the PrivateLink stack in your AWS account from this link; nothing becomes public:${choose}
|
|
34813
|
+
${cfn}${where}${tf}`, { cfn_url: cfn, stack_name: stackNameFor(ep.id) });
|
|
34814
|
+
return {
|
|
34815
|
+
step: "privatelink_stack",
|
|
34816
|
+
cfn_url: cfn,
|
|
34817
|
+
stack_name: stackNameFor(ep.id),
|
|
34818
|
+
region: ep.region,
|
|
34819
|
+
...inputs.externalId ? { external_id: inputs.externalId } : {},
|
|
34820
|
+
...dir ? { terraform_dir: dir } : {},
|
|
34821
|
+
...blanks.length ? { choose_in_form: blanks.map((l) => l.trim().split(/\s+/)[0]) } : {}
|
|
34822
|
+
};
|
|
34823
|
+
};
|
|
34824
|
+
const resume = () => {
|
|
34825
|
+
const output = opts.terraform ? "vpce_service_name output" : "VpceServiceName output";
|
|
34826
|
+
const role = c.hostStats && !roleArn ? ` and ${flag("--host-stats-role", "host_stats_role")} <its ${opts.terraform ? "host_stats_role_arn" : "HostStatsRoleArn"} output>` : "";
|
|
34827
|
+
return aws ? opts.agent ? "call deploy_monitoring again" : "re-run pgai mon deploy" : `${opts.agent ? "call deploy_monitoring again with" : "re-run with"} ${flag("--vpce-service", "vpce_service")} <its ${output}>${role}`;
|
|
34828
|
+
};
|
|
34829
|
+
const deadline = deps.now() + STACK_WAIT_MS2;
|
|
34830
|
+
let refusedPaste;
|
|
34831
|
+
let saidMisnamed = false;
|
|
34832
|
+
let readStack = false;
|
|
34833
|
+
const seenMaking = new Set;
|
|
34834
|
+
const saidFailed = new Set;
|
|
34835
|
+
const startedAt = Date.now() - 60000;
|
|
34836
|
+
const thisRuns = (s) => s.name === stackNameFor(ep.id) || seenMaking.has(s.name) || !!s.created && Date.parse(s.created) >= startedAt;
|
|
34837
|
+
for (let pastes = 0, empties = 0;!vpce; ) {
|
|
34838
|
+
let found = [];
|
|
34839
|
+
if (aws) {
|
|
34840
|
+
try {
|
|
34841
|
+
const r = await findStacks(aws, ep.id, ep.region);
|
|
34842
|
+
found = r.stacks;
|
|
34843
|
+
if (r.misnamed && !saidMisnamed) {
|
|
34844
|
+
saidMisnamed = true;
|
|
34845
|
+
progress("privatelink", `Note: stack ${r.misnamed.name} is for RDS ${r.misnamed.rdsId ?? "(none)"}, not ${ep.id}: not used. Give the new stack another name in the form`);
|
|
34846
|
+
}
|
|
34847
|
+
} catch (err) {
|
|
34848
|
+
if (!(err instanceof AwsError))
|
|
34849
|
+
throw err;
|
|
34850
|
+
progress("privatelink", `Note: could not read CloudFormation (${err.message}); going on without AWS`);
|
|
34851
|
+
aws = undefined;
|
|
34852
|
+
}
|
|
34853
|
+
}
|
|
34854
|
+
const ready = found.filter((s) => stackState(s.status) === "ready" && s.outputs.VpceServiceName);
|
|
34855
|
+
let stack;
|
|
34856
|
+
if (ready.length === 1)
|
|
34857
|
+
stack = ready[0];
|
|
34858
|
+
else if (ready.length > 1) {
|
|
34859
|
+
const rows = ready.map((s, i) => ` ${i + 1}. ${s.name} ${s.status}${s.created ? ` created ${utc2(s.created)}` : ""} ${s.outputs.VpceServiceName}`);
|
|
34860
|
+
const listed = {
|
|
34861
|
+
step: "privatelink_stack",
|
|
34862
|
+
region: ep.region,
|
|
34863
|
+
stacks: ready.map((s) => ({ name: s.name, status: s.status, created: s.created ?? null, vpce_service_name: s.outputs.VpceServiceName }))
|
|
34864
|
+
};
|
|
34865
|
+
if (!ask) {
|
|
34866
|
+
return c.stop(`Several stacks publish ${ep.id}: ${opts.agent ? "ask the user which one, then call deploy_monitoring again with vpce_service" : "re-run with --vpce-service"} <the VpceServiceName of the one to use>. Nothing was created on PostgresAI yet.
|
|
34867
|
+
${rows.join(`
|
|
34868
|
+
`)}`, listed);
|
|
34869
|
+
}
|
|
34870
|
+
for (let tries = 0;!stack && tries < 3; tries++) {
|
|
34871
|
+
const a = (await ask(`The stack to use (1-${ready.length}, Enter to stop): `, tries ? [`Type a number from 1 to ${ready.length}.`] : [`Several stacks publish ${ep.id}:`, ...rows])).trim();
|
|
34872
|
+
if (!a)
|
|
34873
|
+
return c.stop(`Stopped: re-run with --vpce-service <the VpceServiceName of the stack to use>. Nothing was created on PostgresAI yet.`, listed);
|
|
34874
|
+
const i = Number(a);
|
|
34875
|
+
if (Number.isInteger(i) && i >= 1 && i <= ready.length)
|
|
34876
|
+
stack = ready[i - 1];
|
|
34877
|
+
}
|
|
34878
|
+
if (!stack)
|
|
34879
|
+
return c.stop(`No stack picked: re-run with --vpce-service <the VpceServiceName of the stack to use>. Nothing was created on PostgresAI yet.`, listed);
|
|
34880
|
+
}
|
|
34881
|
+
if (stack) {
|
|
34882
|
+
vpce = stack.outputs.VpceServiceName;
|
|
34883
|
+
roleArn ??= stack.outputs.HostStatsRoleArn;
|
|
34884
|
+
readStack = true;
|
|
34885
|
+
progress("privatelink", `Using stack ${stack.name}${stack.created ? ` (created ${utc2(stack.created)})` : ""}: ${vpce}`, { stack_name: stack.name });
|
|
34886
|
+
break;
|
|
34887
|
+
}
|
|
34888
|
+
for (const s of found)
|
|
34889
|
+
if (stackState(s.status) === "in_progress")
|
|
34890
|
+
seenMaking.add(s.name);
|
|
34891
|
+
const foreign = found.find((s) => s.name === stackNameFor(ep.id) && stackState(s.status) === "ready" && !s.outputs.VpceServiceName);
|
|
34892
|
+
if (foreign && !found.some((s) => stackState(s.status) === "in_progress")) {
|
|
34893
|
+
return c.stop(`Stack ${foreign.name} has no VpceServiceName output: it was not made from the PostgresAI template. Delete it, create the stack from the link deploy prints, then ${resume()}`, { step: "privatelink_stack", stack_name: foreign.name, region: ep.region });
|
|
34894
|
+
}
|
|
34895
|
+
const failedNow = found.filter((s) => stackState(s.status) === "failed").sort((a, b) => Date.parse(b.created ?? "") - Date.parse(a.created ?? "") || 0);
|
|
34896
|
+
const waits = terminal && !!aws;
|
|
34897
|
+
const failed = failedNow.find(thisRuns) ?? (waits ? undefined : failedNow[0]);
|
|
34898
|
+
for (const s of failedNow) {
|
|
34899
|
+
if (s === failed || saidFailed.has(s.name))
|
|
34900
|
+
continue;
|
|
34901
|
+
saidFailed.add(s.name);
|
|
34902
|
+
progress("privatelink", `Note: stack ${s.name} for ${ep.id} failed earlier (${s.status}): not used`);
|
|
34903
|
+
}
|
|
34904
|
+
if (failed && !found.some((s) => stackState(s.status) === "in_progress")) {
|
|
34905
|
+
const why = (aws && await stackFailure(aws, failed.name, ep.region) || failed.reason || failed.status).replace(/\s*\(RequestId: [^)]*\)/g, "");
|
|
34906
|
+
const fix = /^(CREATE_FAILED|ROLLBACK_|DELETE_FAILED)/.test(failed.status) ? `delete the stack, then re-run pgai mon deploy:
|
|
34907
|
+
aws cloudformation delete-stack --stack-name ${failed.name} --region ${ep.region}` : "repair the stack in the CloudFormation console (Stack actions, Continue update rollback), then re-run pgai mon deploy";
|
|
34908
|
+
return c.stop(`Stack ${failed.name} failed (${failed.status}): ${why}
|
|
34909
|
+
Fix that, ${fix}`, { step: "privatelink_stack", stack_name: failed.name, region: ep.region });
|
|
34910
|
+
}
|
|
34911
|
+
const being = found.find((s) => stackState(s.status) === "in_progress");
|
|
34912
|
+
const first = !shown;
|
|
34913
|
+
shown ??= await show();
|
|
34914
|
+
const making = being ? `Stack ${being.name} is ${/ROLLBACK/.test(being.status) ? "rolling back" : "being created"} (${being.status})` : undefined;
|
|
34915
|
+
if (!terminal || !aws && !ask) {
|
|
34916
|
+
const make = shown.terraform_dir ? `Run terraform init && terraform apply in ${shown.terraform_dir}` : opts.agent ? `Give the user cfn_url to open in the AWS account of ${ep.id}, to create the stack there${shown.choose_in_form ? ` (choosing ${shown.choose_in_form.join(", ")} in the form)` : ""}, or call again with terraform: true for the same as Terraform` : `Open the link below in the AWS account of ${ep.id} and create the stack there${shown.choose_in_form ? ` (choose ${shown.choose_in_form.join(", ")} in the form)` : ""}, or re-run with --terraform for the same as Terraform`;
|
|
34917
|
+
return c.stop(`${making ? `${making}: once it is CREATE_COMPLETE` : `${make}; once it is done`}, ${resume()}. Nothing was created on PostgresAI yet.`, shown);
|
|
34918
|
+
}
|
|
34919
|
+
const openIt = !shown.terraform_dir && deps.openUrl ? ", [o] to open the link in your browser" : "";
|
|
34920
|
+
if (aws) {
|
|
34921
|
+
if (first && openIt && ask) {
|
|
34922
|
+
const a = (await ask(`[o] opens the link in your browser; Enter waits for the stack: `)).trim().toLowerCase();
|
|
34923
|
+
if (a === "o")
|
|
34924
|
+
deps.openUrl(shown.cfn_url);
|
|
34925
|
+
}
|
|
34926
|
+
if (deps.aborted?.())
|
|
34927
|
+
throw new CancelledError2;
|
|
34928
|
+
if (deps.now() >= deadline)
|
|
34929
|
+
return c.stop(`No stack for ${ep.id} was ready within ${STACK_WAIT_MS2 / 60000} minutes: re-run pgai mon deploy once it is. Nothing was created on PostgresAI yet.`, shown);
|
|
34930
|
+
progress("privatelink", making ?? `Waiting for you to create the stack from the link above (any name; deploy finds it by its RDS; up to ${STACK_WAIT_MS2 / 60000} min)`, { stack_name: being?.name ?? stackNameFor(ep.id) });
|
|
34931
|
+
await deps.sleep(STACK_POLL_MS2);
|
|
34932
|
+
continue;
|
|
34933
|
+
}
|
|
34934
|
+
const output = opts.terraform ? "vpce_service_name output" : "VpceServiceName (CloudFormation, the stack, Outputs)";
|
|
34935
|
+
const a = (await ask(`Paste the stack's ${output}${openIt}, or [s] to stop: `, refusedPaste ? [refusedPaste] : [])).trim();
|
|
34936
|
+
if (a.toLowerCase() === "s")
|
|
34937
|
+
return c.stop(`Stopped: once the stack is done, ${resume()}. Nothing was created on PostgresAI yet.`, shown);
|
|
34938
|
+
if (!a) {
|
|
34939
|
+
if (++empties >= 3)
|
|
34940
|
+
return c.stop(`Stopped: once the stack is done, ${resume()}. Nothing was created on PostgresAI yet.`, shown);
|
|
34941
|
+
refusedPaste = `Paste the stack's ${opts.terraform ? "vpce_service_name output" : "VpceServiceName"} once it is created, or s to stop.`;
|
|
34942
|
+
continue;
|
|
34943
|
+
}
|
|
34944
|
+
empties = 0;
|
|
34945
|
+
if (openIt && a.toLowerCase() === "o") {
|
|
34946
|
+
deps.openUrl(shown.cfn_url);
|
|
34947
|
+
refusedPaste = undefined;
|
|
34948
|
+
continue;
|
|
34949
|
+
}
|
|
34950
|
+
refusedPaste = badVpce(a);
|
|
34951
|
+
if (!refusedPaste)
|
|
34952
|
+
vpce = a;
|
|
34953
|
+
else if (++pastes >= 3)
|
|
34954
|
+
return c.stop(refusedPaste, shown);
|
|
34955
|
+
}
|
|
34956
|
+
if (c.hostStats && !roleArn && !readStack) {
|
|
34957
|
+
const out = opts.terraform ? "host_stats_role_arn" : "HostStatsRoleArn";
|
|
34958
|
+
return c.stop(`Host metrics need the metrics role too: ${opts.agent ? "call deploy_monitoring again with" : "re-run with"} ${flag("--vpce-service", "vpce_service")} ${vpce} and ${flag("--host-stats-role", "host_stats_role")} <its ${out} output>. If it has none, it was made without this PostgresAiExternalId: ${opts.terraform ? "set postgres_ai_external_id in terraform.tfvars and apply again" : "update the stack with it"}; or deploy without ${flag("--host-stats", "host_stats")}.
|
|
34959
|
+
PostgresAiExternalId = ${await externalIdOf()}`, { external_id: externalId });
|
|
34960
|
+
}
|
|
34961
|
+
if (c.hostStats && !roleArn) {
|
|
34962
|
+
return c.stop(`Host metrics need the stack's metrics role (its HostStatsRoleArn output), and it has none: update the stack with this PostgresAiExternalId (it then creates a read-only role), then ${resume()}; or deploy without ${flag("--host-stats", "host_stats")}.
|
|
34963
|
+
PostgresAiExternalId = ${await externalIdOf()}`, { external_id: externalId });
|
|
34964
|
+
}
|
|
34965
|
+
return { vpce, ...roleArn ? { roleArn } : {} };
|
|
34966
|
+
}
|
|
34967
|
+
async function privateLinkRole2(url, db, opts, deps, progress, stop) {
|
|
34968
|
+
let password = opts.monitoringPassword;
|
|
34969
|
+
let nonce = opts.nonce;
|
|
34970
|
+
const ask = opts.yes ? undefined : deps.ask;
|
|
34971
|
+
const warned = new Set;
|
|
34972
|
+
const warn = (m) => {
|
|
34973
|
+
if (!warned.has(m)) {
|
|
34974
|
+
warned.add(m);
|
|
34975
|
+
progress("preparing", m);
|
|
34976
|
+
}
|
|
34977
|
+
};
|
|
34978
|
+
const resumeWith = (n) => opts.agent ? `call deploy_monitoring again with monitoring_password and nonce from this result` : `re-run with PGAI_MON_NONCE=${n} and PGAI_MON_PASSWORD set to the v_password value near the top of the SQL`;
|
|
34979
|
+
if (password === undefined || nonce === undefined) {
|
|
34980
|
+
let p;
|
|
34981
|
+
try {
|
|
34982
|
+
p = await deps.rdsPrepareSql(db);
|
|
34983
|
+
} catch (err) {
|
|
34984
|
+
if (err instanceof HttpStatusError && err.status === 403)
|
|
34985
|
+
return stop(notAdminNext);
|
|
34986
|
+
if (err instanceof HttpStatusError && err.status < 500)
|
|
34987
|
+
return stop(errorText2(err));
|
|
34988
|
+
throw err;
|
|
34989
|
+
}
|
|
34990
|
+
if (!/^[A-Za-z0-9_-]{1,128}$/.test(p.nonce))
|
|
34991
|
+
throw new Error("cloud_monitoring_rds_prepare_sql returned a malformed nonce: nothing was changed");
|
|
34992
|
+
password = p.db_password;
|
|
34993
|
+
nonce = p.nonce;
|
|
34994
|
+
progress("preparing", `Preparing ${maskConnectionString(url)}`);
|
|
34995
|
+
const failed = await deps.runSql(inDatabase2(url, db), p.prepare_db_sql, warn).then(() => {
|
|
34996
|
+
return;
|
|
34997
|
+
}, (err) => err);
|
|
34998
|
+
if (failed) {
|
|
34999
|
+
const e = failed;
|
|
35000
|
+
const refusedLogin = typeof e !== "object" || !e || !("severity" in e) || e.severity === "FATAL" || /^(28|3D|08)/.test(String(e.code ?? ""));
|
|
35001
|
+
if (refusedLogin)
|
|
35002
|
+
return stop(`Could not log in to ${maskConnectionString(inDatabase2(url, db))}: ${errorText2(failed)}. Check the URL's user and password, and run pgai mon deploy where the database is reachable (in its VPC)`);
|
|
35003
|
+
const why = `The user in this URL (${decodeURIComponent(new URL(url).username)}) can't create ${DEFAULT_MONITORING_USER} (${errorText2(failed)})`;
|
|
35004
|
+
const printed = { step: "prepare_db", sql: p.prepare_db_sql, nonce, monitoring_password: password };
|
|
35005
|
+
const run = `Run the SQL as the master user, connected to database ${db}`;
|
|
35006
|
+
const only = `A deploy of this database without ${opts.agent ? "them" : "PGAI_MON_PASSWORD and PGAI_MON_NONCE"} issues a new SQL and replaces this one`;
|
|
35007
|
+
if (!ask)
|
|
35008
|
+
return stop(`${why}. ${run}, then ${resumeWith(nonce)}. ${only}.`, printed);
|
|
35009
|
+
const a = (await ask(`${run}, then press Enter ([s] to stop): `, [`${why}. The SQL:`, "", p.prepare_db_sql, ""])).trim().toLowerCase();
|
|
35010
|
+
if (a === "s" || a === "stop")
|
|
35011
|
+
return stop(`Stopped. ${run}, then ${resumeWith(nonce)}.`, { ...printed, sql: undefined });
|
|
35012
|
+
}
|
|
35013
|
+
}
|
|
35014
|
+
progress("preparing", `Checking that ${DEFAULT_MONITORING_USER} can log in to ${db} with the password the SQL set`);
|
|
35015
|
+
let found;
|
|
35016
|
+
try {
|
|
35017
|
+
found = await deps.readNonce(roleUrl2(url, db, password), warn);
|
|
35018
|
+
} catch (err) {
|
|
35019
|
+
const fresh = opts.agent ? "call again without monitoring_password and nonce for a new SQL" : "re-run without PGAI_MON_PASSWORD and PGAI_MON_NONCE for a new SQL";
|
|
35020
|
+
return stop(opts.monitoringPassword === undefined ? `${DEFAULT_MONITORING_USER} could not log in to ${db} with the password the SQL set (${errorText2(err)}): run the SQL again, connected to database ${db}` : opts.agent ? `${DEFAULT_MONITORING_USER} could not log in to ${db} with monitoring_password (${errorText2(err)}): pass the monitoring_password of the prepare_db result whose SQL ran, or ${fresh}` : `${DEFAULT_MONITORING_USER} could not log in to ${db} with PGAI_MON_PASSWORD (${errorText2(err)}): set it to the v_password value near the top of the SQL you ran, or ${fresh}`);
|
|
35021
|
+
}
|
|
35022
|
+
const again = opts.monitoringPassword !== undefined ? `${opts.agent ? "call again without monitoring_password and nonce" : "re-run without PGAI_MON_PASSWORD and PGAI_MON_NONCE"} for a new SQL, and run only that one` : "re-run pgai mon deploy";
|
|
35023
|
+
if (found === undefined)
|
|
35024
|
+
return stop(`The SQL has not run in database ${db} (perhaps in another database): run it connected to database ${db}, then ${resumeWith(nonce)}`);
|
|
35025
|
+
if (found !== nonce)
|
|
35026
|
+
return stop(`A newer PostgresAI setup SQL ran in database ${db} after this one (another deploy, or the Console): ${again}`);
|
|
35027
|
+
return { password, nonce };
|
|
35028
|
+
}
|
|
33667
35029
|
var STILL_IN_PROGRESS2 = "Still in progress";
|
|
33668
35030
|
var CONNECT_TIMEOUT_MS2 = 55000;
|
|
33669
35031
|
var AGENT_CONNECT_TIMEOUT_MS2 = 25000;
|
|
@@ -33697,7 +35059,14 @@ async function follow2(row, f, opts, deps, progress) {
|
|
|
33697
35059
|
const url = (_) => f.url ?? name;
|
|
33698
35060
|
const billing = { ...f.billing, ...f.place };
|
|
33699
35061
|
let shown;
|
|
35062
|
+
let unverified;
|
|
33700
35063
|
for (const deadline = deps.now() + opts.waitMs;; ) {
|
|
35064
|
+
if (f.verify) {
|
|
35065
|
+
const why = await f.verify(row);
|
|
35066
|
+
if (why && why !== unverified)
|
|
35067
|
+
progress("box", `Note: ${why}`, { id: row.id });
|
|
35068
|
+
unverified = why;
|
|
35069
|
+
}
|
|
33701
35070
|
if (row.billing_error && disconnecting2(row.status)) {
|
|
33702
35071
|
if (undoRole)
|
|
33703
35072
|
await undoRole();
|
|
@@ -33727,15 +35096,16 @@ async function follow2(row, f, opts, deps, progress) {
|
|
|
33727
35096
|
final.next += "; for CPU, memory and disk, delete the instance (pgai mon instances delete) and deploy again with --clickhouse-key <key-id>:<key-secret>";
|
|
33728
35097
|
}
|
|
33729
35098
|
const { next, ...rest } = final;
|
|
35099
|
+
const why = unverified && final.status !== "ready" ? `${unverified} ` : "";
|
|
33730
35100
|
if (final.status === "in_progress" && opts.waitMs > 0) {
|
|
33731
35101
|
return {
|
|
33732
35102
|
...rest,
|
|
33733
35103
|
status: "action_required",
|
|
33734
35104
|
...billing,
|
|
33735
|
-
next: `${STILL_IN_PROGRESS2} (box: ${boxWords2(boxState2(row))}) when the wait ended: resume with ${next}${note}`
|
|
35105
|
+
next: `${STILL_IN_PROGRESS2} (box: ${boxWords2(boxState2(row))}) when the wait ended: ${why}${why ? "Resume" : "resume"} with ${next}${note}`
|
|
33736
35106
|
};
|
|
33737
35107
|
}
|
|
33738
|
-
return { ...rest, ...billing, next: next + note };
|
|
35108
|
+
return { ...rest, ...billing, next: why + next + note };
|
|
33739
35109
|
}
|
|
33740
35110
|
await deps.sleep(POLL_MS2);
|
|
33741
35111
|
const listed = await deps.list().catch(() => {
|
|
@@ -33816,7 +35186,8 @@ async function prepareDatabase2(url, provider, opts = {}) {
|
|
|
33816
35186
|
or (pg_has_role(current_user, 'pg_monitor', 'USAGE WITH ADMIN OPTION')
|
|
33817
35187
|
and pg_has_role(current_user, 'pg_read_all_stats', 'USAGE WITH ADMIN OPTION')))
|
|
33818
35188
|
and has_database_privilege(current_user, current_database(), 'CREATE')
|
|
33819
|
-
and exists (select 1 from pg_extension where extname = 'pg_stat_statements')
|
|
35189
|
+
and (exists (select 1 from pg_extension where extname = 'pg_stat_statements')
|
|
35190
|
+
or exists (select 1 from pg_roles where rolname = 'rds_superuser' and pg_has_role(current_user, oid, 'MEMBER')))) as admin,
|
|
33820
35191
|
exists (select 1 from pg_roles where rolname = ${quoteLiteral(role)}) as mon_exists,
|
|
33821
35192
|
exists (select 1 from pg_roles where rolname = ${quoteLiteral(role)} and (rolsuper or rolcreatedb or rolcreaterole or rolreplication or rolbypassrls)) as mon_privileged,
|
|
33822
35193
|
(select string_agg(g.rolname::text, ', ' order by g.rolname::text collate "C") from pg_roles m join pg_roles g
|
|
@@ -34083,10 +35454,50 @@ function platformDeps2(p) {
|
|
|
34083
35454
|
supabaseConnect: (body) => rpc("cloud_monitoring_supabase_connect", body),
|
|
34084
35455
|
supabaseProjects: () => rpc("cloud_monitoring_supabase_projects", {}),
|
|
34085
35456
|
deployOptions: (args) => rpc("monitoring_deploy_options", args),
|
|
34086
|
-
|
|
34087
|
-
|
|
34088
|
-
|
|
34089
|
-
|
|
35457
|
+
rdsExternalId: async () => (await rpc("cloud_monitoring_rds_external_id")).external_id,
|
|
35458
|
+
rdsPrepareSql: (dbName) => rpc("cloud_monitoring_rds_prepare_sql", { db_name: dbName }),
|
|
35459
|
+
rdsProvision: (body) => rpc("cloud_monitoring_rds_provision", body, p.agent ? AGENT_CONNECT_TIMEOUT_MS2 : CONNECT_TIMEOUT_MS2),
|
|
35460
|
+
rdsStatus: (id) => rpc("cloud_monitoring_rds_status", { instance_id: id }),
|
|
35461
|
+
aws: awsRunner,
|
|
35462
|
+
awsProfiles: () => new Promise((resolve) => execFile3("aws", ["configure", "list-profiles"], { timeout: 1e4 }, (err, stdout) => resolve(err ? [] : stdout.split(`
|
|
35463
|
+
`).map((l) => l.trim()).filter(Boolean)))),
|
|
35464
|
+
publicAddresses: (host) => publicAddresses(host),
|
|
35465
|
+
runSql: async (url, sql, warn) => {
|
|
35466
|
+
const { client } = await openConnection2(url, { agent: p.agent, warn });
|
|
35467
|
+
try {
|
|
35468
|
+
await client.query(sql);
|
|
35469
|
+
} finally {
|
|
35470
|
+
await client.end().catch(() => {});
|
|
35471
|
+
}
|
|
35472
|
+
},
|
|
35473
|
+
readNonce: async (url, warn) => {
|
|
35474
|
+
const { client } = await openConnection2(url, { agent: p.agent, warn });
|
|
35475
|
+
try {
|
|
35476
|
+
const r = await client.query("select nonce from postgres_ai.monitoring_ownership order by issued_at desc limit 1");
|
|
35477
|
+
return r.rows[0]?.nonce;
|
|
35478
|
+
} catch (err) {
|
|
35479
|
+
if (["3F000", "42P01"].includes(err.code ?? ""))
|
|
35480
|
+
return;
|
|
35481
|
+
throw err;
|
|
35482
|
+
} finally {
|
|
35483
|
+
await client.end().catch(() => {});
|
|
35484
|
+
}
|
|
35485
|
+
},
|
|
35486
|
+
terraformModule: async () => {
|
|
35487
|
+
const r = await fetch(TERRAFORM_URL, { signal: AbortSignal.timeout(30000) });
|
|
35488
|
+
if (!r.ok)
|
|
35489
|
+
throw new Error(`Could not download the Terraform module (${TERRAFORM_URL}: HTTP ${r.status})`);
|
|
35490
|
+
return r.text();
|
|
35491
|
+
},
|
|
35492
|
+
writeTerraform: async (dirName, files) => {
|
|
35493
|
+
const dir = path6.resolve(dirName);
|
|
35494
|
+
await fs7.promises.mkdir(dir, { recursive: true });
|
|
35495
|
+
await fs7.promises.writeFile(path6.join(dir, "main.tf"), files.mainTf);
|
|
35496
|
+
await fs7.promises.writeFile(path6.join(dir, "terraform.tfvars"), files.tfvars, { flag: "wx" }).catch((err) => {
|
|
35497
|
+
if (err.code !== "EEXIST")
|
|
35498
|
+
throw err;
|
|
35499
|
+
});
|
|
35500
|
+
return dir;
|
|
34090
35501
|
},
|
|
34091
35502
|
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
34092
35503
|
now: () => Date.now()
|
|
@@ -35767,6 +37178,13 @@ var SUPABASE_STEPS = [
|
|
|
35767
37178
|
{ key: "box", label: "Prepare the database and install monitoring (about 5 min)" },
|
|
35768
37179
|
{ key: "ready", label: "Ready" }
|
|
35769
37180
|
];
|
|
37181
|
+
var PRIVATELINK_STEPS = [
|
|
37182
|
+
{ key: "privatelink", label: "Wait for you to create the PrivateLink stack in AWS" },
|
|
37183
|
+
{ key: "preparing", label: "Prepare the database" },
|
|
37184
|
+
{ key: "provisioning", label: "Create the monitoring box" },
|
|
37185
|
+
{ key: "box", label: "Connect over PrivateLink and install monitoring (about 5 min)" },
|
|
37186
|
+
{ key: "ready", label: "Ready" }
|
|
37187
|
+
];
|
|
35770
37188
|
var monitoringSteps = (provider) => provider === "supabase" ? SUPABASE_STEPS : MONITORING_STEPS;
|
|
35771
37189
|
var FRAMES2 = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
35772
37190
|
var GREEN2 = "\x1B[32m";
|
|
@@ -36381,8 +37799,8 @@ async function registerAasCollection(apiKey, instanceId, opts) {
|
|
|
36381
37799
|
}
|
|
36382
37800
|
|
|
36383
37801
|
// lib/storage.ts
|
|
36384
|
-
import * as
|
|
36385
|
-
import * as
|
|
37802
|
+
import * as fs8 from "fs";
|
|
37803
|
+
import * as path7 from "path";
|
|
36386
37804
|
var MAX_UPLOAD_SIZE2 = 500 * 1024 * 1024;
|
|
36387
37805
|
var MAX_DOWNLOAD_SIZE2 = 500 * 1024 * 1024;
|
|
36388
37806
|
var MIME_TYPES2 = {
|
|
@@ -36423,11 +37841,11 @@ async function uploadFile2(params) {
|
|
|
36423
37841
|
if (!filePath) {
|
|
36424
37842
|
throw new Error("filePath is required");
|
|
36425
37843
|
}
|
|
36426
|
-
const resolvedPath =
|
|
36427
|
-
if (!
|
|
37844
|
+
const resolvedPath = path7.resolve(filePath);
|
|
37845
|
+
if (!fs8.existsSync(resolvedPath)) {
|
|
36428
37846
|
throw new Error(`File not found: ${resolvedPath}`);
|
|
36429
37847
|
}
|
|
36430
|
-
const stat =
|
|
37848
|
+
const stat = fs8.statSync(resolvedPath);
|
|
36431
37849
|
if (!stat.isFile()) {
|
|
36432
37850
|
throw new Error(`Not a file: ${resolvedPath}`);
|
|
36433
37851
|
}
|
|
@@ -36439,9 +37857,9 @@ async function uploadFile2(params) {
|
|
|
36439
37857
|
console.error("Warning: storage URL uses HTTP — API key will be sent unencrypted");
|
|
36440
37858
|
}
|
|
36441
37859
|
const url = `${base}/upload`;
|
|
36442
|
-
const fileBuffer =
|
|
36443
|
-
const fileName =
|
|
36444
|
-
const ext =
|
|
37860
|
+
const fileBuffer = fs8.readFileSync(resolvedPath);
|
|
37861
|
+
const fileName = path7.basename(resolvedPath);
|
|
37862
|
+
const ext = path7.extname(fileName).toLowerCase();
|
|
36445
37863
|
const mimeType = MIME_TYPES2[ext] || "application/octet-stream";
|
|
36446
37864
|
const formData = new FormData;
|
|
36447
37865
|
formData.append("file", new Blob([fileBuffer], { type: mimeType }), fileName);
|
|
@@ -36501,15 +37919,15 @@ async function downloadFile2(params) {
|
|
|
36501
37919
|
const relativePath = fileUrl.startsWith("/") ? fileUrl : `/${fileUrl}`;
|
|
36502
37920
|
fullUrl = `${base}${relativePath}`;
|
|
36503
37921
|
}
|
|
36504
|
-
const urlFilename =
|
|
37922
|
+
const urlFilename = path7.basename(new URL(fullUrl).pathname);
|
|
36505
37923
|
if (!urlFilename) {
|
|
36506
37924
|
throw new Error("Cannot derive filename from URL; please specify --output");
|
|
36507
37925
|
}
|
|
36508
|
-
const saveTo = outputPath ?
|
|
37926
|
+
const saveTo = outputPath ? path7.resolve(outputPath) : path7.resolve(urlFilename);
|
|
36509
37927
|
if (!outputPath) {
|
|
36510
|
-
const normalizedSave =
|
|
36511
|
-
const cwd =
|
|
36512
|
-
if (normalizedSave !== cwd && !normalizedSave.startsWith(cwd +
|
|
37928
|
+
const normalizedSave = path7.normalize(saveTo);
|
|
37929
|
+
const cwd = path7.normalize(process.cwd());
|
|
37930
|
+
if (normalizedSave !== cwd && !normalizedSave.startsWith(cwd + path7.sep)) {
|
|
36513
37931
|
throw new Error("Derived output path escapes current directory; please specify --output");
|
|
36514
37932
|
}
|
|
36515
37933
|
}
|
|
@@ -36542,11 +37960,11 @@ async function downloadFile2(params) {
|
|
|
36542
37960
|
}
|
|
36543
37961
|
const arrayBuffer = await response.arrayBuffer();
|
|
36544
37962
|
const buffer = Buffer.from(arrayBuffer);
|
|
36545
|
-
const parentDir =
|
|
36546
|
-
if (!
|
|
36547
|
-
|
|
37963
|
+
const parentDir = path7.dirname(saveTo);
|
|
37964
|
+
if (!fs8.existsSync(parentDir)) {
|
|
37965
|
+
fs8.mkdirSync(parentDir, { recursive: true });
|
|
36548
37966
|
}
|
|
36549
|
-
|
|
37967
|
+
fs8.writeFileSync(saveTo, buffer);
|
|
36550
37968
|
return {
|
|
36551
37969
|
savedTo: saveTo,
|
|
36552
37970
|
size: buffer.length,
|
|
@@ -36558,9 +37976,9 @@ function buildMarkdownLink2(fileUrl, storageBaseUrl, filename) {
|
|
|
36558
37976
|
const base = normalizeBaseUrl(storageBaseUrl);
|
|
36559
37977
|
const normalizedFileUrl = fileUrl.startsWith("/") ? fileUrl : `/${fileUrl}`;
|
|
36560
37978
|
const fullUrl = fileUrl.startsWith("http://") || fileUrl.startsWith("https://") ? fileUrl : `${base}${normalizedFileUrl}`;
|
|
36561
|
-
const name = filename ||
|
|
37979
|
+
const name = filename || path7.basename(new URL(fullUrl).pathname);
|
|
36562
37980
|
const safeName = name.replace(/[\[\]()]/g, "\\$&");
|
|
36563
|
-
const ext =
|
|
37981
|
+
const ext = path7.extname(name).toLowerCase();
|
|
36564
37982
|
if (IMAGE_EXTENSIONS2.has(ext)) {
|
|
36565
37983
|
return ``;
|
|
36566
37984
|
}
|
|
@@ -36606,7 +38024,7 @@ ${links}`;
|
|
|
36606
38024
|
|
|
36607
38025
|
// lib/clickhouse.ts
|
|
36608
38026
|
import { existsSync as existsSync7, renameSync, rmSync as rmSync2, writeFileSync as writeFileSync7 } from "node:fs";
|
|
36609
|
-
import { join as
|
|
38027
|
+
import { join as join7 } from "node:path";
|
|
36610
38028
|
function validateName(name) {
|
|
36611
38029
|
if (!/^[A-Za-z0-9_-]+$/.test(name))
|
|
36612
38030
|
throw new Error("Invalid ClickHouse target name.");
|
|
@@ -36673,7 +38091,7 @@ async function addHostMetrics({ projectDir, name, conn, env, cluster = "default"
|
|
|
36673
38091
|
const prefix = `clickhouse-${name}`;
|
|
36674
38092
|
const text = renderScrapeConfig({ name, cluster, nodeName, orgId, serviceId: service.id, keyId, passwordFile: `/etc/pgai/host-metrics/${prefix}.secret`, apiUrl });
|
|
36675
38093
|
const dir = hostMetricsDir(projectDir);
|
|
36676
|
-
const secretFile =
|
|
38094
|
+
const secretFile = join7(dir, `${prefix}.secret`);
|
|
36677
38095
|
const tmpFile = `${secretFile}.tmp`;
|
|
36678
38096
|
rmSync2(tmpFile, { force: true });
|
|
36679
38097
|
try {
|
|
@@ -36688,7 +38106,7 @@ async function addHostMetrics({ projectDir, name, conn, env, cluster = "default"
|
|
|
36688
38106
|
function removeHostMetrics(projectDir, name) {
|
|
36689
38107
|
if (!/^[A-Za-z0-9_-]+$/.test(name))
|
|
36690
38108
|
return false;
|
|
36691
|
-
const files = ["yml", "secret", "secret.tmp"].map((ext) =>
|
|
38109
|
+
const files = ["yml", "secret", "secret.tmp"].map((ext) => join7(projectDir, "host-metrics", `clickhouse-${name}.${ext}`)).filter((file) => existsSync7(file));
|
|
36692
38110
|
for (const file of files)
|
|
36693
38111
|
rmSync2(file, { force: true });
|
|
36694
38112
|
return files.length > 0;
|
|
@@ -36697,21 +38115,21 @@ function removeHostMetrics(projectDir, name) {
|
|
|
36697
38115
|
// lib/host-metrics.ts
|
|
36698
38116
|
import { createHash as createHash3 } from "node:crypto";
|
|
36699
38117
|
import { chmodSync as chmodSync4, lstatSync as lstatSync3, mkdirSync as mkdirSync6, readFileSync as readFileSync9, writeFileSync as writeFileSync8 } from "node:fs";
|
|
36700
|
-
import { join as
|
|
38118
|
+
import { join as join8 } from "node:path";
|
|
36701
38119
|
function scrapeFileText2(config) {
|
|
36702
38120
|
const revision = `r${createHash3("sha256").update(dump2([config], { lineWidth: -1 })).digest("hex").slice(0, 16)}`;
|
|
36703
38121
|
Object.assign(config.static_configs[0].labels, { __pgai_rev: revision });
|
|
36704
38122
|
return dump2([config], { lineWidth: -1 });
|
|
36705
38123
|
}
|
|
36706
38124
|
function scrapeRevision(projectDir, file) {
|
|
36707
|
-
const [config] = load2(readFileSync9(
|
|
38125
|
+
const [config] = load2(readFileSync9(join8(projectDir, "host-metrics", file), "utf8"));
|
|
36708
38126
|
const revision = config?.static_configs?.[0]?.labels?.__pgai_rev;
|
|
36709
38127
|
if (typeof revision !== "string" || !/^r[0-9a-f]{16}$/.test(revision))
|
|
36710
38128
|
throw new Error(`host-metrics/${file} has no __pgai_rev label.`);
|
|
36711
38129
|
return revision;
|
|
36712
38130
|
}
|
|
36713
38131
|
function hostMetricsDir2(projectDir) {
|
|
36714
|
-
const dir =
|
|
38132
|
+
const dir = join8(projectDir, "host-metrics");
|
|
36715
38133
|
if (lstatSync3(dir, { throwIfNoEntry: false })?.isSymbolicLink())
|
|
36716
38134
|
throw new Error(`${dir}: host-metrics must be a directory, not a symlink.`);
|
|
36717
38135
|
mkdirSync6(dir, { recursive: true, mode: 448 });
|
|
@@ -36719,8 +38137,8 @@ function hostMetricsDir2(projectDir) {
|
|
|
36719
38137
|
return dir;
|
|
36720
38138
|
}
|
|
36721
38139
|
function writeScrapeFile2(dir, file, text) {
|
|
36722
|
-
writeFileSync8(
|
|
36723
|
-
chmodSync4(
|
|
38140
|
+
writeFileSync8(join8(dir, file), text, { mode: 420 });
|
|
38141
|
+
chmodSync4(join8(dir, file), 420);
|
|
36724
38142
|
}
|
|
36725
38143
|
var HOST_METRICS_VERIFY_SCRIPT = `auth="Authorization: Basic $(printf '%s:%s' "$VM_AUTH_USERNAME" "$VM_AUTH_PASSWORD" | base64 | tr -d '\\n')"; end=$(($(date +%s) + 10)); while [ "$(date +%s)" -lt "$end" ]; do if t=$(wget -qO- -T 2 --header "$auth" http://127.0.0.1:9090/api/v1/targets); then case "$t" in '{"status":"success"'*) case "$t" in *"$1"*) [ "$2" = present ] && exit 0 ;; *) [ "$2" = absent ] && exit 0 ;; esac ;; esac; fi; sleep 0.5; done; exit 1`;
|
|
36726
38144
|
var SUPABASE_JOB = "supabase-host-metrics";
|
|
@@ -36749,8 +38167,8 @@ function rdsInstance(host) {
|
|
|
36749
38167
|
// lib/init.ts
|
|
36750
38168
|
import { randomBytes as randomBytes3 } from "crypto";
|
|
36751
38169
|
import { URL as URL3, fileURLToPath as fileURLToPath2 } from "url";
|
|
36752
|
-
import * as
|
|
36753
|
-
import * as
|
|
38170
|
+
import * as fs9 from "fs";
|
|
38171
|
+
import * as path8 from "path";
|
|
36754
38172
|
var DEFAULT_MONITORING_USER2 = "postgres_ai_mon";
|
|
36755
38173
|
var KNOWN_PROVIDERS = ["self-managed", "supabase", "clickhouse"];
|
|
36756
38174
|
var SKIP_ROLE_CREATION_PROVIDERS2 = ["supabase"];
|
|
@@ -36895,21 +38313,21 @@ function describeInitScope(plan) {
|
|
|
36895
38313
|
}
|
|
36896
38314
|
function sqlDir2() {
|
|
36897
38315
|
const currentFile = fileURLToPath2(import.meta.url);
|
|
36898
|
-
const currentDir =
|
|
38316
|
+
const currentDir = path8.dirname(currentFile);
|
|
36899
38317
|
const candidates = [
|
|
36900
|
-
|
|
36901
|
-
|
|
38318
|
+
path8.resolve(currentDir, "..", "sql"),
|
|
38319
|
+
path8.resolve(currentDir, "..", "..", "sql")
|
|
36902
38320
|
];
|
|
36903
38321
|
for (const candidate of candidates) {
|
|
36904
|
-
if (
|
|
38322
|
+
if (fs9.existsSync(candidate)) {
|
|
36905
38323
|
return candidate;
|
|
36906
38324
|
}
|
|
36907
38325
|
}
|
|
36908
38326
|
throw new Error(`SQL directory not found. Searched: ${candidates.join(", ")}`);
|
|
36909
38327
|
}
|
|
36910
38328
|
function loadSqlTemplate2(filename) {
|
|
36911
|
-
const p =
|
|
36912
|
-
return
|
|
38329
|
+
const p = path8.join(sqlDir2(), filename);
|
|
38330
|
+
return fs9.readFileSync(p, "utf8");
|
|
36913
38331
|
}
|
|
36914
38332
|
function applyTemplate2(sql, vars) {
|
|
36915
38333
|
return sql.replace(/\{\{([A-Z0-9_]+)\}\}/g, (_, key) => {
|
|
@@ -38712,8 +40130,8 @@ import { Writable } from "stream";
|
|
|
38712
40130
|
import * as childProcess from "child_process";
|
|
38713
40131
|
|
|
38714
40132
|
// lib/checkup.ts
|
|
38715
|
-
import * as
|
|
38716
|
-
import * as
|
|
40133
|
+
import * as fs10 from "fs";
|
|
40134
|
+
import * as path9 from "path";
|
|
38717
40135
|
|
|
38718
40136
|
// lib/metrics-embedded.ts
|
|
38719
40137
|
var METRICS = {
|
|
@@ -42681,7 +44099,7 @@ function createBaseReport(checkId, checkTitle, nodeName) {
|
|
|
42681
44099
|
}
|
|
42682
44100
|
function readTextFileSafe(p) {
|
|
42683
44101
|
try {
|
|
42684
|
-
const value =
|
|
44102
|
+
const value = fs10.readFileSync(p, "utf8").trim();
|
|
42685
44103
|
return value || null;
|
|
42686
44104
|
} catch {
|
|
42687
44105
|
return null;
|
|
@@ -42694,8 +44112,8 @@ function resolveBuildTs() {
|
|
|
42694
44112
|
if (fromFile)
|
|
42695
44113
|
return fromFile;
|
|
42696
44114
|
try {
|
|
42697
|
-
const pkgRoot =
|
|
42698
|
-
const fromPkgFile = readTextFileSafe(
|
|
44115
|
+
const pkgRoot = path9.resolve(__dirname, "..");
|
|
44116
|
+
const fromPkgFile = readTextFileSafe(path9.join(pkgRoot, "BUILD_TS"));
|
|
42699
44117
|
if (fromPkgFile)
|
|
42700
44118
|
return fromPkgFile;
|
|
42701
44119
|
} catch (err) {
|
|
@@ -42703,8 +44121,8 @@ function resolveBuildTs() {
|
|
|
42703
44121
|
console.warn(`[resolveBuildTs] Warning: path resolution failed: ${errorMsg}`);
|
|
42704
44122
|
}
|
|
42705
44123
|
try {
|
|
42706
|
-
const pkgJsonPath =
|
|
42707
|
-
const st =
|
|
44124
|
+
const pkgJsonPath = path9.resolve(__dirname, "..", "package.json");
|
|
44125
|
+
const st = fs10.statSync(pkgJsonPath);
|
|
42708
44126
|
return st.mtime.toISOString();
|
|
42709
44127
|
} catch (err) {
|
|
42710
44128
|
if (process.env.DEBUG) {
|
|
@@ -44913,7 +46331,7 @@ function runChild(command, args, env, json, source) {
|
|
|
44913
46331
|
}
|
|
44914
46332
|
|
|
44915
46333
|
// lib/instances.ts
|
|
44916
|
-
import * as
|
|
46334
|
+
import * as fs11 from "fs";
|
|
44917
46335
|
class InstancesParseError2 extends Error {
|
|
44918
46336
|
constructor(file, cause) {
|
|
44919
46337
|
const yamlErr = cause;
|
|
@@ -44923,11 +46341,11 @@ class InstancesParseError2 extends Error {
|
|
|
44923
46341
|
}
|
|
44924
46342
|
}
|
|
44925
46343
|
function loadInstances2(file) {
|
|
44926
|
-
if (!
|
|
46344
|
+
if (!fs11.existsSync(file))
|
|
44927
46345
|
return [];
|
|
44928
|
-
if (
|
|
46346
|
+
if (fs11.lstatSync(file).isDirectory())
|
|
44929
46347
|
return [];
|
|
44930
|
-
const text =
|
|
46348
|
+
const text = fs11.readFileSync(file, "utf8");
|
|
44931
46349
|
if (text.trim() === "")
|
|
44932
46350
|
return [];
|
|
44933
46351
|
let parsed;
|
|
@@ -44960,8 +46378,8 @@ function buildInstance(name, connStr) {
|
|
|
44960
46378
|
};
|
|
44961
46379
|
}
|
|
44962
46380
|
function addInstanceToFile(file, instance) {
|
|
44963
|
-
if (
|
|
44964
|
-
|
|
46381
|
+
if (fs11.existsSync(file) && fs11.lstatSync(file).isDirectory()) {
|
|
46382
|
+
fs11.rmSync(file, { recursive: true, force: true });
|
|
44965
46383
|
}
|
|
44966
46384
|
const existing = loadInstances2(file);
|
|
44967
46385
|
if (existing.some((i) => i.name === instance.name)) {
|
|
@@ -44980,18 +46398,18 @@ function removeInstanceFromFile(file, name) {
|
|
|
44980
46398
|
}
|
|
44981
46399
|
function writeInstancesFile(file, instances) {
|
|
44982
46400
|
const content = dump2(instances);
|
|
44983
|
-
const fd =
|
|
46401
|
+
const fd = fs11.openSync(file, "w", 384);
|
|
44984
46402
|
try {
|
|
44985
46403
|
try {
|
|
44986
|
-
|
|
46404
|
+
fs11.fchmodSync(fd, 384);
|
|
44987
46405
|
} catch (err) {
|
|
44988
46406
|
const message = err instanceof Error ? err.message : String(err);
|
|
44989
46407
|
console.error(`Warning: could not restrict permissions on ${file}: ${message}`);
|
|
44990
46408
|
console.error(" It may be readable by other users on this machine.");
|
|
44991
46409
|
}
|
|
44992
|
-
|
|
46410
|
+
fs11.writeFileSync(fd, content, "utf8");
|
|
44993
46411
|
} finally {
|
|
44994
|
-
|
|
46412
|
+
fs11.closeSync(fd);
|
|
44995
46413
|
}
|
|
44996
46414
|
}
|
|
44997
46415
|
function extractSslmode2(connStr) {
|
|
@@ -45102,7 +46520,7 @@ async function verifyCollectorTls2(url) {
|
|
|
45102
46520
|
u.searchParams.delete(key);
|
|
45103
46521
|
const client = new Client2({
|
|
45104
46522
|
...buildClientConfig2(splitChannelBinding2(u.toString()).uri, { connectionTimeoutMillis: 1e4 }),
|
|
45105
|
-
ssl: { rejectUnauthorized: true, servername: u.hostname, ...ca ? { ca:
|
|
46523
|
+
ssl: { rejectUnauthorized: true, servername: u.hostname, ...ca ? { ca: fs11.readFileSync(ca, "utf8") } : {} }
|
|
45106
46524
|
});
|
|
45107
46525
|
try {
|
|
45108
46526
|
await client.connect();
|
|
@@ -45191,8 +46609,8 @@ var REQUIRED_ENV_KEYS = [
|
|
|
45191
46609
|
{ key: "VM_PPROF_AUTH_KEY", defaultValue: () => crypto2.randomBytes(32).toString("hex"), introducedIn: "0.17" }
|
|
45192
46610
|
];
|
|
45193
46611
|
function ensureRequiredEnvVars(projectDir) {
|
|
45194
|
-
const envFile =
|
|
45195
|
-
const existing =
|
|
46612
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
46613
|
+
const existing = fs12.existsSync(envFile) ? fs12.readFileSync(envFile, "utf8") : "";
|
|
45196
46614
|
const added = [];
|
|
45197
46615
|
const appendLines = [];
|
|
45198
46616
|
let content = existing;
|
|
@@ -45227,23 +46645,23 @@ function ensureRequiredEnvVars(projectDir) {
|
|
|
45227
46645
|
function writeEnvFile(envFile, content) {
|
|
45228
46646
|
let targetFile = envFile;
|
|
45229
46647
|
try {
|
|
45230
|
-
if (
|
|
45231
|
-
targetFile =
|
|
46648
|
+
if (fs12.existsSync(envFile))
|
|
46649
|
+
targetFile = fs12.realpathSync(envFile);
|
|
45232
46650
|
} catch {}
|
|
45233
46651
|
const tmpFile = `${targetFile}.tmp`;
|
|
45234
|
-
|
|
46652
|
+
fs12.writeFileSync(tmpFile, content, { encoding: "utf8", mode: 384 });
|
|
45235
46653
|
try {
|
|
45236
|
-
|
|
45237
|
-
if (
|
|
45238
|
-
const st =
|
|
45239
|
-
|
|
46654
|
+
fs12.chmodSync(tmpFile, 384);
|
|
46655
|
+
if (fs12.existsSync(targetFile)) {
|
|
46656
|
+
const st = fs12.statSync(targetFile);
|
|
46657
|
+
fs12.chownSync(tmpFile, st.uid, st.gid);
|
|
45240
46658
|
}
|
|
45241
46659
|
} catch {}
|
|
45242
46660
|
try {
|
|
45243
|
-
|
|
46661
|
+
fs12.renameSync(tmpFile, targetFile);
|
|
45244
46662
|
} catch (error) {
|
|
45245
46663
|
try {
|
|
45246
|
-
|
|
46664
|
+
fs12.unlinkSync(tmpFile);
|
|
45247
46665
|
} catch {}
|
|
45248
46666
|
throw error;
|
|
45249
46667
|
}
|
|
@@ -45300,7 +46718,7 @@ function statOwner(file) {
|
|
|
45300
46718
|
return;
|
|
45301
46719
|
let st;
|
|
45302
46720
|
try {
|
|
45303
|
-
st =
|
|
46721
|
+
st = fs12.statSync(file);
|
|
45304
46722
|
} catch {
|
|
45305
46723
|
return;
|
|
45306
46724
|
}
|
|
@@ -45444,7 +46862,7 @@ function expandHomePath(p) {
|
|
|
45444
46862
|
if (s === "~")
|
|
45445
46863
|
return os3.homedir();
|
|
45446
46864
|
if (s.startsWith("~/") || s.startsWith("~\\")) {
|
|
45447
|
-
return
|
|
46865
|
+
return path10.join(os3.homedir(), s.slice(2));
|
|
45448
46866
|
}
|
|
45449
46867
|
return s;
|
|
45450
46868
|
}
|
|
@@ -45493,10 +46911,10 @@ function prepareOutputDirectory(outputOpt) {
|
|
|
45493
46911
|
if (!outputOpt)
|
|
45494
46912
|
return;
|
|
45495
46913
|
const outputDir = expandHomePath(outputOpt);
|
|
45496
|
-
const outputPath =
|
|
45497
|
-
if (!
|
|
46914
|
+
const outputPath = path10.isAbsolute(outputDir) ? outputDir : path10.resolve(process.cwd(), outputDir);
|
|
46915
|
+
if (!fs12.existsSync(outputPath)) {
|
|
45498
46916
|
try {
|
|
45499
|
-
|
|
46917
|
+
fs12.mkdirSync(outputPath, { recursive: true });
|
|
45500
46918
|
} catch (e) {
|
|
45501
46919
|
const errAny = e;
|
|
45502
46920
|
const code = typeof errAny?.code === "string" ? errAny.code : "";
|
|
@@ -45587,8 +47005,8 @@ async function uploadCheckupReports(uploadCfg, reports, spinner, logUpload) {
|
|
|
45587
47005
|
}
|
|
45588
47006
|
function writeReportFiles(reports, outputPath) {
|
|
45589
47007
|
for (const [checkId, report] of Object.entries(reports)) {
|
|
45590
|
-
const filePath =
|
|
45591
|
-
|
|
47008
|
+
const filePath = path10.join(outputPath, `${checkId}.json`);
|
|
47009
|
+
fs12.writeFileSync(filePath, JSON.stringify(report, null, 2), "utf8");
|
|
45592
47010
|
const title = report.checkTitle || checkId;
|
|
45593
47011
|
console.log(`\u2713 ${checkId} ${title}: ${filePath}`);
|
|
45594
47012
|
}
|
|
@@ -45633,7 +47051,7 @@ function getDefaultMonitoringProjectDir() {
|
|
|
45633
47051
|
const override = process.env.PGAI_PROJECT_DIR;
|
|
45634
47052
|
if (override && override.trim())
|
|
45635
47053
|
return override.trim();
|
|
45636
|
-
return
|
|
47054
|
+
return path10.join(getConfigDir(), "monitoring");
|
|
45637
47055
|
}
|
|
45638
47056
|
async function downloadText(url) {
|
|
45639
47057
|
const controller = new AbortController;
|
|
@@ -45658,52 +47076,52 @@ function composeRefCandidates() {
|
|
|
45658
47076
|
}
|
|
45659
47077
|
async function ensureDefaultMonitoringProject() {
|
|
45660
47078
|
const projectDir = getDefaultMonitoringProjectDir();
|
|
45661
|
-
const composeFile =
|
|
45662
|
-
const instancesFile =
|
|
45663
|
-
if (!
|
|
45664
|
-
|
|
47079
|
+
const composeFile = path10.resolve(projectDir, "docker-compose.yml");
|
|
47080
|
+
const instancesFile = path10.resolve(projectDir, "instances.yml");
|
|
47081
|
+
if (!fs12.existsSync(projectDir)) {
|
|
47082
|
+
fs12.mkdirSync(projectDir, { recursive: true, mode: 448 });
|
|
45665
47083
|
}
|
|
45666
|
-
if (!
|
|
47084
|
+
if (!fs12.existsSync(composeFile)) {
|
|
45667
47085
|
const refs = composeRefCandidates();
|
|
45668
47086
|
let lastErr;
|
|
45669
47087
|
for (const ref of refs) {
|
|
45670
47088
|
const url = `https://gitlab.com/postgres-ai/postgres_ai/-/raw/${encodeURIComponent(ref)}/docker-compose.yml`;
|
|
45671
47089
|
try {
|
|
45672
47090
|
const text = await downloadText(url);
|
|
45673
|
-
|
|
47091
|
+
fs12.writeFileSync(composeFile, text, { encoding: "utf8", mode: 384 });
|
|
45674
47092
|
break;
|
|
45675
47093
|
} catch (err) {
|
|
45676
47094
|
lastErr = err;
|
|
45677
47095
|
}
|
|
45678
47096
|
}
|
|
45679
|
-
if (!
|
|
47097
|
+
if (!fs12.existsSync(composeFile)) {
|
|
45680
47098
|
const msg = lastErr instanceof Error ? lastErr.message : String(lastErr);
|
|
45681
47099
|
throw new Error(`Failed to bootstrap docker-compose.yml: ${msg}`);
|
|
45682
47100
|
}
|
|
45683
47101
|
}
|
|
45684
47102
|
ensureHostMetricsDir(projectDir);
|
|
45685
|
-
if (
|
|
45686
|
-
|
|
47103
|
+
if (fs12.existsSync(instancesFile) && fs12.lstatSync(instancesFile).isDirectory()) {
|
|
47104
|
+
fs12.rmSync(instancesFile, { recursive: true, force: true });
|
|
45687
47105
|
}
|
|
45688
|
-
if (!
|
|
47106
|
+
if (!fs12.existsSync(instancesFile)) {
|
|
45689
47107
|
const header = `# PostgreSQL instances to monitor
|
|
45690
47108
|
` + `# Add your instances using: pgai mon targets add <connection-string> <name>
|
|
45691
47109
|
|
|
45692
47110
|
`;
|
|
45693
|
-
|
|
47111
|
+
fs12.writeFileSync(instancesFile, header, { encoding: "utf8", mode: 384 });
|
|
45694
47112
|
}
|
|
45695
|
-
const pgwatchConfig =
|
|
45696
|
-
if (!
|
|
45697
|
-
|
|
47113
|
+
const pgwatchConfig = path10.resolve(projectDir, ".pgwatch-config");
|
|
47114
|
+
if (!fs12.existsSync(pgwatchConfig)) {
|
|
47115
|
+
fs12.writeFileSync(pgwatchConfig, "", { encoding: "utf8", mode: 384 });
|
|
45698
47116
|
}
|
|
45699
|
-
const envFile =
|
|
45700
|
-
if (!
|
|
47117
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
47118
|
+
if (!fs12.existsSync(envFile)) {
|
|
45701
47119
|
const envText = `PGAI_TAG=${package_default.version}
|
|
45702
47120
|
# PGAI_REGISTRY=registry.gitlab.com/postgres-ai/postgres_ai
|
|
45703
47121
|
`;
|
|
45704
|
-
|
|
47122
|
+
fs12.writeFileSync(envFile, envText, { encoding: "utf8", mode: 384 });
|
|
45705
47123
|
}
|
|
45706
|
-
return { fs:
|
|
47124
|
+
return { fs: fs12, path: path10, projectDir, composeFile, instancesFile };
|
|
45707
47125
|
}
|
|
45708
47126
|
function sanitizeTagForBackup(tag) {
|
|
45709
47127
|
if (tag == null)
|
|
@@ -45712,10 +47130,10 @@ function sanitizeTagForBackup(tag) {
|
|
|
45712
47130
|
return /^[A-Za-z0-9._-]{1,64}$/.test(stripped) ? stripped : null;
|
|
45713
47131
|
}
|
|
45714
47132
|
function readDeployedTag(projectDir) {
|
|
45715
|
-
const envFile =
|
|
45716
|
-
if (!
|
|
47133
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
47134
|
+
if (!fs12.existsSync(envFile))
|
|
45717
47135
|
return null;
|
|
45718
|
-
const m =
|
|
47136
|
+
const m = fs12.readFileSync(envFile, "utf8").match(/^PGAI_TAG=(.+)$/m);
|
|
45719
47137
|
return m ? sanitizeTagForBackup(m[1]) : null;
|
|
45720
47138
|
}
|
|
45721
47139
|
function isValidComposeYaml(text) {
|
|
@@ -45752,12 +47170,12 @@ function composeContentEqual(a, b) {
|
|
|
45752
47170
|
return a.replace(/\s+$/, "") === b.replace(/\s+$/, "");
|
|
45753
47171
|
}
|
|
45754
47172
|
async function refreshBundledComposeIfStale(projectDir, oldTag) {
|
|
45755
|
-
if (
|
|
47173
|
+
if (fs12.existsSync(path10.resolve(projectDir, ".git")))
|
|
45756
47174
|
return false;
|
|
45757
47175
|
if (process.env.PGAI_SKIP_COMPOSE_REFRESH === "1")
|
|
45758
47176
|
return false;
|
|
45759
|
-
const composeFile =
|
|
45760
|
-
if (!
|
|
47177
|
+
const composeFile = path10.resolve(projectDir, "docker-compose.yml");
|
|
47178
|
+
if (!fs12.existsSync(composeFile))
|
|
45761
47179
|
return false;
|
|
45762
47180
|
const refs = composeRefCandidates();
|
|
45763
47181
|
const fetched = await fetchTargetCompose(refs);
|
|
@@ -45766,24 +47184,24 @@ async function refreshBundledComposeIfStale(projectDir, oldTag) {
|
|
|
45766
47184
|
console.error(" Keeping the existing compose. If dashboards are blank after upgrade, re-run this command once network is available.");
|
|
45767
47185
|
return false;
|
|
45768
47186
|
}
|
|
45769
|
-
const existing =
|
|
47187
|
+
const existing = fs12.readFileSync(composeFile, "utf8");
|
|
45770
47188
|
if (composeContentEqual(existing, fetched))
|
|
45771
47189
|
return false;
|
|
45772
47190
|
const deployedTag = sanitizeTagForBackup(oldTag ?? readDeployedTag(projectDir));
|
|
45773
47191
|
const tagPart = deployedTag ?? new Date().toISOString().replace(/[:.]/g, "-");
|
|
45774
47192
|
const oldHash = crypto2.createHash("sha256").update(existing).digest("hex").slice(0, 8);
|
|
45775
|
-
const backup =
|
|
47193
|
+
const backup = path10.resolve(projectDir, `docker-compose.yml.bak-${tagPart}-${oldHash}`);
|
|
45776
47194
|
let backupName = null;
|
|
45777
47195
|
try {
|
|
45778
|
-
|
|
45779
|
-
backupName =
|
|
47196
|
+
fs12.writeFileSync(backup, existing, { encoding: "utf8", mode: 384, flag: "wx" });
|
|
47197
|
+
backupName = path10.basename(backup);
|
|
45780
47198
|
} catch (err) {
|
|
45781
47199
|
const e = err;
|
|
45782
47200
|
if (e && e.code === "EEXIST") {
|
|
45783
|
-
backupName =
|
|
47201
|
+
backupName = path10.basename(backup);
|
|
45784
47202
|
}
|
|
45785
47203
|
}
|
|
45786
|
-
|
|
47204
|
+
fs12.writeFileSync(composeFile, fetched, { encoding: "utf8", mode: 384 });
|
|
45787
47205
|
const backupNote = backupName ? ` (backup: ${backupName})` : "";
|
|
45788
47206
|
console.log(`\u2713 Refreshed docker-compose.yml to ${package_default.version}${backupNote}`);
|
|
45789
47207
|
return true;
|
|
@@ -47208,12 +48626,12 @@ function resolvePaths() {
|
|
|
47208
48626
|
const startDir = process.cwd();
|
|
47209
48627
|
let currentDir = startDir;
|
|
47210
48628
|
while (true) {
|
|
47211
|
-
const composeFile =
|
|
47212
|
-
if (
|
|
47213
|
-
const instancesFile =
|
|
47214
|
-
return { fs:
|
|
48629
|
+
const composeFile = path10.resolve(currentDir, "docker-compose.yml");
|
|
48630
|
+
if (fs12.existsSync(composeFile)) {
|
|
48631
|
+
const instancesFile = path10.resolve(currentDir, "instances.yml");
|
|
48632
|
+
return { fs: fs12, path: path10, projectDir: currentDir, composeFile, instancesFile };
|
|
47215
48633
|
}
|
|
47216
|
-
const parentDir =
|
|
48634
|
+
const parentDir = path10.dirname(currentDir);
|
|
47217
48635
|
if (parentDir === currentDir)
|
|
47218
48636
|
break;
|
|
47219
48637
|
currentDir = parentDir;
|
|
@@ -47232,7 +48650,7 @@ async function resolveOrInitPaths() {
|
|
|
47232
48650
|
}
|
|
47233
48651
|
function ensureHostMetricsDir(projectDir) {
|
|
47234
48652
|
try {
|
|
47235
|
-
|
|
48653
|
+
fs12.mkdirSync(path10.join(projectDir, "host-metrics"), { recursive: true, mode: 448 });
|
|
47236
48654
|
} catch {}
|
|
47237
48655
|
}
|
|
47238
48656
|
function isDockerRunning() {
|
|
@@ -47373,13 +48791,13 @@ function resolveAdoptedProject(reg) {
|
|
|
47373
48791
|
}
|
|
47374
48792
|
function updatePgwatchConfig(configPath, updates) {
|
|
47375
48793
|
let lines = [];
|
|
47376
|
-
if (
|
|
47377
|
-
const stats =
|
|
48794
|
+
if (fs12.existsSync(configPath)) {
|
|
48795
|
+
const stats = fs12.statSync(configPath);
|
|
47378
48796
|
if (!stats.isFile()) {
|
|
47379
48797
|
throw new Error(`${configPath} exists but is not a regular file; refusing to write through it`);
|
|
47380
48798
|
}
|
|
47381
48799
|
{
|
|
47382
|
-
const content =
|
|
48800
|
+
const content = fs12.readFileSync(configPath, "utf8").replace(/^\uFEFF+/, "");
|
|
47383
48801
|
lines = content.split(/\r?\n/).filter((l) => l.trim() !== "");
|
|
47384
48802
|
}
|
|
47385
48803
|
}
|
|
@@ -47392,18 +48810,18 @@ function updatePgwatchConfig(configPath, updates) {
|
|
|
47392
48810
|
lines.push(`${key}=${value}`);
|
|
47393
48811
|
}
|
|
47394
48812
|
}
|
|
47395
|
-
|
|
48813
|
+
fs12.writeFileSync(configPath, lines.join(`
|
|
47396
48814
|
`) + `
|
|
47397
48815
|
`, { encoding: "utf8", mode: 384 });
|
|
47398
48816
|
try {
|
|
47399
|
-
|
|
48817
|
+
fs12.chmodSync(configPath, 384);
|
|
47400
48818
|
} catch (err) {
|
|
47401
48819
|
const message = err instanceof Error ? err.message : String(err);
|
|
47402
48820
|
console.error(`Warning: could not restrict permissions on ${configPath}: ${message}`);
|
|
47403
48821
|
}
|
|
47404
48822
|
}
|
|
47405
48823
|
function applyApiKey(projectDir, apiKey, opts) {
|
|
47406
|
-
const configPath =
|
|
48824
|
+
const configPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
47407
48825
|
try {
|
|
47408
48826
|
updatePgwatchConfig(configPath, { api_key: apiKey, api_url: resolveBaseUrls2(opts).apiBaseUrl });
|
|
47409
48827
|
} catch (err) {
|
|
@@ -47438,10 +48856,10 @@ function parseDurationSeconds(v) {
|
|
|
47438
48856
|
function readInstanceIdFromProject() {
|
|
47439
48857
|
try {
|
|
47440
48858
|
const { projectDir } = resolvePaths();
|
|
47441
|
-
const configPath =
|
|
47442
|
-
if (!
|
|
48859
|
+
const configPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
48860
|
+
if (!fs12.existsSync(configPath))
|
|
47443
48861
|
return null;
|
|
47444
|
-
const line =
|
|
48862
|
+
const line = fs12.readFileSync(configPath, "utf8").replace(/^\uFEFF+/, "").split(/\r?\n/).find((l) => l.startsWith("instance_id="));
|
|
47445
48863
|
const value = line?.slice("instance_id=".length).trim();
|
|
47446
48864
|
return value && INSTANCE_ID_RE.test(value) ? value : null;
|
|
47447
48865
|
} catch {
|
|
@@ -47449,7 +48867,7 @@ function readInstanceIdFromProject() {
|
|
|
47449
48867
|
}
|
|
47450
48868
|
}
|
|
47451
48869
|
function applyProjectName(projectDir, project) {
|
|
47452
|
-
const configPath =
|
|
48870
|
+
const configPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
47453
48871
|
try {
|
|
47454
48872
|
updatePgwatchConfig(configPath, { project_name: project });
|
|
47455
48873
|
} catch (err) {
|
|
@@ -47465,7 +48883,7 @@ function persistInstanceId(projectDir, reg, requestedId, alsoWrite = {}) {
|
|
|
47465
48883
|
const updates = { ...alsoWrite, ...instanceId ? { instance_id: instanceId } : {} };
|
|
47466
48884
|
if (Object.keys(updates).length === 0)
|
|
47467
48885
|
return false;
|
|
47468
|
-
const configPath =
|
|
48886
|
+
const configPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
47469
48887
|
try {
|
|
47470
48888
|
updatePgwatchConfig(configPath, updates);
|
|
47471
48889
|
return instanceId != null;
|
|
@@ -47567,12 +48985,12 @@ async function runCompose(args, grafanaPassword, extraEnv) {
|
|
|
47567
48985
|
if (grafanaPassword) {
|
|
47568
48986
|
env.GF_SECURITY_ADMIN_PASSWORD = grafanaPassword;
|
|
47569
48987
|
} else {
|
|
47570
|
-
const cfgPath =
|
|
47571
|
-
if (
|
|
48988
|
+
const cfgPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
48989
|
+
if (fs12.existsSync(cfgPath)) {
|
|
47572
48990
|
try {
|
|
47573
|
-
const stats =
|
|
48991
|
+
const stats = fs12.statSync(cfgPath);
|
|
47574
48992
|
if (!stats.isDirectory()) {
|
|
47575
|
-
const content =
|
|
48993
|
+
const content = fs12.readFileSync(cfgPath, "utf8");
|
|
47576
48994
|
const match = content.match(/^grafana_password=([^\r\n]+)/m);
|
|
47577
48995
|
if (match) {
|
|
47578
48996
|
env.GF_SECURITY_ADMIN_PASSWORD = match[1].trim();
|
|
@@ -47585,10 +49003,10 @@ async function runCompose(args, grafanaPassword, extraEnv) {
|
|
|
47585
49003
|
}
|
|
47586
49004
|
}
|
|
47587
49005
|
}
|
|
47588
|
-
const envFilePath =
|
|
47589
|
-
if (
|
|
49006
|
+
const envFilePath = path10.resolve(projectDir, ".env");
|
|
49007
|
+
if (fs12.existsSync(envFilePath)) {
|
|
47590
49008
|
try {
|
|
47591
|
-
const envContent =
|
|
49009
|
+
const envContent = fs12.readFileSync(envFilePath, "utf8");
|
|
47592
49010
|
if (!env.VM_AUTH_USERNAME) {
|
|
47593
49011
|
const m = envContent.match(/^VM_AUTH_USERNAME=([^\r\n]+)/m);
|
|
47594
49012
|
if (m)
|
|
@@ -47632,7 +49050,7 @@ async function applySinkPrometheusConfig(projectDir) {
|
|
|
47632
49050
|
const { stdout } = await execFilePromise(composeCmd[0], [
|
|
47633
49051
|
...composeCmd.slice(1),
|
|
47634
49052
|
"-f",
|
|
47635
|
-
|
|
49053
|
+
path10.resolve(projectDir, "docker-compose.yml"),
|
|
47636
49054
|
"ps",
|
|
47637
49055
|
"-q",
|
|
47638
49056
|
"sink-prometheus"
|
|
@@ -47679,10 +49097,10 @@ function readComposeProfiles(knownProjectDir) {
|
|
|
47679
49097
|
projectDir = getDefaultMonitoringProjectDir();
|
|
47680
49098
|
}
|
|
47681
49099
|
}
|
|
47682
|
-
const envFile =
|
|
49100
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
47683
49101
|
let content;
|
|
47684
49102
|
try {
|
|
47685
|
-
content =
|
|
49103
|
+
content = fs12.readFileSync(envFile, "utf8");
|
|
47686
49104
|
} catch (err) {
|
|
47687
49105
|
if (err?.code !== "ENOENT") {
|
|
47688
49106
|
console.error(`\u26A0 Could not read ${envFile}: ${err instanceof Error ? err.message : String(err)}`);
|
|
@@ -47727,7 +49145,7 @@ async function composeServiceRunning(projectDir, service) {
|
|
|
47727
49145
|
if (!composeCmd)
|
|
47728
49146
|
return null;
|
|
47729
49147
|
try {
|
|
47730
|
-
const { stdout } = await execFilePromise(composeCmd[0], [...composeCmd.slice(1), "-f",
|
|
49148
|
+
const { stdout } = await execFilePromise(composeCmd[0], [...composeCmd.slice(1), "-f", path10.resolve(projectDir, "docker-compose.yml"), "ps", "-q", service], { cwd: projectDir });
|
|
47731
49149
|
return stdout.trim().length > 0;
|
|
47732
49150
|
} catch {
|
|
47733
49151
|
return null;
|
|
@@ -47884,10 +49302,11 @@ async function runConnect(urlArg, opts) {
|
|
|
47884
49302
|
const tty = !json && !!process.stderr.isTTY && !process.env.CI && process.env.TERM !== "dumb";
|
|
47885
49303
|
let createdId;
|
|
47886
49304
|
let stopping = false;
|
|
49305
|
+
let privateLink = false;
|
|
47887
49306
|
const steps = stepProgress({
|
|
47888
49307
|
json,
|
|
47889
49308
|
tty,
|
|
47890
|
-
makeView: () => new StepView2(`Deploying monitoring for ${shownName || "the Supabase project"}`, monitoringSteps(provider), true),
|
|
49309
|
+
makeView: () => new StepView2(`Deploying monitoring for ${shownName || "the Supabase project"}`, privateLink ? PRIVATELINK_STEPS : monitoringSteps(provider), true),
|
|
47891
49310
|
writeLine: (line) => console.error(line),
|
|
47892
49311
|
writeEvent: writeEvent2,
|
|
47893
49312
|
onId: (id) => {
|
|
@@ -47926,7 +49345,8 @@ ${reply}`);
|
|
|
47926
49345
|
process.on("SIGINT", onSigint);
|
|
47927
49346
|
let result;
|
|
47928
49347
|
try {
|
|
47929
|
-
|
|
49348
|
+
const resumed = provider === "rds" && process.env.PGAI_MON_NONCE ? { nonce: process.env.PGAI_MON_NONCE, monitoringPassword: process.env.PGAI_MON_PASSWORD } : {};
|
|
49349
|
+
result = await connect2(url, { ...opts, ...resumed, vcpus, waitMs: minutes * 60000 }, {
|
|
47930
49350
|
...api,
|
|
47931
49351
|
selfHosted: async (monitoringUrl, env) => {
|
|
47932
49352
|
const scope = getActiveOrgScope2();
|
|
@@ -47967,6 +49387,19 @@ ${reply}`);
|
|
|
47967
49387
|
closeReadline();
|
|
47968
49388
|
}
|
|
47969
49389
|
},
|
|
49390
|
+
...interactive(opts.json) ? { ask: async (q, lines = []) => {
|
|
49391
|
+
steps.view()?.stop();
|
|
49392
|
+
for (const line of lines)
|
|
49393
|
+
console.error(terminalText2(line));
|
|
49394
|
+
process.exitCode = 130;
|
|
49395
|
+
try {
|
|
49396
|
+
return await question(q);
|
|
49397
|
+
} finally {
|
|
49398
|
+
process.exitCode = undefined;
|
|
49399
|
+
closeReadline();
|
|
49400
|
+
steps.reset();
|
|
49401
|
+
}
|
|
49402
|
+
} } : {},
|
|
47970
49403
|
...interactive(opts.json) ? { allowIp: async (lines) => {
|
|
47971
49404
|
steps.view()?.stop();
|
|
47972
49405
|
console.error("");
|
|
@@ -48022,7 +49455,7 @@ Projects in the Supabase organization "${org}":`));
|
|
|
48022
49455
|
closeReadline();
|
|
48023
49456
|
}
|
|
48024
49457
|
} } : {},
|
|
48025
|
-
...interactive(opts.json) ? { openUrl: openInBrowser } : {},
|
|
49458
|
+
...interactive(opts.json) && canOpenBrowser() ? { openUrl: openInBrowser } : {},
|
|
48026
49459
|
progress: (e) => {
|
|
48027
49460
|
if (e.event === "provisioning")
|
|
48028
49461
|
requested = true;
|
|
@@ -48032,6 +49465,8 @@ Projects in the Supabase organization "${org}":`));
|
|
|
48032
49465
|
looking = false;
|
|
48033
49466
|
if (e.project)
|
|
48034
49467
|
shownName = terminalText2(e.project);
|
|
49468
|
+
if (e.location_reason === "privatelink")
|
|
49469
|
+
privateLink = true;
|
|
48035
49470
|
steps.progress(e);
|
|
48036
49471
|
},
|
|
48037
49472
|
aborted: () => stopping
|
|
@@ -48077,7 +49512,7 @@ Projects in the Supabase organization "${org}":`));
|
|
|
48077
49512
|
}
|
|
48078
49513
|
}
|
|
48079
49514
|
var mon = program2.command("mon").description("monitoring services management");
|
|
48080
|
-
sharedDeployOptions2(withOrgOptions(mon.command("deploy")), 20, "instance name (default: host[:port]/database)", "the box's region on its cloud (default: the database's region when that cloud has it; see below)", "where the monitoring box runs: aws or hetzner (default: the database's cloud, else aws)").description("deploy PostgresAI Cloud monitoring for a database: prepare it, provision the monitoring box, print its Health Matrix and dashboard").option("--provider <provider>", "generic (direct PostgreSQL URL; alias: self-managed) | clickhouse | rds | supabase (default: detected from the host)").option("--clickhouse-key <id:secret>", "ClickHouse Cloud API key (Basic Service API Reader) for CPU, memory and disk; or CLICKHOUSE_KEY_ID + CLICKHOUSE_KEY_SECRET").option("--self-hosted", "run the monitoring stack on this machine (mon local-install) instead of PostgresAI Cloud").option("--reset-password", "the monitoring role exists and its password is lost: set a new one (admin URL; refused while another database on the server is monitored)").option("--monitoring-user <name>", "monitoring role to create or use", DEFAULT_MONITORING_USER2).option("--coupon <code>", "promotion code for the organization's monitoring subscription (applies when it is first created)").option("--vcpus <n>", "vCPUs of the database server; without it there is no AAS (database load) data (asked at a terminal; Supabase: from the project's compute size)").option("--supabase-project <name|project-id>", "the Supabase project to monitor (its exact name, or its project ID), instead of a URL; implies --provider supabase").option("--ip-first", "reserve the monitoring box's IP and stop before the box: the IP is kept for a week, so you can allow it on the database and continue the deploy afterwards with --ip-allowed (PostgresAI Cloud only)").option("--ip-allowed", "the database now allows the reserved IP: create the box with it").addHelpText("after", [
|
|
49515
|
+
sharedDeployOptions2(withOrgOptions(mon.command("deploy")), 20, "instance name (default: host[:port]/database)", "the box's region on its cloud (default: the database's region when that cloud has it; see below)", "where the monitoring box runs: aws or hetzner (default: the database's cloud, else aws)").description("deploy PostgresAI Cloud monitoring for a database: prepare it, provision the monitoring box, print its Health Matrix and dashboard").option("--provider <provider>", "generic (direct PostgreSQL URL; alias: self-managed) | clickhouse | rds | supabase (default: detected from the host)").option("--clickhouse-key <id:secret>", "ClickHouse Cloud API key (Basic Service API Reader) for CPU, memory and disk; or CLICKHOUSE_KEY_ID + CLICKHOUSE_KEY_SECRET").option("--self-hosted", "run the monitoring stack on this machine (mon local-install) instead of PostgresAI Cloud").option("--reset-password", "the monitoring role exists and its password is lost: set a new one (admin URL; refused while another database on the server is monitored)").option("--monitoring-user <name>", "monitoring role to create or use (not over AWS PrivateLink, which uses postgres_ai_mon)", DEFAULT_MONITORING_USER2).option("--coupon <code>", "promotion code for the organization's monitoring subscription (applies when it is first created)").option("--vcpus <n>", "vCPUs of the database server; without it there is no AAS (database load) data (asked at a terminal; Supabase: from the project's compute size)").option("--supabase-project <name|project-id>", "the Supabase project to monitor (its exact name, or its project ID), instead of a URL; implies --provider supabase").option("--ip-first", "reserve the monitoring box's IP and stop before the box: the IP is kept for a week, so you can allow it on the database and continue the deploy afterwards with --ip-allowed (PostgresAI Cloud only)").option("--ip-allowed", "the database now allows the reserved IP: create the box with it").option("--privatelink", "RDS: monitor over AWS PrivateLink (the instance is not publicly accessible; default: decided from AWS or public DNS)").option("--no-privatelink", "RDS: monitor directly (the instance is publicly accessible)").option("--vpce-service <name>", "RDS over PrivateLink: the stack's VpceServiceName output, when this machine can't read it from AWS").option("--host-stats", "RDS over PrivateLink: also host CPU, memory, disk and IOPS, through a read-only role the stack creates").option("--host-stats-role <arn>", "RDS over PrivateLink: the stack's HostStatsRoleArn output, when not read from AWS (implies --host-stats)").option("--aws-profile <name>", "RDS: the aws CLI profile that reads the instance and its stack, read-only (default: the aws CLI's own)").option("--no-aws", "RDS: do not use the aws CLI on this machine").option("--terraform", "RDS over PrivateLink: write the stack as a Terraform module (./postgresai-privatelink-<instance>/) instead of the CloudFormation link").addHelpText("after", [
|
|
48081
49516
|
"",
|
|
48082
49517
|
"Steps (each skipped when already done; safe to re-run): sign in, prepare the database",
|
|
48083
49518
|
"(an admin URL creates the postgres_ai_mon role; otherwise the SQL is printed), provision",
|
|
@@ -48103,7 +49538,21 @@ sharedDeployOptions2(withOrgOptions(mon.command("deploy")), 20, "instance name (
|
|
|
48103
49538
|
"at a terminal). Allow it on the database, then re-run with --ip-allowed (at a terminal: Enter).",
|
|
48104
49539
|
"The IP is held 7 days and stays the box's for its life. If the box still can't connect, it waits",
|
|
48105
49540
|
"up to 90 minutes, then is removed and the IP kept: allow it and re-run with --ip-allowed.",
|
|
48106
|
-
"
|
|
49541
|
+
"",
|
|
49542
|
+
"Amazon RDS that is not publicly accessible: over AWS PrivateLink (nothing becomes public, no",
|
|
49543
|
+
"port is opened). Run deploy where the database is reachable (in its VPC). It reads the instance",
|
|
49544
|
+
"with the aws CLI when it can (read-only: the profile it uses is printed; --aws-profile picks one,",
|
|
49545
|
+
"--no-aws skips it), else asks public DNS; --privatelink / --no-privatelink decide instead. Then:",
|
|
49546
|
+
"the price, a CloudFormation link that creates the stack in your AWS account (filled in with what",
|
|
49547
|
+
"AWS said; --terraform writes the same as Terraform), the monitoring role, and the box on AWS in the",
|
|
49548
|
+
"RDS's region. At a terminal deploy waits up to 30 minutes for the stack (any name: it is found by",
|
|
49549
|
+
"its RDS; with several, you pick one), or without AWS asks for its VpceServiceName;",
|
|
49550
|
+
'elsewhere it stops (exit 3, "step":"privatelink_stack" with cfn_url): create the stack,',
|
|
49551
|
+
"then re-run (with --vpce-service <VpceServiceName> when AWS is not read here). When the URL's",
|
|
49552
|
+
`user can't create the role, the SQL is printed ("step":"prepare_db" with sql, nonce,`,
|
|
49553
|
+
"monitoring_password): run it as the master user in that database, then re-run with",
|
|
49554
|
+
"PGAI_MON_PASSWORD and PGAI_MON_NONCE set to those two. Aurora and RDS Proxy: not over PrivateLink.",
|
|
49555
|
+
"Any database can also be monitored from this machine with --self-hosted.",
|
|
48107
49556
|
"",
|
|
48108
49557
|
"Billing is per Postgres cluster observed: another database in a cluster the organization already",
|
|
48109
49558
|
'monitors is included. Once the URL is checked, deploy shows the price (or "free (N of M free slots)"). A billed',
|
|
@@ -48136,7 +49585,8 @@ sharedDeployOptions2(withOrgOptions(mon.command("deploy")), 20, "instance name (
|
|
|
48136
49585
|
"for another database on a server PostgresAI already monitors for the org, with",
|
|
48137
49586
|
"sslmode=require or verify-* in the URL: PostgresAI fills in the password it keeps, over TLS only),",
|
|
48138
49587
|
"PGPASSWORD (the password for a URL without one),",
|
|
48139
|
-
"CLICKHOUSE_KEY_ID + CLICKHOUSE_KEY_SECRET (instead of --clickhouse-key)
|
|
49588
|
+
"CLICKHOUSE_KEY_ID + CLICKHOUSE_KEY_SECRET (instead of --clickhouse-key),",
|
|
49589
|
+
"PGAI_MON_NONCE (RDS over PrivateLink: with PGAI_MON_PASSWORD, the printed SQL ran; see above).",
|
|
48140
49590
|
"",
|
|
48141
49591
|
"Examples:",
|
|
48142
49592
|
" pgai mon deploy 'postgresql://postgres:<password>@<host>.pg.clickhouse.cloud:5432/postgres?sslmode=require'",
|
|
@@ -48145,7 +49595,9 @@ sharedDeployOptions2(withOrgOptions(mon.command("deploy")), 20, "instance name (
|
|
|
48145
49595
|
" pgai mon deploy --supabase-project <name|project-id> --yes --json",
|
|
48146
49596
|
" pgai mon deploy 'postgresql://postgres@db.<project-ref>.supabase.co:5432/postgres'",
|
|
48147
49597
|
" pgai mon deploy '<url>' --cloud hetzner --location nbg1",
|
|
48148
|
-
" pgai mon deploy '<url>' --self-hosted"
|
|
49598
|
+
" pgai mon deploy '<url>' --self-hosted",
|
|
49599
|
+
" pgai mon deploy 'postgresql://postgres:<password>@<instance>.<id>.<region>.rds.amazonaws.com:5432/app'",
|
|
49600
|
+
" pgai mon deploy '<rds-url>' --yes --json --vpce-service com.amazonaws.vpce.<region>.vpce-svc-<id>"
|
|
48149
49601
|
].join(`
|
|
48150
49602
|
`)).action((urlArg, opts, cmd) => {
|
|
48151
49603
|
const supabase = providerArg2(opts.provider) === "supabase" || !opts.provider && !!opts.supabaseProject?.trim();
|
|
@@ -48177,7 +49629,10 @@ async function cloudInstance(opts, ref, live = false) {
|
|
|
48177
49629
|
}
|
|
48178
49630
|
async function linkedDatabases(opts) {
|
|
48179
49631
|
const api = cloudApi(opts.debug);
|
|
48180
|
-
return Promise.all((await api.list()).map(
|
|
49632
|
+
return Promise.all((await api.list()).map(async (d) => {
|
|
49633
|
+
const [linked, pl] = await Promise.all([api.links(d), privateLinkOf(d, api.rdsStatus)]);
|
|
49634
|
+
return pl ? { ...linked, privatelink: { region: pl.region, vpce_service_name: pl.vpce_service_name, verified: verifiedWords(pl.verified_status) } } : linked;
|
|
49635
|
+
}));
|
|
48181
49636
|
}
|
|
48182
49637
|
var monPollMs = () => {
|
|
48183
49638
|
const v = Number.parseInt(process.env.PGAI_DEPLOY_POLL_MS ?? "", 10);
|
|
@@ -48208,7 +49663,9 @@ withOrgOptions(monInstances.command("watch <id-or-name>")).alias("status").descr
|
|
|
48208
49663
|
} finally {
|
|
48209
49664
|
undetach();
|
|
48210
49665
|
}
|
|
48211
|
-
|
|
49666
|
+
const pl = await privateLinkOf(row, cloudApi(opts.debug).rdsStatus);
|
|
49667
|
+
const status = connectStatus2(await cloudApi(opts.debug).links(row));
|
|
49668
|
+
emitConnect(pl ? overPrivateLink2(status, privateLinkFailure(pl)) : status, opts.json);
|
|
48212
49669
|
} catch (err) {
|
|
48213
49670
|
failCloud(err, opts.json);
|
|
48214
49671
|
}
|
|
@@ -48219,6 +49676,7 @@ withOrgOptions(monInstances.command("delete <id-or-name>")).description("stop mo
|
|
|
48219
49676
|
if (!opts.yes && !(interactive(opts.json) && /^y/i.test(await question(`Stop monitoring ${row.name} and delete its monitoring box? (y/N): `)))) {
|
|
48220
49677
|
return emitConnect({ status: "action_required", provider: row.provider, name: row.name, id: row.id, next: `pgai mon instances delete ${row.id} --yes` }, opts.json);
|
|
48221
49678
|
}
|
|
49679
|
+
const pl = await privateLinkOf(row, cloudApi(opts.debug).rdsStatus);
|
|
48222
49680
|
const billing = disconnectBilling(await cloudApi(opts.debug).disconnect(row.id));
|
|
48223
49681
|
emitConnect({
|
|
48224
49682
|
status: "deleting",
|
|
@@ -48226,7 +49684,7 @@ withOrgOptions(monInstances.command("delete <id-or-name>")).description("stop mo
|
|
|
48226
49684
|
name: row.name,
|
|
48227
49685
|
id: row.id,
|
|
48228
49686
|
...billing ? { billing } : {},
|
|
48229
|
-
next: clickhouseKeyHeld(row) ? "Delete the ClickHouse Cloud API key you gave us" : "none"
|
|
49687
|
+
next: pl ? stackTeardown(pl, row.id) : clickhouseKeyHeld(row) ? "Delete the ClickHouse Cloud API key you gave us" : "none"
|
|
48230
49688
|
}, opts.json);
|
|
48231
49689
|
} catch (err) {
|
|
48232
49690
|
failCloud(err, opts.json);
|
|
@@ -48311,8 +49769,8 @@ mon.command("local-install").description("install local monitoring stack (genera
|
|
|
48311
49769
|
process.exitCode = 1;
|
|
48312
49770
|
return;
|
|
48313
49771
|
}
|
|
48314
|
-
const envFile =
|
|
48315
|
-
const existingEnv =
|
|
49772
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
49773
|
+
const existingEnv = fs12.existsSync(envFile) ? fs12.readFileSync(envFile, "utf8") : "";
|
|
48316
49774
|
const previousTagMatch = existingEnv.match(/^PGAI_TAG=(.+)$/m);
|
|
48317
49775
|
const previousTag = previousTagMatch ? previousTagMatch[1].trim() : null;
|
|
48318
49776
|
const imageTag = opts.tag || package_default.version;
|
|
@@ -48323,7 +49781,7 @@ mon.command("local-install").description("install local monitoring stack (genera
|
|
|
48323
49781
|
return;
|
|
48324
49782
|
}
|
|
48325
49783
|
const instanceJobs = instanceJobsRequest.enabled;
|
|
48326
|
-
const { content: envContent, preservedKeys } = buildLocalInstallEnv(existingEnv, imageTag, opts.instanceId || process.env.PGAI_INSTANCE_ID || null,
|
|
49784
|
+
const { content: envContent, preservedKeys } = buildLocalInstallEnv(existingEnv, imageTag, opts.instanceId || process.env.PGAI_INSTANCE_ID || null, path10.resolve(projectDir, ".pgwatch-config"), instanceJobs);
|
|
48327
49785
|
writeEnvFile(envFile, envContent);
|
|
48328
49786
|
if (preservedKeys.length > 0) {
|
|
48329
49787
|
console.log(`Preserved existing .env settings: ${preservedKeys.join(", ")}
|
|
@@ -48418,7 +49876,7 @@ Use demo mode without API key: postgres-ai mon local-install --demo`);
|
|
|
48418
49876
|
# Add your instances using: postgres-ai mon targets add
|
|
48419
49877
|
|
|
48420
49878
|
`;
|
|
48421
|
-
|
|
49879
|
+
fs12.writeFileSync(instancesPath, emptyInstancesContent, "utf8");
|
|
48422
49880
|
console.log(`Instances file: ${instancesPath}`);
|
|
48423
49881
|
console.log(`Project directory: ${projectDir}
|
|
48424
49882
|
`);
|
|
@@ -48509,17 +49967,17 @@ You can provide either:`);
|
|
|
48509
49967
|
}
|
|
48510
49968
|
} else {
|
|
48511
49969
|
console.log("Step 2: Demo mode enabled - using included demo PostgreSQL database");
|
|
48512
|
-
const currentDir =
|
|
49970
|
+
const currentDir = path10.dirname(fileURLToPath3(import.meta.url));
|
|
48513
49971
|
const demoCandidates = [
|
|
48514
|
-
|
|
48515
|
-
|
|
49972
|
+
path10.resolve(currentDir, "..", "..", "instances.demo.yml"),
|
|
49973
|
+
path10.resolve(currentDir, "..", "..", "..", "instances.demo.yml")
|
|
48516
49974
|
];
|
|
48517
|
-
const demoSrc = demoCandidates.find((p) =>
|
|
49975
|
+
const demoSrc = demoCandidates.find((p) => fs12.existsSync(p));
|
|
48518
49976
|
if (demoSrc) {
|
|
48519
|
-
if (
|
|
48520
|
-
|
|
49977
|
+
if (fs12.existsSync(instancesPath) && fs12.lstatSync(instancesPath).isDirectory()) {
|
|
49978
|
+
fs12.rmSync(instancesPath, { recursive: true, force: true });
|
|
48521
49979
|
}
|
|
48522
|
-
|
|
49980
|
+
fs12.copyFileSync(demoSrc, instancesPath);
|
|
48523
49981
|
console.log(`\u2713 Demo monitoring target configured
|
|
48524
49982
|
`);
|
|
48525
49983
|
} else {
|
|
@@ -48538,15 +49996,15 @@ Searched: ${demoCandidates.join(", ")}
|
|
|
48538
49996
|
console.log(`\u2713 Configuration updated
|
|
48539
49997
|
`);
|
|
48540
49998
|
console.log(opts.demo ? "Step 4: Configuring Grafana security..." : "Step 4: Configuring Grafana security...");
|
|
48541
|
-
const cfgPath =
|
|
49999
|
+
const cfgPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
48542
50000
|
let grafanaPassword = "";
|
|
48543
50001
|
let vmAuthUsername = "";
|
|
48544
50002
|
let vmAuthPassword = "";
|
|
48545
50003
|
try {
|
|
48546
|
-
if (
|
|
48547
|
-
const stats =
|
|
50004
|
+
if (fs12.existsSync(cfgPath)) {
|
|
50005
|
+
const stats = fs12.statSync(cfgPath);
|
|
48548
50006
|
if (!stats.isDirectory()) {
|
|
48549
|
-
const content =
|
|
50007
|
+
const content = fs12.readFileSync(cfgPath, "utf8");
|
|
48550
50008
|
const match = content.match(/^grafana_password=([^\r\n]+)/m);
|
|
48551
50009
|
if (match) {
|
|
48552
50010
|
grafanaPassword = match[1].trim();
|
|
@@ -48558,22 +50016,22 @@ Searched: ${demoCandidates.join(", ")}
|
|
|
48558
50016
|
const { stdout: password } = await execFilePromise("openssl", ["rand", "-base64", "12"]);
|
|
48559
50017
|
grafanaPassword = password.trim().replace(/\n/g, "");
|
|
48560
50018
|
let configContent = "";
|
|
48561
|
-
if (
|
|
48562
|
-
const stats =
|
|
50019
|
+
if (fs12.existsSync(cfgPath)) {
|
|
50020
|
+
const stats = fs12.statSync(cfgPath);
|
|
48563
50021
|
if (!stats.isDirectory()) {
|
|
48564
|
-
configContent =
|
|
50022
|
+
configContent = fs12.readFileSync(cfgPath, "utf8");
|
|
48565
50023
|
}
|
|
48566
50024
|
}
|
|
48567
50025
|
const lines = configContent.split(/\r?\n/).filter((l) => !/^grafana_password=/.test(l));
|
|
48568
50026
|
lines.push(`grafana_password=${grafanaPassword}`);
|
|
48569
|
-
|
|
50027
|
+
fs12.writeFileSync(cfgPath, lines.filter(Boolean).join(`
|
|
48570
50028
|
`) + `
|
|
48571
50029
|
`, {
|
|
48572
50030
|
encoding: "utf8",
|
|
48573
50031
|
mode: 384
|
|
48574
50032
|
});
|
|
48575
50033
|
try {
|
|
48576
|
-
|
|
50034
|
+
fs12.chmodSync(cfgPath, 384);
|
|
48577
50035
|
} catch {}
|
|
48578
50036
|
}
|
|
48579
50037
|
console.log(`\u2713 Grafana password configured
|
|
@@ -48585,9 +50043,9 @@ Searched: ${demoCandidates.join(", ")}
|
|
|
48585
50043
|
grafanaPassword = "demo";
|
|
48586
50044
|
}
|
|
48587
50045
|
try {
|
|
48588
|
-
const envFile =
|
|
48589
|
-
if (
|
|
48590
|
-
const envContent =
|
|
50046
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
50047
|
+
if (fs12.existsSync(envFile)) {
|
|
50048
|
+
const envContent = fs12.readFileSync(envFile, "utf8");
|
|
48591
50049
|
const userMatch = envContent.match(/^VM_AUTH_USERNAME=([^\r\n]+)/m);
|
|
48592
50050
|
const passMatch = envContent.match(/^VM_AUTH_PASSWORD=([^\r\n]+)/m);
|
|
48593
50051
|
if (userMatch)
|
|
@@ -48603,8 +50061,8 @@ Searched: ${demoCandidates.join(", ")}
|
|
|
48603
50061
|
vmAuthPassword = vmPass.trim().replace(/\n/g, "");
|
|
48604
50062
|
}
|
|
48605
50063
|
let envContent = "";
|
|
48606
|
-
if (
|
|
48607
|
-
envContent =
|
|
50064
|
+
if (fs12.existsSync(envFile)) {
|
|
50065
|
+
envContent = fs12.readFileSync(envFile, "utf8");
|
|
48608
50066
|
}
|
|
48609
50067
|
const envLines = envContent.split(/\r?\n/).filter((l) => !/^VM_AUTH_USERNAME=/.test(l) && !/^VM_AUTH_PASSWORD=/.test(l)).filter((l, i, arr) => !(i === arr.length - 1 && l === ""));
|
|
48610
50068
|
envLines.push(`VM_AUTH_USERNAME=${vmAuthUsername}`);
|
|
@@ -48900,11 +50358,11 @@ mon.command("config").description("show monitoring services configuration").acti
|
|
|
48900
50358
|
console.log(`Project Directory: ${projectDir}`);
|
|
48901
50359
|
console.log(`Docker Compose File: ${composeFile}`);
|
|
48902
50360
|
console.log(`Instances File: ${instancesFile}`);
|
|
48903
|
-
if (
|
|
50361
|
+
if (fs12.existsSync(instancesFile) && !fs12.lstatSync(instancesFile).isDirectory()) {
|
|
48904
50362
|
console.log(`
|
|
48905
50363
|
Instances configuration:
|
|
48906
50364
|
`);
|
|
48907
|
-
const text =
|
|
50365
|
+
const text = fs12.readFileSync(instancesFile, "utf8");
|
|
48908
50366
|
process.stdout.write(text);
|
|
48909
50367
|
if (!/\n$/.test(text))
|
|
48910
50368
|
console.log();
|
|
@@ -48958,8 +50416,8 @@ mon.command("update").description("update monitoring stack (migrate .env, pull i
|
|
|
48958
50416
|
console.log("\u2713 .env is up to date");
|
|
48959
50417
|
}
|
|
48960
50418
|
console.log();
|
|
48961
|
-
const gitDir =
|
|
48962
|
-
if (
|
|
50419
|
+
const gitDir = path10.resolve(projectDir, ".git");
|
|
50420
|
+
if (fs12.existsSync(gitDir)) {
|
|
48963
50421
|
console.log("Fetching latest changes...");
|
|
48964
50422
|
await execFilePromise("git", ["fetch", "origin"]);
|
|
48965
50423
|
const { stdout: branch } = await execFilePromise("git", ["rev-parse", "--abbrev-ref", "HEAD"]);
|
|
@@ -49206,10 +50664,10 @@ function parseEnvValue(content, key) {
|
|
|
49206
50664
|
return quoted ?? raw.replace(/ #.*$/, "").trimEnd();
|
|
49207
50665
|
}
|
|
49208
50666
|
function readEnvValue(projectDir, key) {
|
|
49209
|
-
const envFile =
|
|
49210
|
-
if (!
|
|
50667
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
50668
|
+
if (!fs12.existsSync(envFile))
|
|
49211
50669
|
return;
|
|
49212
|
-
return parseEnvValue(
|
|
50670
|
+
return parseEnvValue(fs12.readFileSync(envFile, "utf8"), key);
|
|
49213
50671
|
}
|
|
49214
50672
|
function supabaseHostMetricsOn(projectDir, env) {
|
|
49215
50673
|
return /^\s*true\s*$/i.test(env.PGAI_SUPABASE_HOST_METRICS ?? readEnvValue(projectDir, "PGAI_SUPABASE_HOST_METRICS") ?? "");
|
|
@@ -49217,8 +50675,8 @@ function supabaseHostMetricsOn(projectDir, env) {
|
|
|
49217
50675
|
var SUPABASE_TARGET_NAME_RE = /^[A-Za-z0-9_-]+$/;
|
|
49218
50676
|
var SUPABASE_TARGET_NAME_ERROR = "Host metrics: a Supabase target name may use only letters, digits, '_' and '-' while PGAI_SUPABASE_HOST_METRICS is true. Choose another name.";
|
|
49219
50677
|
function setEnvValues(projectDir, values) {
|
|
49220
|
-
const envFile =
|
|
49221
|
-
let content =
|
|
50678
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
50679
|
+
let content = fs12.existsSync(envFile) ? fs12.readFileSync(envFile, "utf8") : "";
|
|
49222
50680
|
for (const [key, value] of Object.entries(values)) {
|
|
49223
50681
|
const line = `^[ \\t]*(?:export[ \\t]+)?${key}=.*(?:\\n|$)`;
|
|
49224
50682
|
if (value === null)
|
|
@@ -49273,9 +50731,9 @@ async function setUpHostMetrics(projectDir, instance, connStr, env, apply) {
|
|
|
49273
50731
|
throw new Error(SUPABASE_TARGET_NAME_ERROR);
|
|
49274
50732
|
const dir = hostMetricsDir2(projectDir);
|
|
49275
50733
|
const file = `supabase-${name}.yml`;
|
|
49276
|
-
const stale =
|
|
50734
|
+
const stale = fs12.readdirSync(dir).filter((f) => /^supabase-.*\.yml$/.test(f) && !(on && f === file));
|
|
49277
50735
|
for (const f of stale)
|
|
49278
|
-
|
|
50736
|
+
fs12.rmSync(path10.join(dir, f), { force: true });
|
|
49279
50737
|
if (on)
|
|
49280
50738
|
writeScrapeFile2(dir, file, renderSupabaseScrapeConfig({ projectRef: projectRef.toLowerCase(), cluster, nodeName }));
|
|
49281
50739
|
else if (stale.length === 0)
|
|
@@ -49305,8 +50763,8 @@ async function setUpHostMetrics(projectDir, instance, connStr, env, apply) {
|
|
|
49305
50763
|
}
|
|
49306
50764
|
}
|
|
49307
50765
|
async function syncVmalert(projectDir, apply, addOnly = false) {
|
|
49308
|
-
const dir =
|
|
49309
|
-
const want =
|
|
50766
|
+
const dir = path10.join(projectDir, "host-metrics");
|
|
50767
|
+
const want = fs12.existsSync(dir) && fs12.readdirSync(dir).some((f) => /^(clickhouse|supabase)-.*\.yml$/.test(f));
|
|
49310
50768
|
const current = readEnvValue(projectDir, "COMPOSE_PROFILES") ?? "";
|
|
49311
50769
|
const exported = process.env.COMPOSE_PROFILES;
|
|
49312
50770
|
if (want && exported !== undefined && !instanceJobsProfileEnabled(exported, "host-metrics")) {
|
|
@@ -49392,7 +50850,7 @@ async function addTarget(file, projectDir, connStr, name, env, { apply = true, v
|
|
|
49392
50850
|
var targets = mon.command("targets").description("manage databases to monitor");
|
|
49393
50851
|
targets.command("list").description("list monitoring target databases").action(async () => {
|
|
49394
50852
|
const { instancesFile: instancesPath, projectDir } = await resolveOrInitPaths();
|
|
49395
|
-
if (!
|
|
50853
|
+
if (!fs12.existsSync(instancesPath) || fs12.lstatSync(instancesPath).isDirectory()) {
|
|
49396
50854
|
console.error(`instances.yml not found in ${projectDir}`);
|
|
49397
50855
|
process.exitCode = 1;
|
|
49398
50856
|
return;
|
|
@@ -49483,7 +50941,7 @@ Environment:
|
|
|
49483
50941
|
});
|
|
49484
50942
|
targets.command("remove <name>").description("remove monitoring target database").action(async (name) => {
|
|
49485
50943
|
const { instancesFile: file, projectDir } = await resolveOrInitPaths();
|
|
49486
|
-
if (!
|
|
50944
|
+
if (!fs12.existsSync(file) || fs12.lstatSync(file).isDirectory()) {
|
|
49487
50945
|
console.error("instances.yml not found");
|
|
49488
50946
|
process.exitCode = 1;
|
|
49489
50947
|
return;
|
|
@@ -49499,9 +50957,9 @@ targets.command("remove <name>").description("remove monitoring target database"
|
|
|
49499
50957
|
console.log(`Monitoring target '${name}' removed`);
|
|
49500
50958
|
if (removeHostMetrics(projectDir, name) && !await reloadHostMetrics(projectDir, `clickhouse-${name}`))
|
|
49501
50959
|
process.exitCode = 1;
|
|
49502
|
-
const supabaseFile =
|
|
49503
|
-
if (SUPABASE_TARGET_NAME_RE.test(name) &&
|
|
49504
|
-
|
|
50960
|
+
const supabaseFile = path10.join(projectDir, "host-metrics", `supabase-${name}.yml`);
|
|
50961
|
+
if (SUPABASE_TARGET_NAME_RE.test(name) && fs12.existsSync(supabaseFile)) {
|
|
50962
|
+
fs12.rmSync(supabaseFile);
|
|
49505
50963
|
if (!await reloadHostMetrics(projectDir, SUPABASE_JOB))
|
|
49506
50964
|
process.exitCode = 1;
|
|
49507
50965
|
}
|
|
@@ -49531,7 +50989,7 @@ targets.command("remove <name>").description("remove monitoring target database"
|
|
|
49531
50989
|
});
|
|
49532
50990
|
targets.command("test <name>").description("test monitoring target database connectivity").action(async (name) => {
|
|
49533
50991
|
const { instancesFile: instancesPath } = await resolveOrInitPaths();
|
|
49534
|
-
if (!
|
|
50992
|
+
if (!fs12.existsSync(instancesPath) || fs12.lstatSync(instancesPath).isDirectory()) {
|
|
49535
50993
|
console.error("instances.yml not found");
|
|
49536
50994
|
process.exitCode = 1;
|
|
49537
50995
|
return;
|
|
@@ -49846,15 +51304,15 @@ To authenticate, run: pgai auth`);
|
|
|
49846
51304
|
});
|
|
49847
51305
|
auth.command("remove-key").description("remove API key").action(async () => {
|
|
49848
51306
|
const newConfigPath = getConfigPath();
|
|
49849
|
-
const hasNewConfig =
|
|
51307
|
+
const hasNewConfig = fs12.existsSync(newConfigPath);
|
|
49850
51308
|
let legacyPath;
|
|
49851
51309
|
try {
|
|
49852
51310
|
const { projectDir } = await resolveOrInitPaths();
|
|
49853
|
-
legacyPath =
|
|
51311
|
+
legacyPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
49854
51312
|
} catch {
|
|
49855
|
-
legacyPath =
|
|
51313
|
+
legacyPath = path10.resolve(process.cwd(), ".pgwatch-config");
|
|
49856
51314
|
}
|
|
49857
|
-
const hasLegacyConfig =
|
|
51315
|
+
const hasLegacyConfig = fs12.existsSync(legacyPath) && fs12.statSync(legacyPath).isFile();
|
|
49858
51316
|
if (!hasNewConfig && !hasLegacyConfig) {
|
|
49859
51317
|
console.log("No API key configured");
|
|
49860
51318
|
return;
|
|
@@ -49864,11 +51322,11 @@ auth.command("remove-key").description("remove API key").action(async () => {
|
|
|
49864
51322
|
}
|
|
49865
51323
|
if (hasLegacyConfig) {
|
|
49866
51324
|
try {
|
|
49867
|
-
const content =
|
|
51325
|
+
const content = fs12.readFileSync(legacyPath, "utf8");
|
|
49868
51326
|
const filtered = content.split(/\r?\n/).filter((l) => !/^api_key=/.test(l)).join(`
|
|
49869
51327
|
`).replace(/\n+$/g, `
|
|
49870
51328
|
`);
|
|
49871
|
-
|
|
51329
|
+
fs12.writeFileSync(legacyPath, filtered, "utf8");
|
|
49872
51330
|
} catch (err) {
|
|
49873
51331
|
console.warn(`Warning: Could not update legacy config: ${err instanceof Error ? err.message : String(err)}`);
|
|
49874
51332
|
}
|
|
@@ -49879,7 +51337,7 @@ To authenticate again, run: pgai auth`);
|
|
|
49879
51337
|
});
|
|
49880
51338
|
mon.command("generate-grafana-password").description("generate Grafana password for monitoring services").action(async () => {
|
|
49881
51339
|
const { projectDir } = await resolveOrInitPaths();
|
|
49882
|
-
const cfgPath =
|
|
51340
|
+
const cfgPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
49883
51341
|
try {
|
|
49884
51342
|
const { stdout: password } = await execFilePromise("openssl", ["rand", "-base64", "12"]);
|
|
49885
51343
|
const newPassword = password.trim().replace(/\n/g, "");
|
|
@@ -49889,24 +51347,24 @@ mon.command("generate-grafana-password").description("generate Grafana password
|
|
|
49889
51347
|
return;
|
|
49890
51348
|
}
|
|
49891
51349
|
let configContent = "";
|
|
49892
|
-
if (
|
|
49893
|
-
const stats =
|
|
51350
|
+
if (fs12.existsSync(cfgPath)) {
|
|
51351
|
+
const stats = fs12.statSync(cfgPath);
|
|
49894
51352
|
if (stats.isDirectory()) {
|
|
49895
51353
|
console.error(".pgwatch-config is a directory, expected a file. Skipping read.");
|
|
49896
51354
|
} else {
|
|
49897
|
-
configContent =
|
|
51355
|
+
configContent = fs12.readFileSync(cfgPath, "utf8");
|
|
49898
51356
|
}
|
|
49899
51357
|
}
|
|
49900
51358
|
const lines = configContent.split(/\r?\n/).filter((l) => !/^grafana_password=/.test(l));
|
|
49901
51359
|
lines.push(`grafana_password=${newPassword}`);
|
|
49902
|
-
|
|
51360
|
+
fs12.writeFileSync(cfgPath, lines.filter(Boolean).join(`
|
|
49903
51361
|
`) + `
|
|
49904
51362
|
`, {
|
|
49905
51363
|
encoding: "utf8",
|
|
49906
51364
|
mode: 384
|
|
49907
51365
|
});
|
|
49908
51366
|
try {
|
|
49909
|
-
|
|
51367
|
+
fs12.chmodSync(cfgPath, 384);
|
|
49910
51368
|
} catch {}
|
|
49911
51369
|
console.log("\u2713 New Grafana password generated and saved");
|
|
49912
51370
|
console.log(`
|
|
@@ -49927,19 +51385,19 @@ Note: This command requires 'openssl' to be installed`);
|
|
|
49927
51385
|
});
|
|
49928
51386
|
mon.command("show-grafana-credentials").description("show Grafana credentials for monitoring services").action(async () => {
|
|
49929
51387
|
const { projectDir } = await resolveOrInitPaths();
|
|
49930
|
-
const cfgPath =
|
|
49931
|
-
if (!
|
|
51388
|
+
const cfgPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
51389
|
+
if (!fs12.existsSync(cfgPath)) {
|
|
49932
51390
|
console.error("Configuration file not found. Run 'postgres-ai mon local-install' first.");
|
|
49933
51391
|
process.exitCode = 1;
|
|
49934
51392
|
return;
|
|
49935
51393
|
}
|
|
49936
|
-
const stats =
|
|
51394
|
+
const stats = fs12.statSync(cfgPath);
|
|
49937
51395
|
if (stats.isDirectory()) {
|
|
49938
51396
|
console.error(".pgwatch-config is a directory, expected a file. Cannot read credentials.");
|
|
49939
51397
|
process.exitCode = 1;
|
|
49940
51398
|
return;
|
|
49941
51399
|
}
|
|
49942
|
-
const content =
|
|
51400
|
+
const content = fs12.readFileSync(cfgPath, "utf8");
|
|
49943
51401
|
const lines = content.split(/\r?\n/);
|
|
49944
51402
|
let password = "";
|
|
49945
51403
|
for (const line of lines) {
|
|
@@ -49959,9 +51417,9 @@ Grafana credentials:`);
|
|
|
49959
51417
|
console.log(" URL: http://localhost:3000");
|
|
49960
51418
|
console.log(" Username: monitor");
|
|
49961
51419
|
console.log(` Password: ${password}`);
|
|
49962
|
-
const envFile =
|
|
49963
|
-
if (
|
|
49964
|
-
const envContent =
|
|
51420
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
51421
|
+
if (fs12.existsSync(envFile)) {
|
|
51422
|
+
const envContent = fs12.readFileSync(envFile, "utf8");
|
|
49965
51423
|
const vmUser = envContent.match(/^VM_AUTH_USERNAME=([^\r\n]+)/m);
|
|
49966
51424
|
const vmPass = envContent.match(/^VM_AUTH_PASSWORD=([^\r\n]+)/m);
|
|
49967
51425
|
if (vmUser && vmPass) {
|
|
@@ -50846,13 +52304,13 @@ withOrgOptions(reports.command("data [reportId]")).description("get checkup repo
|
|
|
50846
52304
|
});
|
|
50847
52305
|
spinner.stop();
|
|
50848
52306
|
if (opts.output) {
|
|
50849
|
-
const dir =
|
|
50850
|
-
|
|
52307
|
+
const dir = path10.resolve(opts.output);
|
|
52308
|
+
fs12.mkdirSync(dir, { recursive: true });
|
|
50851
52309
|
for (const f of result) {
|
|
50852
|
-
const safeName =
|
|
50853
|
-
const filePath =
|
|
52310
|
+
const safeName = path10.basename(f.filename);
|
|
52311
|
+
const filePath = path10.join(dir, safeName);
|
|
50854
52312
|
const content = f.type === "json" ? JSON.stringify(tryParseJson(f.data), null, 2) : f.data;
|
|
50855
|
-
|
|
52313
|
+
fs12.writeFileSync(filePath, content, "utf-8");
|
|
50856
52314
|
console.log(filePath);
|
|
50857
52315
|
}
|
|
50858
52316
|
} else if (opts.json) {
|
|
@@ -51350,29 +52808,29 @@ mcp.command("install [client]").description("install MCP server configuration fo
|
|
|
51350
52808
|
let configDir;
|
|
51351
52809
|
switch (client) {
|
|
51352
52810
|
case "cursor":
|
|
51353
|
-
configPath =
|
|
51354
|
-
configDir =
|
|
52811
|
+
configPath = path10.join(homeDir, ".cursor", "mcp.json");
|
|
52812
|
+
configDir = path10.dirname(configPath);
|
|
51355
52813
|
break;
|
|
51356
52814
|
case "windsurf":
|
|
51357
|
-
configPath =
|
|
51358
|
-
configDir =
|
|
52815
|
+
configPath = path10.join(homeDir, ".windsurf", "mcp.json");
|
|
52816
|
+
configDir = path10.dirname(configPath);
|
|
51359
52817
|
break;
|
|
51360
52818
|
case "codex":
|
|
51361
|
-
configPath =
|
|
51362
|
-
configDir =
|
|
52819
|
+
configPath = path10.join(homeDir, ".codex", "mcp.json");
|
|
52820
|
+
configDir = path10.dirname(configPath);
|
|
51363
52821
|
break;
|
|
51364
52822
|
default:
|
|
51365
52823
|
console.error(`Configuration not implemented for: ${client}`);
|
|
51366
52824
|
process.exitCode = 1;
|
|
51367
52825
|
return;
|
|
51368
52826
|
}
|
|
51369
|
-
if (!
|
|
51370
|
-
|
|
52827
|
+
if (!fs12.existsSync(configDir)) {
|
|
52828
|
+
fs12.mkdirSync(configDir, { recursive: true });
|
|
51371
52829
|
}
|
|
51372
52830
|
let config = { mcpServers: {} };
|
|
51373
|
-
if (
|
|
52831
|
+
if (fs12.existsSync(configPath)) {
|
|
51374
52832
|
try {
|
|
51375
|
-
const content =
|
|
52833
|
+
const content = fs12.readFileSync(configPath, "utf8");
|
|
51376
52834
|
config = JSON.parse(content);
|
|
51377
52835
|
if (!config.mcpServers) {
|
|
51378
52836
|
config.mcpServers = {};
|
|
@@ -51385,7 +52843,7 @@ mcp.command("install [client]").description("install MCP server configuration fo
|
|
|
51385
52843
|
command: pgaiPath,
|
|
51386
52844
|
args: ["mcp", "start"]
|
|
51387
52845
|
};
|
|
51388
|
-
|
|
52846
|
+
fs12.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf8");
|
|
51389
52847
|
console.log(`\u2713 PostgresAI MCP server configured for ${client}`);
|
|
51390
52848
|
console.log(` Config file: ${configPath}`);
|
|
51391
52849
|
console.log("");
|