postgresai 0.18.0-dev.15 → 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 +1821 -291
- package/dist/sql/01.role.sql +2 -0
- package/dist/sql/02.extensions.sql +1 -0
- package/dist/sql/03.permissions.sql +112 -8
- package/dist/sql/04.optional_rds.sql +1 -0
- package/dist/sql/05.optional_self_managed.sql +1 -0
- package/dist/sql/06.helpers.sql +1 -0
- package/dist/sql/uninit/01.helpers.sql +3 -1
- package/dist/sql/uninit/02.permissions.sql +14 -2
- package/dist/sql/uninit/03.role.sql +5 -4
- package/package.json +1 -1
- package/sql/01.role.sql +2 -0
- package/sql/02.extensions.sql +1 -0
- package/sql/03.permissions.sql +112 -8
- package/sql/04.optional_rds.sql +1 -0
- package/sql/05.optional_self_managed.sql +1 -0
- package/sql/06.helpers.sql +1 -0
- package/sql/uninit/01.helpers.sql +3 -1
- package/sql/uninit/02.permissions.sql +14 -2
- package/sql/uninit/03.role.sql +5 -4
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;
|
|
@@ -22045,6 +22055,42 @@ function safeDoDelimiter(...values) {
|
|
|
22045
22055
|
function redactPasswordsInSql(sql) {
|
|
22046
22056
|
return sql.replace(/password\s+'(?:''|[^'])*'/gi, "password '<redacted>'");
|
|
22047
22057
|
}
|
|
22058
|
+
function renderInitPlanSql(plan) {
|
|
22059
|
+
const lines = [
|
|
22060
|
+
"-- Run the complete plan in one session. For psql, use -v ON_ERROR_STOP=1.",
|
|
22061
|
+
"begin;"
|
|
22062
|
+
];
|
|
22063
|
+
for (const step of plan.steps) {
|
|
22064
|
+
const sql = redactPasswordsInSql(step.sql);
|
|
22065
|
+
lines.push(`
|
|
22066
|
+
-- ${step.name}${step.optional ? " (optional)" : ""}`);
|
|
22067
|
+
if (!step.optional) {
|
|
22068
|
+
lines.push(sql);
|
|
22069
|
+
continue;
|
|
22070
|
+
}
|
|
22071
|
+
const delimiter = (base) => {
|
|
22072
|
+
let i = 0;
|
|
22073
|
+
while (sql.includes(`$${base}_${i}$`))
|
|
22074
|
+
i++;
|
|
22075
|
+
return `$${base}_${i}$`;
|
|
22076
|
+
};
|
|
22077
|
+
const outer = delimiter("pgai_optional_outer");
|
|
22078
|
+
const inner = delimiter("pgai_optional_sql");
|
|
22079
|
+
lines.push(`do ${outer}
|
|
22080
|
+
begin
|
|
22081
|
+
begin
|
|
22082
|
+
execute ${inner}
|
|
22083
|
+
${sql}
|
|
22084
|
+
${inner};
|
|
22085
|
+
exception when others then
|
|
22086
|
+
raise warning 'optional SQL step ${step.name} skipped: %', sqlerrm;
|
|
22087
|
+
end;
|
|
22088
|
+
end ${outer};`);
|
|
22089
|
+
}
|
|
22090
|
+
lines.push("commit;");
|
|
22091
|
+
return lines.join(`
|
|
22092
|
+
`);
|
|
22093
|
+
}
|
|
22048
22094
|
function maskConnectionString(dbUrl) {
|
|
22049
22095
|
let masked = dbUrl;
|
|
22050
22096
|
const head = dbUrl.match(/^(\s*[a-z][a-z0-9+.-]*:\/\/)([^:@\/?#]*):([\s\S]*)$/i);
|
|
@@ -22793,6 +22839,178 @@ function isNumericProjectRef(ref) {
|
|
|
22793
22839
|
return /^[0-9]+$/.test(ref.trim());
|
|
22794
22840
|
}
|
|
22795
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
|
+
|
|
22796
23014
|
// lib/supabase.ts
|
|
22797
23015
|
var SUPABASE_API_BASE = "https://api.supabase.com";
|
|
22798
23016
|
function extractProjectRefFromUrl(dbUrl) {
|
|
@@ -23133,9 +23351,17 @@ async function connect(url, opts, deps) {
|
|
|
23133
23351
|
const key = parseClickhouseKey(opts.clickhouseKey, provider === "clickhouse" && !opts.agent ? process.env : {});
|
|
23134
23352
|
if (key && provider !== "clickhouse")
|
|
23135
23353
|
throw new Error("--clickhouse-key applies to ClickHouse Managed Postgres only");
|
|
23354
|
+
const plFlag = privateLinkFlag(opts);
|
|
23136
23355
|
if (!opts.selfHosted && provider === "rds") {
|
|
23137
|
-
const
|
|
23138
|
-
|
|
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` };
|
|
23139
23365
|
}
|
|
23140
23366
|
if (opts.selfHosted && opts.resetPassword) {
|
|
23141
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" };
|
|
@@ -23842,6 +24068,540 @@ async function supabaseBox(ref, name, opts, deps, progress, stop, label) {
|
|
|
23842
24068
|
return { ...connectStatus(box, provider, true), ...b.billing };
|
|
23843
24069
|
return follow(box, { provider, name, fresh: true, billing: b.billing, billingUrl: b.billingUrl, note: "", place }, opts, deps, progress);
|
|
23844
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
|
+
}
|
|
23845
24605
|
var STILL_IN_PROGRESS = "Still in progress";
|
|
23846
24606
|
var CONNECT_TIMEOUT_MS = 55000;
|
|
23847
24607
|
var AGENT_CONNECT_TIMEOUT_MS = 25000;
|
|
@@ -23875,7 +24635,14 @@ async function follow(row, f, opts, deps, progress) {
|
|
|
23875
24635
|
const url = (_) => f.url ?? name;
|
|
23876
24636
|
const billing = { ...f.billing, ...f.place };
|
|
23877
24637
|
let shown;
|
|
24638
|
+
let unverified;
|
|
23878
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
|
+
}
|
|
23879
24646
|
if (row.billing_error && disconnecting(row.status)) {
|
|
23880
24647
|
if (undoRole)
|
|
23881
24648
|
await undoRole();
|
|
@@ -23905,15 +24672,16 @@ async function follow(row, f, opts, deps, progress) {
|
|
|
23905
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>";
|
|
23906
24673
|
}
|
|
23907
24674
|
const { next, ...rest } = final;
|
|
24675
|
+
const why = unverified && final.status !== "ready" ? `${unverified} ` : "";
|
|
23908
24676
|
if (final.status === "in_progress" && opts.waitMs > 0) {
|
|
23909
24677
|
return {
|
|
23910
24678
|
...rest,
|
|
23911
24679
|
status: "action_required",
|
|
23912
24680
|
...billing,
|
|
23913
|
-
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}`
|
|
23914
24682
|
};
|
|
23915
24683
|
}
|
|
23916
|
-
return { ...rest, ...billing, next: next + note };
|
|
24684
|
+
return { ...rest, ...billing, next: why + next + note };
|
|
23917
24685
|
}
|
|
23918
24686
|
await deps.sleep(POLL_MS);
|
|
23919
24687
|
const listed = await deps.list().catch(() => {
|
|
@@ -23994,7 +24762,8 @@ async function prepareDatabase(url, provider, opts = {}) {
|
|
|
23994
24762
|
or (pg_has_role(current_user, 'pg_monitor', 'USAGE WITH ADMIN OPTION')
|
|
23995
24763
|
and pg_has_role(current_user, 'pg_read_all_stats', 'USAGE WITH ADMIN OPTION')))
|
|
23996
24764
|
and has_database_privilege(current_user, current_database(), 'CREATE')
|
|
23997
|
-
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,
|
|
23998
24767
|
exists (select 1 from pg_roles where rolname = ${quoteLiteral(role)}) as mon_exists,
|
|
23999
24768
|
exists (select 1 from pg_roles where rolname = ${quoteLiteral(role)} and (rolsuper or rolcreatedb or rolcreaterole or rolreplication or rolbypassrls)) as mon_privileged,
|
|
24000
24769
|
(select string_agg(g.rolname::text, ', ' order by g.rolname::text collate "C") from pg_roles m join pg_roles g
|
|
@@ -24117,10 +24886,7 @@ async function prepareDatabase(url, provider, opts = {}) {
|
|
|
24117
24886
|
}
|
|
24118
24887
|
const plan = await buildInitPlan({ database: me.db, monitoringUser: role, monitoringPassword: "<password>", includeOptionalPermissions: true, provider: pgProvider, keepExistingPassword: true });
|
|
24119
24888
|
return {
|
|
24120
|
-
sql: plan
|
|
24121
|
-
${redactPasswordsInSql(s.sql)}`).join(`
|
|
24122
|
-
|
|
24123
|
-
`),
|
|
24889
|
+
sql: renderInitPlanSql(plan),
|
|
24124
24890
|
next: `Run the SQL as an admin, with a password of your choice in place of <redacted> (or re-run with an admin URL), then pgai mon deploy again with the ${role} URL`
|
|
24125
24891
|
};
|
|
24126
24892
|
} finally {
|
|
@@ -24248,10 +25014,50 @@ function platformDeps(p) {
|
|
|
24248
25014
|
supabaseConnect: (body) => rpc("cloud_monitoring_supabase_connect", body),
|
|
24249
25015
|
supabaseProjects: () => rpc("cloud_monitoring_supabase_projects", {}),
|
|
24250
25016
|
deployOptions: (args) => rpc("monitoring_deploy_options", args),
|
|
24251
|
-
|
|
24252
|
-
|
|
24253
|
-
|
|
24254
|
-
|
|
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;
|
|
24255
25061
|
},
|
|
24256
25062
|
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
24257
25063
|
now: () => Date.now()
|
|
@@ -30930,6 +31736,7 @@ async function handleToolCall(req, rootOpts, extra) {
|
|
|
30930
31736
|
}
|
|
30931
31737
|
try {
|
|
30932
31738
|
if (toolName === "deploy_monitoring") {
|
|
31739
|
+
const str = (v) => v === undefined || v === null ? undefined : String(v);
|
|
30933
31740
|
const scope = resolveMcpOrgScope(args, apiKey, cfg);
|
|
30934
31741
|
if (scope.error)
|
|
30935
31742
|
return scope.error;
|
|
@@ -30949,6 +31756,15 @@ async function handleToolCall(req, rootOpts, extra) {
|
|
|
30949
31756
|
location: args.location === undefined || args.location === null ? undefined : String(args.location),
|
|
30950
31757
|
ipFirst: args.ip_first === true,
|
|
30951
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),
|
|
30952
31768
|
waitMs: 0,
|
|
30953
31769
|
agent: true
|
|
30954
31770
|
}, {
|
|
@@ -31523,7 +32339,7 @@ async function startMcpServer(rootOpts, extra) {
|
|
|
31523
32339
|
},
|
|
31524
32340
|
{
|
|
31525
32341
|
name: "deploy_monitoring",
|
|
31526
|
-
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).",
|
|
31527
32343
|
inputSchema: {
|
|
31528
32344
|
type: "object",
|
|
31529
32345
|
properties: {
|
|
@@ -31536,9 +32352,18 @@ async function startMcpServer(rootOpts, extra) {
|
|
|
31536
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)" },
|
|
31537
32353
|
coupon: { type: "string", description: "Stripe promotion code for the organization's monitoring subscription (applies when it is first created)" },
|
|
31538
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" },
|
|
31539
|
-
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" },
|
|
31540
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" },
|
|
31541
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" },
|
|
31542
32367
|
debug: { type: "boolean", description: "Enable verbose debug logs" }
|
|
31543
32368
|
},
|
|
31544
32369
|
required: ["database_url"],
|
|
@@ -32441,6 +33266,9 @@ function renderMarkdownForTerminal(md) {
|
|
|
32441
33266
|
}
|
|
32442
33267
|
|
|
32443
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";
|
|
32444
33272
|
var clickhouseKeyHeld = (d) => d.host_metrics && (d.host_metrics_source === undefined || d.host_metrics_source === "clickhouse");
|
|
32445
33273
|
function httpStatus2(err) {
|
|
32446
33274
|
const status = err instanceof Error ? err.status : undefined;
|
|
@@ -32677,8 +33505,11 @@ function stepProgress(o) {
|
|
|
32677
33505
|
view.start();
|
|
32678
33506
|
}
|
|
32679
33507
|
view.advance(e.event);
|
|
32680
|
-
|
|
33508
|
+
const kept = e.event === "privatelink" && (!!e.cfn_url || e.message.startsWith("Using stack "));
|
|
33509
|
+
if (/^(Note|Warning):/.test(e.message) || kept)
|
|
32681
33510
|
view.log(e.message);
|
|
33511
|
+
if (e.event === "privatelink" && !kept && !/^(Note|Warning):/.test(e.message))
|
|
33512
|
+
view.setNote(e.message);
|
|
32682
33513
|
if (e.event === "box" && e.state) {
|
|
32683
33514
|
view.setNote(e.state === "waiting_for_access" && e.ip ? `box: waiting for database access from ${e.ip}` : `box: ${boxWords2(e.state)}`);
|
|
32684
33515
|
if (e.state === "waiting_for_access" && e.ip && e.error && e.error !== refused) {
|
|
@@ -32696,7 +33527,7 @@ function stepProgress(o) {
|
|
|
32696
33527
|
function stepViewEnd(r) {
|
|
32697
33528
|
if (r.status === "ready")
|
|
32698
33529
|
return "complete";
|
|
32699
|
-
if (r.status === "in_progress" || r.step
|
|
33530
|
+
if (r.status === "in_progress" || r.step || r.next.startsWith(STILL_IN_PROGRESS2))
|
|
32700
33531
|
return "stop";
|
|
32701
33532
|
return "fail";
|
|
32702
33533
|
}
|
|
@@ -32717,6 +33548,7 @@ function personLines(r) {
|
|
|
32717
33548
|
...r.price ? [` Price: ${r.price}`] : [],
|
|
32718
33549
|
...r.cloud ? [` Runs on: ${cloudWord2(r.cloud)}, ${r.location}`] : [],
|
|
32719
33550
|
...r.egress_ip ? [` Connects from: ${r.egress_ip}`] : [],
|
|
33551
|
+
...r.provider === "rds" && r.host_metrics ? [" Host metrics: on (CPU, memory, disk, IOPS)"] : [],
|
|
32720
33552
|
...!r.health_url && !r.dashboard_url && r.next && r.next !== "none" ? [`Next: ${r.next}`] : [],
|
|
32721
33553
|
...extra && (r.health_url || r.dashboard_url) ? [`Note: ${extra}`] : []
|
|
32722
33554
|
];
|
|
@@ -32737,6 +33569,8 @@ function personLines(r) {
|
|
|
32737
33569
|
}
|
|
32738
33570
|
if (r.projects?.length)
|
|
32739
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}`];
|
|
32740
33574
|
return r.choices ? [r.next, ...Object.entries(r.choices).map(([cloud, codes]) => ` --cloud ${cloud} --location ${codes.join("|")}`)] : [r.next];
|
|
32741
33575
|
}
|
|
32742
33576
|
}
|
|
@@ -32854,9 +33688,17 @@ async function connect2(url, opts, deps) {
|
|
|
32854
33688
|
const key = parseClickhouseKey2(opts.clickhouseKey, provider === "clickhouse" && !opts.agent ? process.env : {});
|
|
32855
33689
|
if (key && provider !== "clickhouse")
|
|
32856
33690
|
throw new Error("--clickhouse-key applies to ClickHouse Managed Postgres only");
|
|
33691
|
+
const plFlag = privateLinkFlag2(opts);
|
|
32857
33692
|
if (!opts.selfHosted && provider === "rds") {
|
|
32858
|
-
const
|
|
32859
|
-
|
|
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` };
|
|
32860
33702
|
}
|
|
32861
33703
|
if (opts.selfHosted && opts.resetPassword) {
|
|
32862
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" };
|
|
@@ -33631,6 +34473,559 @@ async function supabaseBox2(ref, name, opts, deps, progress, stop, label) {
|
|
|
33631
34473
|
return { ...connectStatus2(box, provider, true), ...b.billing };
|
|
33632
34474
|
return follow2(box, { provider, name, fresh: true, billing: b.billing, billingUrl: b.billingUrl, note: "", place }, opts, deps, progress);
|
|
33633
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
|
+
}
|
|
33634
35029
|
var STILL_IN_PROGRESS2 = "Still in progress";
|
|
33635
35030
|
var CONNECT_TIMEOUT_MS2 = 55000;
|
|
33636
35031
|
var AGENT_CONNECT_TIMEOUT_MS2 = 25000;
|
|
@@ -33664,7 +35059,14 @@ async function follow2(row, f, opts, deps, progress) {
|
|
|
33664
35059
|
const url = (_) => f.url ?? name;
|
|
33665
35060
|
const billing = { ...f.billing, ...f.place };
|
|
33666
35061
|
let shown;
|
|
35062
|
+
let unverified;
|
|
33667
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
|
+
}
|
|
33668
35070
|
if (row.billing_error && disconnecting2(row.status)) {
|
|
33669
35071
|
if (undoRole)
|
|
33670
35072
|
await undoRole();
|
|
@@ -33694,15 +35096,16 @@ async function follow2(row, f, opts, deps, progress) {
|
|
|
33694
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>";
|
|
33695
35097
|
}
|
|
33696
35098
|
const { next, ...rest } = final;
|
|
35099
|
+
const why = unverified && final.status !== "ready" ? `${unverified} ` : "";
|
|
33697
35100
|
if (final.status === "in_progress" && opts.waitMs > 0) {
|
|
33698
35101
|
return {
|
|
33699
35102
|
...rest,
|
|
33700
35103
|
status: "action_required",
|
|
33701
35104
|
...billing,
|
|
33702
|
-
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}`
|
|
33703
35106
|
};
|
|
33704
35107
|
}
|
|
33705
|
-
return { ...rest, ...billing, next: next + note };
|
|
35108
|
+
return { ...rest, ...billing, next: why + next + note };
|
|
33706
35109
|
}
|
|
33707
35110
|
await deps.sleep(POLL_MS2);
|
|
33708
35111
|
const listed = await deps.list().catch(() => {
|
|
@@ -33783,7 +35186,8 @@ async function prepareDatabase2(url, provider, opts = {}) {
|
|
|
33783
35186
|
or (pg_has_role(current_user, 'pg_monitor', 'USAGE WITH ADMIN OPTION')
|
|
33784
35187
|
and pg_has_role(current_user, 'pg_read_all_stats', 'USAGE WITH ADMIN OPTION')))
|
|
33785
35188
|
and has_database_privilege(current_user, current_database(), 'CREATE')
|
|
33786
|
-
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,
|
|
33787
35191
|
exists (select 1 from pg_roles where rolname = ${quoteLiteral(role)}) as mon_exists,
|
|
33788
35192
|
exists (select 1 from pg_roles where rolname = ${quoteLiteral(role)} and (rolsuper or rolcreatedb or rolcreaterole or rolreplication or rolbypassrls)) as mon_privileged,
|
|
33789
35193
|
(select string_agg(g.rolname::text, ', ' order by g.rolname::text collate "C") from pg_roles m join pg_roles g
|
|
@@ -33906,10 +35310,7 @@ async function prepareDatabase2(url, provider, opts = {}) {
|
|
|
33906
35310
|
}
|
|
33907
35311
|
const plan = await buildInitPlan({ database: me.db, monitoringUser: role, monitoringPassword: "<password>", includeOptionalPermissions: true, provider: pgProvider, keepExistingPassword: true });
|
|
33908
35312
|
return {
|
|
33909
|
-
sql: plan
|
|
33910
|
-
${redactPasswordsInSql(s.sql)}`).join(`
|
|
33911
|
-
|
|
33912
|
-
`),
|
|
35313
|
+
sql: renderInitPlanSql(plan),
|
|
33913
35314
|
next: `Run the SQL as an admin, with a password of your choice in place of <redacted> (or re-run with an admin URL), then pgai mon deploy again with the ${role} URL`
|
|
33914
35315
|
};
|
|
33915
35316
|
} finally {
|
|
@@ -34053,10 +35454,50 @@ function platformDeps2(p) {
|
|
|
34053
35454
|
supabaseConnect: (body) => rpc("cloud_monitoring_supabase_connect", body),
|
|
34054
35455
|
supabaseProjects: () => rpc("cloud_monitoring_supabase_projects", {}),
|
|
34055
35456
|
deployOptions: (args) => rpc("monitoring_deploy_options", args),
|
|
34056
|
-
|
|
34057
|
-
|
|
34058
|
-
|
|
34059
|
-
|
|
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;
|
|
34060
35501
|
},
|
|
34061
35502
|
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
34062
35503
|
now: () => Date.now()
|
|
@@ -35737,6 +37178,13 @@ var SUPABASE_STEPS = [
|
|
|
35737
37178
|
{ key: "box", label: "Prepare the database and install monitoring (about 5 min)" },
|
|
35738
37179
|
{ key: "ready", label: "Ready" }
|
|
35739
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
|
+
];
|
|
35740
37188
|
var monitoringSteps = (provider) => provider === "supabase" ? SUPABASE_STEPS : MONITORING_STEPS;
|
|
35741
37189
|
var FRAMES2 = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
35742
37190
|
var GREEN2 = "\x1B[32m";
|
|
@@ -36351,8 +37799,8 @@ async function registerAasCollection(apiKey, instanceId, opts) {
|
|
|
36351
37799
|
}
|
|
36352
37800
|
|
|
36353
37801
|
// lib/storage.ts
|
|
36354
|
-
import * as
|
|
36355
|
-
import * as
|
|
37802
|
+
import * as fs8 from "fs";
|
|
37803
|
+
import * as path7 from "path";
|
|
36356
37804
|
var MAX_UPLOAD_SIZE2 = 500 * 1024 * 1024;
|
|
36357
37805
|
var MAX_DOWNLOAD_SIZE2 = 500 * 1024 * 1024;
|
|
36358
37806
|
var MIME_TYPES2 = {
|
|
@@ -36393,11 +37841,11 @@ async function uploadFile2(params) {
|
|
|
36393
37841
|
if (!filePath) {
|
|
36394
37842
|
throw new Error("filePath is required");
|
|
36395
37843
|
}
|
|
36396
|
-
const resolvedPath =
|
|
36397
|
-
if (!
|
|
37844
|
+
const resolvedPath = path7.resolve(filePath);
|
|
37845
|
+
if (!fs8.existsSync(resolvedPath)) {
|
|
36398
37846
|
throw new Error(`File not found: ${resolvedPath}`);
|
|
36399
37847
|
}
|
|
36400
|
-
const stat =
|
|
37848
|
+
const stat = fs8.statSync(resolvedPath);
|
|
36401
37849
|
if (!stat.isFile()) {
|
|
36402
37850
|
throw new Error(`Not a file: ${resolvedPath}`);
|
|
36403
37851
|
}
|
|
@@ -36409,9 +37857,9 @@ async function uploadFile2(params) {
|
|
|
36409
37857
|
console.error("Warning: storage URL uses HTTP — API key will be sent unencrypted");
|
|
36410
37858
|
}
|
|
36411
37859
|
const url = `${base}/upload`;
|
|
36412
|
-
const fileBuffer =
|
|
36413
|
-
const fileName =
|
|
36414
|
-
const ext =
|
|
37860
|
+
const fileBuffer = fs8.readFileSync(resolvedPath);
|
|
37861
|
+
const fileName = path7.basename(resolvedPath);
|
|
37862
|
+
const ext = path7.extname(fileName).toLowerCase();
|
|
36415
37863
|
const mimeType = MIME_TYPES2[ext] || "application/octet-stream";
|
|
36416
37864
|
const formData = new FormData;
|
|
36417
37865
|
formData.append("file", new Blob([fileBuffer], { type: mimeType }), fileName);
|
|
@@ -36471,15 +37919,15 @@ async function downloadFile2(params) {
|
|
|
36471
37919
|
const relativePath = fileUrl.startsWith("/") ? fileUrl : `/${fileUrl}`;
|
|
36472
37920
|
fullUrl = `${base}${relativePath}`;
|
|
36473
37921
|
}
|
|
36474
|
-
const urlFilename =
|
|
37922
|
+
const urlFilename = path7.basename(new URL(fullUrl).pathname);
|
|
36475
37923
|
if (!urlFilename) {
|
|
36476
37924
|
throw new Error("Cannot derive filename from URL; please specify --output");
|
|
36477
37925
|
}
|
|
36478
|
-
const saveTo = outputPath ?
|
|
37926
|
+
const saveTo = outputPath ? path7.resolve(outputPath) : path7.resolve(urlFilename);
|
|
36479
37927
|
if (!outputPath) {
|
|
36480
|
-
const normalizedSave =
|
|
36481
|
-
const cwd =
|
|
36482
|
-
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)) {
|
|
36483
37931
|
throw new Error("Derived output path escapes current directory; please specify --output");
|
|
36484
37932
|
}
|
|
36485
37933
|
}
|
|
@@ -36512,11 +37960,11 @@ async function downloadFile2(params) {
|
|
|
36512
37960
|
}
|
|
36513
37961
|
const arrayBuffer = await response.arrayBuffer();
|
|
36514
37962
|
const buffer = Buffer.from(arrayBuffer);
|
|
36515
|
-
const parentDir =
|
|
36516
|
-
if (!
|
|
36517
|
-
|
|
37963
|
+
const parentDir = path7.dirname(saveTo);
|
|
37964
|
+
if (!fs8.existsSync(parentDir)) {
|
|
37965
|
+
fs8.mkdirSync(parentDir, { recursive: true });
|
|
36518
37966
|
}
|
|
36519
|
-
|
|
37967
|
+
fs8.writeFileSync(saveTo, buffer);
|
|
36520
37968
|
return {
|
|
36521
37969
|
savedTo: saveTo,
|
|
36522
37970
|
size: buffer.length,
|
|
@@ -36528,9 +37976,9 @@ function buildMarkdownLink2(fileUrl, storageBaseUrl, filename) {
|
|
|
36528
37976
|
const base = normalizeBaseUrl(storageBaseUrl);
|
|
36529
37977
|
const normalizedFileUrl = fileUrl.startsWith("/") ? fileUrl : `/${fileUrl}`;
|
|
36530
37978
|
const fullUrl = fileUrl.startsWith("http://") || fileUrl.startsWith("https://") ? fileUrl : `${base}${normalizedFileUrl}`;
|
|
36531
|
-
const name = filename ||
|
|
37979
|
+
const name = filename || path7.basename(new URL(fullUrl).pathname);
|
|
36532
37980
|
const safeName = name.replace(/[\[\]()]/g, "\\$&");
|
|
36533
|
-
const ext =
|
|
37981
|
+
const ext = path7.extname(name).toLowerCase();
|
|
36534
37982
|
if (IMAGE_EXTENSIONS2.has(ext)) {
|
|
36535
37983
|
return ``;
|
|
36536
37984
|
}
|
|
@@ -36576,7 +38024,7 @@ ${links}`;
|
|
|
36576
38024
|
|
|
36577
38025
|
// lib/clickhouse.ts
|
|
36578
38026
|
import { existsSync as existsSync7, renameSync, rmSync as rmSync2, writeFileSync as writeFileSync7 } from "node:fs";
|
|
36579
|
-
import { join as
|
|
38027
|
+
import { join as join7 } from "node:path";
|
|
36580
38028
|
function validateName(name) {
|
|
36581
38029
|
if (!/^[A-Za-z0-9_-]+$/.test(name))
|
|
36582
38030
|
throw new Error("Invalid ClickHouse target name.");
|
|
@@ -36643,7 +38091,7 @@ async function addHostMetrics({ projectDir, name, conn, env, cluster = "default"
|
|
|
36643
38091
|
const prefix = `clickhouse-${name}`;
|
|
36644
38092
|
const text = renderScrapeConfig({ name, cluster, nodeName, orgId, serviceId: service.id, keyId, passwordFile: `/etc/pgai/host-metrics/${prefix}.secret`, apiUrl });
|
|
36645
38093
|
const dir = hostMetricsDir(projectDir);
|
|
36646
|
-
const secretFile =
|
|
38094
|
+
const secretFile = join7(dir, `${prefix}.secret`);
|
|
36647
38095
|
const tmpFile = `${secretFile}.tmp`;
|
|
36648
38096
|
rmSync2(tmpFile, { force: true });
|
|
36649
38097
|
try {
|
|
@@ -36658,7 +38106,7 @@ async function addHostMetrics({ projectDir, name, conn, env, cluster = "default"
|
|
|
36658
38106
|
function removeHostMetrics(projectDir, name) {
|
|
36659
38107
|
if (!/^[A-Za-z0-9_-]+$/.test(name))
|
|
36660
38108
|
return false;
|
|
36661
|
-
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));
|
|
36662
38110
|
for (const file of files)
|
|
36663
38111
|
rmSync2(file, { force: true });
|
|
36664
38112
|
return files.length > 0;
|
|
@@ -36667,21 +38115,21 @@ function removeHostMetrics(projectDir, name) {
|
|
|
36667
38115
|
// lib/host-metrics.ts
|
|
36668
38116
|
import { createHash as createHash3 } from "node:crypto";
|
|
36669
38117
|
import { chmodSync as chmodSync4, lstatSync as lstatSync3, mkdirSync as mkdirSync6, readFileSync as readFileSync9, writeFileSync as writeFileSync8 } from "node:fs";
|
|
36670
|
-
import { join as
|
|
38118
|
+
import { join as join8 } from "node:path";
|
|
36671
38119
|
function scrapeFileText2(config) {
|
|
36672
38120
|
const revision = `r${createHash3("sha256").update(dump2([config], { lineWidth: -1 })).digest("hex").slice(0, 16)}`;
|
|
36673
38121
|
Object.assign(config.static_configs[0].labels, { __pgai_rev: revision });
|
|
36674
38122
|
return dump2([config], { lineWidth: -1 });
|
|
36675
38123
|
}
|
|
36676
38124
|
function scrapeRevision(projectDir, file) {
|
|
36677
|
-
const [config] = load2(readFileSync9(
|
|
38125
|
+
const [config] = load2(readFileSync9(join8(projectDir, "host-metrics", file), "utf8"));
|
|
36678
38126
|
const revision = config?.static_configs?.[0]?.labels?.__pgai_rev;
|
|
36679
38127
|
if (typeof revision !== "string" || !/^r[0-9a-f]{16}$/.test(revision))
|
|
36680
38128
|
throw new Error(`host-metrics/${file} has no __pgai_rev label.`);
|
|
36681
38129
|
return revision;
|
|
36682
38130
|
}
|
|
36683
38131
|
function hostMetricsDir2(projectDir) {
|
|
36684
|
-
const dir =
|
|
38132
|
+
const dir = join8(projectDir, "host-metrics");
|
|
36685
38133
|
if (lstatSync3(dir, { throwIfNoEntry: false })?.isSymbolicLink())
|
|
36686
38134
|
throw new Error(`${dir}: host-metrics must be a directory, not a symlink.`);
|
|
36687
38135
|
mkdirSync6(dir, { recursive: true, mode: 448 });
|
|
@@ -36689,8 +38137,8 @@ function hostMetricsDir2(projectDir) {
|
|
|
36689
38137
|
return dir;
|
|
36690
38138
|
}
|
|
36691
38139
|
function writeScrapeFile2(dir, file, text) {
|
|
36692
|
-
writeFileSync8(
|
|
36693
|
-
chmodSync4(
|
|
38140
|
+
writeFileSync8(join8(dir, file), text, { mode: 420 });
|
|
38141
|
+
chmodSync4(join8(dir, file), 420);
|
|
36694
38142
|
}
|
|
36695
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`;
|
|
36696
38144
|
var SUPABASE_JOB = "supabase-host-metrics";
|
|
@@ -36719,8 +38167,8 @@ function rdsInstance(host) {
|
|
|
36719
38167
|
// lib/init.ts
|
|
36720
38168
|
import { randomBytes as randomBytes3 } from "crypto";
|
|
36721
38169
|
import { URL as URL3, fileURLToPath as fileURLToPath2 } from "url";
|
|
36722
|
-
import * as
|
|
36723
|
-
import * as
|
|
38170
|
+
import * as fs9 from "fs";
|
|
38171
|
+
import * as path8 from "path";
|
|
36724
38172
|
var DEFAULT_MONITORING_USER2 = "postgres_ai_mon";
|
|
36725
38173
|
var KNOWN_PROVIDERS = ["self-managed", "supabase", "clickhouse"];
|
|
36726
38174
|
var SKIP_ROLE_CREATION_PROVIDERS2 = ["supabase"];
|
|
@@ -36865,21 +38313,21 @@ function describeInitScope(plan) {
|
|
|
36865
38313
|
}
|
|
36866
38314
|
function sqlDir2() {
|
|
36867
38315
|
const currentFile = fileURLToPath2(import.meta.url);
|
|
36868
|
-
const currentDir =
|
|
38316
|
+
const currentDir = path8.dirname(currentFile);
|
|
36869
38317
|
const candidates = [
|
|
36870
|
-
|
|
36871
|
-
|
|
38318
|
+
path8.resolve(currentDir, "..", "sql"),
|
|
38319
|
+
path8.resolve(currentDir, "..", "..", "sql")
|
|
36872
38320
|
];
|
|
36873
38321
|
for (const candidate of candidates) {
|
|
36874
|
-
if (
|
|
38322
|
+
if (fs9.existsSync(candidate)) {
|
|
36875
38323
|
return candidate;
|
|
36876
38324
|
}
|
|
36877
38325
|
}
|
|
36878
38326
|
throw new Error(`SQL directory not found. Searched: ${candidates.join(", ")}`);
|
|
36879
38327
|
}
|
|
36880
38328
|
function loadSqlTemplate2(filename) {
|
|
36881
|
-
const p =
|
|
36882
|
-
return
|
|
38329
|
+
const p = path8.join(sqlDir2(), filename);
|
|
38330
|
+
return fs9.readFileSync(p, "utf8");
|
|
36883
38331
|
}
|
|
36884
38332
|
function applyTemplate2(sql, vars) {
|
|
36885
38333
|
return sql.replace(/\{\{([A-Z0-9_]+)\}\}/g, (_, key) => {
|
|
@@ -36912,6 +38360,42 @@ function safeDoDelimiter2(...values) {
|
|
|
36912
38360
|
function redactPasswordsInSql2(sql) {
|
|
36913
38361
|
return sql.replace(/password\s+'(?:''|[^'])*'/gi, "password '<redacted>'");
|
|
36914
38362
|
}
|
|
38363
|
+
function renderInitPlanSql2(plan) {
|
|
38364
|
+
const lines = [
|
|
38365
|
+
"-- Run the complete plan in one session. For psql, use -v ON_ERROR_STOP=1.",
|
|
38366
|
+
"begin;"
|
|
38367
|
+
];
|
|
38368
|
+
for (const step of plan.steps) {
|
|
38369
|
+
const sql = redactPasswordsInSql2(step.sql);
|
|
38370
|
+
lines.push(`
|
|
38371
|
+
-- ${step.name}${step.optional ? " (optional)" : ""}`);
|
|
38372
|
+
if (!step.optional) {
|
|
38373
|
+
lines.push(sql);
|
|
38374
|
+
continue;
|
|
38375
|
+
}
|
|
38376
|
+
const delimiter = (base) => {
|
|
38377
|
+
let i = 0;
|
|
38378
|
+
while (sql.includes(`$${base}_${i}$`))
|
|
38379
|
+
i++;
|
|
38380
|
+
return `$${base}_${i}$`;
|
|
38381
|
+
};
|
|
38382
|
+
const outer = delimiter("pgai_optional_outer");
|
|
38383
|
+
const inner = delimiter("pgai_optional_sql");
|
|
38384
|
+
lines.push(`do ${outer}
|
|
38385
|
+
begin
|
|
38386
|
+
begin
|
|
38387
|
+
execute ${inner}
|
|
38388
|
+
${sql}
|
|
38389
|
+
${inner};
|
|
38390
|
+
exception when others then
|
|
38391
|
+
raise warning 'optional SQL step ${step.name} skipped: %', sqlerrm;
|
|
38392
|
+
end;
|
|
38393
|
+
end ${outer};`);
|
|
38394
|
+
}
|
|
38395
|
+
lines.push("commit;");
|
|
38396
|
+
return lines.join(`
|
|
38397
|
+
`);
|
|
38398
|
+
}
|
|
36915
38399
|
function maskConnectionString2(dbUrl) {
|
|
36916
38400
|
let masked = dbUrl;
|
|
36917
38401
|
const head = dbUrl.match(/^(\s*[a-z][a-z0-9+.-]*:\/\/)([^:@\/?#]*):([\s\S]*)$/i);
|
|
@@ -38539,6 +40023,32 @@ function raiseConnectAttemptTimeout(execArgv = process.execArgv, nodeOptions = p
|
|
|
38539
40023
|
return nodeNet.getDefaultAutoSelectFamilyAttemptTimeout();
|
|
38540
40024
|
}
|
|
38541
40025
|
|
|
40026
|
+
// lib/prepare-error.ts
|
|
40027
|
+
function terminalDiagnosticLine(value) {
|
|
40028
|
+
return terminalText(value).replace(/\r/g, "\\r").replace(/\n/g, "\\n").replace(/[\u202a-\u202e\u2066-\u2069]/g, "");
|
|
40029
|
+
}
|
|
40030
|
+
function prepareDbPrivilegeAdvice(code, failedStep, message, supabase) {
|
|
40031
|
+
if (code !== "42501")
|
|
40032
|
+
return [];
|
|
40033
|
+
if (failedStep === "03.permissions" && message.includes("postgres_ai schema ")) {
|
|
40034
|
+
return ["Fix: ask a DBA to inspect the named schema ownership, grants, or object before retrying"];
|
|
40035
|
+
}
|
|
40036
|
+
const lines = [];
|
|
40037
|
+
if (failedStep === "01.role") {
|
|
40038
|
+
lines.push("Context: role creation/update requires CREATEROLE or superuser");
|
|
40039
|
+
} else if (failedStep === "03.permissions") {
|
|
40040
|
+
lines.push("Context: grants/view/search_path require sufficient GRANT/DDL privileges");
|
|
40041
|
+
}
|
|
40042
|
+
if (supabase) {
|
|
40043
|
+
lines.push("Fix: ensure your Supabase access token has sufficient permissions");
|
|
40044
|
+
} else {
|
|
40045
|
+
lines.push("Fix: connect as a superuser (or a role with CREATEROLE and sufficient GRANT privileges)");
|
|
40046
|
+
lines.push("Fix: on managed Postgres, use the provider's admin/master user");
|
|
40047
|
+
}
|
|
40048
|
+
lines.push("Tip: run with --print-sql to review the exact SQL plan");
|
|
40049
|
+
return lines;
|
|
40050
|
+
}
|
|
40051
|
+
|
|
38542
40052
|
// lib/feedback.ts
|
|
38543
40053
|
var FEEDBACK_URL = "https://gitlab.com/postgres-ai/postgresai/-/work_items/300";
|
|
38544
40054
|
var FEEDBACK_SUPPRESS_ENV = "PGAI_NO_FEEDBACK_TIP";
|
|
@@ -38620,8 +40130,8 @@ import { Writable } from "stream";
|
|
|
38620
40130
|
import * as childProcess from "child_process";
|
|
38621
40131
|
|
|
38622
40132
|
// lib/checkup.ts
|
|
38623
|
-
import * as
|
|
38624
|
-
import * as
|
|
40133
|
+
import * as fs10 from "fs";
|
|
40134
|
+
import * as path9 from "path";
|
|
38625
40135
|
|
|
38626
40136
|
// lib/metrics-embedded.ts
|
|
38627
40137
|
var METRICS = {
|
|
@@ -42589,7 +44099,7 @@ function createBaseReport(checkId, checkTitle, nodeName) {
|
|
|
42589
44099
|
}
|
|
42590
44100
|
function readTextFileSafe(p) {
|
|
42591
44101
|
try {
|
|
42592
|
-
const value =
|
|
44102
|
+
const value = fs10.readFileSync(p, "utf8").trim();
|
|
42593
44103
|
return value || null;
|
|
42594
44104
|
} catch {
|
|
42595
44105
|
return null;
|
|
@@ -42602,8 +44112,8 @@ function resolveBuildTs() {
|
|
|
42602
44112
|
if (fromFile)
|
|
42603
44113
|
return fromFile;
|
|
42604
44114
|
try {
|
|
42605
|
-
const pkgRoot =
|
|
42606
|
-
const fromPkgFile = readTextFileSafe(
|
|
44115
|
+
const pkgRoot = path9.resolve(__dirname, "..");
|
|
44116
|
+
const fromPkgFile = readTextFileSafe(path9.join(pkgRoot, "BUILD_TS"));
|
|
42607
44117
|
if (fromPkgFile)
|
|
42608
44118
|
return fromPkgFile;
|
|
42609
44119
|
} catch (err) {
|
|
@@ -42611,8 +44121,8 @@ function resolveBuildTs() {
|
|
|
42611
44121
|
console.warn(`[resolveBuildTs] Warning: path resolution failed: ${errorMsg}`);
|
|
42612
44122
|
}
|
|
42613
44123
|
try {
|
|
42614
|
-
const pkgJsonPath =
|
|
42615
|
-
const st =
|
|
44124
|
+
const pkgJsonPath = path9.resolve(__dirname, "..", "package.json");
|
|
44125
|
+
const st = fs10.statSync(pkgJsonPath);
|
|
42616
44126
|
return st.mtime.toISOString();
|
|
42617
44127
|
} catch (err) {
|
|
42618
44128
|
if (process.env.DEBUG) {
|
|
@@ -44821,7 +46331,7 @@ function runChild(command, args, env, json, source) {
|
|
|
44821
46331
|
}
|
|
44822
46332
|
|
|
44823
46333
|
// lib/instances.ts
|
|
44824
|
-
import * as
|
|
46334
|
+
import * as fs11 from "fs";
|
|
44825
46335
|
class InstancesParseError2 extends Error {
|
|
44826
46336
|
constructor(file, cause) {
|
|
44827
46337
|
const yamlErr = cause;
|
|
@@ -44831,11 +46341,11 @@ class InstancesParseError2 extends Error {
|
|
|
44831
46341
|
}
|
|
44832
46342
|
}
|
|
44833
46343
|
function loadInstances2(file) {
|
|
44834
|
-
if (!
|
|
46344
|
+
if (!fs11.existsSync(file))
|
|
44835
46345
|
return [];
|
|
44836
|
-
if (
|
|
46346
|
+
if (fs11.lstatSync(file).isDirectory())
|
|
44837
46347
|
return [];
|
|
44838
|
-
const text =
|
|
46348
|
+
const text = fs11.readFileSync(file, "utf8");
|
|
44839
46349
|
if (text.trim() === "")
|
|
44840
46350
|
return [];
|
|
44841
46351
|
let parsed;
|
|
@@ -44868,8 +46378,8 @@ function buildInstance(name, connStr) {
|
|
|
44868
46378
|
};
|
|
44869
46379
|
}
|
|
44870
46380
|
function addInstanceToFile(file, instance) {
|
|
44871
|
-
if (
|
|
44872
|
-
|
|
46381
|
+
if (fs11.existsSync(file) && fs11.lstatSync(file).isDirectory()) {
|
|
46382
|
+
fs11.rmSync(file, { recursive: true, force: true });
|
|
44873
46383
|
}
|
|
44874
46384
|
const existing = loadInstances2(file);
|
|
44875
46385
|
if (existing.some((i) => i.name === instance.name)) {
|
|
@@ -44888,18 +46398,18 @@ function removeInstanceFromFile(file, name) {
|
|
|
44888
46398
|
}
|
|
44889
46399
|
function writeInstancesFile(file, instances) {
|
|
44890
46400
|
const content = dump2(instances);
|
|
44891
|
-
const fd =
|
|
46401
|
+
const fd = fs11.openSync(file, "w", 384);
|
|
44892
46402
|
try {
|
|
44893
46403
|
try {
|
|
44894
|
-
|
|
46404
|
+
fs11.fchmodSync(fd, 384);
|
|
44895
46405
|
} catch (err) {
|
|
44896
46406
|
const message = err instanceof Error ? err.message : String(err);
|
|
44897
46407
|
console.error(`Warning: could not restrict permissions on ${file}: ${message}`);
|
|
44898
46408
|
console.error(" It may be readable by other users on this machine.");
|
|
44899
46409
|
}
|
|
44900
|
-
|
|
46410
|
+
fs11.writeFileSync(fd, content, "utf8");
|
|
44901
46411
|
} finally {
|
|
44902
|
-
|
|
46412
|
+
fs11.closeSync(fd);
|
|
44903
46413
|
}
|
|
44904
46414
|
}
|
|
44905
46415
|
function extractSslmode2(connStr) {
|
|
@@ -45010,7 +46520,7 @@ async function verifyCollectorTls2(url) {
|
|
|
45010
46520
|
u.searchParams.delete(key);
|
|
45011
46521
|
const client = new Client2({
|
|
45012
46522
|
...buildClientConfig2(splitChannelBinding2(u.toString()).uri, { connectionTimeoutMillis: 1e4 }),
|
|
45013
|
-
ssl: { rejectUnauthorized: true, servername: u.hostname, ...ca ? { ca:
|
|
46523
|
+
ssl: { rejectUnauthorized: true, servername: u.hostname, ...ca ? { ca: fs11.readFileSync(ca, "utf8") } : {} }
|
|
45014
46524
|
});
|
|
45015
46525
|
try {
|
|
45016
46526
|
await client.connect();
|
|
@@ -45099,8 +46609,8 @@ var REQUIRED_ENV_KEYS = [
|
|
|
45099
46609
|
{ key: "VM_PPROF_AUTH_KEY", defaultValue: () => crypto2.randomBytes(32).toString("hex"), introducedIn: "0.17" }
|
|
45100
46610
|
];
|
|
45101
46611
|
function ensureRequiredEnvVars(projectDir) {
|
|
45102
|
-
const envFile =
|
|
45103
|
-
const existing =
|
|
46612
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
46613
|
+
const existing = fs12.existsSync(envFile) ? fs12.readFileSync(envFile, "utf8") : "";
|
|
45104
46614
|
const added = [];
|
|
45105
46615
|
const appendLines = [];
|
|
45106
46616
|
let content = existing;
|
|
@@ -45135,23 +46645,23 @@ function ensureRequiredEnvVars(projectDir) {
|
|
|
45135
46645
|
function writeEnvFile(envFile, content) {
|
|
45136
46646
|
let targetFile = envFile;
|
|
45137
46647
|
try {
|
|
45138
|
-
if (
|
|
45139
|
-
targetFile =
|
|
46648
|
+
if (fs12.existsSync(envFile))
|
|
46649
|
+
targetFile = fs12.realpathSync(envFile);
|
|
45140
46650
|
} catch {}
|
|
45141
46651
|
const tmpFile = `${targetFile}.tmp`;
|
|
45142
|
-
|
|
46652
|
+
fs12.writeFileSync(tmpFile, content, { encoding: "utf8", mode: 384 });
|
|
45143
46653
|
try {
|
|
45144
|
-
|
|
45145
|
-
if (
|
|
45146
|
-
const st =
|
|
45147
|
-
|
|
46654
|
+
fs12.chmodSync(tmpFile, 384);
|
|
46655
|
+
if (fs12.existsSync(targetFile)) {
|
|
46656
|
+
const st = fs12.statSync(targetFile);
|
|
46657
|
+
fs12.chownSync(tmpFile, st.uid, st.gid);
|
|
45148
46658
|
}
|
|
45149
46659
|
} catch {}
|
|
45150
46660
|
try {
|
|
45151
|
-
|
|
46661
|
+
fs12.renameSync(tmpFile, targetFile);
|
|
45152
46662
|
} catch (error) {
|
|
45153
46663
|
try {
|
|
45154
|
-
|
|
46664
|
+
fs12.unlinkSync(tmpFile);
|
|
45155
46665
|
} catch {}
|
|
45156
46666
|
throw error;
|
|
45157
46667
|
}
|
|
@@ -45208,7 +46718,7 @@ function statOwner(file) {
|
|
|
45208
46718
|
return;
|
|
45209
46719
|
let st;
|
|
45210
46720
|
try {
|
|
45211
|
-
st =
|
|
46721
|
+
st = fs12.statSync(file);
|
|
45212
46722
|
} catch {
|
|
45213
46723
|
return;
|
|
45214
46724
|
}
|
|
@@ -45352,7 +46862,7 @@ function expandHomePath(p) {
|
|
|
45352
46862
|
if (s === "~")
|
|
45353
46863
|
return os3.homedir();
|
|
45354
46864
|
if (s.startsWith("~/") || s.startsWith("~\\")) {
|
|
45355
|
-
return
|
|
46865
|
+
return path10.join(os3.homedir(), s.slice(2));
|
|
45356
46866
|
}
|
|
45357
46867
|
return s;
|
|
45358
46868
|
}
|
|
@@ -45401,10 +46911,10 @@ function prepareOutputDirectory(outputOpt) {
|
|
|
45401
46911
|
if (!outputOpt)
|
|
45402
46912
|
return;
|
|
45403
46913
|
const outputDir = expandHomePath(outputOpt);
|
|
45404
|
-
const outputPath =
|
|
45405
|
-
if (!
|
|
46914
|
+
const outputPath = path10.isAbsolute(outputDir) ? outputDir : path10.resolve(process.cwd(), outputDir);
|
|
46915
|
+
if (!fs12.existsSync(outputPath)) {
|
|
45406
46916
|
try {
|
|
45407
|
-
|
|
46917
|
+
fs12.mkdirSync(outputPath, { recursive: true });
|
|
45408
46918
|
} catch (e) {
|
|
45409
46919
|
const errAny = e;
|
|
45410
46920
|
const code = typeof errAny?.code === "string" ? errAny.code : "";
|
|
@@ -45495,8 +47005,8 @@ async function uploadCheckupReports(uploadCfg, reports, spinner, logUpload) {
|
|
|
45495
47005
|
}
|
|
45496
47006
|
function writeReportFiles(reports, outputPath) {
|
|
45497
47007
|
for (const [checkId, report] of Object.entries(reports)) {
|
|
45498
|
-
const filePath =
|
|
45499
|
-
|
|
47008
|
+
const filePath = path10.join(outputPath, `${checkId}.json`);
|
|
47009
|
+
fs12.writeFileSync(filePath, JSON.stringify(report, null, 2), "utf8");
|
|
45500
47010
|
const title = report.checkTitle || checkId;
|
|
45501
47011
|
console.log(`\u2713 ${checkId} ${title}: ${filePath}`);
|
|
45502
47012
|
}
|
|
@@ -45541,7 +47051,7 @@ function getDefaultMonitoringProjectDir() {
|
|
|
45541
47051
|
const override = process.env.PGAI_PROJECT_DIR;
|
|
45542
47052
|
if (override && override.trim())
|
|
45543
47053
|
return override.trim();
|
|
45544
|
-
return
|
|
47054
|
+
return path10.join(getConfigDir(), "monitoring");
|
|
45545
47055
|
}
|
|
45546
47056
|
async function downloadText(url) {
|
|
45547
47057
|
const controller = new AbortController;
|
|
@@ -45566,52 +47076,52 @@ function composeRefCandidates() {
|
|
|
45566
47076
|
}
|
|
45567
47077
|
async function ensureDefaultMonitoringProject() {
|
|
45568
47078
|
const projectDir = getDefaultMonitoringProjectDir();
|
|
45569
|
-
const composeFile =
|
|
45570
|
-
const instancesFile =
|
|
45571
|
-
if (!
|
|
45572
|
-
|
|
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 });
|
|
45573
47083
|
}
|
|
45574
|
-
if (!
|
|
47084
|
+
if (!fs12.existsSync(composeFile)) {
|
|
45575
47085
|
const refs = composeRefCandidates();
|
|
45576
47086
|
let lastErr;
|
|
45577
47087
|
for (const ref of refs) {
|
|
45578
47088
|
const url = `https://gitlab.com/postgres-ai/postgres_ai/-/raw/${encodeURIComponent(ref)}/docker-compose.yml`;
|
|
45579
47089
|
try {
|
|
45580
47090
|
const text = await downloadText(url);
|
|
45581
|
-
|
|
47091
|
+
fs12.writeFileSync(composeFile, text, { encoding: "utf8", mode: 384 });
|
|
45582
47092
|
break;
|
|
45583
47093
|
} catch (err) {
|
|
45584
47094
|
lastErr = err;
|
|
45585
47095
|
}
|
|
45586
47096
|
}
|
|
45587
|
-
if (!
|
|
47097
|
+
if (!fs12.existsSync(composeFile)) {
|
|
45588
47098
|
const msg = lastErr instanceof Error ? lastErr.message : String(lastErr);
|
|
45589
47099
|
throw new Error(`Failed to bootstrap docker-compose.yml: ${msg}`);
|
|
45590
47100
|
}
|
|
45591
47101
|
}
|
|
45592
47102
|
ensureHostMetricsDir(projectDir);
|
|
45593
|
-
if (
|
|
45594
|
-
|
|
47103
|
+
if (fs12.existsSync(instancesFile) && fs12.lstatSync(instancesFile).isDirectory()) {
|
|
47104
|
+
fs12.rmSync(instancesFile, { recursive: true, force: true });
|
|
45595
47105
|
}
|
|
45596
|
-
if (!
|
|
47106
|
+
if (!fs12.existsSync(instancesFile)) {
|
|
45597
47107
|
const header = `# PostgreSQL instances to monitor
|
|
45598
47108
|
` + `# Add your instances using: pgai mon targets add <connection-string> <name>
|
|
45599
47109
|
|
|
45600
47110
|
`;
|
|
45601
|
-
|
|
47111
|
+
fs12.writeFileSync(instancesFile, header, { encoding: "utf8", mode: 384 });
|
|
45602
47112
|
}
|
|
45603
|
-
const pgwatchConfig =
|
|
45604
|
-
if (!
|
|
45605
|
-
|
|
47113
|
+
const pgwatchConfig = path10.resolve(projectDir, ".pgwatch-config");
|
|
47114
|
+
if (!fs12.existsSync(pgwatchConfig)) {
|
|
47115
|
+
fs12.writeFileSync(pgwatchConfig, "", { encoding: "utf8", mode: 384 });
|
|
45606
47116
|
}
|
|
45607
|
-
const envFile =
|
|
45608
|
-
if (!
|
|
47117
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
47118
|
+
if (!fs12.existsSync(envFile)) {
|
|
45609
47119
|
const envText = `PGAI_TAG=${package_default.version}
|
|
45610
47120
|
# PGAI_REGISTRY=registry.gitlab.com/postgres-ai/postgres_ai
|
|
45611
47121
|
`;
|
|
45612
|
-
|
|
47122
|
+
fs12.writeFileSync(envFile, envText, { encoding: "utf8", mode: 384 });
|
|
45613
47123
|
}
|
|
45614
|
-
return { fs:
|
|
47124
|
+
return { fs: fs12, path: path10, projectDir, composeFile, instancesFile };
|
|
45615
47125
|
}
|
|
45616
47126
|
function sanitizeTagForBackup(tag) {
|
|
45617
47127
|
if (tag == null)
|
|
@@ -45620,10 +47130,10 @@ function sanitizeTagForBackup(tag) {
|
|
|
45620
47130
|
return /^[A-Za-z0-9._-]{1,64}$/.test(stripped) ? stripped : null;
|
|
45621
47131
|
}
|
|
45622
47132
|
function readDeployedTag(projectDir) {
|
|
45623
|
-
const envFile =
|
|
45624
|
-
if (!
|
|
47133
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
47134
|
+
if (!fs12.existsSync(envFile))
|
|
45625
47135
|
return null;
|
|
45626
|
-
const m =
|
|
47136
|
+
const m = fs12.readFileSync(envFile, "utf8").match(/^PGAI_TAG=(.+)$/m);
|
|
45627
47137
|
return m ? sanitizeTagForBackup(m[1]) : null;
|
|
45628
47138
|
}
|
|
45629
47139
|
function isValidComposeYaml(text) {
|
|
@@ -45660,12 +47170,12 @@ function composeContentEqual(a, b) {
|
|
|
45660
47170
|
return a.replace(/\s+$/, "") === b.replace(/\s+$/, "");
|
|
45661
47171
|
}
|
|
45662
47172
|
async function refreshBundledComposeIfStale(projectDir, oldTag) {
|
|
45663
|
-
if (
|
|
47173
|
+
if (fs12.existsSync(path10.resolve(projectDir, ".git")))
|
|
45664
47174
|
return false;
|
|
45665
47175
|
if (process.env.PGAI_SKIP_COMPOSE_REFRESH === "1")
|
|
45666
47176
|
return false;
|
|
45667
|
-
const composeFile =
|
|
45668
|
-
if (!
|
|
47177
|
+
const composeFile = path10.resolve(projectDir, "docker-compose.yml");
|
|
47178
|
+
if (!fs12.existsSync(composeFile))
|
|
45669
47179
|
return false;
|
|
45670
47180
|
const refs = composeRefCandidates();
|
|
45671
47181
|
const fetched = await fetchTargetCompose(refs);
|
|
@@ -45674,24 +47184,24 @@ async function refreshBundledComposeIfStale(projectDir, oldTag) {
|
|
|
45674
47184
|
console.error(" Keeping the existing compose. If dashboards are blank after upgrade, re-run this command once network is available.");
|
|
45675
47185
|
return false;
|
|
45676
47186
|
}
|
|
45677
|
-
const existing =
|
|
47187
|
+
const existing = fs12.readFileSync(composeFile, "utf8");
|
|
45678
47188
|
if (composeContentEqual(existing, fetched))
|
|
45679
47189
|
return false;
|
|
45680
47190
|
const deployedTag = sanitizeTagForBackup(oldTag ?? readDeployedTag(projectDir));
|
|
45681
47191
|
const tagPart = deployedTag ?? new Date().toISOString().replace(/[:.]/g, "-");
|
|
45682
47192
|
const oldHash = crypto2.createHash("sha256").update(existing).digest("hex").slice(0, 8);
|
|
45683
|
-
const backup =
|
|
47193
|
+
const backup = path10.resolve(projectDir, `docker-compose.yml.bak-${tagPart}-${oldHash}`);
|
|
45684
47194
|
let backupName = null;
|
|
45685
47195
|
try {
|
|
45686
|
-
|
|
45687
|
-
backupName =
|
|
47196
|
+
fs12.writeFileSync(backup, existing, { encoding: "utf8", mode: 384, flag: "wx" });
|
|
47197
|
+
backupName = path10.basename(backup);
|
|
45688
47198
|
} catch (err) {
|
|
45689
47199
|
const e = err;
|
|
45690
47200
|
if (e && e.code === "EEXIST") {
|
|
45691
|
-
backupName =
|
|
47201
|
+
backupName = path10.basename(backup);
|
|
45692
47202
|
}
|
|
45693
47203
|
}
|
|
45694
|
-
|
|
47204
|
+
fs12.writeFileSync(composeFile, fetched, { encoding: "utf8", mode: 384 });
|
|
45695
47205
|
const backupNote = backupName ? ` (backup: ${backupName})` : "";
|
|
45696
47206
|
console.log(`\u2713 Refreshed docker-compose.yml to ${package_default.version}${backupNote}`);
|
|
45697
47207
|
return true;
|
|
@@ -45964,7 +47474,6 @@ program2.command("prepare-db [conn]").description("prepare database for monitori
|
|
|
45964
47474
|
conn = process.env.PGAI_DB_URL || undefined;
|
|
45965
47475
|
}
|
|
45966
47476
|
const shouldPrintSql = !!opts.printSql;
|
|
45967
|
-
const redactPasswords = (sql) => redactPasswordsInSql2(sql);
|
|
45968
47477
|
const { provider, detected } = resolveProvider(opts.provider, conn ?? opts.dbUrl ?? opts.host ?? process.env.PGHOST);
|
|
45969
47478
|
const note = opts.json ? console.error : console.log;
|
|
45970
47479
|
if (detected)
|
|
@@ -45996,11 +47505,7 @@ program2.command("prepare-db [conn]").description("prepare database for monitori
|
|
|
45996
47505
|
console.log(`-- monitoring user: ${opts.monitoringUser}`);
|
|
45997
47506
|
console.log(`-- provider: ${provider}`);
|
|
45998
47507
|
console.log(`-- optional permissions: ${includeOptionalPermissions ? "enabled" : "skipped"}`);
|
|
45999
|
-
|
|
46000
|
-
console.log(`
|
|
46001
|
-
-- ${step.name}${step.optional ? " (optional)" : ""}`);
|
|
46002
|
-
console.log(redactPasswords(step.sql));
|
|
46003
|
-
}
|
|
47508
|
+
console.log(renderInitPlanSql2(plan));
|
|
46004
47509
|
console.log(`
|
|
46005
47510
|
--- end SQL plan ---
|
|
46006
47511
|
`);
|
|
@@ -46162,11 +47667,7 @@ program2.command("prepare-db [conn]").description("prepare database for monitori
|
|
|
46162
47667
|
if (shouldPrintSql) {
|
|
46163
47668
|
console.log(`
|
|
46164
47669
|
--- SQL plan ---`);
|
|
46165
|
-
|
|
46166
|
-
console.log(`
|
|
46167
|
-
-- ${step.name}${step.optional ? " (optional)" : ""}`);
|
|
46168
|
-
console.log(redactPasswords(step.sql));
|
|
46169
|
-
}
|
|
47670
|
+
console.log(renderInitPlanSql2(effectivePlan));
|
|
46170
47671
|
console.log(`
|
|
46171
47672
|
--- end SQL plan ---
|
|
46172
47673
|
`);
|
|
@@ -46257,24 +47758,18 @@ program2.command("prepare-db [conn]").description("prepare database for monitori
|
|
|
46257
47758
|
console.error(` Code: ${errAny.code}`);
|
|
46258
47759
|
}
|
|
46259
47760
|
if (typeof errAny.detail === "string" && errAny.detail) {
|
|
46260
|
-
console.error(` Detail: ${errAny.detail}`);
|
|
47761
|
+
console.error(` Detail: ${terminalDiagnosticLine(errAny.detail)}`);
|
|
46261
47762
|
}
|
|
46262
47763
|
if (typeof errAny.hint === "string" && errAny.hint) {
|
|
46263
|
-
console.error(` Hint: ${errAny.hint}`);
|
|
47764
|
+
console.error(` Hint: ${terminalDiagnosticLine(errAny.hint)}`);
|
|
46264
47765
|
}
|
|
46265
47766
|
if (typeof errAny.httpStatus === "number") {
|
|
46266
47767
|
console.error(` HTTP Status: ${errAny.httpStatus}`);
|
|
46267
47768
|
}
|
|
46268
47769
|
}
|
|
46269
47770
|
if (errAny && typeof errAny === "object" && typeof errAny.code === "string") {
|
|
46270
|
-
|
|
46271
|
-
|
|
46272
|
-
console.error(" Context: role creation/update requires CREATEROLE or superuser");
|
|
46273
|
-
} else if (failedStep === "03.permissions") {
|
|
46274
|
-
console.error(" Context: grants/view/search_path require sufficient GRANT/DDL privileges");
|
|
46275
|
-
}
|
|
46276
|
-
console.error(" Fix: ensure your Supabase access token has sufficient permissions");
|
|
46277
|
-
console.error(" Tip: run with --print-sql to review the exact SQL plan");
|
|
47771
|
+
for (const line of prepareDbPrivilegeAdvice(errAny.code, failedStep, message, true)) {
|
|
47772
|
+
console.error(` ${line}`);
|
|
46278
47773
|
}
|
|
46279
47774
|
if (errAny.code === "42P06" || message.includes("already exists") && failedStep === "03.permissions") {
|
|
46280
47775
|
console.error(" Hint: postgres_ai schema or objects already exist from a previous setup.");
|
|
@@ -46460,11 +47955,7 @@ program2.command("prepare-db [conn]").description("prepare database for monitori
|
|
|
46460
47955
|
if (shouldPrintSql) {
|
|
46461
47956
|
console.log(`
|
|
46462
47957
|
--- SQL plan ---`);
|
|
46463
|
-
|
|
46464
|
-
console.log(`
|
|
46465
|
-
-- ${step.name}${step.optional ? " (optional)" : ""}`);
|
|
46466
|
-
console.log(redactPasswords(step.sql));
|
|
46467
|
-
}
|
|
47958
|
+
console.log(renderInitPlanSql2(effectivePlan));
|
|
46468
47959
|
console.log(`
|
|
46469
47960
|
--- end SQL plan ---
|
|
46470
47961
|
`);
|
|
@@ -46549,24 +48040,17 @@ program2.command("prepare-db [conn]").description("prepare database for monitori
|
|
|
46549
48040
|
console.error(` Code: ${errAny.code}`);
|
|
46550
48041
|
}
|
|
46551
48042
|
if (typeof errAny.detail === "string" && errAny.detail) {
|
|
46552
|
-
console.error(` Detail: ${errAny.detail}`);
|
|
48043
|
+
console.error(` Detail: ${terminalDiagnosticLine(errAny.detail)}`);
|
|
46553
48044
|
}
|
|
46554
48045
|
if (typeof errAny.hint === "string" && errAny.hint) {
|
|
46555
|
-
console.error(` Hint: ${errAny.hint}`);
|
|
48046
|
+
console.error(` Hint: ${terminalDiagnosticLine(errAny.hint)}`);
|
|
46556
48047
|
}
|
|
46557
48048
|
}
|
|
46558
48049
|
for (const w of legacyWarnings)
|
|
46559
48050
|
console.error(`\u26A0 ${w}`);
|
|
46560
48051
|
if (errAny && typeof errAny === "object" && typeof errAny.code === "string") {
|
|
46561
|
-
|
|
46562
|
-
|
|
46563
|
-
console.error(" Context: role creation/update requires CREATEROLE or superuser");
|
|
46564
|
-
} else if (failedStep === "03.permissions") {
|
|
46565
|
-
console.error(" Context: grants/view/search_path require sufficient GRANT/DDL privileges");
|
|
46566
|
-
}
|
|
46567
|
-
console.error(" Fix: connect as a superuser (or a role with CREATEROLE and sufficient GRANT privileges)");
|
|
46568
|
-
console.error(" Fix: on managed Postgres, use the provider's admin/master user");
|
|
46569
|
-
console.error(" Tip: run with --print-sql to review the exact SQL plan");
|
|
48052
|
+
for (const line of prepareDbPrivilegeAdvice(errAny.code, failedStep, message, false)) {
|
|
48053
|
+
console.error(` ${line}`);
|
|
46570
48054
|
}
|
|
46571
48055
|
if (errAny.code === "42P06" || message.includes("already exists") && failedStep === "03.permissions") {
|
|
46572
48056
|
console.error(" Hint: postgres_ai schema or objects already exist from a previous setup.");
|
|
@@ -46663,11 +48147,14 @@ program2.command("unprepare-db [conn]").description("remove monitoring setup: dr
|
|
|
46663
48147
|
console.log(`-- monitoring user: ${opts.monitoringUser}`);
|
|
46664
48148
|
console.log(`-- provider: ${provider}`);
|
|
46665
48149
|
console.log(`-- drop role: ${dropRole}`);
|
|
48150
|
+
console.log("-- Run the complete plan in one session. For psql, use -v ON_ERROR_STOP=1.");
|
|
48151
|
+
console.log("begin;");
|
|
46666
48152
|
for (const step of plan.steps) {
|
|
46667
48153
|
console.log(`
|
|
46668
48154
|
-- ${step.name}`);
|
|
46669
48155
|
console.log(step.sql);
|
|
46670
48156
|
}
|
|
48157
|
+
console.log("commit;");
|
|
46671
48158
|
console.log(`
|
|
46672
48159
|
--- end SQL plan ---
|
|
46673
48160
|
`);
|
|
@@ -46733,11 +48220,14 @@ program2.command("unprepare-db [conn]").description("remove monitoring setup: dr
|
|
|
46733
48220
|
if (shouldPrintSql) {
|
|
46734
48221
|
console.log(`
|
|
46735
48222
|
--- SQL plan ---`);
|
|
48223
|
+
console.log("-- Run the complete plan in one session. For psql, use -v ON_ERROR_STOP=1.");
|
|
48224
|
+
console.log("begin;");
|
|
46736
48225
|
for (const step of plan.steps) {
|
|
46737
48226
|
console.log(`
|
|
46738
48227
|
-- ${step.name}`);
|
|
46739
48228
|
console.log(step.sql);
|
|
46740
48229
|
}
|
|
48230
|
+
console.log("commit;");
|
|
46741
48231
|
console.log(`
|
|
46742
48232
|
--- end SQL plan ---
|
|
46743
48233
|
`);
|
|
@@ -47136,12 +48626,12 @@ function resolvePaths() {
|
|
|
47136
48626
|
const startDir = process.cwd();
|
|
47137
48627
|
let currentDir = startDir;
|
|
47138
48628
|
while (true) {
|
|
47139
|
-
const composeFile =
|
|
47140
|
-
if (
|
|
47141
|
-
const instancesFile =
|
|
47142
|
-
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 };
|
|
47143
48633
|
}
|
|
47144
|
-
const parentDir =
|
|
48634
|
+
const parentDir = path10.dirname(currentDir);
|
|
47145
48635
|
if (parentDir === currentDir)
|
|
47146
48636
|
break;
|
|
47147
48637
|
currentDir = parentDir;
|
|
@@ -47160,7 +48650,7 @@ async function resolveOrInitPaths() {
|
|
|
47160
48650
|
}
|
|
47161
48651
|
function ensureHostMetricsDir(projectDir) {
|
|
47162
48652
|
try {
|
|
47163
|
-
|
|
48653
|
+
fs12.mkdirSync(path10.join(projectDir, "host-metrics"), { recursive: true, mode: 448 });
|
|
47164
48654
|
} catch {}
|
|
47165
48655
|
}
|
|
47166
48656
|
function isDockerRunning() {
|
|
@@ -47301,13 +48791,13 @@ function resolveAdoptedProject(reg) {
|
|
|
47301
48791
|
}
|
|
47302
48792
|
function updatePgwatchConfig(configPath, updates) {
|
|
47303
48793
|
let lines = [];
|
|
47304
|
-
if (
|
|
47305
|
-
const stats =
|
|
48794
|
+
if (fs12.existsSync(configPath)) {
|
|
48795
|
+
const stats = fs12.statSync(configPath);
|
|
47306
48796
|
if (!stats.isFile()) {
|
|
47307
48797
|
throw new Error(`${configPath} exists but is not a regular file; refusing to write through it`);
|
|
47308
48798
|
}
|
|
47309
48799
|
{
|
|
47310
|
-
const content =
|
|
48800
|
+
const content = fs12.readFileSync(configPath, "utf8").replace(/^\uFEFF+/, "");
|
|
47311
48801
|
lines = content.split(/\r?\n/).filter((l) => l.trim() !== "");
|
|
47312
48802
|
}
|
|
47313
48803
|
}
|
|
@@ -47320,18 +48810,18 @@ function updatePgwatchConfig(configPath, updates) {
|
|
|
47320
48810
|
lines.push(`${key}=${value}`);
|
|
47321
48811
|
}
|
|
47322
48812
|
}
|
|
47323
|
-
|
|
48813
|
+
fs12.writeFileSync(configPath, lines.join(`
|
|
47324
48814
|
`) + `
|
|
47325
48815
|
`, { encoding: "utf8", mode: 384 });
|
|
47326
48816
|
try {
|
|
47327
|
-
|
|
48817
|
+
fs12.chmodSync(configPath, 384);
|
|
47328
48818
|
} catch (err) {
|
|
47329
48819
|
const message = err instanceof Error ? err.message : String(err);
|
|
47330
48820
|
console.error(`Warning: could not restrict permissions on ${configPath}: ${message}`);
|
|
47331
48821
|
}
|
|
47332
48822
|
}
|
|
47333
48823
|
function applyApiKey(projectDir, apiKey, opts) {
|
|
47334
|
-
const configPath =
|
|
48824
|
+
const configPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
47335
48825
|
try {
|
|
47336
48826
|
updatePgwatchConfig(configPath, { api_key: apiKey, api_url: resolveBaseUrls2(opts).apiBaseUrl });
|
|
47337
48827
|
} catch (err) {
|
|
@@ -47366,10 +48856,10 @@ function parseDurationSeconds(v) {
|
|
|
47366
48856
|
function readInstanceIdFromProject() {
|
|
47367
48857
|
try {
|
|
47368
48858
|
const { projectDir } = resolvePaths();
|
|
47369
|
-
const configPath =
|
|
47370
|
-
if (!
|
|
48859
|
+
const configPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
48860
|
+
if (!fs12.existsSync(configPath))
|
|
47371
48861
|
return null;
|
|
47372
|
-
const line =
|
|
48862
|
+
const line = fs12.readFileSync(configPath, "utf8").replace(/^\uFEFF+/, "").split(/\r?\n/).find((l) => l.startsWith("instance_id="));
|
|
47373
48863
|
const value = line?.slice("instance_id=".length).trim();
|
|
47374
48864
|
return value && INSTANCE_ID_RE.test(value) ? value : null;
|
|
47375
48865
|
} catch {
|
|
@@ -47377,7 +48867,7 @@ function readInstanceIdFromProject() {
|
|
|
47377
48867
|
}
|
|
47378
48868
|
}
|
|
47379
48869
|
function applyProjectName(projectDir, project) {
|
|
47380
|
-
const configPath =
|
|
48870
|
+
const configPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
47381
48871
|
try {
|
|
47382
48872
|
updatePgwatchConfig(configPath, { project_name: project });
|
|
47383
48873
|
} catch (err) {
|
|
@@ -47393,7 +48883,7 @@ function persistInstanceId(projectDir, reg, requestedId, alsoWrite = {}) {
|
|
|
47393
48883
|
const updates = { ...alsoWrite, ...instanceId ? { instance_id: instanceId } : {} };
|
|
47394
48884
|
if (Object.keys(updates).length === 0)
|
|
47395
48885
|
return false;
|
|
47396
|
-
const configPath =
|
|
48886
|
+
const configPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
47397
48887
|
try {
|
|
47398
48888
|
updatePgwatchConfig(configPath, updates);
|
|
47399
48889
|
return instanceId != null;
|
|
@@ -47495,12 +48985,12 @@ async function runCompose(args, grafanaPassword, extraEnv) {
|
|
|
47495
48985
|
if (grafanaPassword) {
|
|
47496
48986
|
env.GF_SECURITY_ADMIN_PASSWORD = grafanaPassword;
|
|
47497
48987
|
} else {
|
|
47498
|
-
const cfgPath =
|
|
47499
|
-
if (
|
|
48988
|
+
const cfgPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
48989
|
+
if (fs12.existsSync(cfgPath)) {
|
|
47500
48990
|
try {
|
|
47501
|
-
const stats =
|
|
48991
|
+
const stats = fs12.statSync(cfgPath);
|
|
47502
48992
|
if (!stats.isDirectory()) {
|
|
47503
|
-
const content =
|
|
48993
|
+
const content = fs12.readFileSync(cfgPath, "utf8");
|
|
47504
48994
|
const match = content.match(/^grafana_password=([^\r\n]+)/m);
|
|
47505
48995
|
if (match) {
|
|
47506
48996
|
env.GF_SECURITY_ADMIN_PASSWORD = match[1].trim();
|
|
@@ -47513,10 +49003,10 @@ async function runCompose(args, grafanaPassword, extraEnv) {
|
|
|
47513
49003
|
}
|
|
47514
49004
|
}
|
|
47515
49005
|
}
|
|
47516
|
-
const envFilePath =
|
|
47517
|
-
if (
|
|
49006
|
+
const envFilePath = path10.resolve(projectDir, ".env");
|
|
49007
|
+
if (fs12.existsSync(envFilePath)) {
|
|
47518
49008
|
try {
|
|
47519
|
-
const envContent =
|
|
49009
|
+
const envContent = fs12.readFileSync(envFilePath, "utf8");
|
|
47520
49010
|
if (!env.VM_AUTH_USERNAME) {
|
|
47521
49011
|
const m = envContent.match(/^VM_AUTH_USERNAME=([^\r\n]+)/m);
|
|
47522
49012
|
if (m)
|
|
@@ -47560,7 +49050,7 @@ async function applySinkPrometheusConfig(projectDir) {
|
|
|
47560
49050
|
const { stdout } = await execFilePromise(composeCmd[0], [
|
|
47561
49051
|
...composeCmd.slice(1),
|
|
47562
49052
|
"-f",
|
|
47563
|
-
|
|
49053
|
+
path10.resolve(projectDir, "docker-compose.yml"),
|
|
47564
49054
|
"ps",
|
|
47565
49055
|
"-q",
|
|
47566
49056
|
"sink-prometheus"
|
|
@@ -47607,10 +49097,10 @@ function readComposeProfiles(knownProjectDir) {
|
|
|
47607
49097
|
projectDir = getDefaultMonitoringProjectDir();
|
|
47608
49098
|
}
|
|
47609
49099
|
}
|
|
47610
|
-
const envFile =
|
|
49100
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
47611
49101
|
let content;
|
|
47612
49102
|
try {
|
|
47613
|
-
content =
|
|
49103
|
+
content = fs12.readFileSync(envFile, "utf8");
|
|
47614
49104
|
} catch (err) {
|
|
47615
49105
|
if (err?.code !== "ENOENT") {
|
|
47616
49106
|
console.error(`\u26A0 Could not read ${envFile}: ${err instanceof Error ? err.message : String(err)}`);
|
|
@@ -47655,7 +49145,7 @@ async function composeServiceRunning(projectDir, service) {
|
|
|
47655
49145
|
if (!composeCmd)
|
|
47656
49146
|
return null;
|
|
47657
49147
|
try {
|
|
47658
|
-
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 });
|
|
47659
49149
|
return stdout.trim().length > 0;
|
|
47660
49150
|
} catch {
|
|
47661
49151
|
return null;
|
|
@@ -47812,10 +49302,11 @@ async function runConnect(urlArg, opts) {
|
|
|
47812
49302
|
const tty = !json && !!process.stderr.isTTY && !process.env.CI && process.env.TERM !== "dumb";
|
|
47813
49303
|
let createdId;
|
|
47814
49304
|
let stopping = false;
|
|
49305
|
+
let privateLink = false;
|
|
47815
49306
|
const steps = stepProgress({
|
|
47816
49307
|
json,
|
|
47817
49308
|
tty,
|
|
47818
|
-
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),
|
|
47819
49310
|
writeLine: (line) => console.error(line),
|
|
47820
49311
|
writeEvent: writeEvent2,
|
|
47821
49312
|
onId: (id) => {
|
|
@@ -47854,7 +49345,8 @@ ${reply}`);
|
|
|
47854
49345
|
process.on("SIGINT", onSigint);
|
|
47855
49346
|
let result;
|
|
47856
49347
|
try {
|
|
47857
|
-
|
|
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 }, {
|
|
47858
49350
|
...api,
|
|
47859
49351
|
selfHosted: async (monitoringUrl, env) => {
|
|
47860
49352
|
const scope = getActiveOrgScope2();
|
|
@@ -47895,6 +49387,19 @@ ${reply}`);
|
|
|
47895
49387
|
closeReadline();
|
|
47896
49388
|
}
|
|
47897
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
|
+
} } : {},
|
|
47898
49403
|
...interactive(opts.json) ? { allowIp: async (lines) => {
|
|
47899
49404
|
steps.view()?.stop();
|
|
47900
49405
|
console.error("");
|
|
@@ -47950,7 +49455,7 @@ Projects in the Supabase organization "${org}":`));
|
|
|
47950
49455
|
closeReadline();
|
|
47951
49456
|
}
|
|
47952
49457
|
} } : {},
|
|
47953
|
-
...interactive(opts.json) ? { openUrl: openInBrowser } : {},
|
|
49458
|
+
...interactive(opts.json) && canOpenBrowser() ? { openUrl: openInBrowser } : {},
|
|
47954
49459
|
progress: (e) => {
|
|
47955
49460
|
if (e.event === "provisioning")
|
|
47956
49461
|
requested = true;
|
|
@@ -47960,6 +49465,8 @@ Projects in the Supabase organization "${org}":`));
|
|
|
47960
49465
|
looking = false;
|
|
47961
49466
|
if (e.project)
|
|
47962
49467
|
shownName = terminalText2(e.project);
|
|
49468
|
+
if (e.location_reason === "privatelink")
|
|
49469
|
+
privateLink = true;
|
|
47963
49470
|
steps.progress(e);
|
|
47964
49471
|
},
|
|
47965
49472
|
aborted: () => stopping
|
|
@@ -48005,7 +49512,7 @@ Projects in the Supabase organization "${org}":`));
|
|
|
48005
49512
|
}
|
|
48006
49513
|
}
|
|
48007
49514
|
var mon = program2.command("mon").description("monitoring services management");
|
|
48008
|
-
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", [
|
|
48009
49516
|
"",
|
|
48010
49517
|
"Steps (each skipped when already done; safe to re-run): sign in, prepare the database",
|
|
48011
49518
|
"(an admin URL creates the postgres_ai_mon role; otherwise the SQL is printed), provision",
|
|
@@ -48031,7 +49538,21 @@ sharedDeployOptions2(withOrgOptions(mon.command("deploy")), 20, "instance name (
|
|
|
48031
49538
|
"at a terminal). Allow it on the database, then re-run with --ip-allowed (at a terminal: Enter).",
|
|
48032
49539
|
"The IP is held 7 days and stays the box's for its life. If the box still can't connect, it waits",
|
|
48033
49540
|
"up to 90 minutes, then is removed and the IP kept: allow it and re-run with --ip-allowed.",
|
|
48034
|
-
"
|
|
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.",
|
|
48035
49556
|
"",
|
|
48036
49557
|
"Billing is per Postgres cluster observed: another database in a cluster the organization already",
|
|
48037
49558
|
'monitors is included. Once the URL is checked, deploy shows the price (or "free (N of M free slots)"). A billed',
|
|
@@ -48064,7 +49585,8 @@ sharedDeployOptions2(withOrgOptions(mon.command("deploy")), 20, "instance name (
|
|
|
48064
49585
|
"for another database on a server PostgresAI already monitors for the org, with",
|
|
48065
49586
|
"sslmode=require or verify-* in the URL: PostgresAI fills in the password it keeps, over TLS only),",
|
|
48066
49587
|
"PGPASSWORD (the password for a URL without one),",
|
|
48067
|
-
"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).",
|
|
48068
49590
|
"",
|
|
48069
49591
|
"Examples:",
|
|
48070
49592
|
" pgai mon deploy 'postgresql://postgres:<password>@<host>.pg.clickhouse.cloud:5432/postgres?sslmode=require'",
|
|
@@ -48073,7 +49595,9 @@ sharedDeployOptions2(withOrgOptions(mon.command("deploy")), 20, "instance name (
|
|
|
48073
49595
|
" pgai mon deploy --supabase-project <name|project-id> --yes --json",
|
|
48074
49596
|
" pgai mon deploy 'postgresql://postgres@db.<project-ref>.supabase.co:5432/postgres'",
|
|
48075
49597
|
" pgai mon deploy '<url>' --cloud hetzner --location nbg1",
|
|
48076
|
-
" 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>"
|
|
48077
49601
|
].join(`
|
|
48078
49602
|
`)).action((urlArg, opts, cmd) => {
|
|
48079
49603
|
const supabase = providerArg2(opts.provider) === "supabase" || !opts.provider && !!opts.supabaseProject?.trim();
|
|
@@ -48105,7 +49629,10 @@ async function cloudInstance(opts, ref, live = false) {
|
|
|
48105
49629
|
}
|
|
48106
49630
|
async function linkedDatabases(opts) {
|
|
48107
49631
|
const api = cloudApi(opts.debug);
|
|
48108
|
-
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
|
+
}));
|
|
48109
49636
|
}
|
|
48110
49637
|
var monPollMs = () => {
|
|
48111
49638
|
const v = Number.parseInt(process.env.PGAI_DEPLOY_POLL_MS ?? "", 10);
|
|
@@ -48136,7 +49663,9 @@ withOrgOptions(monInstances.command("watch <id-or-name>")).alias("status").descr
|
|
|
48136
49663
|
} finally {
|
|
48137
49664
|
undetach();
|
|
48138
49665
|
}
|
|
48139
|
-
|
|
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);
|
|
48140
49669
|
} catch (err) {
|
|
48141
49670
|
failCloud(err, opts.json);
|
|
48142
49671
|
}
|
|
@@ -48147,6 +49676,7 @@ withOrgOptions(monInstances.command("delete <id-or-name>")).description("stop mo
|
|
|
48147
49676
|
if (!opts.yes && !(interactive(opts.json) && /^y/i.test(await question(`Stop monitoring ${row.name} and delete its monitoring box? (y/N): `)))) {
|
|
48148
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);
|
|
48149
49678
|
}
|
|
49679
|
+
const pl = await privateLinkOf(row, cloudApi(opts.debug).rdsStatus);
|
|
48150
49680
|
const billing = disconnectBilling(await cloudApi(opts.debug).disconnect(row.id));
|
|
48151
49681
|
emitConnect({
|
|
48152
49682
|
status: "deleting",
|
|
@@ -48154,7 +49684,7 @@ withOrgOptions(monInstances.command("delete <id-or-name>")).description("stop mo
|
|
|
48154
49684
|
name: row.name,
|
|
48155
49685
|
id: row.id,
|
|
48156
49686
|
...billing ? { billing } : {},
|
|
48157
|
-
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"
|
|
48158
49688
|
}, opts.json);
|
|
48159
49689
|
} catch (err) {
|
|
48160
49690
|
failCloud(err, opts.json);
|
|
@@ -48239,8 +49769,8 @@ mon.command("local-install").description("install local monitoring stack (genera
|
|
|
48239
49769
|
process.exitCode = 1;
|
|
48240
49770
|
return;
|
|
48241
49771
|
}
|
|
48242
|
-
const envFile =
|
|
48243
|
-
const existingEnv =
|
|
49772
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
49773
|
+
const existingEnv = fs12.existsSync(envFile) ? fs12.readFileSync(envFile, "utf8") : "";
|
|
48244
49774
|
const previousTagMatch = existingEnv.match(/^PGAI_TAG=(.+)$/m);
|
|
48245
49775
|
const previousTag = previousTagMatch ? previousTagMatch[1].trim() : null;
|
|
48246
49776
|
const imageTag = opts.tag || package_default.version;
|
|
@@ -48251,7 +49781,7 @@ mon.command("local-install").description("install local monitoring stack (genera
|
|
|
48251
49781
|
return;
|
|
48252
49782
|
}
|
|
48253
49783
|
const instanceJobs = instanceJobsRequest.enabled;
|
|
48254
|
-
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);
|
|
48255
49785
|
writeEnvFile(envFile, envContent);
|
|
48256
49786
|
if (preservedKeys.length > 0) {
|
|
48257
49787
|
console.log(`Preserved existing .env settings: ${preservedKeys.join(", ")}
|
|
@@ -48346,7 +49876,7 @@ Use demo mode without API key: postgres-ai mon local-install --demo`);
|
|
|
48346
49876
|
# Add your instances using: postgres-ai mon targets add
|
|
48347
49877
|
|
|
48348
49878
|
`;
|
|
48349
|
-
|
|
49879
|
+
fs12.writeFileSync(instancesPath, emptyInstancesContent, "utf8");
|
|
48350
49880
|
console.log(`Instances file: ${instancesPath}`);
|
|
48351
49881
|
console.log(`Project directory: ${projectDir}
|
|
48352
49882
|
`);
|
|
@@ -48437,17 +49967,17 @@ You can provide either:`);
|
|
|
48437
49967
|
}
|
|
48438
49968
|
} else {
|
|
48439
49969
|
console.log("Step 2: Demo mode enabled - using included demo PostgreSQL database");
|
|
48440
|
-
const currentDir =
|
|
49970
|
+
const currentDir = path10.dirname(fileURLToPath3(import.meta.url));
|
|
48441
49971
|
const demoCandidates = [
|
|
48442
|
-
|
|
48443
|
-
|
|
49972
|
+
path10.resolve(currentDir, "..", "..", "instances.demo.yml"),
|
|
49973
|
+
path10.resolve(currentDir, "..", "..", "..", "instances.demo.yml")
|
|
48444
49974
|
];
|
|
48445
|
-
const demoSrc = demoCandidates.find((p) =>
|
|
49975
|
+
const demoSrc = demoCandidates.find((p) => fs12.existsSync(p));
|
|
48446
49976
|
if (demoSrc) {
|
|
48447
|
-
if (
|
|
48448
|
-
|
|
49977
|
+
if (fs12.existsSync(instancesPath) && fs12.lstatSync(instancesPath).isDirectory()) {
|
|
49978
|
+
fs12.rmSync(instancesPath, { recursive: true, force: true });
|
|
48449
49979
|
}
|
|
48450
|
-
|
|
49980
|
+
fs12.copyFileSync(demoSrc, instancesPath);
|
|
48451
49981
|
console.log(`\u2713 Demo monitoring target configured
|
|
48452
49982
|
`);
|
|
48453
49983
|
} else {
|
|
@@ -48466,15 +49996,15 @@ Searched: ${demoCandidates.join(", ")}
|
|
|
48466
49996
|
console.log(`\u2713 Configuration updated
|
|
48467
49997
|
`);
|
|
48468
49998
|
console.log(opts.demo ? "Step 4: Configuring Grafana security..." : "Step 4: Configuring Grafana security...");
|
|
48469
|
-
const cfgPath =
|
|
49999
|
+
const cfgPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
48470
50000
|
let grafanaPassword = "";
|
|
48471
50001
|
let vmAuthUsername = "";
|
|
48472
50002
|
let vmAuthPassword = "";
|
|
48473
50003
|
try {
|
|
48474
|
-
if (
|
|
48475
|
-
const stats =
|
|
50004
|
+
if (fs12.existsSync(cfgPath)) {
|
|
50005
|
+
const stats = fs12.statSync(cfgPath);
|
|
48476
50006
|
if (!stats.isDirectory()) {
|
|
48477
|
-
const content =
|
|
50007
|
+
const content = fs12.readFileSync(cfgPath, "utf8");
|
|
48478
50008
|
const match = content.match(/^grafana_password=([^\r\n]+)/m);
|
|
48479
50009
|
if (match) {
|
|
48480
50010
|
grafanaPassword = match[1].trim();
|
|
@@ -48486,22 +50016,22 @@ Searched: ${demoCandidates.join(", ")}
|
|
|
48486
50016
|
const { stdout: password } = await execFilePromise("openssl", ["rand", "-base64", "12"]);
|
|
48487
50017
|
grafanaPassword = password.trim().replace(/\n/g, "");
|
|
48488
50018
|
let configContent = "";
|
|
48489
|
-
if (
|
|
48490
|
-
const stats =
|
|
50019
|
+
if (fs12.existsSync(cfgPath)) {
|
|
50020
|
+
const stats = fs12.statSync(cfgPath);
|
|
48491
50021
|
if (!stats.isDirectory()) {
|
|
48492
|
-
configContent =
|
|
50022
|
+
configContent = fs12.readFileSync(cfgPath, "utf8");
|
|
48493
50023
|
}
|
|
48494
50024
|
}
|
|
48495
50025
|
const lines = configContent.split(/\r?\n/).filter((l) => !/^grafana_password=/.test(l));
|
|
48496
50026
|
lines.push(`grafana_password=${grafanaPassword}`);
|
|
48497
|
-
|
|
50027
|
+
fs12.writeFileSync(cfgPath, lines.filter(Boolean).join(`
|
|
48498
50028
|
`) + `
|
|
48499
50029
|
`, {
|
|
48500
50030
|
encoding: "utf8",
|
|
48501
50031
|
mode: 384
|
|
48502
50032
|
});
|
|
48503
50033
|
try {
|
|
48504
|
-
|
|
50034
|
+
fs12.chmodSync(cfgPath, 384);
|
|
48505
50035
|
} catch {}
|
|
48506
50036
|
}
|
|
48507
50037
|
console.log(`\u2713 Grafana password configured
|
|
@@ -48513,9 +50043,9 @@ Searched: ${demoCandidates.join(", ")}
|
|
|
48513
50043
|
grafanaPassword = "demo";
|
|
48514
50044
|
}
|
|
48515
50045
|
try {
|
|
48516
|
-
const envFile =
|
|
48517
|
-
if (
|
|
48518
|
-
const envContent =
|
|
50046
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
50047
|
+
if (fs12.existsSync(envFile)) {
|
|
50048
|
+
const envContent = fs12.readFileSync(envFile, "utf8");
|
|
48519
50049
|
const userMatch = envContent.match(/^VM_AUTH_USERNAME=([^\r\n]+)/m);
|
|
48520
50050
|
const passMatch = envContent.match(/^VM_AUTH_PASSWORD=([^\r\n]+)/m);
|
|
48521
50051
|
if (userMatch)
|
|
@@ -48531,8 +50061,8 @@ Searched: ${demoCandidates.join(", ")}
|
|
|
48531
50061
|
vmAuthPassword = vmPass.trim().replace(/\n/g, "");
|
|
48532
50062
|
}
|
|
48533
50063
|
let envContent = "";
|
|
48534
|
-
if (
|
|
48535
|
-
envContent =
|
|
50064
|
+
if (fs12.existsSync(envFile)) {
|
|
50065
|
+
envContent = fs12.readFileSync(envFile, "utf8");
|
|
48536
50066
|
}
|
|
48537
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 === ""));
|
|
48538
50068
|
envLines.push(`VM_AUTH_USERNAME=${vmAuthUsername}`);
|
|
@@ -48828,11 +50358,11 @@ mon.command("config").description("show monitoring services configuration").acti
|
|
|
48828
50358
|
console.log(`Project Directory: ${projectDir}`);
|
|
48829
50359
|
console.log(`Docker Compose File: ${composeFile}`);
|
|
48830
50360
|
console.log(`Instances File: ${instancesFile}`);
|
|
48831
|
-
if (
|
|
50361
|
+
if (fs12.existsSync(instancesFile) && !fs12.lstatSync(instancesFile).isDirectory()) {
|
|
48832
50362
|
console.log(`
|
|
48833
50363
|
Instances configuration:
|
|
48834
50364
|
`);
|
|
48835
|
-
const text =
|
|
50365
|
+
const text = fs12.readFileSync(instancesFile, "utf8");
|
|
48836
50366
|
process.stdout.write(text);
|
|
48837
50367
|
if (!/\n$/.test(text))
|
|
48838
50368
|
console.log();
|
|
@@ -48886,8 +50416,8 @@ mon.command("update").description("update monitoring stack (migrate .env, pull i
|
|
|
48886
50416
|
console.log("\u2713 .env is up to date");
|
|
48887
50417
|
}
|
|
48888
50418
|
console.log();
|
|
48889
|
-
const gitDir =
|
|
48890
|
-
if (
|
|
50419
|
+
const gitDir = path10.resolve(projectDir, ".git");
|
|
50420
|
+
if (fs12.existsSync(gitDir)) {
|
|
48891
50421
|
console.log("Fetching latest changes...");
|
|
48892
50422
|
await execFilePromise("git", ["fetch", "origin"]);
|
|
48893
50423
|
const { stdout: branch } = await execFilePromise("git", ["rev-parse", "--abbrev-ref", "HEAD"]);
|
|
@@ -49134,10 +50664,10 @@ function parseEnvValue(content, key) {
|
|
|
49134
50664
|
return quoted ?? raw.replace(/ #.*$/, "").trimEnd();
|
|
49135
50665
|
}
|
|
49136
50666
|
function readEnvValue(projectDir, key) {
|
|
49137
|
-
const envFile =
|
|
49138
|
-
if (!
|
|
50667
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
50668
|
+
if (!fs12.existsSync(envFile))
|
|
49139
50669
|
return;
|
|
49140
|
-
return parseEnvValue(
|
|
50670
|
+
return parseEnvValue(fs12.readFileSync(envFile, "utf8"), key);
|
|
49141
50671
|
}
|
|
49142
50672
|
function supabaseHostMetricsOn(projectDir, env) {
|
|
49143
50673
|
return /^\s*true\s*$/i.test(env.PGAI_SUPABASE_HOST_METRICS ?? readEnvValue(projectDir, "PGAI_SUPABASE_HOST_METRICS") ?? "");
|
|
@@ -49145,8 +50675,8 @@ function supabaseHostMetricsOn(projectDir, env) {
|
|
|
49145
50675
|
var SUPABASE_TARGET_NAME_RE = /^[A-Za-z0-9_-]+$/;
|
|
49146
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.";
|
|
49147
50677
|
function setEnvValues(projectDir, values) {
|
|
49148
|
-
const envFile =
|
|
49149
|
-
let content =
|
|
50678
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
50679
|
+
let content = fs12.existsSync(envFile) ? fs12.readFileSync(envFile, "utf8") : "";
|
|
49150
50680
|
for (const [key, value] of Object.entries(values)) {
|
|
49151
50681
|
const line = `^[ \\t]*(?:export[ \\t]+)?${key}=.*(?:\\n|$)`;
|
|
49152
50682
|
if (value === null)
|
|
@@ -49201,9 +50731,9 @@ async function setUpHostMetrics(projectDir, instance, connStr, env, apply) {
|
|
|
49201
50731
|
throw new Error(SUPABASE_TARGET_NAME_ERROR);
|
|
49202
50732
|
const dir = hostMetricsDir2(projectDir);
|
|
49203
50733
|
const file = `supabase-${name}.yml`;
|
|
49204
|
-
const stale =
|
|
50734
|
+
const stale = fs12.readdirSync(dir).filter((f) => /^supabase-.*\.yml$/.test(f) && !(on && f === file));
|
|
49205
50735
|
for (const f of stale)
|
|
49206
|
-
|
|
50736
|
+
fs12.rmSync(path10.join(dir, f), { force: true });
|
|
49207
50737
|
if (on)
|
|
49208
50738
|
writeScrapeFile2(dir, file, renderSupabaseScrapeConfig({ projectRef: projectRef.toLowerCase(), cluster, nodeName }));
|
|
49209
50739
|
else if (stale.length === 0)
|
|
@@ -49233,8 +50763,8 @@ async function setUpHostMetrics(projectDir, instance, connStr, env, apply) {
|
|
|
49233
50763
|
}
|
|
49234
50764
|
}
|
|
49235
50765
|
async function syncVmalert(projectDir, apply, addOnly = false) {
|
|
49236
|
-
const dir =
|
|
49237
|
-
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));
|
|
49238
50768
|
const current = readEnvValue(projectDir, "COMPOSE_PROFILES") ?? "";
|
|
49239
50769
|
const exported = process.env.COMPOSE_PROFILES;
|
|
49240
50770
|
if (want && exported !== undefined && !instanceJobsProfileEnabled(exported, "host-metrics")) {
|
|
@@ -49320,7 +50850,7 @@ async function addTarget(file, projectDir, connStr, name, env, { apply = true, v
|
|
|
49320
50850
|
var targets = mon.command("targets").description("manage databases to monitor");
|
|
49321
50851
|
targets.command("list").description("list monitoring target databases").action(async () => {
|
|
49322
50852
|
const { instancesFile: instancesPath, projectDir } = await resolveOrInitPaths();
|
|
49323
|
-
if (!
|
|
50853
|
+
if (!fs12.existsSync(instancesPath) || fs12.lstatSync(instancesPath).isDirectory()) {
|
|
49324
50854
|
console.error(`instances.yml not found in ${projectDir}`);
|
|
49325
50855
|
process.exitCode = 1;
|
|
49326
50856
|
return;
|
|
@@ -49411,7 +50941,7 @@ Environment:
|
|
|
49411
50941
|
});
|
|
49412
50942
|
targets.command("remove <name>").description("remove monitoring target database").action(async (name) => {
|
|
49413
50943
|
const { instancesFile: file, projectDir } = await resolveOrInitPaths();
|
|
49414
|
-
if (!
|
|
50944
|
+
if (!fs12.existsSync(file) || fs12.lstatSync(file).isDirectory()) {
|
|
49415
50945
|
console.error("instances.yml not found");
|
|
49416
50946
|
process.exitCode = 1;
|
|
49417
50947
|
return;
|
|
@@ -49427,9 +50957,9 @@ targets.command("remove <name>").description("remove monitoring target database"
|
|
|
49427
50957
|
console.log(`Monitoring target '${name}' removed`);
|
|
49428
50958
|
if (removeHostMetrics(projectDir, name) && !await reloadHostMetrics(projectDir, `clickhouse-${name}`))
|
|
49429
50959
|
process.exitCode = 1;
|
|
49430
|
-
const supabaseFile =
|
|
49431
|
-
if (SUPABASE_TARGET_NAME_RE.test(name) &&
|
|
49432
|
-
|
|
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);
|
|
49433
50963
|
if (!await reloadHostMetrics(projectDir, SUPABASE_JOB))
|
|
49434
50964
|
process.exitCode = 1;
|
|
49435
50965
|
}
|
|
@@ -49459,7 +50989,7 @@ targets.command("remove <name>").description("remove monitoring target database"
|
|
|
49459
50989
|
});
|
|
49460
50990
|
targets.command("test <name>").description("test monitoring target database connectivity").action(async (name) => {
|
|
49461
50991
|
const { instancesFile: instancesPath } = await resolveOrInitPaths();
|
|
49462
|
-
if (!
|
|
50992
|
+
if (!fs12.existsSync(instancesPath) || fs12.lstatSync(instancesPath).isDirectory()) {
|
|
49463
50993
|
console.error("instances.yml not found");
|
|
49464
50994
|
process.exitCode = 1;
|
|
49465
50995
|
return;
|
|
@@ -49774,15 +51304,15 @@ To authenticate, run: pgai auth`);
|
|
|
49774
51304
|
});
|
|
49775
51305
|
auth.command("remove-key").description("remove API key").action(async () => {
|
|
49776
51306
|
const newConfigPath = getConfigPath();
|
|
49777
|
-
const hasNewConfig =
|
|
51307
|
+
const hasNewConfig = fs12.existsSync(newConfigPath);
|
|
49778
51308
|
let legacyPath;
|
|
49779
51309
|
try {
|
|
49780
51310
|
const { projectDir } = await resolveOrInitPaths();
|
|
49781
|
-
legacyPath =
|
|
51311
|
+
legacyPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
49782
51312
|
} catch {
|
|
49783
|
-
legacyPath =
|
|
51313
|
+
legacyPath = path10.resolve(process.cwd(), ".pgwatch-config");
|
|
49784
51314
|
}
|
|
49785
|
-
const hasLegacyConfig =
|
|
51315
|
+
const hasLegacyConfig = fs12.existsSync(legacyPath) && fs12.statSync(legacyPath).isFile();
|
|
49786
51316
|
if (!hasNewConfig && !hasLegacyConfig) {
|
|
49787
51317
|
console.log("No API key configured");
|
|
49788
51318
|
return;
|
|
@@ -49792,11 +51322,11 @@ auth.command("remove-key").description("remove API key").action(async () => {
|
|
|
49792
51322
|
}
|
|
49793
51323
|
if (hasLegacyConfig) {
|
|
49794
51324
|
try {
|
|
49795
|
-
const content =
|
|
51325
|
+
const content = fs12.readFileSync(legacyPath, "utf8");
|
|
49796
51326
|
const filtered = content.split(/\r?\n/).filter((l) => !/^api_key=/.test(l)).join(`
|
|
49797
51327
|
`).replace(/\n+$/g, `
|
|
49798
51328
|
`);
|
|
49799
|
-
|
|
51329
|
+
fs12.writeFileSync(legacyPath, filtered, "utf8");
|
|
49800
51330
|
} catch (err) {
|
|
49801
51331
|
console.warn(`Warning: Could not update legacy config: ${err instanceof Error ? err.message : String(err)}`);
|
|
49802
51332
|
}
|
|
@@ -49807,7 +51337,7 @@ To authenticate again, run: pgai auth`);
|
|
|
49807
51337
|
});
|
|
49808
51338
|
mon.command("generate-grafana-password").description("generate Grafana password for monitoring services").action(async () => {
|
|
49809
51339
|
const { projectDir } = await resolveOrInitPaths();
|
|
49810
|
-
const cfgPath =
|
|
51340
|
+
const cfgPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
49811
51341
|
try {
|
|
49812
51342
|
const { stdout: password } = await execFilePromise("openssl", ["rand", "-base64", "12"]);
|
|
49813
51343
|
const newPassword = password.trim().replace(/\n/g, "");
|
|
@@ -49817,24 +51347,24 @@ mon.command("generate-grafana-password").description("generate Grafana password
|
|
|
49817
51347
|
return;
|
|
49818
51348
|
}
|
|
49819
51349
|
let configContent = "";
|
|
49820
|
-
if (
|
|
49821
|
-
const stats =
|
|
51350
|
+
if (fs12.existsSync(cfgPath)) {
|
|
51351
|
+
const stats = fs12.statSync(cfgPath);
|
|
49822
51352
|
if (stats.isDirectory()) {
|
|
49823
51353
|
console.error(".pgwatch-config is a directory, expected a file. Skipping read.");
|
|
49824
51354
|
} else {
|
|
49825
|
-
configContent =
|
|
51355
|
+
configContent = fs12.readFileSync(cfgPath, "utf8");
|
|
49826
51356
|
}
|
|
49827
51357
|
}
|
|
49828
51358
|
const lines = configContent.split(/\r?\n/).filter((l) => !/^grafana_password=/.test(l));
|
|
49829
51359
|
lines.push(`grafana_password=${newPassword}`);
|
|
49830
|
-
|
|
51360
|
+
fs12.writeFileSync(cfgPath, lines.filter(Boolean).join(`
|
|
49831
51361
|
`) + `
|
|
49832
51362
|
`, {
|
|
49833
51363
|
encoding: "utf8",
|
|
49834
51364
|
mode: 384
|
|
49835
51365
|
});
|
|
49836
51366
|
try {
|
|
49837
|
-
|
|
51367
|
+
fs12.chmodSync(cfgPath, 384);
|
|
49838
51368
|
} catch {}
|
|
49839
51369
|
console.log("\u2713 New Grafana password generated and saved");
|
|
49840
51370
|
console.log(`
|
|
@@ -49855,19 +51385,19 @@ Note: This command requires 'openssl' to be installed`);
|
|
|
49855
51385
|
});
|
|
49856
51386
|
mon.command("show-grafana-credentials").description("show Grafana credentials for monitoring services").action(async () => {
|
|
49857
51387
|
const { projectDir } = await resolveOrInitPaths();
|
|
49858
|
-
const cfgPath =
|
|
49859
|
-
if (!
|
|
51388
|
+
const cfgPath = path10.resolve(projectDir, ".pgwatch-config");
|
|
51389
|
+
if (!fs12.existsSync(cfgPath)) {
|
|
49860
51390
|
console.error("Configuration file not found. Run 'postgres-ai mon local-install' first.");
|
|
49861
51391
|
process.exitCode = 1;
|
|
49862
51392
|
return;
|
|
49863
51393
|
}
|
|
49864
|
-
const stats =
|
|
51394
|
+
const stats = fs12.statSync(cfgPath);
|
|
49865
51395
|
if (stats.isDirectory()) {
|
|
49866
51396
|
console.error(".pgwatch-config is a directory, expected a file. Cannot read credentials.");
|
|
49867
51397
|
process.exitCode = 1;
|
|
49868
51398
|
return;
|
|
49869
51399
|
}
|
|
49870
|
-
const content =
|
|
51400
|
+
const content = fs12.readFileSync(cfgPath, "utf8");
|
|
49871
51401
|
const lines = content.split(/\r?\n/);
|
|
49872
51402
|
let password = "";
|
|
49873
51403
|
for (const line of lines) {
|
|
@@ -49887,9 +51417,9 @@ Grafana credentials:`);
|
|
|
49887
51417
|
console.log(" URL: http://localhost:3000");
|
|
49888
51418
|
console.log(" Username: monitor");
|
|
49889
51419
|
console.log(` Password: ${password}`);
|
|
49890
|
-
const envFile =
|
|
49891
|
-
if (
|
|
49892
|
-
const envContent =
|
|
51420
|
+
const envFile = path10.resolve(projectDir, ".env");
|
|
51421
|
+
if (fs12.existsSync(envFile)) {
|
|
51422
|
+
const envContent = fs12.readFileSync(envFile, "utf8");
|
|
49893
51423
|
const vmUser = envContent.match(/^VM_AUTH_USERNAME=([^\r\n]+)/m);
|
|
49894
51424
|
const vmPass = envContent.match(/^VM_AUTH_PASSWORD=([^\r\n]+)/m);
|
|
49895
51425
|
if (vmUser && vmPass) {
|
|
@@ -50774,13 +52304,13 @@ withOrgOptions(reports.command("data [reportId]")).description("get checkup repo
|
|
|
50774
52304
|
});
|
|
50775
52305
|
spinner.stop();
|
|
50776
52306
|
if (opts.output) {
|
|
50777
|
-
const dir =
|
|
50778
|
-
|
|
52307
|
+
const dir = path10.resolve(opts.output);
|
|
52308
|
+
fs12.mkdirSync(dir, { recursive: true });
|
|
50779
52309
|
for (const f of result) {
|
|
50780
|
-
const safeName =
|
|
50781
|
-
const filePath =
|
|
52310
|
+
const safeName = path10.basename(f.filename);
|
|
52311
|
+
const filePath = path10.join(dir, safeName);
|
|
50782
52312
|
const content = f.type === "json" ? JSON.stringify(tryParseJson(f.data), null, 2) : f.data;
|
|
50783
|
-
|
|
52313
|
+
fs12.writeFileSync(filePath, content, "utf-8");
|
|
50784
52314
|
console.log(filePath);
|
|
50785
52315
|
}
|
|
50786
52316
|
} else if (opts.json) {
|
|
@@ -51278,29 +52808,29 @@ mcp.command("install [client]").description("install MCP server configuration fo
|
|
|
51278
52808
|
let configDir;
|
|
51279
52809
|
switch (client) {
|
|
51280
52810
|
case "cursor":
|
|
51281
|
-
configPath =
|
|
51282
|
-
configDir =
|
|
52811
|
+
configPath = path10.join(homeDir, ".cursor", "mcp.json");
|
|
52812
|
+
configDir = path10.dirname(configPath);
|
|
51283
52813
|
break;
|
|
51284
52814
|
case "windsurf":
|
|
51285
|
-
configPath =
|
|
51286
|
-
configDir =
|
|
52815
|
+
configPath = path10.join(homeDir, ".windsurf", "mcp.json");
|
|
52816
|
+
configDir = path10.dirname(configPath);
|
|
51287
52817
|
break;
|
|
51288
52818
|
case "codex":
|
|
51289
|
-
configPath =
|
|
51290
|
-
configDir =
|
|
52819
|
+
configPath = path10.join(homeDir, ".codex", "mcp.json");
|
|
52820
|
+
configDir = path10.dirname(configPath);
|
|
51291
52821
|
break;
|
|
51292
52822
|
default:
|
|
51293
52823
|
console.error(`Configuration not implemented for: ${client}`);
|
|
51294
52824
|
process.exitCode = 1;
|
|
51295
52825
|
return;
|
|
51296
52826
|
}
|
|
51297
|
-
if (!
|
|
51298
|
-
|
|
52827
|
+
if (!fs12.existsSync(configDir)) {
|
|
52828
|
+
fs12.mkdirSync(configDir, { recursive: true });
|
|
51299
52829
|
}
|
|
51300
52830
|
let config = { mcpServers: {} };
|
|
51301
|
-
if (
|
|
52831
|
+
if (fs12.existsSync(configPath)) {
|
|
51302
52832
|
try {
|
|
51303
|
-
const content =
|
|
52833
|
+
const content = fs12.readFileSync(configPath, "utf8");
|
|
51304
52834
|
config = JSON.parse(content);
|
|
51305
52835
|
if (!config.mcpServers) {
|
|
51306
52836
|
config.mcpServers = {};
|
|
@@ -51313,7 +52843,7 @@ mcp.command("install [client]").description("install MCP server configuration fo
|
|
|
51313
52843
|
command: pgaiPath,
|
|
51314
52844
|
args: ["mcp", "start"]
|
|
51315
52845
|
};
|
|
51316
|
-
|
|
52846
|
+
fs12.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf8");
|
|
51317
52847
|
console.log(`\u2713 PostgresAI MCP server configured for ${client}`);
|
|
51318
52848
|
console.log(` Config file: ${configPath}`);
|
|
51319
52849
|
console.log("");
|