-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlxc-autoscale-install.sh
More file actions
442 lines (368 loc) · 14.7 KB
/
Copy pathlxc-autoscale-install.sh
File metadata and controls
442 lines (368 loc) · 14.7 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
442
#!/usr/bin/env bash
# Copyright (c) 2025 droltr
# License: MIT
# https://github.com/droltr/lxc-auto-scaler
# ==============================================================================
# LXC AUTOSCALE INSTALL SCRIPT
# ==============================================================================
#
# This script runs INSIDE the LXC container
# Called automatically by build.func after container creation
#
# ==============================================================================
set -e
# Source the functions file from build.func
if [[ -n "${FUNCTIONS_FILE_PATH}" ]]; then
source <(echo "${FUNCTIONS_FILE_PATH}")
fi
# ==============================================================================
# SECTION 1: SYSTEM UPDATE
# ==============================================================================
msg_info "Updating container system"
$STD apt-get update
$STD apt-get -y upgrade
msg_ok "System updated"
# ==============================================================================
# SECTION 2: LOCALE CONFIGURATION
# ==============================================================================
msg_info "Configuring locale: ${LOCALE_NAME:-English}"
$STD apt-get install -y locales
# Use locale from parent script or default to en_US.UTF-8
SELECTED_LOCALE="${SELECTED_LOCALE:-en_US.UTF-8}"
if [[ "$SELECTED_LOCALE" == "tr_TR.UTF-8" ]]; then
echo "tr_TR.UTF-8 UTF-8" >> /etc/locale.gen
fi
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
$STD locale-gen
$STD update-locale LANG=$SELECTED_LOCALE
export LANG=$SELECTED_LOCALE
export LC_ALL=$SELECTED_LOCALE
msg_ok "Locale configured: ${LOCALE_NAME:-English}"
# ==============================================================================
# SECTION 3: INSTALL DEPENDENCIES
# ==============================================================================
msg_info "Installing dependencies"
$STD apt-get install -y \
curl \
git \
python3 \
python3-pip \
python3-yaml \
python3-requests \
python3-paramiko \
python3-cryptography \
python3-flask \
python3-flask-cors \
openssh-client \
nginx \
ca-certificates \
gnupg \
build-essential \
python3-dev
msg_ok "Dependencies installed"
# Install Flask-SocketIO via pip (not available in Debian repos)
msg_info "Installing Flask-SocketIO"
$STD pip3 install flask-socketio --break-system-packages
msg_ok "Flask-SocketIO installed"
# ==============================================================================
# SECTION 4: SSH KEY GENERATION
# ==============================================================================
msg_info "Creating SSH key"
mkdir -p /root/.ssh
ssh-keygen -t rsa -b 4096 -f /root/.ssh/id_rsa -N "" -q
SSH_PUBLIC_KEY=$(cat /root/.ssh/id_rsa.pub)
msg_ok "SSH key created"
# ==============================================================================
# SECTION 5: CLONE REPOSITORY
# ==============================================================================
msg_info "Cloning LXC AutoScale repository"
cd /opt
$STD git clone https://github.com/fabriziosalmi/proxmox-lxc-autoscale.git lxc-autoscale
msg_ok "Repository cloned"
# ==============================================================================
# SECTION 6: CREATE DIRECTORIES
# ==============================================================================
msg_info "Creating directories"
mkdir -p /etc/lxc_autoscale
mkdir -p /var/lib/lxc_autoscale/backups
mkdir -p /var/log
msg_ok "Directories created"
# ==============================================================================
# SECTION 7: SETUP APPLICATION
# ==============================================================================
msg_info "Setting up LXC AutoScale"
cp /opt/lxc-autoscale/lxc_autoscale/lxc_autoscale.py /usr/local/bin/
chmod +x /usr/local/bin/lxc_autoscale.py
msg_ok "Application setup complete"
# ==============================================================================
# SECTION 8: CREATE CONFIGURATION
# ==============================================================================
msg_info "Creating configuration file"
# Get Proxmox host IP
HOST_IP=$(ip route get 1 | awk '{print $7}' | head -1)
# Use detected containers from parent script or empty array
DETECTED_LXC="${DETECTED_LXC:-}"
if [[ -n "$DETECTED_LXC" ]]; then
LXC_ARRAY="[$DETECTED_LXC]"
else
LXC_ARRAY="[]"
fi
# IMPORTANT: Using 'DEFAULT' (singular) not 'DEFAULTS' (plural)
# Reason: lxc_utils.py line 69 expects config.get('DEFAULT', {})
# This is a bug in the upstream project where example config uses 'DEFAULTS'
# but the Python code looks for 'DEFAULT'
# Issue reported to: https://github.com/fabriziosalmi/proxmox-lxc-autoscale
cat > /etc/lxc_autoscale/lxc_autoscale.yaml << EOF
DEFAULT:
log_file: /var/log/lxc_autoscale.log
lock_file: /var/lock/lxc_autoscale.lock
backup_dir: /var/lib/lxc_autoscale/backups
reserve_cpu_percent: 10
reserve_memory_mb: 2048
off_peak_start: 22
off_peak_end: 6
behaviour: normal
cpu_upper_threshold: 80
cpu_lower_threshold: 20
memory_upper_threshold: 70
memory_lower_threshold: 20
min_cores: 1
max_cores: 4
min_memory: 512
max_memory: 4096
check_interval: 300
use_remote_proxmox: true
ssh_user: root
ssh_key: /root/.ssh/id_rsa
proxmox_host: ${HOST_IP}
TIER_DEFAULT:
cpu_upper_threshold: 80
cpu_lower_threshold: 20
memory_upper_threshold: 70
memory_lower_threshold: 20
min_cores: 1
max_cores: 4
min_memory: 512
max_memory: 4096
lxc_containers: ${LXC_ARRAY}
EOF
if [[ -n "$DETECTED_LXC" ]]; then
msg_ok "Configuration created with detected containers: $DETECTED_LXC"
else
msg_ok "Configuration created (containers must be added manually to TIER_DEFAULT)"
fi
# ==============================================================================
# SECTION 9: CREATE SYSTEMD SERVICE
# ==============================================================================
msg_info "Creating systemd service"
cat > /etc/systemd/system/lxc_autoscale.service << 'EOFSERVICE'
[Unit]
Description=LXC AutoScale Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/opt/lxc-autoscale/lxc_autoscale
ExecStart=/usr/bin/python3 lxc_autoscale.py
Restart=on-failure
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOFSERVICE
$STD systemctl daemon-reload
$STD systemctl enable lxc_autoscale.service
$STD systemctl start lxc_autoscale.service
msg_ok "Service created and started"
# ==============================================================================
# SECTION 10: SETUP ORIGINAL FLASK WEB INTERFACE
# ==============================================================================
msg_info "Setting up original Flask web interface"
# Check if original UI exists in repo
if [ -d "/opt/lxc-autoscale/lxc_autoscale/ui" ] && [ -f "/opt/lxc-autoscale/lxc_autoscale/ui/lxc_autoscale_ui.py" ]; then
msg_info "Installing original Flask UI from repository"
# Create UI directory
mkdir -p /var/www/lxc-autoscale
# Copy entire UI directory
cp -r /opt/lxc-autoscale/lxc_autoscale/ui/* /var/www/lxc-autoscale/
# Fix Flask-SocketIO production mode issue
msg_info "Patching Flask UI for production mode"
sed -i 's/socketio.run(app, host/socketio.run(app, allow_unsafe_werkzeug=True, host/' /var/www/lxc-autoscale/lxc_autoscale_ui.py
# Set proper permissions
chown -R www-data:www-data /var/www/lxc-autoscale
chmod -R 755 /var/www/lxc-autoscale
find /var/www/lxc-autoscale -type f -exec chmod 644 {} \;
chmod +x /var/www/lxc-autoscale/lxc_autoscale_ui.py
# Create systemd service for Flask UI
cat > /etc/systemd/system/lxc_autoscale_ui.service << 'EOFUISERVICE'
[Unit]
Description=LXC AutoScale Web UI (Flask)
After=network.target lxc_autoscale.service
Wants=lxc_autoscale.service
[Service]
Type=simple
User=root
Group=root
Environment="PYTHONUNBUFFERED=1"
ExecStart=/bin/bash -c 'cd /var/www/lxc-autoscale && /usr/bin/python3 -u lxc_autoscale_ui.py'
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOFUISERVICE
# Enable and start Flask UI service
$STD systemctl daemon-reload
$STD systemctl enable lxc_autoscale_ui.service
$STD systemctl start lxc_autoscale_ui.service
msg_ok "Original Flask UI installed and started"
else
msg_error "Original UI not found in repository"
exit 1
fi
# ==============================================================================
# SECTION 11: CONFIGURE NGINX AS REVERSE PROXY
# ==============================================================================
msg_info "Configuring Nginx as reverse proxy for Flask UI"
cat > /etc/nginx/sites-available/lxc-autoscale << 'EOFNGINX'
server {
listen 8080;
server_name _;
# Proxy to Flask UI running on port 5000
location / {
proxy_pass http://127.0.0.1:5000;
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;
# WebSocket support (if needed)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# Direct access to static files (optimization)
location /static/ {
alias /var/www/lxc-autoscale/static/;
expires 30d;
add_header Cache-Control "public, immutable";
}
# API endpoints for logs and metrics
location /api/logs {
alias /var/log/lxc_autoscale.log;
default_type text/plain;
add_header Content-Type 'text/plain; charset=utf-8';
}
location /api/metrics {
alias /var/log/lxc_autoscale.json;
default_type application/json;
add_header Content-Type 'application/json; charset=utf-8';
}
# Logging
access_log /var/log/nginx/lxc_autoscale_access.log;
error_log /var/log/nginx/lxc_autoscale_error.log;
}
EOFNGINX
ln -sf /etc/nginx/sites-available/lxc-autoscale /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
$STD systemctl restart nginx
msg_ok "Nginx configured as reverse proxy"
# ==============================================================================
# SECTION 12: DISPLAY SSH KEY
# ==============================================================================
msg_info "Installation Complete!"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " SSH Public Key (Add to Proxmox host):"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
cat /root/.ssh/id_rsa.pub
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Run this command on Proxmox host:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "echo '$(cat /root/.ssh/id_rsa.pub)' >> /root/.ssh/authorized_keys"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if [[ -n "$DETECTED_LXC" ]]; then
echo " Detected containers: $DETECTED_LXC"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
fi
echo ""
# ==============================================================================
# CLEANUP
# ==============================================================================
$STD apt-get autoremove -y
$STD apt-get autoclean -y
msg_ok "LXC AutoScale installation completed successfully!"
# ==============================================================================
# SECTION 13: FINAL INFORMATION DISPLAY
# ==============================================================================
# Get container IP
CONTAINER_IP=$(hostname -I | awk '{print $1}')
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " 🎉 LXC AutoScale Installation Complete!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo " 📍 Container IP: ${CONTAINER_IP}"
echo ""
echo " 🌐 Web Interface (Flask UI):"
echo " http://${CONTAINER_IP}:8080"
echo ""
echo " 📁 Configuration:"
echo " /etc/lxc_autoscale/lxc_autoscale.yaml"
echo ""
echo " 🔧 AutoScale Service:"
echo " systemctl status lxc_autoscale"
echo " systemctl restart lxc_autoscale"
echo " journalctl -u lxc_autoscale -f"
echo ""
echo " 🌐 Web UI Service:"
echo " systemctl status lxc_autoscale_ui"
echo " systemctl restart lxc_autoscale_ui"
echo " journalctl -u lxc_autoscale_ui -f"
echo ""
echo " 📊 View Logs:"
echo " tail -f /var/log/lxc_autoscale.log"
echo ""
echo " 🔑 SSH to container:"
echo " ssh root@${CONTAINER_IP}"
echo ""
if [[ -n "$DETECTED_LXC" ]]; then
echo " 📦 Detected Containers: ${DETECTED_LXC}"
echo ""
fi
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# ==============================================================================
# SECTION 14: CREATE MOTD (LOGIN BANNER)
# ==============================================================================
msg_info "Creating login banner (MOTD)"
CONTAINER_IP=$(hostname -I | awk '{print $1}')
HOSTNAME=$(hostname)
cat > /etc/motd << 'EOFMOTDINNER'
LXC AutoScale - Container Resource Management
🌐 Provided by: github.com/droltr/lxc-auto-scaler
🖥️ Based on: github.com/fabriziosalmi/proxmox-lxc-autoscale
📦 Framework: community-scripts.github.io/ProxmoxVE
EOFMOTDINNER
# Add dynamic info
cat >> /etc/motd << EOFDYNAMIC
🏠 Hostname: ${HOSTNAME}
💡 IP Address: ${CONTAINER_IP}
🌐 Web UI: http://${CONTAINER_IP}:8080
📁 Config: /etc/lxc_autoscale/lxc_autoscale.yaml
EOFDYNAMIC
msg_ok "Login banner created"
# ==============================================================================
# CLEANUP
# ==============================================================================
$STD apt-get autoremove -y
$STD apt-get autoclean -y