shell脚本实现公司项目部署交付环境预检查的思路

(编辑:jimmy 日期: 2024/12/30 浏览:2)

大型项目环境预检查脚本,根据自己实际情况修改脚本中变量,给大家一个思路,转载请注明出处~

#!/usr/bin/env bash
root=$(
  cd $(dirname $0)
  pwd
)

source "$root"/dragonrc

CPU=$(echo "`cat /proc/cpuinfo | grep "physical id" | uniq | wc -l`H")
MEM=$(echo "$((`cat /proc/meminfo | grep MemTotal |awk '{print $2}'` / 1000000))G")
TIME=$(date)

rpm -q ansible &> /dev/null

if [ $"请按回车继续:"
}

COPY_KEY(){
    echo
    echo
sh ${root}/scripts/copy-key.sh
    echo
    echo

}

CK_UMASK(){
UMASK_NUM=$(ansible dragon -m shell -a "umask"|grep 0022|wc -l)
ALL_HOSTS=$(echo "${ALL_NODES}"|awk -F"," '{for(i=1;i<=NF;i++) print $i}'|wc -l)
    echo
    echo -e "\033[5;32;31m正在为您检测umask值,请耐心等待。。。\033[0m"
    echo
    if [ ${UMASK_NUM} -ne ${ALL_HOSTS} ];then
        echo -e "\033[1;32;31m检测到节点umask值非022,将为您执行修改操作!\033[0m"
        sleep 3
            ansible dragon -m shell -a "sed -i 's/^.*umask.*/umask 022/g' /etc/profile"
            ansible dragon -m shell -a "source /etc/profile"
            ansible dragon -m shell -a "sed -i 's/^.*umask.*/umask 022/g' /etc/bashrc"
            ansible dragon -m shell -a "source /etc/bashrc"
        echo -e "\033[1;32;31m-------------------------------以下为各节点umask值-------------------------------\033[0m"
            ansible dragon -m shell -a "umask"
        echo -e "\033[1;32;31m提示:如果以上返回结果非0022,那就是自动设置失败,请手动调整umask值!\033[0m"
    else
clear
        echo -e "\033[1;32;31m所有节点umask值全部为022\033[0m"
echo
    fi
}
CK_NETWORK_NAME(){
    echo
    echo -e "\033[1;32;31m-------------------------------检测所有节点主机网卡名-------------------------------\033[0m"
    IFS=',' read -r -a nodes <<<"${ALL_NODES}"
    for p in "${nodes[@]}"; do
    echo
    ssh -q -o 'StrictHostKeyChecking no' -o 'UserKnownHostsFile /dev/null' -T $p ip addr |grep $p | awk '{print $2,$7}'
    echo
    done
    echo -e "\033[1;32;35m提示:请您把网卡名复制到dragonrc配置~\033[0m"
    echo -e "\033[1;32;31m-------------------------------以上为所有节点主机网卡名-------------------------------\033[0m"
    echo
}
CK_HOST_NAME(){
    echo
    echo -e "\033[1;32;31m-------------------------------检测所有节点主机名-------------------------------\033[0m"
    ansible dragon -m shell -a "hostname"
            echo -e "\033[1;32;31m提示:检查主机名,不能是出现大写或特殊符号~\033[0m"
    echo -e "\033[1;32;31m-------------------------------以上为所有节点主机名-------------------------------\033[0m"
    echo
}
CK_HOSTS(){
    echo
    echo -e "\033[1;32;31m-------------------------------检测所有节点Hosts配置-------------------------------\033[0m"
    sh ${root}/scripts/hostname.sh
    echo -e "\033[1;32;31m-------------------------------以上为所有节点Hosts配置-------------------------------\033[0m"
    echo
}
CK_TIME(){
    echo
    echo -e "\033[1;32;31m----------------------------------检测所有节点时间----------------------------------\033[0m"
    ansible dragon -m shell -a "date"
    echo -e "\033[1;32;35m提示:误差为1~2秒为正常情况,如差距太大则需要做ntpd时间同步~\033[0m"
    echo -e "\033[1;32;31m----------------------------------以上为所有节点时间----------------------------------\033[0m"
    echo
}
CK_DISK(){
    echo
    echo -e "\033[1;32;31m----------------------------------检测所有节点磁盘挂载----------------------------------\033[0m"
    ansible dragon -m shell -a "df -h"
    echo -e "\033[1;32;31m----------------------------------以上为所有节点磁盘挂载----------------------------------\033[0m"
    echo
}
CK_SYSTEM(){
    echo
    echo -e "\033[1;32;31m----------------------------------检测所有节点系统版本信息----------------------------------\033[0m"
    ansible dragon -m shell -a "cat /etc/redhat-release"
    echo -e "\033[1;32;31m----------------------------------以上为所有节点系统版本信息----------------------------------\033[0m"
    echo
}
CK_FIREWALLD(){
    echo
    echo -e "\033[1;32;31m----------------------------------检测所有节点防火墙是否关闭----------------------------------\033[0m"
    ansible dragon -m shell -a "firewall-cmd --state"
    echo -e "\033[1;32;35m提示:如果发现防火墙状态为Running,不用担心,all_sh.sh环境初始化配置已经配好永久关闭哦~\033[0m"
    echo -e "\033[1;32;31m----------------------------------以上为所有节点防火墙状态信息----------------------------------\033[0m"
    echo
}
CK_SELINUX(){
    echo
    echo -e "\033[1;32;31m----------------------------------检测所有节点SELINUX是否关闭----------------------------------\033[0m"
    ansible dragon -m shell -a "sestatus"
    echo -e "\033[1;32;35m提示:如果发现状态为enabled,不用担心,all_sh.sh环境初始化配置已经配好永久关闭哦~\033[0m"
    echo -e "\033[1;32;31m----------------------------------以上为所有节点SELINUX状态信息----------------------------------\033[0m"
    echo
}
while true
do
    clear
    echo "-------------------------------------------------------"
    echo "|                           |"
    echo "|       XXXXXXXXXXX部署预检查环境        |"
    echo "|                           |"
    echo "-------------------------------------------------------"
    echo "   ***** 检测到您的机器配置为:${CPU} ${MEM}  ***** "
    echo "-------------------------------------------------------"
    echo "|        1. 一键SSH免密互信          |"
    echo "|        2. 检查各节点主机umask        |"
    echo "|        3. 检查各节点网卡名          |"
    echo "|        4. 检查各节点主机名          |"
    echo "|        5. 检查各节点hosts配置        |"
    echo "|        6. 检查各节点主机时间         |"
    echo "|        7. 检查各节点主机磁盘挂载       |"
    echo "|        8. 检查各节点主机系统版本       |"
    echo "|        9. 检查各节点防火墙是否关闭      |"
    echo "|       10. 检查各节点SELINUX是否关闭     |"
    echo "-------------------------------------------------------"
    echo
    echo "  // 当前时间:${TIME} //"
    echo
    read -p "请您选择[1-10/q退出]选项:" THING
case $THING in
       1)
       COPY_KEY
       HINT
       ;;
       2)
       CK_UMASK
       HINT
       ;;
       3)
       CK_NETWORK_NAME
       HINT
       ;;
       4)
       CK_HOST_NAME
       HINT
       ;;
       5)
       CK_HOSTS
       HINT
       ;;
       6)
       CK_TIME
       HINT
       ;;
       7)
       CK_DISK
       HINT
       ;;
       8)
       CK_SYSTEM
       HINT
       ;;
       9)
       CK_FIREWALLD
       HINT
       ;;
       10)
       CK_SELINUX
       HINT
       ;;
       q)
       exit
       ;;
       *)
       read -p "请您选择[1-10/q退出],输入回车继续:"
       ;;
       esac
done