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:
|
if not target_server:
|
||||||
frappe.throw("Server name is required")
|
frappe.throw("Server name is required")
|
||||||
|
|
||||||
# Generate sid inside the container via bench execute
|
# Run Python via bench venv inside the container to create a session
|
||||||
# This Python snippet creates a session for Administrator and prints the sid
|
# Uses heredoc to avoid escaping issues
|
||||||
python_cmd = (
|
shell_cmd = """su frappe -c 'cd /home/frappe/frappe-bench && ./env/bin/python << "PYEOF"
|
||||||
"import frappe;"
|
import frappe
|
||||||
"frappe.init(site='frontend');"
|
frappe.init(site="frontend")
|
||||||
"frappe.connect();"
|
frappe.connect()
|
||||||
"sid = frappe.generate_hash();"
|
sid = frappe.generate_hash()
|
||||||
"frappe.db.sql("
|
frappe.db.sql(
|
||||||
"\"INSERT INTO `tabSessions` (`sid`, `user`, `data`, `lastused`, `status`) "
|
"INSERT INTO `tabSessions` (`sid`, `user`, `data`, `lastused`, `status`) "
|
||||||
"VALUES (%s, 'Administrator', '{}', NOW(), 'Active')\", (sid,));"
|
"VALUES (%s, %s, %s, NOW(), %s)",
|
||||||
"frappe.db.commit();"
|
(sid, "Administrator", "{}", "Active")
|
||||||
"print(sid)"
|
)
|
||||||
)
|
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
|
output = result.get("output", "").strip()
|
||||||
result = _exec_in_container(target_server, container_name,
|
|
||||||
["python3", "-c", python_cmd],
|
|
||||||
timeout=30,
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.get("return_code") != 0:
|
if result.get("return_code") != 0:
|
||||||
return {
|
return {
|
||||||
"success": False,
|
"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()
|
# Get the last non-empty line (sid) — skip any Python warnings/output
|
||||||
if not sid:
|
sid = ""
|
||||||
return {"success": False, "error": "Empty sid returned"}
|
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")
|
host_domain = frappe.conf.get("host_domain", "host.jeyerp.az")
|
||||||
site_url = f"http://{container_name}.{host_domain}"
|
site_url = f"http://{container_name}.{host_domain}"
|
||||||
|
|
||||||
|
|
@ -1153,5 +1159,4 @@ def get_login_url(container_name, target_server=None):
|
||||||
"success": True,
|
"success": True,
|
||||||
"url": site_url,
|
"url": site_url,
|
||||||
"sid": sid,
|
"sid": sid,
|
||||||
"login_url": f"{site_url}/api/method/login?sid={sid}",
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue