Files
proxmox/scripts/complete-all-installations-and-tasks.sh.bak

202 lines
7.0 KiB
Bash
Raw Normal View History

#!/bin/bash
# Complete All Installations and Tasks - Final Execution
# Installs PostgreSQL, Redis, configures databases, and completes all remaining tasks
set -uo pipefail
NODE_IP="192.168.11.11"
log_info() { echo -e "\033[0;32m[INFO]\033[0m $1"; }
log_error() { echo -e "\033[0;31m[ERROR]\033[0m $1"; }
log_success() { echo -e "\033[0;32m[✓]\033[0m $1"; }
# Install PostgreSQL via host mount
install_postgresql_complete() {
local vmid="$1"
log_info "Installing PostgreSQL on CT $vmid..."
ssh -o ConnectTimeout=20 -o StrictHostKeyChecking=no root@${NODE_IP} "
pct stop $vmid 2>/dev/null || true
sleep 2
MOUNT_OUT=\$(pct mount $vmid 2>&1)
MOUNT_POINT=\$(echo \"\$MOUNT_OUT\" | grep -o '/var/lib/lxc/[0-9]*/rootfs' | head -1)
if [ -n \"\$MOUNT_POINT\" ] && [ -d \"\$MOUNT_POINT\" ]; then
chroot \$MOUNT_POINT bash -c '
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get install -y -qq postgresql-15 postgresql-contrib-15 || exit 1
# Configure PostgreSQL
sed -i \"s/#listen_addresses = .*/listen_addresses = '\''*'\''/\" /etc/postgresql/15/main/postgresql.conf 2>/dev/null || true
echo \"host all all 0.0.0.0/0 md5\" >> /etc/postgresql/15/main/pg_hba.conf 2>/dev/null || true
systemctl enable postgresql@15-main
echo \"PostgreSQL installed and configured\"
' && echo 'PostgreSQL installed' || echo 'Installation failed'
pct unmount $vmid 2>/dev/null || true
fi
pct start $vmid 2>/dev/null || true
sleep 5
" && log_success "PostgreSQL installed on CT $vmid" || log_error "Failed to install PostgreSQL on CT $vmid"
}
# Install Redis via host mount
install_redis_complete() {
local vmid="$1"
log_info "Installing Redis on CT $vmid..."
ssh -o ConnectTimeout=20 -o StrictHostKeyChecking=no root@${NODE_IP} "
pct stop $vmid 2>/dev/null || true
sleep 2
MOUNT_OUT=\$(pct mount $vmid 2>&1)
MOUNT_POINT=\$(echo \"\$MOUNT_OUT\" | grep -o '/var/lib/lxc/[0-9]*/rootfs' | head -1)
if [ -n \"\$MOUNT_POINT\" ] && [ -d \"\$MOUNT_POINT\" ]; then
chroot \$MOUNT_POINT bash -c '
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get install -y -qq redis-server || exit 1
sed -i \"s/^bind .*/bind 0.0.0.0/\" /etc/redis/redis.conf 2>/dev/null || true
systemctl enable redis-server
echo \"Redis installed and configured\"
' && echo 'Redis installed' || echo 'Installation failed'
pct unmount $vmid 2>/dev/null || true
fi
pct start $vmid 2>/dev/null || true
sleep 5
" && log_success "Redis installed on CT $vmid" || log_error "Failed to install Redis on CT $vmid"
}
# Start and configure PostgreSQL
start_and_configure_postgresql() {
local vmid="$1"
local db_type="$2" # "order" or "dbis"
log_info "Starting and configuring $db_type database on CT $vmid..."
ssh -o ConnectTimeout=15 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'CONFIG_EOF'
# Start PostgreSQL
systemctl start postgresql@15-main
sleep 3
# Wait for PostgreSQL to be ready
for i in {1..10}; do
systemctl is-active postgresql@15-main >/dev/null 2>&1 && break
sleep 1
done
if systemctl is-active postgresql@15-main >/dev/null 2>&1; then
if [ \"$db_type\" = \"order\" ]; then
su - postgres -c \"psql << 'SQL_EOF'
CREATE DATABASE order_db;
CREATE USER order_user WITH PASSWORD 'order_password';
GRANT ALL PRIVILEGES ON DATABASE order_db TO order_user;
ALTER DATABASE order_db OWNER TO order_user;
SQL_EOF
\" && echo 'Order DB configured' || echo 'Config failed'
else
su - postgres -c \"psql << 'SQL_EOF'
CREATE DATABASE dbis_core;
CREATE USER dbis WITH PASSWORD '8cba649443f97436db43b34ab2c0e75b5cf15611bef9c099cee6fb22cc3d7771';
GRANT ALL PRIVILEGES ON DATABASE dbis_core TO dbis;
ALTER DATABASE dbis_core OWNER TO dbis;
SQL_EOF
\" && echo 'DBIS DB configured' || echo 'Config failed'
fi
else
echo 'PostgreSQL not active'
fi
CONFIG_EOF
" && log_success "$db_type DB configured on CT $vmid" || log_error "Failed to configure $db_type DB on CT $vmid"
}
# Start Redis
start_redis() {
local vmid="$1"
log_info "Starting Redis on CT $vmid..."
ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'START_EOF'
systemctl start redis-server
sleep 2
systemctl is-active redis-server && echo 'Redis started' || echo 'Redis start failed'
START_EOF
" && log_success "Redis started on CT $vmid" || log_error "Failed to start Redis on CT $vmid"
}
echo "═══════════════════════════════════════════════════════════"
echo "Complete All Installations and Tasks - Final"
echo "═══════════════════════════════════════════════════════════"
echo ""
# Install PostgreSQL
log_info "Installing PostgreSQL on database containers..."
for vmid in 10000 10001 10100 10101; do
install_postgresql_complete "$vmid"
sleep 3
done
# Start and configure PostgreSQL
log_info "Starting and configuring PostgreSQL databases..."
for vmid in 10000 10001; do
start_and_configure_postgresql "$vmid" "order"
sleep 2
done
for vmid in 10100 10101; do
start_and_configure_postgresql "$vmid" "dbis"
sleep 2
done
# Install Redis
log_info "Installing Redis on cache containers..."
for vmid in 10020 10120; do
install_redis_complete "$vmid"
sleep 3
done
# Start Redis
log_info "Starting Redis services..."
for vmid in 10020 10120; do
start_redis "$vmid"
sleep 2
done
# Execute all remaining tasks
log_info "Executing all remaining tasks..."
bash /home/intlc/projects/proxmox/scripts/execute-all-remaining-tasks.sh
# Final verification
echo ""
log_info "Final Service Status:"
echo "PostgreSQL:"
for vmid in 10000 10001 10100 10101; do
status=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
"pct exec $vmid -- systemctl is-active postgresql@15-main 2>&1 || echo 'inactive'")
echo " CT $vmid: $status"
done
echo "Redis:"
for vmid in 10020 10120; do
status=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
"pct exec $vmid -- systemctl is-active redis-server 2>&1 || echo 'inactive'")
echo " CT $vmid: $status"
done
echo "Node.js:"
for vmid in 10030 10040 10050 10060 10070 10080 10090 10091 10092 10130 10150 10151; do
status=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} \
"pct exec $vmid -- node --version 2>&1 | head -1 || echo 'not installed'")
echo " CT $vmid: $status"
done
echo ""
log_success "All installations and tasks completed!"