Files
ai-customer-service/guest-cs-stack/_patch_remote_app.py
T
2026-08-15 14:51:51 +08:00

61 lines
2.0 KiB
Python

"""Patch running guest-chat container: confirm admin + dynamic base_url in app.py."""
import os
import textwrap
import paramiko
pw = os.environ["DEPLOY_PASSWORD"]
LOCAL_APP = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "flask", "app.py"
)
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("20.191.152.26", username="wakka", password=pw, timeout=30)
# 1) confirm admin
confirm = r"""
sudo docker exec guest-chat bundle exec rails runner "
u = User.find_by(email: 'admin@localhost.local')
u.confirm if u&.respond_to?(:confirm)
u.update!(confirmed_at: Time.current) if u && u.respond_to?(:confirmed_at) && u.confirmed_at.nil?
puts u ? \"confirmed #{u.email}\" : 'no user'
"
"""
print(">>> confirm admin")
_, o, _ = c.exec_command(confirm, timeout=120)
print(o.read().decode())
# 2) upload patched app.py into container
sftp = c.open_sftp()
remote_tmp = "/home/wakka/app.py.patch"
with open(LOCAL_APP, "rb") as fh:
data = fh.read().replace(b"\r\n", b"\n")
with sftp.file(remote_tmp, "w") as rf:
rf.write(data)
sftp.close()
patch = f"""
sudo docker cp {remote_tmp} guest-chat:/opt/guest-cs/app.py
sudo docker exec guest-chat supervisorctl restart gunicorn 2>/dev/null || sudo docker restart guest-chat
sleep 15
curl -s http://127.0.0.1:7777/guest-customer-service | grep -o 'base_url[^,]*' | head -1
"""
print(">>> patch app.py")
_, o, e = c.exec_command(patch, timeout=120)
print(o.read().decode())
print(e.read().decode())
# 3) test guest-chat/new
test = r"""
rm -f /tmp/gc.jar /tmp/g.html
curl -s -c /tmp/gc.jar http://127.0.0.1:7777/guest-customer-service -o /tmp/g.html
csrf=$(python3 -c "import re; print(re.search(r'\"csrf_token\":\s*\"([^\"]+)\"', open('/tmp/g.html').read()).group(1))")
curl -s -b /tmp/gc.jar -X POST http://127.0.0.1:7777/guest-chat/new -H "Content-Type: application/json" -H "X-CSRF-Token: $csrf" -d '{}' | head -c 200
echo
"""
print(">>> test guest-chat/new")
_, o, _ = c.exec_command(test, timeout=60)
print(o.read().decode())
c.close()