#!/bin/bash
set -e
set -x
echo "Tag is $1"
TAG=$(echo "$1" |tr 'A-Z' 'a-z')
work_dir=/root
cd $work_dir

BASHRC_FILE="$HOME/.bashrc"
PYENV_LINE='export PYENV_ROOT="$HOME/.pyenv"'

# 获取操作系统信息
os_name=$(uname -s)
os_version=$(lsb_release -rs)
os_id=$(lsb_release -is)

# 检查操作系统是否为 Ubuntu 22
if [[ "$os_name" == "Linux" && "$os_id" == "Ubuntu" && "$os_version" == "22."* ]]; then
    sudo apt update & apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl
    curl https://pyenv.run | bash
    if [ -f "$BASHRC_FILE" ]; then
            if ! grep -qF "$PYENV_LINE" "$BASHRC_FILE"; then
                echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
                echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
                echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
                echo 'eval "$(pyenv init -)"' >> ~/.bashrc
                source ~/.bashrc
            else
                    echo ""
            fi
    else
        echo ""
    fi
    /root/.pyenv/bin/pyenv install -v 3.8.10
    /root/.pyenv/bin/pyenv global 3.8.10
    source ~/.bashrc
    pip install virtualenv
    cd $work_dir
    virtualenv -p /root/.pyenv/versions/3.8.10/bin/python3.8 env

else
    apt update && apt install -y python3-pip
    pip install virtualenv
    cd $work_dir
    virtualenv env
fi


#clone code
git config --global credential.helper store
git clone https://clay:UtuDrmOPqVrA@gitlab.cloudclusters.net/clusterpanels/vps_agent.git

cd vps_agent && git checkout release && mkdir -p log

#install python
#apt update && apt-get install -y python3

#apt update && apt install -y python3-pip

#install virtual env
#pip install virtualenv

#cd $work_dir
#virtualenv -p /root/.pyenv/versions/3.8.10/bin/python3.8 env
# virtualenv env

#install libvirt-python
apt-get install -y libvirt-dev
apt-get install -y pkg-config
apt-get install -y build-essential python3-dev libssl-dev libffi-dev libxml2 libxml2-dev libxslt1-dev zlib1g-dev
$work_dir/env/bin/pip install -r /root/vps_agent/api_server/requirements.txt

cd /root/vps_agent/api_server/ && $work_dir/env/bin/pybabel compile -d i18n && cd -

#install uwsgi
apt-get install -y uwsgi-plugin-python3
$work_dir/env/bin/pip install uwsgi


#supervior
apt-get install -y supervisor

cp /root/vps_agent/config/agent.conf /etc/supervisor/conf.d/vps.conf

sed -i 's/RABBITMQ_VHOST="develop"/RABBITMQ_VHOST="release"/g' /etc/supervisor/conf.d/vps.conf
sed -i 's/TRIPANELSENV="develop"/TRIPANELSENV="production"/g' /etc/supervisor/conf.d/vps.conf
sed -i "s/{tag}/$1/g" /etc/supervisor/conf.d/vps.conf

systemctl start supervisor
supervisorctl update
supervisorctl status


#nginx
apt-get install -y nginx
cp /root/vps_agent/config/vps_agent_api /etc/nginx/sites-enabled/vps_agent_api.conf

sed -i "s/{server_tag}/$TAG/g" /etc/nginx/sites-enabled/vps_agent_api.conf


echo 'tripanels:$apr1$S8sZ0F/P$2Wh20lEg8vxgVyMEUeYPr0' > /etc/nginx/.htpasswd
rm /etc/nginx/sites-enabled/default

apt-get update
apt-get install -y python3-certbot-nginx


sed -i 's/user www-data/user root/g' /etc/nginx/nginx.conf

systemctl restart nginx

#install virsh
apt install -y libguestfs-tools
apt install -y cloud-utils


# offline backup disk
cd /root/vps_agent/api_server && /root/env/bin/python -c "from handlers.task_handler import KVMHandler;KVMHandler.init_backup_conf_file();KVMHandler.offline_backup_disk()" && cd -
