Skip to content

Commit 7fdbcba

Browse files
committed
fix: install and log in the DeepSQL CLI during install.sh
Previously install.sh only reported the CLI's install/update status at the end of a run — it never installed it, so a fresh self-host following the README end to end had no `deepsql` command at all, and never logged it in against the stack it just created. setup_deepsql_cli() now: - installs @deepsql/mcp via `npm i -g` when missing, retrying once with non-interactive `sudo -n` (never an interactive sudo prompt buried in an otherwise unattended installer) - logs the CLI in as the admin account install.sh just created, via the --password/--password-stdin flow, using the already-required DEEPSQL_INITIAL_ADMIN_EMAIL/DEEPSQL_INITIAL_ADMIN_PASSWORD - skips login if a token already exists for this URL, so re-running install.sh (upgrades, credential rotation) doesn't pile up tokens under `deepsql whoami` Every step stays non-fatal, consistent with the rest of the script: install or login failure prints the manual command and falls through rather than aborting. Verified against a live self-hosted stack: detects an existing install, skips a redundant login when already authorized, and (separately, on a fresh venv) performs a clean install + password-stdin login end to end.
1 parent 051691f commit 7fdbcba

1 file changed

Lines changed: 78 additions & 24 deletions

File tree

scripts/self-host/install.sh

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -477,47 +477,101 @@ else
477477
fi
478478

