From 038469b6f7bec57a2a4f1e553e3d90fd088f0232 Mon Sep 17 00:00:00 2001 From: dv Date: Wed, 8 Apr 2026 00:11:09 +0300 Subject: [PATCH] fix ssh 4 --- backend/app/services/device_discovery.py | 53 +++++++++++++++++------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/backend/app/services/device_discovery.py b/backend/app/services/device_discovery.py index e06aa38..24aa85c 100644 --- a/backend/app/services/device_discovery.py +++ b/backend/app/services/device_discovery.py @@ -194,19 +194,21 @@ async def _autodetect_db_from_server( parser = RawConfigParser(strict=False) parser.read_string(config_text) - db_uri = parser.get("server", "db_uri", fallback=None) - if db_uri: + # Search all sections + DEFAULT (keys before any section header) for db_uri + sections_to_check = list(parser.sections()) + ["DEFAULT"] + for section in sections_to_check: + try: + db_uri = parser.get(section, "db_uri", fallback=None) + if db_uri and db_uri.startswith("postgresql"): + return _parse_db_uri(db_uri) + except Exception: + continue + + # Also check raw defaults (keys before any section) + db_uri = parser.defaults().get("db_uri") + if db_uri and db_uri.startswith("postgresql"): return _parse_db_uri(db_uri) - # Fallback: try [database] section with individual fields - if parser.has_section("database"): - return { - "host": parser.get("database", "host", fallback=None), - "port": int(parser.get("database", "port", fallback="5432")), - "user": parser.get("database", "user", fallback=None), - "password": parser.get("database", "password", fallback=None), - "dbname": parser.get("database", "name", fallback=None), - } return None @@ -308,11 +310,30 @@ async def discover_via_ssh( ssh_password=ssh_password, ) if not detected or not all([detected.get("host"), detected.get("user"), detected.get("dbname")]): - result.errors.append( - "DB connection not configured and could not be auto-detected from server config. " - "Fill in the DB connection fields on the object or ensure /etc/caps/config.ini " - "on the server contains a [server] db_uri field." - ) + # Diagnostic: try to read the config and list what sections/keys are there + try: + raw = await _read_remote_config( + host=obj.server_ip, + port=ssh_port, + username=obj.ssh_user, + password=ssh_password, + config_path="/etc/caps/config.ini", + ) + diag_parser = RawConfigParser(strict=False) + diag_parser.read_string(raw) + found_sections = diag_parser.sections() + found_keys = {s: list(diag_parser.options(s)) for s in found_sections} + result.errors.append( + f"db_uri not found in server config. " + f"Sections found: {found_sections}. " + f"Keys per section: {found_keys}. " + f"Fill in DB fields manually on the object." + ) + except Exception as diag_exc: + result.errors.append( + f"Could not read server config for auto-detection: {diag_exc}. " + f"Fill in DB fields manually on the object." + ) return result db_host = detected["host"] db_port = detected["port"]