Auto-scale instances per 250 accounts via hourly cron

This commit is contained in:
bigbong420 committed 2026-03-29 14:15:58 -04:00
1 parent 6b1f324f5e
commit 8d1afbdfd4
4 files changed
+64 -28

No files matched your search

@@ -0,0 +1,6 @@
[Unit]
Description=Auto-scale Tor hosting instances based on account count
[Service]
Type=oneshot
ExecStart=/usr/bin/php8.2 /var/www/autoscale.php
@@ -0,0 +1,9 @@
[Unit]
Description=Check and auto-scale hosting instances every hour
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.target
+3 -28
View File
@@ -329,36 +329,10 @@ if [ -z "$ONION_ADDR" ]; then
fi fi
log_ok "Onion address: ${GREEN}${ONION_ADDR}${NC}" log_ok "Onion address: ${GREEN}${ONION_ADDR}${NC}"
# Auto-calculate instance count based on RAM
# ~250 accounts per instance, ~128MB RAM per instance overhead
# 4GB = 1 instance, 8GB = 2, 16GB = 4, 32GB+ = 8
RAM_GB=$(free -g | awk '/Mem:/{print $2}')
if [ "$RAM_GB" -ge 32 ]; then
INSTANCE_COUNT=8
elif [ "$RAM_GB" -ge 16 ]; then
INSTANCE_COUNT=4
elif [ "$RAM_GB" -ge 8 ]; then
INSTANCE_COUNT=2
else
INSTANCE_COUNT=1
fi
INSTANCE_CHARS="abcdefgh"
INSTANCES=""
for i in $(seq 0 $((INSTANCE_COUNT - 1))); do
c=${INSTANCE_CHARS:$i:1}
[ -n "$INSTANCES" ] && INSTANCES="$INSTANCES,"
INSTANCES="$INSTANCES'$c'"
done
log_info "RAM: ${RAM_GB}GB -> $INSTANCE_COUNT instance(s)"
log_step "Step 8: Configure hosting" log_step "Step 8: Configure hosting"
ADMIN_HASH=$(php8.2 -r "echo password_hash('$ADMIN_PASS', PASSWORD_DEFAULT);" 2>/dev/null || \ ADMIN_HASH=$(php8.2 -r "echo password_hash('$ADMIN_PASS', PASSWORD_DEFAULT);" 2>/dev/null || \
php8.5 -r "echo password_hash('$ADMIN_PASS', PASSWORD_DEFAULT);") php8.5 -r "echo password_hash('$ADMIN_PASS', PASSWORD_DEFAULT);")
sed -i "s|const SERVICE_INSTANCES=\['a'\]|const SERVICE_INSTANCES=[$INSTANCES]|" /var/www/common.php
sed -i "s|const DBPASS='MY_PASSWORD'|const DBPASS='$DB_HOSTING_PASS'|" /var/www/common.php sed -i "s|const DBPASS='MY_PASSWORD'|const DBPASS='$DB_HOSTING_PASS'|" /var/www/common.php
sed -i "s|const ADMIN_PASSWORD='MY_PASSWORD'|const ADMIN_PASSWORD='$ADMIN_HASH'|" /var/www/common.php sed -i "s|const ADMIN_PASSWORD='MY_PASSWORD'|const ADMIN_PASSWORD='$ADMIN_HASH'|" /var/www/common.php
sed -i "s|const ONION_KEY_ENCRYPTION_KEY=''|const ONION_KEY_ENCRYPTION_KEY='$ONION_ENC_KEY'|" /var/www/common.php sed -i "s|const ONION_KEY_ENCRYPTION_KEY=''|const ONION_KEY_ENCRYPTION_KEY='$ONION_ENC_KEY'|" /var/www/common.php
@@ -533,7 +507,8 @@ else
fi fi
log_step "Step 16: Final configuration" log_step "Step 16: Final configuration"
systemctl enable hosting-del.timer hosting.timer systemctl enable hosting-del.timer hosting.timer hosting-autoscale.timer
systemctl start hosting-autoscale.timer
sed -i 's/^Subsystem/#Subsystem/' /etc/ssh/sshd_config sed -i 's/^Subsystem/#Subsystem/' /etc/ssh/sshd_config
systemctl restart nginx postfix dovecot systemctl restart nginx postfix dovecot
systemctl restart tor@default.service systemctl restart tor@default.service
@@ -551,7 +526,7 @@ echo ""
echo -e "${BOLD}Credentials saved to:${NC} $CREDS_FILE" echo -e "${BOLD}Credentials saved to:${NC} $CREDS_FILE"
echo "" echo ""
echo -e "${BOLD}PHP Versions:${NC} 8.2, 8.3, 8.4, 8.5 (default: 8.5)" echo -e "${BOLD}PHP Versions:${NC} 8.2, 8.3, 8.4, 8.5 (default: 8.5)"
echo -e "${BOLD}Instances:${NC} $INSTANCE_COUNT (RAM: ${RAM_GB}GB, ~$((INSTANCE_COUNT * 250)) max accounts)" echo -e "${BOLD}Instances:${NC} auto-scaling (1 per ~250 accounts, checked hourly)"
echo "" echo ""
echo -e "${YELLOW}Recommended: Reboot the server now and wait ~5 minutes for all services to start.${NC}" echo -e "${YELLOW}Recommended: Reboot the server now and wait ~5 minutes for all services to start.${NC}"
echo "" echo ""
+46
View File
@@ -0,0 +1,46 @@
<?php
require(__DIR__ . '/common.php');
$db = get_db_instance();
$accounts_per_instance = 250;
$max_instances = 8;
$instance_chars = 'abcdefgh';
$stmt = $db->query('SELECT instance, COUNT(*) as cnt FROM onions WHERE enabled IN (1, -2) GROUP BY instance;');
$loads = [];
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$loads[$row['instance']] = (int)$row['cnt'];
}
$current = SERVICE_INSTANCES;
$total_accounts = array_sum($loads);
$needed = max(1, (int)ceil($total_accounts / $accounts_per_instance));
$needed = min($needed, $max_instances);
if($needed <= count($current)){
echo "OK: $total_accounts accounts across " . count($current) . " instance(s), no scaling needed\n";
exit(0);
}
$new_instances = $current;
for($i = count($current); $i < $needed; $i++){
$char = $instance_chars[$i];
if(!in_array($char, $new_instances)){
$new_instances[] = $char;
}
}
$old_str = "const SERVICE_INSTANCES=['" . implode("','", $current) . "']";
$new_str = "const SERVICE_INSTANCES=['" . implode("','", $new_instances) . "']";
$common = file_get_contents('/var/www/common.php');
if(strpos($common, $old_str) === false){
echo "ERROR: could not find SERVICE_INSTANCES in common.php\n";
exit(1);
}
file_put_contents('/var/www/common.php', str_replace($old_str, $new_str, $common));
echo "SCALED: $total_accounts accounts, " . count($current) . " -> " . count($new_instances) . " instances ($new_str)\n";
echo "Running setup.php to create new instances...\n";
require(__DIR__ . '/setup.php');