Compare commits

...

2 Commits

Author SHA1 Message Date
js 2719c2276c refactor: use HTTP login API instead of incus exec for admin login
Same approach as Frappe Cloud (press) — POST to site's /api/method/login
with credentials, extract sid from response cookies.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 15:44:53 +00:00
js e965780684 fix: use bench venv python for login token generation inside container
The previous approach used bare python3 which doesn't have frappe.
Now uses ./env/bin/python with heredoc to avoid escaping issues.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 15:27:20 +00:00
1 changed files with 25 additions and 35 deletions

View File

@ -1107,51 +1107,41 @@ def deploy_container_full(container_name, image="erp", pool_name="default", volu
@frappe.whitelist()
def get_login_url(container_name, target_server=None):
"""Generate a one-time login URL for a container site (login as Administrator)."""
"""Generate a login URL for a container site (login as Administrator).
Uses direct HTTP POST to the site's /api/method/login endpoint,
same approach as Frappe Cloud (press).
"""
_check_admin_permission()
if not target_server:
frappe.throw("Server name is required")
# Generate sid inside the container via bench execute
# This Python snippet creates a session for Administrator and prints the sid
python_cmd = (
"import frappe;"
"frappe.init(site='frontend');"
"frappe.connect();"
"sid = frappe.generate_hash();"
"frappe.db.sql("
"\"INSERT INTO `tabSessions` (`sid`, `user`, `data`, `lastused`, `status`) "
"VALUES (%s, 'Administrator', '{}', NOW(), 'Active')\", (sid,));"
"frappe.db.commit();"
"print(sid)"
)
cmd = f"su frappe -c \"cd /home/frappe/frappe-bench && bench --site frontend execute 'frappe.utils.execute_in_shell' 2>/dev/null\" 2>/dev/null || python3 -c \"{python_cmd}\""
# Simpler approach: run python3 directly
result = _exec_in_container(target_server, container_name,
["python3", "-c", python_cmd],
timeout=30,
)
if result.get("return_code") != 0:
return {
"success": False,
"error": result.get("output") or "Failed to generate login token",
}
sid = result.get("output", "").strip().split("\n")[-1].strip()
if not sid:
return {"success": False, "error": "Empty sid returned"}
# Build the site URL
host_domain = frappe.conf.get("host_domain", "host.jeyerp.az")
site_url = f"http://{container_name}.{host_domain}"
# POST to site's login API with admin credentials (like Frappe Press)
try:
response = requests.post(
f"{site_url}/api/method/login",
data={"usr": "Administrator", "pwd": "admin"},
timeout=10,
)
except requests.exceptions.ConnectionError:
return {"success": False, "error": f"Cannot connect to {site_url}"}
except requests.exceptions.Timeout:
return {"success": False, "error": f"Connection to {site_url} timed out"}
sid = response.cookies.get("sid")
if not sid or sid == "Guest":
return {
"success": False,
"error": "Login failed. Check that site is running and admin password is correct.",
}
return {
"success": True,
"url": site_url,
"sid": sid,
"login_url": f"{site_url}/api/method/login?sid={sid}",
}