#!/bin/bash


# 获取环境信息
/usr/local/bin/rmdc-watchdog-node info all
cat /usr/local/etc/wdd/rmdc-watchdog-node-config.yaml

# 手动执行全部的命令
/usr/local/bin/rmdc-watchdog-node base ssh key
/usr/local/bin/rmdc-watchdog-node base ssh config

/usr/local/bin/rmdc-watchdog-node base tools

/usr/local/bin/rmdc-watchdog-node base swap
/usr/local/bin/rmdc-watchdog-node base firewall
/usr/local/bin/rmdc-watchdog-node base selinux
/usr/local/bin/rmdc-watchdog-node base sysconfig

/usr/local/bin/rmdc-watchdog-node zsh cn

# 首先需要下载所有的依赖！

/usr/local/bin/rmdc-watchdog-node base docker local
/usr/local/bin/rmdc-watchdog-node base dockercompose local

/usr/local/bin/rmdc-watchdog-node base harbor install


# 主节点执行
# 安装octopus-agent
mv rmdc-watchdog-node_linux_amd64 /usr/local/bin/rmdc-watchdog-node
chmod +x /usr/local/bin/rmdc-watchdog-node

mv rmdc-watchdog-node_linux_arm64 /usr/local/bin/rmdc-watchdog-node
chmod +x /usr/local/bin/rmdc-watchdog-node

# 主节点安装ssh-key
/usr/local/bin/rmdc-watchdog-node base ssh config
/usr/local/bin/rmdc-watchdog-node base ssh key

# 批量执行命令
host_list=(
   172.16.100.56
   172.16.100.57
   172.16.100.58
)
for server in "${host_list[@]}";do
   echo "current ip is $server"
   ssh root@${server} "rm -rf /var/lib/docker/nfs_data/*"
   echo ""
done


# 主节点批量安装key

# 复制 同步文件
export server=172.16.100.62

scp /usr/local/bin/rmdc-watchdog-node root@${server}:/usr/local/bin/rmdc-watchdog-node
ssh root@${server} "/usr/local/bin/rmdc-watchdog-node base ssh config && /usr/local/bin/rmdc-watchdog-node base ssh key"
ssh root@${server} "echo yes"


# 安装docker-compose
mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose
chmod +x  /usr/local/bin/docker-compose

# 批量执行rmdc-watchdog-node的命令
ssh root@${server} "/usr/local/bin/rmdc-watchdog-node info all"
ssh root@${server} "cat /usr/local/etc/wdd/rmdc-watchdog-node-config.yaml"


ssh root@${server} "/usr/local/bin/rmdc-watchdog-node base swap"
ssh root@${server} "/usr/local/bin/rmdc-watchdog-node base firewall"
ssh root@${server} "/usr/local/bin/rmdc-watchdog-node base selinux"
ssh root@${server} "/usr/local/bin/rmdc-watchdog-node base sysconfig"

# APT代理加速
scp /root/wdd/apt-change.sh  root@${server}:/root/wdd/apt-change.sh
ssh root@${server} "bash /root/wdd/apt-change.sh -y"


ssh root@${server} "echo \"\"> /etc/apt/apt.conf.d/01proxy"
ssh root@${server} "printf '%s\n' \
'Acquire::http::Proxy \"http://10.22.57.8:3142\";' \
'Acquire::https::Proxy \"http://10.22.57.8:3142\";' \
| tee /etc/apt/apt.conf.d/01proxy >/dev/null"
ssh root@${server} "apt-get update"
ssh root@${server} "apt-get install -y parted"

# 磁盘初始化
ssh root@${server} "mkdir /root/wdd"
scp /root/wdd/disk.sh root@${server}:/root/wdd/
ssh root@${server} "bash /root/wdd/disk.sh"

# master节点安装docker

export DOCKER_VERSION=24
bash /root/wdd/docker.sh

# 在线安装docker 通过APT代理
scp /etc/apt/keyrings/docker.gpg root@${server}:/root/wdd/
scp /root/wdd/docker.sh root@${server}:/root/wdd/

ssh root@${server} "bash /root/wdd/docker.sh"
ssh root@${server} "docker info"
ssh root@${server} "docker compose version"

# 复制文件-docker
scp /root/wdd/docker-amd64-20.10.15.tgz root@${server}:/root/wdd/docker-amd64-20.10.15.tgz
scp /root/wdd/docker-compose-v2.18.0-linux-amd64  root@${server}:/root/wdd/

scp /root/wdd/docker-arm64-20.10.15.tgz root@${server}:/root/wdd/docker-arm64-20.10.15.tgz
scp /root/wdd/docker-compose-v2.18.0-linux-arm64 root@${server}:/root/wdd/


ssh root@${server} "/usr/local/bin/rmdc-watchdog-node base docker local"
ssh root@${server} "/usr/local/bin/rmdc-watchdog-node base dockercompose local"

# 仅在主节点执行
/usr/local/bin/rmdc-watchdog-node base docker config

# 下发docker的配置
scp /etc/docker/daemon.json root@${server}:/etc/docker/daemon.json
ssh root@${server} "cat /etc/docker/daemon.json"
ssh root@${server} "systemctl restart docker"
ssh root@${server} "docker info"

# 清理脚本

ssh root@${server} "rm /root/wdd/*.sh"

# 主节点执行 安装harbor仓库
/usr/local/bin/rmdc-watchdog-node base harbor install

# 安装rke kubectl
mv /root/wdd/rke_linux-amd64 /usr/local/bin/rke
chmod +x /usr/local/bin/rke

mv /root/wdd/kubectl_v1.30.14_amd64 /usr/local/bin/kubectl
chmod +x /usr/local/bin/kubectl


## arm64
mv /root/wdd/rke_linux-arm64 /usr/local/bin/rke
chmod +x /usr/local/bin/rke

mv /root/wdd/kubectl_v1.30.14_arm64 /usr/local/bin/kubectl
chmod +x /usr/local/bin/kubectl

# 安装 k8s-证书

mkdir /root/.kube
cp ./kube_config_cluster.yml /root/.kube/config

# 环境测试
DEFAULT_HTTP_BACKEND_IP=$(kubectl -n ingress-nginx get svc default-http-backend -o jsonpath='{.spec.clusterIP}')

# master节点
curl -s "http://${DEFAULT_HTTP_BACKEND_IP}"

# worker节点
ssh root@"$server" "DEFAULT_HTTP_BACKEND_IP='$DEFAULT_HTTP_BACKEND_IP' bash -s" <<'EOF'
echo "DEFAULT_HTTP_BACKEND_IP=$DEFAULT_HTTP_BACKEND_IP"

curl -s "http://${DEFAULT_HTTP_BACKEND_IP}"
echo
EOF