Update deploy script with fixed server.py (serve_forever + installed flag)
This commit is contained in:
+51
-54
@@ -1,11 +1,10 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
echo "[1/5] Creating agent directory..."
|
||||
mkdir -p /opt/xiaoxia-cmd-agent
|
||||
|
||||
echo "[2/5] Writing server.py..."
|
||||
cat > /opt/xiaoxia-cmd-agent/server.py << 'PYEOF'
|
||||
cat > /opt/xiaoxia-cmd-agent/server.py << 'SERVEREOF'
|
||||
#!/usr/bin/env python3
|
||||
import http.server, subprocess, json, os, sys, secrets
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
@@ -143,75 +142,73 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||
except Exception as e: self._j({"error":str(e)},500)
|
||||
|
||||
def main():
|
||||
global AUTH_TOKEN
|
||||
installed_flag = "/etc/xiaoxia-cmd-agent.installed"
|
||||
if not os.path.exists(installed_flag):
|
||||
with open("/etc/xiaoxia-cmd-agent.token","w") as f: f.write(AUTH_TOKEN)
|
||||
os.chmod("/etc/xiaoxia-cmd-agent.token", 0o600)
|
||||
svc = "[Unit]\nDescription=Xiaoxia Command Agent\nAfter=network.target\n[Service]\nType=simple\nExecStart=/usr/bin/python3 /opt/xiaoxia-cmd-agent/server.py\nRestart=always\nRestartSec=5\n[Install]\nWantedBy=multi-user.target\n"
|
||||
with open("/etc/systemd/system/xiaoxia-cmd-agent.service","w") as f: f.write(svc)
|
||||
os.system("systemctl daemon-reload && systemctl enable xiaoxia-cmd-agent")
|
||||
with open(installed_flag,"w") as f: f.write("1")
|
||||
print(f"Service installed. TOKEN: {AUTH_TOKEN}")
|
||||
else:
|
||||
with open("/etc/xiaoxia-cmd-agent.token") as f:
|
||||
AUTH_TOKEN = f.read().strip()
|
||||
print(f"Using existing token: {AUTH_TOKEN}")
|
||||
server = http.server.HTTPServer(("127.0.0.1", LISTEN_PORT), Handler)
|
||||
with open("/etc/xiaoxia-cmd-agent.token","w") as f: f.write(AUTH_TOKEN)
|
||||
os.chmod("/etc/xiaoxia-cmd-agent.token", 0o600)
|
||||
svc = """[Unit]
|
||||
Description=Xiaoxia Command Agent
|
||||
After=network.target
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/usr/bin/python3 /opt/xiaoxia-cmd-agent/server.py
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
"""
|
||||
with open("/etc/systemd/system/xiaoxia-cmd-agent.service","w") as f: f.write(svc)
|
||||
os.system("systemctl daemon-reload && systemctl enable xiaoxia-cmd-agent && systemctl restart xiaoxia-cmd-agent")
|
||||
print(f"TOKEN: {AUTH_TOKEN}")
|
||||
print(f"Listening: 127.0.0.1:{LISTEN_PORT}")
|
||||
print(f"Service started")
|
||||
server.serve_forever()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
PYEOF
|
||||
|
||||
SERVEREOF
|
||||
|
||||
echo "[3/5] Starting agent service..."
|
||||
cd /opt/xiaoxia-cmd-agent
|
||||
python3 server.py &
|
||||
python3 /opt/xiaoxia-cmd-agent/server.py &
|
||||
sleep 2
|
||||
TOKEN=$(cat /etc/xiaoxia-cmd-agent.token)
|
||||
echo "Agent running. Token: $TOKEN"
|
||||
echo "TOKEN: $TOKEN"
|
||||
|
||||
echo "[4/5] Configuring Nginx reverse proxy..."
|
||||
mkdir -p /etc/nginx/ssl
|
||||
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \
|
||||
-keyout /etc/nginx/ssl/cmd-agent.key \
|
||||
-out /etc/nginx/ssl/cmd-agent.crt \
|
||||
-subj "/CN=cmd-agent" 2>/dev/null
|
||||
# Add /cmd-agent/ location to existing 443 server block
|
||||
python3 -c "
|
||||
import os
|
||||
conf_path = '/etc/nginx/sites-enabled/git-xiaoxia.conf'
|
||||
if not os.path.exists(conf_path):
|
||||
# Try default config
|
||||
for d in ['/etc/nginx/conf.d/', '/etc/nginx/sites-enabled/']:
|
||||
for f in os.listdir(d):
|
||||
if f.endswith('.conf'):
|
||||
conf_path = os.path.join(d, f)
|
||||
break
|
||||
|
||||
# Check nginx sites-available structure
|
||||
if [ -d /etc/nginx/sites-available ]; then
|
||||
CONF=/etc/nginx/sites-available/cmd-agent
|
||||
LINK=/etc/nginx/sites-enabled/cmd-agent
|
||||
else
|
||||
CONF=/etc/nginx/conf.d/cmd-agent.conf
|
||||
LINK=""
|
||||
fi
|
||||
with open(conf_path) as f:
|
||||
content = f.read()
|
||||
|
||||
cat > "$CONF" << NGINXEOF
|
||||
server {
|
||||
listen 8443 ssl;
|
||||
server_name _;
|
||||
ssl_certificate /etc/nginx/ssl/cmd-agent.crt;
|
||||
ssl_certificate_key /etc/nginx/ssl/cmd-agent.key;
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:18888;
|
||||
if '/cmd-agent/' not in content:
|
||||
location_block = """
|
||||
location /cmd-agent/ {
|
||||
proxy_pass http://127.0.0.1:18888/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host \$host;
|
||||
proxy_set_header X-Real-IP \$remote_addr;
|
||||
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||
proxy_read_timeout 120s;
|
||||
client_max_body_size 50M;
|
||||
}
|
||||
}
|
||||
NGINXEOF
|
||||
|
||||
[ -n "$LINK" ] && ln -sf "$CONF" "$LINK"
|
||||
nginx -t 2>&1 && systemctl reload nginx
|
||||
"""
|
||||
content = content.replace(' location / {', location_block + '\n location / {')
|
||||
with open(conf_path, 'w') as f:
|
||||
f.write(content)
|
||||
print('Nginx config updated')
|
||||
else:
|
||||
print('Already configured')
|
||||
"
|
||||
|
||||
nginx -t && systemctl reload nginx
|
||||
echo "[5/5] Testing..."
|
||||
curl -s -H "Authorization: $TOKEN" http://127.0.0.1:18888/status
|
||||
echo ""
|
||||
echo "=== DEPLOY COMPLETE ==="
|
||||
echo "Token: $TOKEN"
|
||||
echo "Local: http://127.0.0.1:18888"
|
||||
echo "Nginx: https://127.0.0.1:8443"
|
||||
echo "DONE. Token: $TOKEN"
|
||||
|
||||
Reference in New Issue
Block a user