#!/usr/bin/bash
#define:saltstack 自动化安装
#curr_dir="/data/scripts/saltstac_install"
curr_dir=`pwd`
packge_dir="/data/tools"
yum_Warehouse="/etc/yum.repos.d"
logdir=$curr_dir #日志路径
log=$logdir/log.log #日志文件
is_font=1 #终端是否打印日志: 1打印 0不打印
is_log=1 #是否记录日志: 1记录 0不记录
#version_num=`cat /etc/redhat-release |awk '{print $3}'|cut -d'.' -f1`
version_num=`cat /etc/redhat-release| awk -F"release" '{print $2}'|awk '{print $1}'|cut -d'.' -f1`
datef(){
date "+%Y-%m-%d %H:%M:%S"
}
print_log(){
if [[ $is_log -eq 1 ]];then
[[ -d $logdir ]] || mkdir -p $logdir
echo -e "[ $(datef) ] $1" >> $log
fi
if [[ $is_font -eq 1 ]];then
echo -e "[ $(datef) ] $1"
fi
}
#####判断次命令是否存在,后面创建各系统环境的时候需要用到
wget_judge() {
which wget
if [[ $? -eq 0 ]];then
print_log "wget 命令存在"
else
print_log "wget 命令不存在"
yum -y install wget
fi
[[ -d $packge_dir ]]||mkdir -p $packge_dir
}
####yum源更新
yum_update() {
which expect
if [[ $? -eq 0 ]];then
print_log "expect存在,可以开始后面的"
else
yum -y install expect
fi
expect -c "
set timeout 1200;
spawn /usr/bin/yum update yum
expect {
"*y/N*" {send "y"\r;exp_continue}
}
expect eof;"
expect -c "
set timeout 1200;
spawn /usr/bin/yum update
expect {
"*y/N*" {send "y"\r;exp_continue}
}
expect eof;"
}
#####saltstack master or minion安装
saltstack_install() {
if [[ $1 == master ]];then
print_log "salt-master 开始安装"
yum -y install salt-master
#result=`yum -y install salt-master|grep 'No package'|wc -l`
if [[ $? -eq 0 ]];then
print "salt-master 安装成功"
else
yum_update
yum -y install salt-master
fi
elif [[ $1 == minion ]];then
print_log "salt-minion 开始安装"
yum -y install salt-minion
if [[ $? -eq 0 ]];then
print_log "salt-minin 安装成功"
else
yum_update
yum -y install salt-minion
fi
else
echo "Usage:master|minion"
fi
}
######centos5环境准备
five_env() {
cd $yum_Warehouse
[[ -f CentOS-Base.repo ]]&&mv CentOS-Base.repo CentOS-Base.repo.back
wget http://mirrors.aliyun.com/repo/Centos-5.repo
wget --no-check-certificate -O - http://bootstrap.saltstack.org | sh
if [[ $? -ne 0 ]];then
yum_update
wget --no-check-certificate -O - http://bootstrap.saltstack.org | sh
fi
}
#####centos6环境准备
six_env() {
cd /usr/local/src/
wget -c http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
sed -i 's/^#baseurl/baseurl/g' /etc/yum.repos.d/epel.repo
sed -i 's/^mirrorlist/#mirrorlist/g' /etc/yum.repos.d/epel.repo
}
####centos7环境准备
siven_env() {
cd $packge_dir
wget https://repo.saltstack.com/yum/rhel7/SALTSTACK-GPG-KEY.pub
rpm --import SALTSTACK-GPG-KEY.pub
rm -f SALTSTACK-GPG-KEY.pub
echo -e "####################\n# Enable SaltStack's package repository\n[saltstack-repo]\nname=SaltStack repo for RHEL/CentOS 7\nbaseurl=https://repo.saltstack.com/yum/rhel7\nenabled=1\ngpgcheck=1\ngpgkey=https://repo.saltstack.com/yum/rhel7/SALTSTACK-GPG-KEY.pub" > $yum_Warehouse/saltstack.repo
}
#####判断saltstack是否已经安装
Whether_installed() {
###判断输入的位移参数是否正确
if [[ $1 == master ]]|| [[ $1 == minion ]];then
print_log "参数正确"
else
echo "Usage:master|minion"
exit 1
fi
which salt-$1
if [[ $? -eq 0 ]];then
print_log "salt-"$1" 已经安装,请确认!"
exit 1
else
print_log "salt-"$1" 未安装,继续下面的操作"
fi
}
Whether_installed "$1"
wget_judge
case $version_num in
5)
five_env
#saltstack_install "$1"
;;
6)
six_env
saltstack_install "$1"
;;
7)
siven_env
saltstack_install "$1"
;;
*)
echo "Usage:Operating system version 5|6|7,saltstack master|minion"
;;
esac
exit 1
来源:oschina
链接:https://my.oschina.net/u/2343310/blog/799934