479479
# ── DeepSQL CLI (@deepsql/mcp) ───────────────────────────────────────────────
480-
# Nothing in this repo installed or updated the CLI, so a reader who followed the
481-
# README end to end finished with a running stack and no `deepsql` command at
482-
# all — and anyone who installed it once drifted silently (a machine here sat on
483-
# 0.16.0 while npm was on 0.26.0). The CLI is an agent-facing surface, so a stale
484-
# one misreports which tools and subcommands exist.
480+
# Nothing in this repo installed, updated, or logged in the CLI, so a reader
481+
# who followed the README end to end finished with a running stack and no
482+
# `deepsql` command at all — and anyone who installed it once drifted silently
483+
# (a machine here sat on 0.16.0 while npm was on 0.26.0). The CLI is an
484+
# agent-facing surface, so a stale one misreports which tools and subcommands
485+
# exist.
485486
#
486-
# Report rather than install: this is a global npm mutation, and `npm i -g` can
487-
# need elevated permissions depending on the Node install. Printing the exact
488-
# command keeps the decision with the operator and never fails the install.
489-
report_cli_status() {
487+
# Install and log in automatically when npm is available. `npm i -g` is tried
488+
# first without privilege escalation, then retried once with `sudo -n` (never
489+
# an interactive `sudo` — a password prompt buried in an otherwise unattended
490+
# installer is exactly the kind of silent hang this script avoids elsewhere).
491+
# Every step here is non-fatal: install or login failure only prints the
492+
# manual command and falls through, it never aborts the installer.
493+
install_deepsql_cli() {
494+
if npm i -g @deepsql/mcp >/dev/null 2>&1; then
495+
return 0
496+
fi
497+
if command -v sudo >/dev/null 2>&1 && sudo -n true 2>/dev/null; then
498+
if sudo npm i -g @deepsql/mcp >/dev/null 2>&1; then
499+
return 0
500+
fi
501+
fi
502+
return 1
503+
}
504+
505+
setup_deepsql_cli() {
490506
if ! command -v npm >/dev/null 2>&1; then
491-
echo "DeepSQL CLI: npm not found — skipping check."
507+
echo "DeepSQL CLI: npm not found — skipping install."
492508
echo " The CLI is optional; install Node 20+ then: npm i -g @deepsql/mcp"
493509
echo
494510
return 0
495511
fi
512+
496513
local installed latest
497514
installed="$(deepsql --version 2>/dev/null | tr -d '[:space:]' || true)"
498-
# `npm view` reaches the network; never let it stall or fail the install.
499-
latest="$(npm view @deepsql/mcp version 2>/dev/null | tr -d '[:space:]' || true)"
500515

501516
if [[ -z "$installed" ]]; then
502-
echo "DeepSQL CLI: not installed."
503-
echo " Install it with: npm i -g @deepsql/mcp"
504-
elif [[ -z "$latest" ]]; then
505-
# Don't claim "up to date" on a check that never completed — that is the
506-
# same false-green that let a stale CLI sit unnoticed in the first place.
507-
echo "DeepSQL CLI: ${installed} installed (could not reach npm to check for updates)."
508-
elif [[ "$installed" != "$latest" ]]; then
509-
echo "DeepSQL CLI: ${installed} installed, ${latest} available."
510-
echo " Update with: npm i -g @deepsql/mcp@latest"
517+
echo "Installing DeepSQL CLI (@deepsql/mcp)…"
518+
if install_deepsql_cli; then
519+
installed="$(deepsql --version 2>/dev/null | tr -d '[:space:]' || true)"
520+
echo "DeepSQL CLI: ${installed:-installed}."
521+
else
522+
echo "DeepSQL CLI: install failed (npm i -g @deepsql/mcp may need elevated"
523+
echo " permissions on this system). Install it yourself, then:"
524+
echo " deepsql login --url http://localhost:${DEEPSQL_BACKEND_PORT}"
525+
echo
526+
return 0
527+
fi
511528
else
512-
echo "DeepSQL CLI: ${installed} (up to date)."
529+
# `npm view` reaches the network; never let it stall or fail the install.
530+
latest="$(npm view @deepsql/mcp version 2>/dev/null | tr -d '[:space:]' || true)"
531+
if [[ -z "$latest" ]]; then
532+
# Don't claim "up to date" on a check that never completed — that is the
533+
# same false-green that let a stale CLI sit unnoticed in the first place.
534+
echo "DeepSQL CLI: ${installed} installed (could not reach npm to check for updates)."
535+
elif [[ "$installed" != "$latest" ]]; then
536+
echo "DeepSQL CLI: ${installed} installed, ${latest} available."
537+
echo " Update with: npm i -g @deepsql/mcp@latest"
538+
else
539+
echo "DeepSQL CLI: ${installed} (up to date)."
540+
fi
513541
fi
514-
if [[ -n "$installed" ]]; then
542+
543+
if ! command -v deepsql >/dev/null 2>&1; then
544+
echo
545+
return 0
546+
fi
547+
548+
if [[ -z "${DEEPSQL_INITIAL_ADMIN_EMAIL:-}" || -z "${DEEPSQL_INITIAL_ADMIN_PASSWORD:-}" ]]; then
515549
echo " Point it at this stack: deepsql login --url http://localhost:${DEEPSQL_BACKEND_PORT}"
550+
echo
551+
return 0
552+
fi
553+
554+
# Skip login if a token already exists for this exact stack — install.sh is
555+
# meant to be re-run (upgrades, credential rotation), and login mints a new
556+
# long-lived token every time, so re-running it would otherwise pile up
557+
# tokens the operator never asked for under `deepsql whoami`.
558+
if deepsql whoami --url "http://localhost:${DEEPSQL_BACKEND_PORT}" >/dev/null 2>&1; then
559+
echo "DeepSQL CLI: already logged in as ${DEEPSQL_INITIAL_ADMIN_EMAIL}."
560+
else
561+
echo "Logging in the DeepSQL CLI as ${DEEPSQL_INITIAL_ADMIN_EMAIL}"
562+
if printf '%s' "${DEEPSQL_INITIAL_ADMIN_PASSWORD}" | deepsql login \
563+
--url "http://localhost:${DEEPSQL_BACKEND_PORT}" --password \
564+
--email "${DEEPSQL_INITIAL_ADMIN_EMAIL}" --password-stdin --label install; then
565+
:
566+
else
567+
echo "DeepSQL CLI: login failed. Run manually:"
568+
echo " deepsql login --url http://localhost:${DEEPSQL_BACKEND_PORT}"
569+
fi
516570
fi
517571
echo
518572
}
519573

520-
report_cli_status
574+
setup_deepsql_cli
521575

522576
echo "Useful commands:"
523577
echo " ./scripts/self-host/status.sh"

0 commit comments

Comments
 (0)