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>
This commit is contained in:
parent
c104cc330c
commit
e965780684
|
|
@ -1113,39 +1113,45 @@ def get_login_url(container_name, target_server=None):
|
|||
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)"
|
||||
)
|
||||
# Run Python via bench venv inside the container to create a session
|
||||
# Uses heredoc to avoid escaping issues
|
||||
shell_cmd = """su frappe -c 'cd /home/frappe/frappe-bench && ./env/bin/python << "PYEOF"
|
||||
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, %s, %s, NOW(), %s)",
|
||||
(sid, "Administrator", "{}", "Active")
|
||||
)
|
||||
frappe.db.commit()
|
||||
frappe.destroy()
|
||||
print(sid)
|
||||
PYEOF
|
||||
'"""
|
||||
|
||||
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}\""
|
||||
result = _exec_in_container(target_server, container_name, shell_cmd, timeout=30)
|
||||
|
||||
# Simpler approach: run python3 directly
|
||||
result = _exec_in_container(target_server, container_name,
|
||||
["python3", "-c", python_cmd],
|
||||
timeout=30,
|
||||
)
|
||||
output = result.get("output", "").strip()
|
||||
|
||||
if result.get("return_code") != 0:
|
||||
return {
|
||||
"success": False,
|
||||
"error": result.get("output") or "Failed to generate login token",
|
||||
"error": 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"}
|
||||
# Get the last non-empty line (sid) — skip any Python warnings/output
|
||||
sid = ""
|
||||
for line in reversed(output.split("\n")):
|
||||
line = line.strip()
|
||||
if line and len(line) >= 20 and line.isalnum():
|
||||
sid = line
|
||||
break
|
||||
|
||||
if not sid:
|
||||
return {"success": False, "error": f"Could not parse sid from output: {output[:200]}"}
|
||||
|
||||
# Build the site URL
|
||||
host_domain = frappe.conf.get("host_domain", "host.jeyerp.az")
|
||||
site_url = f"http://{container_name}.{host_domain}"
|
||||
|
||||
|
|
@ -1153,5 +1159,4 @@ def get_login_url(container_name, target_server=None):
|
|||
"success": True,
|
||||
"url": site_url,
|
||||
"sid": sid,
|
||||
"login_url": f"{site_url}/api/method/login?sid={sid}",
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue