Awesome-Developer commited on
Commit
57e8d93
Β·
verified Β·
1 Parent(s): fcf06cf

Create entrypoint.sh

Browse files
Files changed (1) hide show
  1. entrypoint.sh +107 -0
entrypoint.sh ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e
3
+
4
+ echo "πŸ“¦ Installing Python dependencies..."
5
+ pip install --break-system-packages requests
6
+
7
+ # --- GENERATE server.py (Foreground Web Server for HF Health Check) ---
8
+ cat > /app/server.py << 'PYEOF'
9
+ from http.server import HTTPServer, BaseHTTPRequestHandler
10
+ from datetime import datetime
11
+
12
+ class Handler(BaseHTTPRequestHandler):
13
+ def do_GET(self):
14
+ self.send_response(200)
15
+ self.send_header('Content-type', 'text/html')
16
+ self.end_headers()
17
+ html = f"""
18
+ <html><body style="font-family:sans-serif;text-align:center;padding-top:50px;">
19
+ <h1>❀️ Running</h1>
20
+ <p>Space active. Keep-alive & SSHX tunnel operational.</p>
21
+ <small>{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}</small>
22
+ </body></html>"""
23
+ self.wfile.write(html.encode())
24
+ def log_message(self, format, *args):
25
+ pass
26
+
27
+ if __name__ == "__main__":
28
+ print("βœ… Web server running on port 7860")
29
+ HTTPServer(("0.0.0.0", 7860), Handler).serve_forever()
30
+ PYEOF
31
+
32
+ # --- GENERATE keep_alive.py (Background Randomized Pinger) ---
33
+ cat > /app/keep_alive.py << 'PYEOF'
34
+ import requests, time, random
35
+ from datetime import datetime, timedelta
36
+
37
+ SPACE_URL = "https://awesome-developer-meow.hf.space"
38
+ MIN_HOURS, MAX_HOURS = 20, 46 # Must be < 48h to prevent pause
39
+ LOG_FILE = "/data/ping.log"
40
+
41
+ def log_ping(status, msg=""):
42
+ ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
43
+ try:
44
+ with open(LOG_FILE, "a") as f:
45
+ f.write(f"[{ts}] {status} {msg}\n")
46
+ except Exception:
47
+ pass
48
+
49
+ def ping_space():
50
+ try:
51
+ r = requests.get(SPACE_URL, timeout=30)
52
+ if r.status_code == 200:
53
+ log_ping("SUCCESS", f"Status: {r.status_code}")
54
+ return True
55
+ else:
56
+ log_ping("ERROR", f"Unexpected status: {r.status_code}")
57
+ return False
58
+ except Exception as e:
59
+ log_ping("ERROR", str(e))
60
+ return False
61
+
62
+ def main():
63
+ log_ping("STARTUP", "Keep-alive initialized")
64
+ ping_space()
65
+ while True:
66
+ hours = random.uniform(MIN_HOURS, MAX_HOURS)
67
+ sleep_secs = int(hours * 3600)
68
+ next_time = datetime.now() + timedelta(seconds=sleep_secs)
69
+ log_ping("SLEEPING", f"Next in {hours:.2f}h at {next_time.strftime('%H:%M:%S')}")
70
+ time.sleep(sleep_secs)
71
+ ping_space()
72
+
73
+ if __name__ == "__main__":
74
+ main()
75
+ PYEOF
76
+
77
+ # Start SSH service (required for sshx)
78
+ service ssh start 2>/dev/null || true
79
+
80
+ # --- START SSHX AND CAPTURE URL ---
81
+ echo "πŸ”— Starting sshx tunnel..."
82
+ nohup sshx > /tmp/sshx_output.log 2>&1 &
83
+ SSHX_PID=$!
84
+ disown $SSHX_PID
85
+
86
+ for i in {1..15}; do
87
+ if grep -q "sshx.io" /tmp/sshx_output.log; then
88
+ SSHX_URL=$(grep -oP 'https://sshx\.io/[^\s]+' /tmp/sshx_output.log | head -1)
89
+ echo "$SSHX_URL" > /data/sshx_url.txt
90
+ echo "βœ… SSHX URL saved to /data/sshx_url.txt: $SSHX_URL"
91
+ break
92
+ fi
93
+ sleep 1
94
+ done
95
+
96
+ if [ ! -f /data/sshx_url.txt ]; then
97
+ echo "⚠️ SSHX did not connect within 15s. Check /tmp/sshx_output.log"
98
+ fi
99
+
100
+ # --- START SILENT BACKGROUND KEEP-ALIVE PINGER ---
101
+ echo "πŸ”‡ Starting silent background keep-alive..."
102
+ nohup python3 /app/keep_alive.py > /dev/null 2>&1 &
103
+ disown
104
+
105
+ # --- START FOREGROUND WEB SERVER (HF HEALTH CHECK) ---
106
+ echo "πŸš€ Starting main web server on :7860..."
107
+ exec python3 /app/server.py