Compare commits
2 Commits
c104cc330c
...
2719c2276c
| Author | SHA1 | Date |
|---|---|---|
|
|
2719c2276c | |
|
|
e965780684 |
|
|
@ -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}",
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue