postgresai 0.18.0-dev.14 → 0.18.0-dev.15

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 CHANGED
@@ -216,12 +216,17 @@ and the result's `provider` remains `self-managed` for compatibility. It works w
216
216
  PostgreSQL endpoint where the supplied user can create the monitoring role and grant privileges.
217
217
  Use a direct endpoint, not a pooler.
218
218
 
219
- The endpoint must be reachable from this machine, which prepares the database, and from
220
- PostgresAI. Before provisioning, PostgresAI tests the connection from its platform, whose
221
- address is not published, so the port must accept connections from the internet at that point.
222
- After provisioning, the monitoring server connects from its own IP address, shown on its page in
223
- the Console (Monitoring, then the instance). A database that must stay behind an IP allowlist
224
- needs AWS PrivateLink (RDS) or self-hosted monitoring (`--self-hosted`).
219
+ The endpoint must be reachable from this machine, which prepares the database. The monitoring
220
+ box connects from its own static IP. When PostgresAI can't reach the database the way an IP
221
+ allowlist refuses (a timeout, a refused connection, no `pg_hba.conf` entry), deploy reserves the
222
+ box's IP before the box exists and stops at it: at a terminal it shows the IP and waits (Enter
223
+ continues, `s` stops); elsewhere it exits 3 with `"step":"allow_ip"` and `ip`, `cidr`, `host`,
224
+ `port`, `reserved_until`. Allow the IP on the database, then run `pgai mon deploy <url> --yes
225
+ --ip-allowed` to create the box with it. `--ip-first` asks for the IP first even when PostgresAI
226
+ can reach the database. A re-run never reserves a second IP; the IP is held 7 days and stays the
227
+ box's for its life. If the box still can't connect, it waits for up to 90 minutes, then is removed
228
+ and its IP kept: allow it, and run with `--ip-allowed` again. RDS can also use AWS PrivateLink,
229
+ and any database self-hosted monitoring (`--self-hosted`).
225
230
 
226
231
  Without `--provider`, RDS hostnames still go to their console integration, and Supabase hostnames to the Supabase consent flow (`pgai mon deploy` for Supabase).
227
232
 
