#!/bin/bash

# Download script
echo "Starting the download of the installation script..."
wget https://download.cloudclusters.io/hosting_install.tar.gz

# Extract and enter the installation directory
echo "Extracting the installation script..."
tar -zxvf hosting_install.tar.gz && cd hosting_install

# Give executable permissions to the installation scripts
echo "Granting executable permissions..."
chmod +x *

# Check if service software meets the requirements
echo "Checking service software requirements..."
bash server_check.sh

# Prompt for the host tag
read -p "Please enter the host tag (e.g., DBMP-AX05-18): " HOST_TAG
# Execute installation based on host type
read -p "Please enter the host type (gpu/linux): " HOST_TYPE
if [ "$HOST_TYPE" == "gpu" ]; then
    echo "Executing GPU hosting installation..."
    bash gpu_hosting_install.sh
elif [ "$HOST_TYPE" == "linux" ]; then
    echo "Executing Linux hosting installation..."
    bash linux_hosting_install.sh
else
    echo "Invalid host type!"
    exit 1
fi

# Additional settings
echo "Starting additional settings..."
cd /Data1
echo "Extracting base.tar.gz..."
tar -zxvf base.tar.gz
echo "Removing source zip file..."
rm base.tar.gz

# houduan  part
# Step 1: Download the script
echo "Logging in and downloading the deployment script..."
wget https://download.cloudclusters.io/deploy_vps_agent.sh

# Step 2: Change script permissions
echo "Changing script permissions..."
chmod +x deploy_vps_agent.sh

# Step 3: Execute the script with the provided host tag
echo "Executing the deployment script..."
./deploy_vps_agent.sh "$HOST_TAG"

# Step 4: Disable UFW
echo "Disabling UFW..."
ufw disable

# Step 5: Add certificate
echo "Adding certificate..."
certbot --nginx -d "${HOST_TAG}.cloudclusters.io"
echo "Please provide your email for Let's Encrypt notifications."
echo "Email: support@cloudclusters.io"

# Step 6: Enable UFW and add rules
echo "Enabling UFW and adding rules..."
ufw enable
ufw allow from 173.208.142.9   # Panel IP
#ufw allow from 69.30.237.33     # CCS Panel IP
#ufw allow from 108.181.202.146   # HWY Panel IP
#ufw allow from 173.208.142.9    # DBM Panel IP

# Step 7: Confirm UFW status
echo "Confirming UFW status..."
ufw status

# Step 8: Restart Nginx
echo "Restarting Nginx..."
systemctl restart nginx.service

wget https://download.cloudclusters.io/ipmi-install-exporter.sh
bash ipmi-install-exporter.sh

mkdir -p /root/resource-monitor

cd /root/resource-monitor

wget https://download.cloudclusters.io/resource-monitor.py

cd ~


/root/env/bin/pip install prometheus_client


cat <<EOF > /etc/supervisor/conf.d/resource-monitor.conf
[program:resource-monitor]
command=/root/env/bin/python /root/resource-monitor/resource-monitor.py
autostart=true
autorestart=true
stderr_logfile=/var/log/resource-monitor.err.log
stdout_logfile=/var/log/resource-monitor.out.log
EOF


supervisorctl update


supervisorctl status


netstat -atnpl|grep 9177

ufw allow from 108.181.95.28 to any port 9177 proto tcp



# install monitor
mkdir -p /root/resource-monitor

cd /root/resource-monitor

wget https://download.cloudclusters.io/resource-monitor.py

cd ~

/root/env/bin/pip install prometheus_client


cat <<EOF > /etc/supervisor/conf.d/resource-monitor.conf
[program:resource-monitor]
command=/root/env/bin/python /root/resource-monitor/resource-monitor.py
autostart=true
autorestart=true
stderr_logfile=/var/log/resource-monitor.err.log
stdout_logfile=/var/log/resource-monitor.out.log
EOF

supervisorctl update

supervisorctl status

netstat -atnpl|grep 9177

ufw allow from 108.181.95.28 to any port 9177 proto tcp
