-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_production.sh
More file actions
441 lines (377 loc) · 12 KB
/
Copy pathsetup_production.sh
File metadata and controls
441 lines (377 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/bin/bash
set -e
echo "🚀 Hyperliquid Production Setup Script"
echo "======================================"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
INSTALL_DIR="/opt/hyperliquid-data"
SERVICE_USER="hyperliquid"
DATA_DIR="/var/lib/hyperliquid"
LOG_DIR="/var/log/hyperliquid"
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if running as root
if [[ $EUID -ne 0 ]]; then
print_error "This script must be run as root (use sudo)"
exit 1
fi
print_status "Starting production setup..."
# 1. Update system packages
print_status "Updating system packages..."
apt update && apt upgrade -y
# 2. Install essential packages
print_status "Installing essential packages..."
apt install -y \
build-essential cmake git curl wget \
python3 python3-pip python3-venv python3-dev \
zstd libzstd-dev pkg-config \
htop iotop nethogs tree \
nginx logrotate fail2ban \
postgresql-client redis-tools \
prometheus-node-exporter \
systemd-timesyncd \
ufw
# 3. Install Python monitoring dependencies
print_status "Installing Python monitoring dependencies..."
pip3 install psutil requests
# 4. Create service user
print_status "Creating service user..."
if ! id "$SERVICE_USER" &>/dev/null; then
useradd -r -s /bin/bash -d "$DATA_DIR" -m "$SERVICE_USER"
print_success "Created user: $SERVICE_USER"
else
print_warning "User $SERVICE_USER already exists"
fi
# 5. Create directories
print_status "Creating directories..."
mkdir -p "$INSTALL_DIR" "$DATA_DIR" "$LOG_DIR"
chown -R "$SERVICE_USER:$SERVICE_USER" "$DATA_DIR" "$LOG_DIR"
chmod 755 "$INSTALL_DIR"
# 6. Copy application files
print_status "Setting up application files..."
cp -r . "$INSTALL_DIR/"
chown -R "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR"
# 7. Build C++ application
print_status "Building C++ application..."
cd "$INSTALL_DIR"
sudo -u "$SERVICE_USER" bash -c "
mkdir -p build
cd build
cmake ..
make -j\$(nproc)
"
# 8. Create systemd service for data collection
print_status "Creating systemd service..."
cat > /etc/systemd/system/hyperliquid-data.service << EOF
[Unit]
Description=Hyperliquid Market Data Collection
After=network.target
Wants=network.target
[Service]
Type=simple
User=$SERVICE_USER
Group=$SERVICE_USER
WorkingDirectory=$INSTALL_DIR
Environment=PYTHONPATH=$INSTALL_DIR
ExecStart=/usr/bin/python3 $INSTALL_DIR/hyperliquid_client.py BTC,ETH 2 3
ExecReload=/bin/kill -HUP \$MAINPID
KillMode=mixed
KillSignal=SIGTERM
TimeoutStopSec=30
Restart=always
RestartSec=10
StandardOutput=append:$LOG_DIR/hyperliquid.log
StandardError=append:$LOG_DIR/hyperliquid.log
# Resource limits
MemoryMax=2G
CPUQuota=200%
# Security settings
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=$DATA_DIR $LOG_DIR
[Install]
WantedBy=multi-user.target
EOF
# 9. Create systemd service for monitoring
print_status "Creating monitoring service..."
cat > /etc/systemd/system/hyperliquid-monitor.service << EOF
[Unit]
Description=Hyperliquid Production Monitor
After=network.target hyperliquid-data.service
Wants=network.target
[Service]
Type=simple
User=$SERVICE_USER
Group=$SERVICE_USER
WorkingDirectory=$INSTALL_DIR
Environment=PYTHONPATH=$INSTALL_DIR
ExecStart=/usr/bin/python3 $INSTALL_DIR/production_monitor.py
Restart=always
RestartSec=30
StandardOutput=append:$LOG_DIR/monitor.log
StandardError=append:$LOG_DIR/monitor.log
# Resource limits
MemoryMax=512M
CPUQuota=50%
[Install]
WantedBy=multi-user.target
EOF
# 10. Create log rotation configuration
print_status "Setting up log rotation..."
cat > /etc/logrotate.d/hyperliquid << EOF
$LOG_DIR/*.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
create 644 $SERVICE_USER $SERVICE_USER
postrotate
systemctl reload hyperliquid-data hyperliquid-monitor
endscript
}
$DATA_DIR/*/*.zst {
weekly
rotate 52
compress
delaycompress
missingok
notifempty
copytruncate
}
EOF
# 11. Configure firewall
print_status "Configuring firewall..."
ufw --force enable
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw allow 80/tcp # HTTP for status page
ufw allow 443/tcp # HTTPS for status page
ufw allow 9100/tcp # Prometheus node exporter
# 12. Configure time synchronization
print_status "Configuring time synchronization..."
systemctl enable systemd-timesyncd
systemctl start systemd-timesyncd
# 13. Set up Prometheus node exporter
print_status "Configuring Prometheus node exporter..."
systemctl enable prometheus-node-exporter
systemctl start prometheus-node-exporter
# 14. Create backup script
print_status "Creating backup script..."
cat > "$INSTALL_DIR/backup_data.sh" << 'EOF'
#!/bin/bash
# Backup script for Hyperliquid data
BACKUP_DIR="/var/backups/hyperliquid"
DATA_DIR="/var/lib/hyperliquid"
RETENTION_DAYS=7
# Create backup directory
mkdir -p "$BACKUP_DIR"
# Get current date
DATE=$(date +%Y%m%d_%H%M%S)
# Create backup
tar -czf "$BACKUP_DIR/hyperliquid_data_$DATE.tar.gz" -C "$DATA_DIR" .
# Remove old backups
find "$BACKUP_DIR" -name "hyperliquid_data_*.tar.gz" -mtime +$RETENTION_DAYS -delete
echo "Backup completed: $BACKUP_DIR/hyperliquid_data_$DATE.tar.gz"
EOF
chmod +x "$INSTALL_DIR/backup_data.sh"
chown "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR/backup_data.sh"
# 15. Create cron job for daily backups
print_status "Setting up daily backups..."
(crontab -u "$SERVICE_USER" -l 2>/dev/null; echo "0 2 * * * $INSTALL_DIR/backup_data.sh") | crontab -u "$SERVICE_USER" -
# 16. Create status dashboard script
print_status "Creating status dashboard..."
cat > "$INSTALL_DIR/status_dashboard.py" << 'EOF'
#!/usr/bin/env python3
"""Simple web dashboard for system status"""
import json
import time
import os
from http.server import HTTPServer, BaseHTTPRequestHandler
from datetime import datetime
import subprocess
class StatusHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
# Get system status
try:
with open('health_check_results.json', 'r') as f:
status = json.load(f)
except:
status = {"overall_status": "unknown", "timestamp": datetime.now().isoformat()}
# Generate HTML
html = f"""
<!DOCTYPE html>
<html>
<head>
<title>Hyperliquid Data Collection Status</title>
<meta http-equiv="refresh" content="30">
<style>
body {{ font-family: Arial, sans-serif; margin: 40px; }}
.status-healthy {{ color: green; }}
.status-warning {{ color: orange; }}
.status-critical {{ color: red; }}
.metric {{ margin: 10px 0; }}
.timestamp {{ color: gray; font-size: 0.9em; }}
</style>
</head>
<body>
<h1>Hyperliquid Data Collection Status</h1>
<div class="status-{status.get('overall_status', 'unknown')}">
<h2>Overall Status: {status.get('overall_status', 'Unknown').upper()}</h2>
</div>
<div class="timestamp">Last updated: {status.get('timestamp', 'Unknown')}</div>
<h3>System Metrics</h3>
<pre>{json.dumps(status, indent=2)}</pre>
</body>
</html>
"""
self.wfile.write(html.encode())
elif self.path == '/api/status':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
try:
with open('health_check_results.json', 'r') as f:
status = f.read()
self.wfile.write(status.encode())
except:
self.wfile.write(b'{"error": "Status file not found"}')
if __name__ == '__main__':
server = HTTPServer(('0.0.0.0', 8080), StatusHandler)
print("Status dashboard running on http://localhost:8080")
server.serve_forever()
EOF
chmod +x "$INSTALL_DIR/status_dashboard.py"
chown "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR/status_dashboard.py"
# 17. Create simple Nginx configuration for status page
print_status "Setting up Nginx reverse proxy..."
cat > /etc/nginx/sites-available/hyperliquid-status << EOF
server {
listen 80;
server_name _;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
}
location /metrics {
proxy_pass http://localhost:9100;
}
}
EOF
ln -sf /etc/nginx/sites-available/hyperliquid-status /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
# 18. Test Nginx configuration
nginx -t && systemctl reload nginx
# 19. Enable and start services
print_status "Enabling and starting services..."
systemctl daemon-reload
systemctl enable hyperliquid-data
systemctl enable hyperliquid-monitor
systemctl enable nginx
# 20. Create configuration templates
print_status "Creating configuration templates..."
cat > "$INSTALL_DIR/monitor_config.json" << EOF
{
"email": {
"enabled": false,
"smtp_server": "smtp.gmail.com",
"smtp_port": 587,
"username": "your-email@gmail.com",
"password": "your-app-password",
"to_addresses": ["alerts@yourcompany.com"]
},
"slack": {
"enabled": false,
"webhook_url": "https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK"
},
"thresholds": {
"max_gap_minutes": 5,
"min_free_disk_gb": 10,
"max_cpu_percent": 80,
"max_memory_percent": 85,
"max_latency_ms": 1000
},
"currencies": ["BTC", "ETH"],
"check_interval_seconds": 60
}
EOF
chown "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR/monitor_config.json"
# 21. Create startup script
print_status "Creating startup script..."
cat > "$INSTALL_DIR/start_production.sh" << EOF
#!/bin/bash
echo "Starting Hyperliquid Production Services..."
# Start data collection
systemctl start hyperliquid-data
# Start monitoring
systemctl start hyperliquid-monitor
# Start status dashboard (optional)
# sudo -u $SERVICE_USER python3 $INSTALL_DIR/status_dashboard.py &
echo "Services started. Check status with:"
echo " systemctl status hyperliquid-data"
echo " systemctl status hyperliquid-monitor"
echo " journalctl -u hyperliquid-data -f"
echo ""
echo "Status dashboard: http://\$(curl -s ifconfig.me)"
echo "Logs: tail -f $LOG_DIR/hyperliquid.log"
EOF
chmod +x "$INSTALL_DIR/start_production.sh"
# 22. Final permissions
chown -R "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR"
print_success "Production setup completed!"
echo ""
echo "🎉 SETUP COMPLETE!"
echo "=================="
echo ""
echo "📁 Installation directory: $INSTALL_DIR"
echo "📊 Data directory: $DATA_DIR"
echo "📝 Log directory: $LOG_DIR"
echo "👤 Service user: $SERVICE_USER"
echo ""
echo "🚀 To start services:"
echo " sudo systemctl start hyperliquid-data"
echo " sudo systemctl start hyperliquid-monitor"
echo ""
echo "📊 To check status:"
echo " sudo systemctl status hyperliquid-data"
echo " sudo journalctl -u hyperliquid-data -f"
echo ""
echo "🌐 Status dashboard: http://$(curl -s ifconfig.me 2>/dev/null || echo 'your-server-ip')"
echo ""
echo "⚙️ Configuration files:"
echo " - Monitor config: $INSTALL_DIR/monitor_config.json"
echo " - Service config: /etc/systemd/system/hyperliquid-*.service"
echo ""
echo "🔧 Next steps:"
echo " 1. Configure email/Slack alerts in monitor_config.json"
echo " 2. Start services: sudo $INSTALL_DIR/start_production.sh"
echo " 3. Monitor logs: tail -f $LOG_DIR/hyperliquid.log"
echo ""
print_warning "IMPORTANT: Configure firewall rules for your specific needs!"
print_warning "IMPORTANT: Set up SSL certificates for production use!"
echo ""
EOF