#!/bin/bash

examdir=/root/rhce9-exam294
examzip=/root/rhce9-exam294.zip
scripts=${examdir}/scripts

# 解压 exam 材料
[[ -f ${examzip} ]] && unzip -o ${examzip} &> /dev/null || echo ${examzip} is not exist

# 配置 exam.lib
rsync ${examdir}/exam.lib /usr/local/lib/exam.lib
source /usr/local/lib/exam.lib

# 判断执行用户
if [[ `id -u` -ne 0 ]];then
    echo "Please run as root"
    exit 1
fi

# # 配置免密登录，测试 utility是否启动需要
# # 配置 kiosk 免密登录
# rsync -a ${examdir}/.ssh/* ~kiosk/.ssh/
# chmod 600 ~kiosk/.ssh/*
# chown -R kiosk:kiosk ~kiosk/.ssh
# # 配置 root 免密登录
# rsync -a ${examdir}/.ssh/* ~/.ssh
# chmod 600 ~/.ssh/*

# 配置 classroom 解析
if ! grep -q classroom /etc/hosts ;then
  echo "172.25.254.254 classroom.example.com classroom" >> /etc/hosts
else
  sed -ri 's/^.*classroom.*$/172.25.254.254 classroom.example.com classroom/' /etc/hosts
fi

# 重置虚拟机
pad "Reset all VMs"
rht-vmctl reset classroom -q &> /dev/null 
rht-vmctl reset all -q &> /dev/null
print_DONE

# 等待虚拟机运行
echo "Wait for VMs running:"
sleep 30 
for host in classroom bastion workstation utility server{a..d}
do
  pad "  Wait for $host running"
  wait_ssh $host &>/dev/null
  if [ "$host" == "utility" ] ;then
    while :
    do 
      # 判断 registry 是否启动完成
      if ssh utility podman login -u admin -p redhat utility.lab.example.com &>/dev/null;then
        print_DONE
        break
      fi
      # 3 秒后再次判断
      sleep 3
    done
  else
    print_DONE
  fi
done

############# 配置 foundation0 #############
pad "Prepare foundation"

# 清除 ssh 首次登录显示信息
for host in classroom bastion workstation utility server{a..d}
do
  ssh $host date &>/dev/null
done

# 删除受管节点 yum 仓库
for host in workstation server{a..d}
do
  ssh root@$host 'rm -f /etc/yum.repos.d/*.repo' &>/dev/null
done

# 配置 exam materials
materials=/content/courses/rh294/rhel9.0/materials
[[ -d ${materials}/laoma ]] && rm -fr ${materials}/laoma
rsync -a ${examdir}/laoma ${materials}

# exam 试题
rsync -a ${examdir}/EX294-RHCE9-202312.html $materials
mv $materials/EX294-RHCE9-202312.html $materials/index.html

# 桌面图标
rsync ${examdir}/redhat_logo.png /usr/share/icons/redhat_logo.png
rsync ${examdir}/exam.desktop /home/kiosk/.local/share/applications/
chown kiosk:kiosk /home/kiosk/.local/share/applications/exam.desktop

# 设置评分脚本
rsync ${examdir}/scripts/grade* /usr/local/bin
chmod a+x /usr/local/bin/grade


# 配置完成
print_DONE

############# 配置 foundation0 #############


############# 配置 bastion #############
pad "Prepare bastion"

# 配置 ssh HostKeyChecking
ssh root@bastion 'echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config'

# 添加用户
ssh root@bastion 'id devops &> /dev/null || useradd devops' &> /dev/null
ssh root@bastion 'echo "devops ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/devops'

# 配置免密登录
rsync -a ~/.ssh devops@bastion:~

# 配置 lib
rsync -a ${examdir}/exam.lib root@bastion:/usr/local/lib/exam.lib

# 配置 ansible-navigator
rsync ${examdir}/ansible-navigator.yml devops@bastion:~/.ansible-navigator.yml

# 配置 ansible 仓库
rsync ${examdir}/additional.repo root@bastion:/etc/yum.repos.d/

# 配置容器仓库
ssh devops@bastion mkdir -p .config/containers
rsync ${examdir}/registries.conf devops@bastion:~/.config/containers

# 配置完成
print_DONE

############# 配置 bastion #############


############# 配置 servera #############
pad "Prepare servera"

# 重置 servera 的 vdc 磁盘
image=/var/lib/libvirt/images/rh294-servera-vdc.ovl
image_backup=/var/lib/libvirt/images/rh294-servera-vdc.qcow2

# 删除 vdc 磁盘
if grep -q vdc /etc/libvirt/qemu/servera.xml;then
  rht-vmctl poweroff servera -q &>/dev/null
  virsh detach-disk servera --target vdc --config &> /dev/null
  [[ -f ${image} ]] && rm -f ${image}
  [[ -f ${image_backup} ]] && rm -f ${image_backup}
  # 启动 servera
  ( rht-vmctl start servera && wait_ssh servera )&>/dev/null
  # 重启servera，会导致servera重新下载 yum 仓库文件，再次删除
  ssh root@servera 'rm -f /etc/yum.repos.d/*.repo'
  
fi

# 添加 vdc 磁盘
( qemu-img create -f qcow2 ${image_backup} 5G && qemu-img create -f qcow2 -b ${image_backup} -F qcow2 ${image} 5G) &> /dev/null
virsh attach-disk --domain servera --source ${image_backup} --target vdc --subdriver qcow2 --live --config &> /dev/null

# 创建卷组 research
while :
do
  sleep 3
  if ssh root@servera ls /dev/vdc &> /dev/null;then
    ssh root@servera vgcreate research /dev/vdb /dev/vdc &> /dev/null
    break
  fi
done

# 配置完成
print_DONE

############# 配置 servera #############


############# 配置 serverb #############
pad "Prepare serverb"

# 创建卷组 research
ssh root@serverb vgcreate research /dev/vdb &> /dev/null

# 设置 selinux
ssh root@serverb "sed -ir 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config"
ssh root@serverb reboot
wait_ssh serverb &>/dev/null

# 配置完成
print_DONE

############# 配置 serverb #############


# # 设置虚拟机开机自动启动
# for host in bastion utility workstation server{a..d}
# do
#   virsh autostart ${host} &> /dev/null
# done


# 开始做题
read -p '
-----------------------------------------------------------------------
          按 Super 键，搜索 EXAM，打开试题，按回车键开始答题
-----------------------------------------------------------------------'