@@ -13533,7 +13533,7 @@ var {
13533
13533
  // package.json
13534
13534
  var package_default = {
13535
13535
  name: "postgresai",
13536
- version: "0.18.0-dev.14",
13536
+ version: "0.18.0-dev.15",
13537
13537
  description: "postgres_ai CLI",
13538
13538
  license: "Apache-2.0",
13539
13539
  private: false,
@@ -16893,7 +16893,7 @@ var Result = import_lib.default.Result;
16893
16893
  var TypeOverrides = import_lib.default.TypeOverrides;
16894
16894
  var defaults = import_lib.default.defaults;
16895
16895
  // package.json
16896
- var version = "0.18.0-dev.14";
16896
+ var version = "0.18.0-dev.15";
16897
16897
  var package_default2 = {
16898
16898
  name: "postgresai",
16899
16899
  version,
@@ -22295,7 +22295,7 @@ async function buildInitPlan(params) {
22295
22295
  if not exists (select 1 from pg_catalog.pg_roles where rolname = ${qRoleNameLit}) then
22296
22296
  begin
22297
22297
  create user ${qRole} with password ${qPwVerifier};
22298
- exception when duplicate_object then
22298
+ exception when duplicate_object or unique_violation then
22299
22299
  null;
22300
22300
  end;
22301
22301
  end if;${params.keepExistingPassword ? "" : `
@@ -22348,9 +22348,21 @@ async function applyInitPlan(params) {
22348
22348
  throw e;
22349
22349
  }
22350
22350
  };
22351
+ const concurrent = (e) => /tuple concurrently (updated|deleted)/.test(e instanceof Error ? e.message : String(e));
22352
+ const executeRetried = async (step) => {
22353
+ for (let attempt = 1;; attempt++) {
22354
+ try {
22355
+ return await executeStep(step);
22356
+ } catch (e) {
22357
+ if (attempt >= 3 || !concurrent(e))
22358
+ throw e;
22359
+ await new Promise((r) => setTimeout(r, 200 * attempt));
22360
+ }
22361
+ }
22362
+ };
22351
22363
  for (const step of params.plan.steps.filter((s) => !s.optional)) {
22352
22364
  try {
22353
- await executeStep(step);
22365
+ await executeRetried(step);
22354
22366
  applied.push(step.name);
22355
22367
  } catch (e) {
22356
22368
  const msg = e instanceof Error ? e.message : String(e);
@@ -22570,6 +22582,8 @@ function monitoringStatus(raw) {
22570
22582
  return "inactive";
22571
22583
  if (/fail|error/.test(raw ?? ""))
22572
22584
  return "failed";
22585
+ if (raw === "ip_reserved")
22586
+ return "action_required";
22573
22587
  return "in_progress";
22574
22588
  }
22575
22589
  function dblabStatus(deployStatus) {
@@ -22996,6 +23010,7 @@ function connectStatus(row, provider = row.provider, fresh = false) {
22996
23010
  return {
22997
23011
  ...base,
22998
23012
  status,
23013
+ ...row.egress_ip ? { egress_ip: row.egress_ip } : {},
22999
23014
  ...fresh ? { first_checkup_eta: new Date(Date.now() + FIRST_CHECKUP_DELAY_MS).toISOString() } : {},
23000
23015
  next: row.health_url || row.dashboard_url ? `Open ${row.health_url || row.dashboard_url}` : `pgai mon instances watch ${row.id}`
23001
23016
  };
@@ -23004,6 +23019,8 @@ function connectStatus(row, provider = row.provider, fresh = false) {
23004
23019
  return { ...base, status, next: "none" };
23005
23020
  if (status === "failed")
23006
23021
  return { ...base, status, next: `pgai mon instances delete ${row.id} --yes, then pgai mon deploy again` };
23022
+ if (row.status === "ip_reserved" && row.egress_ip)
23023
+ return { ...allowIpResult(row, provider, row.name, { waitMs: 0 }, ""), ...base, status: "action_required", step: "allow_ip" };
23007
23024
  return { ...base, status, next: `pgai mon instances watch ${row.id}` };
23008
23025
  }
23009
23026
  var ESCAPES = /(?:\x1b\[|\x9b)[0-?]*[ -\/]*[@-~]|(?:\x1b\]|\x9d)[^\x07\x1b\x9c]*(?:\x07|\x1b\\|\x9c)?|\x1b[ -~]?/g;
@@ -23017,6 +23034,12 @@ function boxWords(state) {
23017
23034
  return "starting";
23018
23035
  if (state === "registered")
23019
23036
  return "installing monitoring";
23037
+ if (state === "waiting_for_access")
23038
+ return "waiting for database access";
23039
+ if (state === "ip_reserving")
23040
+ return "reserving its IP";
23041
+ if (state === "ip_reserved")
23042
+ return "waiting for its IP to be allowed";
23020
23043
  return monitoringStatus(state).replace("_", " ");
23021
23044
  }
23022
23045
  async function billingFor(q, name, opts, deps, show, ask) {
@@ -23066,6 +23089,9 @@ async function connect(url, opts, deps) {
23066
23089
  if (project !== undefined && provider !== "supabase") {
23067
23090
  return { status: "action_required", provider, name: url ? databaseName2(url) : "", next: "--supabase-project applies to Supabase only: leave it out, or use --provider supabase" };
23068
23091
  }
23092
+ if ((opts.ipFirst || opts.ipAllowed) && (opts.selfHosted || provider === "supabase")) {
23093
+ return { status: "action_required", provider, name: url ? databaseName2(url) : "", next: `${opts.agent ? "ip_first / ip_allowed apply" : "--ip-first and --ip-allowed apply"} to PostgresAI Cloud monitoring of a database URL only` };
23094
+ }
23069
23095
  if (!url) {
23070
23096
  if (provider !== "supabase")
23071
23097
  return { status: "action_required", provider, name: "", next: "Pass the database URL: pgai mon deploy postgresql://user:password@host:5432/dbname (Supabase: --provider supabase)" };
@@ -23124,12 +23150,24 @@ async function connect(url, opts, deps) {
23124
23150
  return live.find(byCluster) ?? live.find(byName);
23125
23151
  };
23126
23152
  const rows = opts.selfHosted ? [] : await deps.list();
23127
- const known = new Set(rows.map((d) => d.id));
23153
+ const known = new Map(rows.map((d) => [d.id, d.status]));
23128
23154
  const findMade = (list) => {
23129
- const fresh = list.filter((d) => !known.has(d.id));
23155
+ const fresh = list.filter((d) => !known.has(d.id) || reservedStatus(known.get(d.id)) && !reservedStatus(d.status));
23130
23156
  return fresh.find(byCluster) ?? fresh.find(byName);
23131
23157
  };
23132
23158
  let row = findDatabase(rows);
23159
+ let continuing = false;
23160
+ let ownId;
23161
+ if (row && reservedStatus(row.status)) {
23162
+ if (!opts.ipAllowed) {
23163
+ const asked = await allowIpStep(row, { provider, name, url, opts, deps, progress });
23164
+ if (asked !== "continue")
23165
+ return asked;
23166
+ }
23167
+ continuing = true;
23168
+ ownId = row.id;
23169
+ row = undefined;
23170
+ }
23133
23171
  const fresh = !row;
23134
23172
  let note = "";
23135
23173
  let billing = {};
@@ -23170,7 +23208,7 @@ async function connect(url, opts, deps) {
23170
23208
  }
23171
23209
  }
23172
23210
  if (row && opts.resetPassword) {
23173
- return { status: "action_required", provider, name, id: row.id, next: `${name} is already monitored, and its monitoring uses the current password: ${disconnectEach([deleteRef(row, rows)])} first` };
23211
+ return { status: "action_required", provider, name, id: row.id, next: `${row.name} is already monitored, and its monitoring uses the current password. Delete it, then run this deploy again with --reset-password: ${disconnectEach([deleteRef(row, rows)])}` };
23174
23212
  }
23175
23213
  if (row && key) {
23176
23214
  const found = await findOrg();
@@ -23199,7 +23237,7 @@ async function connect(url, opts, deps) {
23199
23237
  const unread = (d) => !d.cluster;
23200
23238
  const onServer = (d) => !!d.cluster && serverOf(d.cluster) === cluster;
23201
23239
  const usesRole = (d) => !["deleting", "deleted", "inactive"].includes(stateOf(d.status)) && !(d.status === "failed" && !d.registered_at);
23202
- const othersOnServer = (list) => list.filter((d) => usesRole(d) && (unread(d) || onServer(d)));
23240
+ const othersOnServer = (list) => list.filter((d) => d.id !== ownId && usesRole(d) && (unread(d) || onServer(d)));
23203
23241
  const storedPassword = !monitoringUser && rows.some((d) => d.monitoring_password_stored && !/delet/.test(d.status ?? "") && onServer(d)) || undefined;
23204
23242
  const cutOff = (others, list) => {
23205
23243
  const label = (d) => deleteRef(d, list) === d.id ? `${d.name} (id ${d.id})` : d.name;
@@ -23285,9 +23323,9 @@ async function connect(url, opts, deps) {
23285
23323
  try {
23286
23324
  if (lockId) {
23287
23325
  const now = await deps.list();
23288
- const same = findDatabase(now);
23326
+ const same = findDatabase(now.filter((d) => d.id !== ownId));
23289
23327
  if (same)
23290
- return { status: "action_required", provider, name, id: same.id, ...billing, next: `${name} is already monitored, and its monitoring uses the current password: ${disconnectEach([deleteRef(same, now)])} first` };
23328
+ return { status: "action_required", provider, name, id: same.id, ...billing, next: `${same.name} is already monitored, and its monitoring uses the current password. Delete it, then run this deploy again with --reset-password: ${disconnectEach([deleteRef(same, now)])}` };
23291
23329
  const others = othersOnServer(now);
23292
23330
  if (others.length)
23293
23331
  return { ...cutOff(others, now), ...billing };
@@ -23301,6 +23339,16 @@ async function connect(url, opts, deps) {
23301
23339
  throw new CancelledError;
23302
23340
  }
23303
23341
  if ("next" in prepared) {
23342
+ if ((continuing || opts.ipAllowed) && /created by someone else meanwhile/.test(prepared.next)) {
23343
+ for (let tries = 0;tries < 10; tries++) {
23344
+ const box = findDatabase(await deps.list());
23345
+ if (!box)
23346
+ break;
23347
+ if (!reservedStatus(box.status))
23348
+ return follow(box, { provider, name, fresh, billing, billingUrl, undoRole, note, place, url }, opts, deps, progress);
23349
+ await deps.sleep(2000);
23350
+ }
23351
+ }
23304
23352
  const { resettable, ...refusal } = prepared;
23305
23353
  return { status: "action_required", provider, name, ...billing, ...refusal };
23306
23354
  }
@@ -23313,8 +23361,12 @@ async function connect(url, opts, deps) {
23313
23361
  return { status: "ready", provider, name, dashboard_url: "http://localhost:3000", host_metrics: !!ch, next: `pgai mon health${note}` };
23314
23362
  }
23315
23363
  progress("provisioning", `Provisioning monitoring for ${name}`);
23316
- created = await deps.create({
23317
- db_url: prepared.monitoringUrl,
23364
+ const monitoringUrl = prepared.monitoringUrl;
23365
+ const request = (ipAllowed) => deps.create({
23366
+ db_url: monitoringUrl,
23367
+ allow_ip_fallback: true,
23368
+ ...opts.ipFirst ? { ip_first: true } : {},
23369
+ ...ipAllowed ? { ip_allowed: true } : {},
23318
23370
  ...provider === "clickhouse" ? { provider } : {},
23319
23371
  ...key && ch ? { clickhouse_org_id: ch.orgId, clickhouse_key_id: key.keyId, clickhouse_key_secret: key.keySecret } : {},
23320
23372
  ...opts.coupon ? { promo_code: opts.coupon } : {},
@@ -23322,7 +23374,38 @@ async function connect(url, opts, deps) {
23322
23374
  ...opts.name ? { project_name: opts.name } : {},
23323
23375
  ...place ? { cloud: place.cloud, server_location: place.location } : opts.location ? { server_location: opts.location } : {},
23324
23376
  ...vcpus ? { vcpus } : {}
23377
+ });
23378
+ const refused = async (c) => {
23379
+ const row = await waitForIp(c.id, deps) ?? { id: c.id, name: c.name, status: c.status };
23380
+ const why = [c.error, c.billing_warning].filter(Boolean).join(" ");
23381
+ return { ...c, status: "allow_ip", result: { ...allowIpResult(row, provider, name, opts, url), next: `${why} Then: ${continueCommand(opts, url)}` } };
23382
+ };
23383
+ const continued = async () => {
23384
+ const c = await request(true);
23385
+ return reservedStatus(c.status) && c.error ? refused(c) : c;
23386
+ };
23387
+ created = await request(continuing || !!opts.ipAllowed).then(async (c) => {
23388
+ if (reservedStatus(c.status))
23389
+ known.set(c.id, c.status);
23390
+ if (reservedStatus(c.status) && c.error)
23391
+ return refused(c);
23392
+ if (!reservedStatus(c.status))
23393
+ return c;
23394
+ const reservation = await waitForIp(c.id, deps);
23395
+ if (!reservation || !reservedStatus(reservation.status))
23396
+ return { ...c, status: reservation?.status ?? "failed", error: reservation ? undefined : "The IP reservation did not report back in time; see pgai mon instances list." };
23397
+ if (continuing && c.status === "ip_reserving" && reservation.status === "ip_reserved")
23398
+ return continued();
23399
+ const asked = await allowIpStep(reservation, { provider, name, url, opts, deps, progress });
23400
+ if (asked !== "continue")
23401
+ return { ...c, status: "allow_ip", result: asked };
23402
+ return continued();
23325
23403
  }).catch(async (err) => {
23404
+ if ((continuing || opts.ipAllowed) && err instanceof HttpStatusError && err.status === 409 && /already monitored/.test(err.message)) {
23405
+ const box = findDatabase(await deps.list());
23406
+ if (box && !reservedStatus(box.status))
23407
+ return { id: box.id, name: box.name, status: box.status ?? "launch_requested" };
23408
+ }
23326
23409
  if (err instanceof HttpStatusError && (err.status < 500 || err.sqlstate))
23327
23410
  await undo(prepared);
23328
23411
  else
@@ -23363,6 +23446,12 @@ async function connect(url, opts, deps) {
23363
23446
  const gone = `PostgresAI no longer keeps the password of ${DEFAULT_MONITORING_USER} for this server`;
23364
23447
  return { status: "action_required", provider, name, ...billing, next: opts.agent ? `${gone}: pass its URL as database_url, or run pgai mon deploy <admin-url> --reset-password in a terminal` : `${gone}: re-run with --reset-password (anything else that logs in as ${DEFAULT_MONITORING_USER} then needs the new one), or set PGAI_MON_PASSWORD to its password` };
23365
23448
  }
23449
+ if (created.status === "allow_ip" && created.result) {
23450
+ const now = findDatabase(await deps.list().catch(() => []));
23451
+ if (!now || reservedStatus(now.status))
23452
+ await undo(prepared);
23453
+ return { ...created.result, ...billing };
23454
+ }
23366
23455
  if (created.status === "failed") {
23367
23456
  await undo(prepared);
23368
23457
  return { status: "failed", provider, name, id: created.id, next: `${created.error} Re-run pgai mon deploy later.` };
@@ -23377,7 +23466,70 @@ async function connect(url, opts, deps) {
23377
23466
  const made = prepared;
23378
23467
  undoRole = () => undo(made);
23379
23468
  }
23380
- return follow(row, { provider, name, fresh, billing, billingUrl, undoRole, note, place }, opts, deps, progress);
23469
+ return follow(row, { provider, name, fresh, billing, billingUrl, undoRole, note, place, url }, opts, deps, progress);
23470
+ }
23471
+ var reservedStatus = (status) => status === "ip_reserving" || status === "ip_reserved";
23472
+ var IP_POLL_MS = 3000;
23473
+ var IP_WAIT_MS = 120000;
23474
+ async function waitForIp(id, deps) {
23475
+ for (const deadline = deps.now() + IP_WAIT_MS;; ) {
23476
+ const row = (await deps.list().catch(() => [])).find((d) => d.id === id);
23477
+ if (row && row.status !== "ip_reserving")
23478
+ return row;
23479
+ if (deps.now() >= deadline)
23480
+ return;
23481
+ await deps.sleep(IP_POLL_MS);
23482
+ }
23483
+ }
23484
+ var continueCommand = (opts, url) => opts.agent ? "call deploy_monitoring again with ip_allowed: true" : `pgai mon deploy ${url ? quoteArg(maskConnectionString(url)) : "<database URL>"} --yes --ip-allowed`;
23485
+ var quoteArg = (s) => /^[A-Za-z0-9_@%+=:,./-]+$/.test(s) ? s : `'${s.replace(/'/g, "'\\''")}'`;
23486
+ var heldUntil = (iso) => iso ? iso.replace(/T(\d\d:\d\d).*$/, " $1 UTC") : "";
23487
+ function allowIpResult(row, provider, name, opts, url, stopped = false) {
23488
+ const [host, port] = row.cluster ? [row.cluster.replace(/:\d+$/, ""), Number(row.cluster.match(/:(\d+)$/)?.[1] ?? 5432)] : [undefined, undefined];
23489
+ const ip = row.egress_ip ?? undefined;
23490
+ const cidr = ip ? `${ip}/32` : undefined;
23491
+ const allow = `Allow inbound TCP ${port ?? 5432} from ${cidr} on ${host ?? "the database"}`;
23492
+ return {
23493
+ status: "action_required",
23494
+ step: "allow_ip",
23495
+ provider,
23496
+ name,
23497
+ id: row.id,
23498
+ ...row.cloud ? { cloud: row.cloud, location: row.location ?? undefined } : {},
23499
+ ip,
23500
+ cidr,
23501
+ host,
23502
+ port,
23503
+ ...row.reserved_until ? { reserved_until: row.reserved_until } : {},
23504
+ ...row.last_error ? { last_error: row.last_error } : {},
23505
+ ...row.precheck_error ? { precheck_error: row.precheck_error } : {},
23506
+ egress_ip: ip,
23507
+ next: stopped ? `Stopped. ${ip} stays reserved until ${heldUntil(row.reserved_until)}. ${allow}, then: ${continueCommand(opts, url)}` : `${allow}, then ${opts.agent ? continueCommand(opts, url) : `re-run with --yes --ip-allowed: ${continueCommand(opts, url)}`}`
23508
+ };
23509
+ }
23510
+ async function allowIpStep(row, c) {
23511
+ if (row.status === "ip_reserving")
23512
+ row = await waitForIp(row.id, c.deps) ?? row;
23513
+ if (!reservedStatus(row.status) || !row.egress_ip) {
23514
+ return {
23515
+ status: "action_required",
23516
+ provider: c.provider,
23517
+ name: c.name,
23518
+ id: row.id,
23519
+ next: row.status === "ip_reserving" ? `The IP is still being reserved: re-run in a minute, or see pgai mon instances list` : `See pgai mon instances list (${row.status ?? "unknown"})`
23520
+ };
23521
+ }
23522
+ const r = allowIpResult(row, c.provider, c.name, c.opts, c.url);
23523
+ c.progress("allow_ip", `Reserved IP for the monitoring box: ${r.ip}`, { ip: r.ip, cidr: r.cidr, host: r.host, port: r.port, reserved_until: r.reserved_until, id: row.id });
23524
+ if (!c.deps.allowIp)
23525
+ return r;
23526
+ const why = row.last_error ? `The box could not connect from ${r.ip}: ${row.last_error}` : c.opts.ipFirst ? "You asked for the IP first (--ip-first)." : !row.precheck_error ? "The IP was reserved first, as asked." : `PostgresAI could not connect to ${r.host}:${r.port} (${row.precheck_error}). The database likely allows only listed IPs.`;
23527
+ const answer = await c.deps.allowIp([
23528
+ `Monitoring box IP: ${r.ip}${row.cloud ? ` (${cloudWord(row.cloud)}, ${row.location})` : ""}`,
23529
+ why,
23530
+ `Allow inbound TCP ${r.port} from ${r.cidr} on ${r.host}, then continue. The IP is held until ${heldUntil(row.reserved_until)} and stays this box's IP.`
23531
+ ]);
23532
+ return answer === "continue" ? "continue" : allowIpResult(row, c.provider, c.name, c.opts, c.url, true);
23381
23533
  }
23382
23534
  async function placeBox(deps, opts, where, progress) {
23383
23535
  if (!deps.deployOptions)
@@ -23720,6 +23872,7 @@ async function lookForBox(findMade, opts, deps, started) {
23720
23872
  }
23721
23873
  async function follow(row, f, opts, deps, progress) {
23722
23874
  const { provider, name, fresh, billingUrl, undoRole, note } = f;
23875
+ const url = (_) => f.url ?? name;
23723
23876
  const billing = { ...f.billing, ...f.place };
23724
23877
  let shown;
23725
23878
  for (const deadline = deps.now() + opts.waitMs;; ) {
@@ -23735,10 +23888,16 @@ async function follow(row, f, opts, deps, progress) {
23735
23888
  next: chargeFailed(row.billing_error, "re-run", billingUrl, opts.agent ? "coupon" : "--coupon")
23736
23889
  };
23737
23890
  }
23891
+ if (row.status === "ip_reserved") {
23892
+ if (undoRole)
23893
+ await undoRole();
23894
+ return { ...allowIpResult(row, provider, name, opts, url(row)), ...billing };
23895
+ }
23738
23896
  const result = connectStatus(row, provider, fresh);
23739
23897
  const state = boxState(row);
23740
- if (state !== shown && (shown !== undefined || result.status === "in_progress"))
23741
- progress("box", `Monitoring box: ${boxWords(state)}`, { state, id: row.id });
23898
+ if (state !== shown && (shown !== undefined || result.status === "in_progress")) {
23899
+ progress("box", state === "waiting_for_access" && row.egress_ip ? `Monitoring box: waiting for database access from ${row.egress_ip}${row.last_error ? ` (${row.last_error})` : ""}` : `Monitoring box: ${boxWords(state)}`, { state, id: row.id, ...state === "waiting_for_access" && row.egress_ip ? { ip: row.egress_ip, ...row.last_error ? { error: row.last_error } : {} } : {} });
23900
+ }
23742
23901
  shown = state;
23743
23902
  if (result.status !== "in_progress" || deps.now() >= deadline) {
23744
23903
  const final = deps.links ? connectStatus(await deps.links(row), provider, fresh) : result;
@@ -24038,14 +24197,43 @@ function platformDeps(p) {
24038
24197
  orgAlias = projects = undefined;
24039
24198
  return;
24040
24199
  });
24041
- const { dashboard_url, host_metrics, ...rest } = row;
24042
- return { ...rest, ...health_url ? { health_url } : {}, dashboard_url: grafanaOAuthUrl(dashboard_url), host_metrics };
24200
+ const { dashboard_url, host_metrics, host_metrics_source, ...rest } = row;
24201
+ return {
24202
+ ...rest,
24203
+ ...health_url ? { health_url } : {},
24204
+ dashboard_url: grafanaOAuthUrl(dashboard_url),
24205
+ host_metrics,
24206
+ ...host_metrics_source !== undefined ? { host_metrics_source } : {}
24207
+ };
24043
24208
  };
24044
24209
  return {
24045
24210
  verifyTls: verifyCollectorTls,
24046
24211
  list: () => rpc("cloud_monitoring_list"),
24047
24212
  links: withLinks,
24048
- create: (body) => rpc("cloud_monitoring_connect", body, p.agent ? AGENT_CONNECT_TIMEOUT_MS : CONNECT_TIMEOUT_MS),
24213
+ create: async (body) => {
24214
+ const { allow_ip_fallback: _fallback, ip_first, ip_allowed, ...older } = body;
24215
+ const asked = !!(ip_first || ip_allowed);
24216
+ const connectMs = p.agent ? AGENT_CONNECT_TIMEOUT_MS : CONNECT_TIMEOUT_MS;
24217
+ const connectTimeout = { timeoutMs: connectMs, timeoutCeilingMs: connectMs };
24218
+ try {
24219
+ return await callRpc({
24220
+ apiKey: p.apiKey,
24221
+ apiBaseUrl: p.apiBaseUrl,
24222
+ fn: "cloud_monitoring_connect",
24223
+ body,
24224
+ operation: "cloud monitoring connect",
24225
+ debug: p.debug,
24226
+ orgScope: p.orgScope,
24227
+ ...connectTimeout,
24228
+ ...asked ? {} : { fallbackBody: older }
24229
+ });
24230
+ } catch (err) {
24231
+ if (asked && err instanceof HttpStatusError && err.status === 404 && /cloud_monitoring_connect/.test(err.message)) {
24232
+ throw new Error("This PostgresAI platform does not support --ip-first or --ip-allowed yet: deploy without them");
24233
+ }
24234
+ throw err;
24235
+ }
24236
+ },
24049
24237
  disconnect: (id) => rpc("cloud_monitoring_disconnect", { instance_id: id }),
24050
24238
  setVcpus: (id, vcpus) => rpc("cloud_monitoring_set_vcpus", { instance_id: id, vcpus }),
24051
24239
  resetLock: (server) => rpc("cloud_monitoring_reset_lock", { server }),
@@ -30759,6 +30947,8 @@ async function handleToolCall(req, rootOpts, extra) {
30759
30947
  monitoringUser: args.monitoring_user === undefined || args.monitoring_user === null ? undefined : String(args.monitoring_user),
30760
30948
  cloud: args.cloud === undefined || args.cloud === null ? undefined : String(args.cloud),
30761
30949
  location: args.location === undefined || args.location === null ? undefined : String(args.location),
30950
+ ipFirst: args.ip_first === true,
30951
+ ipAllowed: args.ip_allowed === true,
30762
30952
  waitMs: 0,
30763
30953
  agent: true
30764
30954
  }, {
@@ -31333,7 +31523,7 @@ async function startMcpServer(rootOpts, extra) {
31333
31523
  },
31334
31524
  {
31335
31525
  name: "deploy_monitoring",
31336
- 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. 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).",
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).",
31337
31527
  inputSchema: {
31338
31528
  type: "object",
31339
31529
  properties: {
@@ -31347,6 +31537,8 @@ async function startMcpServer(rootOpts, extra) {
31347
31537
  coupon: { type: "string", description: "Stripe promotion code for the organization's monitoring subscription (applies when it is first created)" },
31348
31538
  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" },
31349
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" },
31540
+ 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
+ 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" },
31350
31542
  debug: { type: "boolean", description: "Enable verbose debug logs" }
31351
31543
  },
31352
31544
  required: ["database_url"],
@@ -32249,6 +32441,7 @@ function renderMarkdownForTerminal(md) {
32249
32441
  }
32250
32442
 
32251
32443
  // lib/connect.ts
32444
+ var clickhouseKeyHeld = (d) => d.host_metrics && (d.host_metrics_source === undefined || d.host_metrics_source === "clickhouse");
32252
32445
  function httpStatus2(err) {
32253
32446
  const status = err instanceof Error ? err.status : undefined;
32254
32447
  return typeof status === "number" ? status : undefined;
@@ -32438,6 +32631,7 @@ function connectStatus2(row, provider = row.provider, fresh = false) {
32438
32631
  return {
32439
32632
  ...base,
32440
32633
  status,
32634
+ ...row.egress_ip ? { egress_ip: row.egress_ip } : {},
32441
32635
  ...fresh ? { first_checkup_eta: new Date(Date.now() + FIRST_CHECKUP_DELAY_MS2).toISOString() } : {},
32442
32636
  next: row.health_url || row.dashboard_url ? `Open ${row.health_url || row.dashboard_url}` : `pgai mon instances watch ${row.id}`
32443
32637
  };
@@ -32446,6 +32640,8 @@ function connectStatus2(row, provider = row.provider, fresh = false) {
32446
32640
  return { ...base, status, next: "none" };
32447
32641
  if (status === "failed")
32448
32642
  return { ...base, status, next: `pgai mon instances delete ${row.id} --yes, then pgai mon deploy again` };
32643
+ if (row.status === "ip_reserved" && row.egress_ip)
32644
+ return { ...allowIpResult2(row, provider, row.name, { waitMs: 0 }, ""), ...base, status: "action_required", step: "allow_ip" };
32449
32645
  return { ...base, status, next: `pgai mon instances watch ${row.id}` };
32450
32646
  }
32451
32647
  var ESCAPES2 = /(?:\x1b\[|\x9b)[0-?]*[ -\/]*[@-~]|(?:\x1b\]|\x9d)[^\x07\x1b\x9c]*(?:\x07|\x1b\\|\x9c)?|\x1b[ -~]?/g;
@@ -32455,12 +32651,19 @@ var terminalText2 = (text) => text.replace(ESCAPES2, "").split(`
32455
32651
  function stepProgress(o) {
32456
32652
  let view;
32457
32653
  let priced = false;
32654
+ let refused = "";
32458
32655
  const progress = (e) => {
32459
- if (e.id)
32656
+ if (e.id && e.event !== "allow_ip")
32460
32657
  o.onId?.(e.id);
32461
32658
  if (o.json)
32462
32659
  return o.writeEvent(e);
32463
- e = { ...e, message: terminalText2(e.message), ...e.state ? { state: terminalText2(e.state) } : {} };
32660
+ e = {
32661
+ ...e,
32662
+ message: terminalText2(e.message),
32663
+ ...e.state ? { state: terminalText2(e.state) } : {},
32664
+ ...e.ip ? { ip: terminalText2(e.ip) } : {},
32665
+ ...e.error ? { error: terminalText2(e.error) } : {}
32666
+ };
32464
32667
  if (!o.tty)
32465
32668
  return o.writeLine(progressText(e));
32466
32669
  if (e.event === "billing") {
@@ -32476,12 +32679,26 @@ function stepProgress(o) {
32476
32679
  view.advance(e.event);
32477
32680
  if (/^(Note|Warning):/.test(e.message))
32478
32681
  view.log(e.message);
32479
- if (e.event === "box" && e.state)
32480
- view.setNote(`box: ${boxWords2(e.state)}`);
32682
+ if (e.event === "box" && e.state) {
32683
+ view.setNote(e.state === "waiting_for_access" && e.ip ? `box: waiting for database access from ${e.ip}` : `box: ${boxWords2(e.state)}`);
32684
+ if (e.state === "waiting_for_access" && e.ip && e.error && e.error !== refused) {
32685
+ refused = e.error;
32686
+ view.log(`The database refused ${e.ip}: ${e.error}. The box keeps trying: allow ${e.ip}/32 on the database.`);
32687
+ }
32688
+ }
32481
32689
  if (e.message === STILL_CREATING2)
32482
32690
  view.setNote(`still creating: PostgresAI is slow to answer (checking for up to ${LOOKUP_MS2 / 60000} min)`);
32483
32691
  };
32484
- return { progress, view: () => view };
32692
+ return { progress, view: () => view, reset: () => {
32693
+ view = undefined;
32694
+ } };
32695
+ }
32696
+ function stepViewEnd(r) {
32697
+ if (r.status === "ready")
32698
+ return "complete";
32699
+ if (r.status === "in_progress" || r.step === "allow_ip" || r.next.startsWith(STILL_IN_PROGRESS2))
32700
+ return "stop";
32701
+ return "fail";
32485
32702
  }
32486
32703
  function resultLines(r) {
32487
32704
  return personLines(r).map(terminalText2);
@@ -32499,6 +32716,7 @@ function personLines(r) {
32499
32716
  ...r.dashboard_url ? [` Grafana: ${r.dashboard_url}`] : [],
32500
32717
  ...r.price ? [` Price: ${r.price}`] : [],
32501
32718
  ...r.cloud ? [` Runs on: ${cloudWord2(r.cloud)}, ${r.location}`] : [],
32719
+ ...r.egress_ip ? [` Connects from: ${r.egress_ip}`] : [],
32502
32720
  ...!r.health_url && !r.dashboard_url && r.next && r.next !== "none" ? [`Next: ${r.next}`] : [],
32503
32721
  ...extra && (r.health_url || r.dashboard_url) ? [`Note: ${extra}`] : []
32504
32722
  ];
@@ -32508,6 +32726,15 @@ function personLines(r) {
32508
32726
  case "deleted":
32509
32727
  return [`Deleting the monitoring of ${who}.${billing ? ` Billing: ${billing}.` : ""}${r.next && r.next !== "none" ? ` ${r.next}` : ""}`];
32510
32728
  default:
32729
+ if (r.step === "allow_ip") {
32730
+ if (r.next.startsWith("Stopped."))
32731
+ return [r.next];
32732
+ return [
32733
+ `Monitoring box IP: ${r.ip}${r.cloud ? ` (${cloudWord2(r.cloud)}, ${r.location})` : ""}${r.reserved_until ? `, held until ${heldUntil2(r.reserved_until)}` : ""}`,
32734
+ r.last_error ? `The box could not connect: ${r.last_error}` : r.precheck_error ? `PostgresAI could not connect to ${r.host}:${r.port} (${r.precheck_error}).` : "The IP was reserved first, as asked.",
32735
+ r.next
32736
+ ];
32737
+ }
32511
32738
  if (r.projects?.length)
32512
32739
  return [r.next, ...supabaseProjectRows(r.projects)];
32513
32740
  return r.choices ? [r.next, ...Object.entries(r.choices).map(([cloud, codes]) => ` --cloud ${cloud} --location ${codes.join("|")}`)] : [r.next];
@@ -32528,6 +32755,12 @@ function boxWords2(state) {
32528
32755
  return "starting";
32529
32756
  if (state === "registered")
32530
32757
  return "installing monitoring";
32758
+ if (state === "waiting_for_access")
32759
+ return "waiting for database access";
32760
+ if (state === "ip_reserving")
32761
+ return "reserving its IP";
32762
+ if (state === "ip_reserved")
32763
+ return "waiting for its IP to be allowed";
32531
32764
  return monitoringStatus(state).replace("_", " ");
32532
32765
  }
32533
32766
  async function billingFor2(q, name, opts, deps, show, ask) {
@@ -32577,6 +32810,9 @@ async function connect2(url, opts, deps) {
32577
32810
  if (project !== undefined && provider !== "supabase") {
32578
32811
  return { status: "action_required", provider, name: url ? databaseName3(url) : "", next: "--supabase-project applies to Supabase only: leave it out, or use --provider supabase" };
32579
32812
  }
32813
+ if ((opts.ipFirst || opts.ipAllowed) && (opts.selfHosted || provider === "supabase")) {
32814
+ return { status: "action_required", provider, name: url ? databaseName3(url) : "", next: `${opts.agent ? "ip_first / ip_allowed apply" : "--ip-first and --ip-allowed apply"} to PostgresAI Cloud monitoring of a database URL only` };
32815
+ }
32580
32816
  if (!url) {
32581
32817
  if (provider !== "supabase")
32582
32818
  return { status: "action_required", provider, name: "", next: "Pass the database URL: pgai mon deploy postgresql://user:password@host:5432/dbname (Supabase: --provider supabase)" };
@@ -32635,12 +32871,24 @@ async function connect2(url, opts, deps) {
32635
32871
  return live.find(byCluster) ?? live.find(byName);
32636
32872
  };
32637
32873
  const rows = opts.selfHosted ? [] : await deps.list();
32638
- const known = new Set(rows.map((d) => d.id));
32874
+ const known = new Map(rows.map((d) => [d.id, d.status]));
32639
32875
  const findMade = (list) => {
32640
- const fresh = list.filter((d) => !known.has(d.id));
32876
+ const fresh = list.filter((d) => !known.has(d.id) || reservedStatus2(known.get(d.id)) && !reservedStatus2(d.status));
32641
32877
  return fresh.find(byCluster) ?? fresh.find(byName);
32642
32878
  };
32643
32879
  let row = findDatabase(rows);
32880
+ let continuing = false;
32881
+ let ownId;
32882
+ if (row && reservedStatus2(row.status)) {
32883
+ if (!opts.ipAllowed) {
32884
+ const asked = await allowIpStep2(row, { provider, name, url, opts, deps, progress });
32885
+ if (asked !== "continue")
32886
+ return asked;
32887
+ }
32888
+ continuing = true;
32889
+ ownId = row.id;
32890
+ row = undefined;
32891
+ }
32644
32892
  const fresh = !row;
32645
32893
  let note = "";
32646
32894
  let billing = {};
@@ -32681,7 +32929,7 @@ async function connect2(url, opts, deps) {
32681
32929
  }
32682
32930
  }
32683
32931
  if (row && opts.resetPassword) {
32684
- return { status: "action_required", provider, name, id: row.id, next: `${name} is already monitored, and its monitoring uses the current password: ${disconnectEach2([deleteRef2(row, rows)])} first` };
32932
+ return { status: "action_required", provider, name, id: row.id, next: `${row.name} is already monitored, and its monitoring uses the current password. Delete it, then run this deploy again with --reset-password: ${disconnectEach2([deleteRef2(row, rows)])}` };
32685
32933
  }
32686
32934
  if (row && key) {
32687
32935
  const found = await findOrg();
@@ -32710,7 +32958,7 @@ async function connect2(url, opts, deps) {
32710
32958
  const unread = (d) => !d.cluster;
32711
32959
  const onServer = (d) => !!d.cluster && serverOf2(d.cluster) === cluster;
32712
32960
  const usesRole = (d) => !["deleting", "deleted", "inactive"].includes(stateOf2(d.status)) && !(d.status === "failed" && !d.registered_at);
32713
- const othersOnServer = (list) => list.filter((d) => usesRole(d) && (unread(d) || onServer(d)));
32961
+ const othersOnServer = (list) => list.filter((d) => d.id !== ownId && usesRole(d) && (unread(d) || onServer(d)));
32714
32962
  const storedPassword = !monitoringUser && rows.some((d) => d.monitoring_password_stored && !/delet/.test(d.status ?? "") && onServer(d)) || undefined;
32715
32963
  const cutOff = (others, list) => {
32716
32964
  const label = (d) => deleteRef2(d, list) === d.id ? `${d.name} (id ${d.id})` : d.name;
@@ -32796,9 +33044,9 @@ async function connect2(url, opts, deps) {
32796
33044
  try {
32797
33045
  if (lockId) {
32798
33046
  const now = await deps.list();
32799
- const same = findDatabase(now);
33047
+ const same = findDatabase(now.filter((d) => d.id !== ownId));
32800
33048
  if (same)
32801
- return { status: "action_required", provider, name, id: same.id, ...billing, next: `${name} is already monitored, and its monitoring uses the current password: ${disconnectEach2([deleteRef2(same, now)])} first` };
33049
+ return { status: "action_required", provider, name, id: same.id, ...billing, next: `${same.name} is already monitored, and its monitoring uses the current password. Delete it, then run this deploy again with --reset-password: ${disconnectEach2([deleteRef2(same, now)])}` };
32802
33050
  const others = othersOnServer(now);
32803
33051
  if (others.length)
32804
33052
  return { ...cutOff(others, now), ...billing };
@@ -32812,6 +33060,16 @@ async function connect2(url, opts, deps) {
32812
33060
  throw new CancelledError2;
32813
33061
  }
32814
33062
  if ("next" in prepared) {
33063
+ if ((continuing || opts.ipAllowed) && /created by someone else meanwhile/.test(prepared.next)) {
33064
+ for (let tries = 0;tries < 10; tries++) {
33065
+ const box = findDatabase(await deps.list());
33066
+ if (!box)
33067
+ break;
33068
+ if (!reservedStatus2(box.status))
33069
+ return follow2(box, { provider, name, fresh, billing, billingUrl, undoRole, note, place, url }, opts, deps, progress);
33070
+ await deps.sleep(2000);
33071
+ }
33072
+ }
32815
33073
  const { resettable, ...refusal } = prepared;
32816
33074
  return { status: "action_required", provider, name, ...billing, ...refusal };
32817
33075
  }
@@ -32824,8 +33082,12 @@ async function connect2(url, opts, deps) {
32824
33082
  return { status: "ready", provider, name, dashboard_url: "http://localhost:3000", host_metrics: !!ch, next: `pgai mon health${note}` };
32825
33083
  }
32826
33084
  progress("provisioning", `Provisioning monitoring for ${name}`);
32827
- created = await deps.create({
32828
- db_url: prepared.monitoringUrl,
33085
+ const monitoringUrl = prepared.monitoringUrl;
33086
+ const request = (ipAllowed) => deps.create({
33087
+ db_url: monitoringUrl,
33088
+ allow_ip_fallback: true,
33089
+ ...opts.ipFirst ? { ip_first: true } : {},
33090
+ ...ipAllowed ? { ip_allowed: true } : {},
32829
33091
  ...provider === "clickhouse" ? { provider } : {},
32830
33092
  ...key && ch ? { clickhouse_org_id: ch.orgId, clickhouse_key_id: key.keyId, clickhouse_key_secret: key.keySecret } : {},
32831
33093
  ...opts.coupon ? { promo_code: opts.coupon } : {},
@@ -32833,7 +33095,38 @@ async function connect2(url, opts, deps) {
32833
33095
  ...opts.name ? { project_name: opts.name } : {},
32834
33096
  ...place ? { cloud: place.cloud, server_location: place.location } : opts.location ? { server_location: opts.location } : {},
32835
33097
  ...vcpus ? { vcpus } : {}
33098
+ });
33099
+ const refused = async (c) => {
33100
+ const row = await waitForIp2(c.id, deps) ?? { id: c.id, name: c.name, status: c.status };
33101
+ const why = [c.error, c.billing_warning].filter(Boolean).join(" ");
33102
+ return { ...c, status: "allow_ip", result: { ...allowIpResult2(row, provider, name, opts, url), next: `${why} Then: ${continueCommand2(opts, url)}` } };
33103
+ };
33104
+ const continued = async () => {
33105
+ const c = await request(true);
33106
+ return reservedStatus2(c.status) && c.error ? refused(c) : c;
33107
+ };
33108
+ created = await request(continuing || !!opts.ipAllowed).then(async (c) => {
33109
+ if (reservedStatus2(c.status))
33110
+ known.set(c.id, c.status);
33111
+ if (reservedStatus2(c.status) && c.error)
33112
+ return refused(c);
33113
+ if (!reservedStatus2(c.status))
33114
+ return c;
33115
+ const reservation = await waitForIp2(c.id, deps);
33116
+ if (!reservation || !reservedStatus2(reservation.status))
33117
+ return { ...c, status: reservation?.status ?? "failed", error: reservation ? undefined : "The IP reservation did not report back in time; see pgai mon instances list." };
33118
+ if (continuing && c.status === "ip_reserving" && reservation.status === "ip_reserved")
33119
+ return continued();
33120
+ const asked = await allowIpStep2(reservation, { provider, name, url, opts, deps, progress });
33121
+ if (asked !== "continue")
33122
+ return { ...c, status: "allow_ip", result: asked };
33123
+ return continued();
32836
33124
  }).catch(async (err) => {
33125
+ if ((continuing || opts.ipAllowed) && err instanceof HttpStatusError && err.status === 409 && /already monitored/.test(err.message)) {
33126
+ const box = findDatabase(await deps.list());
33127
+ if (box && !reservedStatus2(box.status))
33128
+ return { id: box.id, name: box.name, status: box.status ?? "launch_requested" };
33129
+ }
32837
33130
  if (err instanceof HttpStatusError && (err.status < 500 || err.sqlstate))
32838
33131
  await undo(prepared);
32839
33132
  else
@@ -32874,6 +33167,12 @@ async function connect2(url, opts, deps) {
32874
33167
  const gone = `PostgresAI no longer keeps the password of ${DEFAULT_MONITORING_USER} for this server`;
32875
33168
  return { status: "action_required", provider, name, ...billing, next: opts.agent ? `${gone}: pass its URL as database_url, or run pgai mon deploy <admin-url> --reset-password in a terminal` : `${gone}: re-run with --reset-password (anything else that logs in as ${DEFAULT_MONITORING_USER} then needs the new one), or set PGAI_MON_PASSWORD to its password` };
32876
33169
  }
33170
+ if (created.status === "allow_ip" && created.result) {
33171
+ const now = findDatabase(await deps.list().catch(() => []));
33172
+ if (!now || reservedStatus2(now.status))
33173
+ await undo(prepared);
33174
+ return { ...created.result, ...billing };
33175
+ }
32877
33176
  if (created.status === "failed") {
32878
33177
  await undo(prepared);
32879
33178
  return { status: "failed", provider, name, id: created.id, next: `${created.error} Re-run pgai mon deploy later.` };
@@ -32888,7 +33187,73 @@ async function connect2(url, opts, deps) {
32888
33187
  const made = prepared;
32889
33188
  undoRole = () => undo(made);
32890
33189
  }
32891
- return follow2(row, { provider, name, fresh, billing, billingUrl, undoRole, note, place }, opts, deps, progress);
33190
+ return follow2(row, { provider, name, fresh, billing, billingUrl, undoRole, note, place, url }, opts, deps, progress);
33191
+ }
33192
+ function listedDatabase(d) {
33193
+ return { ...d, status: stateOf2(d.status), ...d.status === "ip_reserved" ? { step: "allow_ip" } : {} };
33194
+ }
33195
+ var reservedStatus2 = (status) => status === "ip_reserving" || status === "ip_reserved";
33196
+ var IP_POLL_MS2 = 3000;
33197
+ var IP_WAIT_MS2 = 120000;
33198
+ async function waitForIp2(id, deps) {
33199
+ for (const deadline = deps.now() + IP_WAIT_MS2;; ) {
33200
+ const row = (await deps.list().catch(() => [])).find((d) => d.id === id);
33201
+ if (row && row.status !== "ip_reserving")
33202
+ return row;
33203
+ if (deps.now() >= deadline)
33204
+ return;
33205
+ await deps.sleep(IP_POLL_MS2);
33206
+ }
33207
+ }
33208
+ var continueCommand2 = (opts, url) => opts.agent ? "call deploy_monitoring again with ip_allowed: true" : `pgai mon deploy ${url ? quoteArg2(maskConnectionString(url)) : "<database URL>"} --yes --ip-allowed`;
33209
+ var quoteArg2 = (s) => /^[A-Za-z0-9_@%+=:,./-]+$/.test(s) ? s : `'${s.replace(/'/g, "'\\''")}'`;
33210
+ var heldUntil2 = (iso) => iso ? iso.replace(/T(\d\d:\d\d).*$/, " $1 UTC") : "";
33211
+ function allowIpResult2(row, provider, name, opts, url, stopped = false) {
33212
+ const [host, port] = row.cluster ? [row.cluster.replace(/:\d+$/, ""), Number(row.cluster.match(/:(\d+)$/)?.[1] ?? 5432)] : [undefined, undefined];
33213
+ const ip = row.egress_ip ?? undefined;
33214
+ const cidr = ip ? `${ip}/32` : undefined;
33215
+ const allow = `Allow inbound TCP ${port ?? 5432} from ${cidr} on ${host ?? "the database"}`;
33216
+ return {
33217
+ status: "action_required",
33218
+ step: "allow_ip",
33219
+ provider,
33220
+ name,
33221
+ id: row.id,
33222
+ ...row.cloud ? { cloud: row.cloud, location: row.location ?? undefined } : {},
33223
+ ip,
33224
+ cidr,
33225
+ host,
33226
+ port,
33227
+ ...row.reserved_until ? { reserved_until: row.reserved_until } : {},
33228
+ ...row.last_error ? { last_error: row.last_error } : {},
33229
+ ...row.precheck_error ? { precheck_error: row.precheck_error } : {},
33230
+ egress_ip: ip,
33231
+ next: stopped ? `Stopped. ${ip} stays reserved until ${heldUntil2(row.reserved_until)}. ${allow}, then: ${continueCommand2(opts, url)}` : `${allow}, then ${opts.agent ? continueCommand2(opts, url) : `re-run with --yes --ip-allowed: ${continueCommand2(opts, url)}`}`
33232
+ };
33233
+ }
33234
+ async function allowIpStep2(row, c) {
33235
+ if (row.status === "ip_reserving")
33236
+ row = await waitForIp2(row.id, c.deps) ?? row;
33237
+ if (!reservedStatus2(row.status) || !row.egress_ip) {
33238
+ return {
33239
+ status: "action_required",
33240
+ provider: c.provider,
33241
+ name: c.name,
33242
+ id: row.id,
33243
+ next: row.status === "ip_reserving" ? `The IP is still being reserved: re-run in a minute, or see pgai mon instances list` : `See pgai mon instances list (${row.status ?? "unknown"})`
33244
+ };
33245
+ }
33246
+ const r = allowIpResult2(row, c.provider, c.name, c.opts, c.url);
33247
+ c.progress("allow_ip", `Reserved IP for the monitoring box: ${r.ip}`, { ip: r.ip, cidr: r.cidr, host: r.host, port: r.port, reserved_until: r.reserved_until, id: row.id });
33248
+ if (!c.deps.allowIp)
33249
+ return r;
33250
+ const why = row.last_error ? `The box could not connect from ${r.ip}: ${row.last_error}` : c.opts.ipFirst ? "You asked for the IP first (--ip-first)." : !row.precheck_error ? "The IP was reserved first, as asked." : `PostgresAI could not connect to ${r.host}:${r.port} (${row.precheck_error}). The database likely allows only listed IPs.`;
33251
+ const answer = await c.deps.allowIp([
33252
+ `Monitoring box IP: ${r.ip}${row.cloud ? ` (${cloudWord2(row.cloud)}, ${row.location})` : ""}`,
33253
+ why,
33254
+ `Allow inbound TCP ${r.port} from ${r.cidr} on ${r.host}, then continue. The IP is held until ${heldUntil2(row.reserved_until)} and stays this box's IP.`
33255
+ ]);
33256
+ return answer === "continue" ? "continue" : allowIpResult2(row, c.provider, c.name, c.opts, c.url, true);
32892
33257
  }
32893
33258
  async function placeBox2(deps, opts, where, progress) {
32894
33259
  if (!deps.deployOptions)
@@ -33296,6 +33661,7 @@ async function lookForBox2(findMade, opts, deps, started) {
33296
33661
  }
33297
33662
  async function follow2(row, f, opts, deps, progress) {
33298
33663
  const { provider, name, fresh, billingUrl, undoRole, note } = f;
33664
+ const url = (_) => f.url ?? name;
33299
33665
  const billing = { ...f.billing, ...f.place };
33300
33666
  let shown;
33301
33667
  for (const deadline = deps.now() + opts.waitMs;; ) {
@@ -33311,10 +33677,16 @@ async function follow2(row, f, opts, deps, progress) {
33311
33677
  next: chargeFailed2(row.billing_error, "re-run", billingUrl, opts.agent ? "coupon" : "--coupon")
33312
33678
  };
33313
33679
  }
33680
+ if (row.status === "ip_reserved") {
33681
+ if (undoRole)
33682
+ await undoRole();
33683
+ return { ...allowIpResult2(row, provider, name, opts, url(row)), ...billing };
33684
+ }
33314
33685
  const result = connectStatus2(row, provider, fresh);
33315
33686
  const state = boxState2(row);
33316
- if (state !== shown && (shown !== undefined || result.status === "in_progress"))
33317
- progress("box", `Monitoring box: ${boxWords2(state)}`, { state, id: row.id });
33687
+ if (state !== shown && (shown !== undefined || result.status === "in_progress")) {
33688
+ progress("box", state === "waiting_for_access" && row.egress_ip ? `Monitoring box: waiting for database access from ${row.egress_ip}${row.last_error ? ` (${row.last_error})` : ""}` : `Monitoring box: ${boxWords2(state)}`, { state, id: row.id, ...state === "waiting_for_access" && row.egress_ip ? { ip: row.egress_ip, ...row.last_error ? { error: row.last_error } : {} } : {} });
33689
+ }
33318
33690
  shown = state;
33319
33691
  if (result.status !== "in_progress" || deps.now() >= deadline) {
33320
33692
  const final = deps.links ? connectStatus2(await deps.links(row), provider, fresh) : result;
@@ -33630,14 +34002,43 @@ function platformDeps2(p) {
33630
34002
  orgAlias = projects = undefined;
33631
34003
  return;
33632
34004
  });
33633
- const { dashboard_url, host_metrics, ...rest } = row;
33634
- return { ...rest, ...health_url ? { health_url } : {}, dashboard_url: grafanaOAuthUrl2(dashboard_url), host_metrics };
34005
+ const { dashboard_url, host_metrics, host_metrics_source, ...rest } = row;
34006
+ return {
34007
+ ...rest,
34008
+ ...health_url ? { health_url } : {},
34009
+ dashboard_url: grafanaOAuthUrl2(dashboard_url),
34010
+ host_metrics,
34011
+ ...host_metrics_source !== undefined ? { host_metrics_source } : {}
34012
+ };
33635
34013
  };
33636
34014
  return {
33637
34015
  verifyTls: verifyCollectorTls,
33638
34016
  list: () => rpc("cloud_monitoring_list"),
33639
34017
  links: withLinks,
33640
- create: (body) => rpc("cloud_monitoring_connect", body, p.agent ? AGENT_CONNECT_TIMEOUT_MS2 : CONNECT_TIMEOUT_MS2),
34018
+ create: async (body) => {
34019
+ const { allow_ip_fallback: _fallback, ip_first, ip_allowed, ...older } = body;
34020
+ const asked = !!(ip_first || ip_allowed);
34021
+ const connectMs = p.agent ? AGENT_CONNECT_TIMEOUT_MS2 : CONNECT_TIMEOUT_MS2;
34022
+ const connectTimeout = { timeoutMs: connectMs, timeoutCeilingMs: connectMs };
34023
+ try {
34024
+ return await callRpc({
34025
+ apiKey: p.apiKey,
34026
+ apiBaseUrl: p.apiBaseUrl,
34027
+ fn: "cloud_monitoring_connect",
34028
+ body,
34029
+ operation: "cloud monitoring connect",
34030
+ debug: p.debug,
34031
+ orgScope: p.orgScope,
34032
+ ...connectTimeout,
34033
+ ...asked ? {} : { fallbackBody: older }
34034
+ });
34035
+ } catch (err) {
34036
+ if (asked && err instanceof HttpStatusError && err.status === 404 && /cloud_monitoring_connect/.test(err.message)) {
34037
+ throw new Error("This PostgresAI platform does not support --ip-first or --ip-allowed yet: deploy without them");
34038
+ }
34039
+ throw err;
34040
+ }
34041
+ },
33641
34042
  disconnect: (id) => rpc("cloud_monitoring_disconnect", { instance_id: id }),
33642
34043
  setVcpus: (id, vcpus) => rpc("cloud_monitoring_set_vcpus", { instance_id: id, vcpus }),
33643
34044
  resetLock: (server) => rpc("cloud_monitoring_reset_lock", { server }),
@@ -35513,6 +35914,8 @@ function monitoringStatus2(raw) {
35513
35914
  return "inactive";
35514
35915
  if (/fail|error/.test(raw ?? ""))
35515
35916
  return "failed";
35917
+ if (raw === "ip_reserved")
35918
+ return "action_required";
35516
35919
  return "in_progress";
35517
35920
  }
35518
35921
  function databaseUrlArg2(positional, dbUrl, command) {
@@ -36759,7 +37162,7 @@ async function buildInitPlan2(params) {
36759
37162
  if not exists (select 1 from pg_catalog.pg_roles where rolname = ${qRoleNameLit}) then
36760
37163
  begin
36761
37164
  create user ${qRole} with password ${qPwVerifier};
36762
- exception when duplicate_object then
37165
+ exception when duplicate_object or unique_violation then
36763
37166
  null;
36764
37167
  end;
36765
37168
  end if;${params.keepExistingPassword ? "" : `
@@ -36812,9 +37215,21 @@ async function applyInitPlan2(params) {
36812
37215
  throw e;
36813
37216
  }
36814
37217
  };
37218
+ const concurrent = (e) => /tuple concurrently (updated|deleted)/.test(e instanceof Error ? e.message : String(e));
37219
+ const executeRetried = async (step) => {
37220
+ for (let attempt = 1;; attempt++) {
37221
+ try {
37222
+ return await executeStep(step);
37223
+ } catch (e) {
37224
+ if (attempt >= 3 || !concurrent(e))
37225
+ throw e;
37226
+ await new Promise((r) => setTimeout(r, 200 * attempt));
37227
+ }
37228
+ }
37229
+ };
36815
37230
  for (const step of params.plan.steps.filter((s) => !s.optional)) {
36816
37231
  try {
36817
- await executeStep(step);
37232
+ await executeRetried(step);
36818
37233
  applied.push(step.name);
36819
37234
  } catch (e) {
36820
37235
  const msg = e instanceof Error ? e.message : String(e);
@@ -47480,6 +47895,27 @@ ${reply}`);
47480
47895
  closeReadline();
47481
47896
  }
47482
47897
  },
47898
+ ...interactive(opts.json) ? { allowIp: async (lines) => {
47899
+ steps.view()?.stop();
47900
+ console.error("");
47901
+ for (const line of lines)
47902
+ console.error(terminalText2(line));
47903
+ process.exitCode = 130;
47904
+ try {
47905
+ for (let tries = 0;tries < 3; tries++) {
47906
+ const answer = (await question("[Enter] continue / [s]top: ")).trim().toLowerCase();
47907
+ if (answer === "" || answer === "c" || answer === "continue")
47908
+ return "continue";
47909
+ if (answer === "s" || answer === "stop")
47910
+ return "stop";
47911
+ }
47912
+ return "stop";
47913
+ } finally {
47914
+ process.exitCode = undefined;
47915
+ closeReadline();
47916
+ steps.reset();
47917
+ }
47918
+ } } : {},
47483
47919
  ...interactive(opts.json) ? { chooseSupabaseProject: async (projects, org) => {
47484
47920
  const selectable = projects.filter((x) => x.selectable);
47485
47921
  console.error(terminalText2(`
@@ -47551,9 +47987,10 @@ Projects in the Supabase organization "${org}":`));
47551
47987
  view.setNote("stopped watching");
47552
47988
  else if (looking)
47553
47989
  view.setNote("");
47554
- if (result.status === "ready")
47990
+ const end = stepViewEnd(result);
47991
+ if (end === "complete")
47555
47992
  view.complete();
47556
- else if (result.status !== "in_progress" && !result.next.startsWith(STILL_IN_PROGRESS2))
47993
+ else if (end === "fail")
47557
47994
  view.fail();
47558
47995
  view.stop();
47559
47996
  }
@@ -47568,7 +48005,7 @@ Projects in the Supabase organization "${org}":`));
47568
48005
  }
47569
48006
  }
47570
48007
  var mon = program2.command("mon").description("monitoring services management");
47571
- 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").addHelpText("after", [
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", [
47572
48009
  "",
47573
48010
  "Steps (each skipped when already done; safe to re-run): sign in, prepare the database",
47574
48011
  "(an admin URL creates the postgres_ai_mon role; otherwise the SQL is printed), provision",
@@ -47587,10 +48024,14 @@ sharedDeployOptions2(withOrgOptions(mon.command("deploy")), 20, "instance name (
47587
48024
  'place, the price and the choices ("choices" in the JSON result): re-run with --yes, and with',
47588
48025
  "--cloud/--location to pick another place.",
47589
48026
  "",
47590
- "Network: the database must be reachable from this machine and from PostgresAI. Before provisioning,",
47591
- "PostgresAI tests the connection from an unpublished address, so the port must accept connections",
47592
- "from the internet; after it, the monitoring box connects from the IP shown on its Console page.",
47593
- "Behind an IP allowlist, use AWS PrivateLink (RDS) or --self-hosted.",
48027
+ "Network: the database must be reachable from this machine. The monitoring box connects from its",
48028
+ "own static IP, shown before the box is created when PostgresAI can't reach the database the way",
48029
+ "an IP allowlist refuses (timeout, refused, no pg_hba.conf entry), or with --ip-first: deploy then",
48030
+ 'stops at the IP ("step":"allow_ip" with ip, cidr, host, port, reserved_until; exit 3 when not',
48031
+ "at a terminal). Allow it on the database, then re-run with --ip-allowed (at a terminal: Enter).",
48032
+ "The IP is held 7 days and stays the box's for its life. If the box still can't connect, it waits",
48033
+ "up to 90 minutes, then is removed and the IP kept: allow it and re-run with --ip-allowed.",
48034
+ "RDS can also use AWS PrivateLink (no public IP), or monitor with --self-hosted.",
47594
48035
  "",
47595
48036
  "Billing is per Postgres cluster observed: another database in a cluster the organization already",
47596
48037
  'monitors is included. Once the URL is checked, deploy shows the price (or "free (N of M free slots)"). A billed',
@@ -47673,7 +48114,7 @@ var monPollMs = () => {
47673
48114
  var monInstances = mon.command("instances").description("monitoring instances in PostgresAI Cloud (pgai mon deploy)");
47674
48115
  withOrgOptions(monInstances.command("list")).description("list the organization's monitoring instances").option("--json", "JSON output").option("--debug", "print HTTP requests (secrets masked)").action(async (opts) => {
47675
48116
  try {
47676
- printResult((await linkedDatabases(opts)).map((d) => ({ ...d, status: stateOf2(d.status) })), opts.json);
48117
+ printResult((await linkedDatabases(opts)).map(listedDatabase), opts.json);
47677
48118
  } catch (err) {
47678
48119
  failCloud(err, opts.json);
47679
48120
  }
@@ -47713,7 +48154,7 @@ withOrgOptions(monInstances.command("delete <id-or-name>")).description("stop mo
47713
48154
  name: row.name,
47714
48155
  id: row.id,
47715
48156
  ...billing ? { billing } : {},
47716
- next: row.host_metrics ? "Delete the ClickHouse Cloud API key you gave us" : "none"
48157
+ next: clickhouseKeyHeld(row) ? "Delete the ClickHouse Cloud API key you gave us" : "none"
47717
48158
  }, opts.json);
47718
48159
  } catch (err) {
47719
48160
  failCloud(err, opts.json);
@@ -6,12 +6,10 @@
6
6
  -- if not exists (select 1 from pg_catalog.pg_roles where rolname = '...') then
7
7
  -- begin
8
8
  -- create user "..." with password '...';
9
- -- exception when duplicate_object then
9
+ -- exception when duplicate_object or unique_violation then
10
10
  -- null;
11
11
  -- end;
12
12
  -- end if;
13
13
  -- alter user "..." with password '...';
14
14
  -- end $$;
15
15
  {{ROLE_STMT}}
16
-
17
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postgresai",
3
- "version": "0.18.0-dev.14",
3
+ "version": "0.18.0-dev.15",
4
4
  "description": "postgres_ai CLI",
5
5
  "license": "Apache-2.0",
6
6
  "private": false,
package/sql/01.role.sql CHANGED
@@ -6,12 +6,10 @@
6
6
  -- if not exists (select 1 from pg_catalog.pg_roles where rolname = '...') then
7
7
  -- begin
8
8
  -- create user "..." with password '...';
9
- -- exception when duplicate_object then
9
+ -- exception when duplicate_object or unique_violation then
10
10
  -- null;
11
11
  -- end;
12
12
  -- end if;
13
13
  -- alter user "..." with password '...';
14
14
  -- end $$;
15
15
  {{ROLE_STMT}}
16
-
17
-