#先写你所需要的IP及hostname到hostname.txt文件中
#!/bin/bash
# 将所需要的文件放入/tmp/local(需要创建该文件夹)中
master_ip=$(cat /tmp/local/hostname.txt|head -n 1|awk '{print #1}')
master_name=$(cat /tmp/local/hostname.txt|head -n 1|awk '{print $2}')
all_ip=$(awk '{print $1}' /tmp/local/hostname.txt)
name=$( cat /tmp/local/hostname.txt|awk 'NR!=1 {print $2}')
#静态ip及主机名
function Hosts()
{
cat /tmp/local/hostname.txt |while read line
do
ip=$(echo $line|awk '{print $1}');
name=$(echo $line|awk '{print $2}');
echo "\n 选择 \n"
echo "1. ip,name"
echo "2. ssh"
echo "3. flash"
select answer in {1..3}
do
echo
case "$answer" in
1)
for i in $ip
do
if [ $i -eq $master_ip ]
then
enp=$(ls /etc/sysconfig/network-scripts/|egrep ifcfg |grep en.*|awk -F"-" '{print $2}')
enpway=/etc/sysconfig/network-scripts/ifcfg-$enp
sed -i 's/dhcp/static/g' $enpway
sed -i '/ONBOOT/c ONBOOT=yes' $enpway
cat >> $enpway <<EOF
IPADDR=$i
EOF
hostnamectl set-hostname $master_name
cat /tmp/local/hostname.txt >> /etc/hosts
# 免密
ssh-keygen -t ras -N '' << EOF
/root/.ssh/id_rsa
yes
EOF
else
ssh $i >/dev/null 2>&1 <<eeooff
yes
cqie
enp=$(ls /etc/sysconfig/network-scripts/|egrep ifcfg |grep en.*|awk -F"-" '{print $2}')
enpway=/etc/sysconfig/network-scripts/ifcfg-$enp
sed -i 's/dhcp/static/g' $enpway
sed -i '/ONBOOT/c ONBOOT=yes' $enpway
echo 'IPADDR=$i' >> $enpway
names=$(grep $i|awk '{print $2}')
hostnamectl set-hostname $names
cat /tmp/local/hostname.txt >> /etc/hosts
ssh-keygen -t ras -N '' << EOF
/root/.ssh/id_rsa
yes
EOF
exit
eeooff
fi
done
;;
2)
for i in $name
do
ssh-copy-id $name << EOF
yes
cqie
EOF
done
;;
3)
break
;;
esac
done
done
}
# 关闭不需要的服务
function Close()
{
cat /tmp/local/hostname.txt |while read line
do
name=$(echo $line|awk 'NR!=1 {print $2}')
for i in $name
do
ssh $name >/dev/null 2>&1 <<eeooff
systemctl stop firewalld
systemctl disable firewalld
# 透明大页面关闭(enforcing)(shell更改)
sed -i "s/enforcing/disabled/g" /etc/sysconfig/selinux
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.d/rc.local
echo "echo never > /sys/kernel/mm/transparent_hugepage/defrag" >> /etc/rc.d/rc.local
source /etc/rc.d/rc.local
# swappiness设置
echo "swappiness=0" >> /etc/sysctl.conf
# 时间同步
sed -i "s/^server/#/g" /etc/chrony.conf
cat >> /etc/chrony.conf <<EOF
allow all
server $master_ip iburst
local stratum 10
EOF
exit
eeooff
done
systemctl stop firewalld
systemctl disable firewalld
# 透明大页面关闭(enforcing)(shell更改)
sed -i "s/enforcing/disabled/g" /etc/sysconfig/selinux
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.d/rc.local
echo "echo never > /sys/kernel/mm/transparent_hugepage/defrag" >> /etc/rc.d/rc.local
source /etc/rc.d/rc.local
# swappiness设置
echo "swappiness=0" >> /etc/sysctl.conf
# 时间同步
sed -i "s/^server/#/g" /etc/chrony.conf
cat >> /etc/chrony.conf <<EOF
allow all
server ntp.aliyun.com iburst
local stratum 10
EOF
dir=$(dirname $(find / -name httpd*.rpm))
cd $dir
rpm -ivh httpd*.rpm
cd
systemctl restart httpd
systemctl enable httpd
done
}
# 安装包
function Install_rpm()
{
app="wget vim net-tools createrepo"
if [ $? -eq 0 ] ; then
dir=$(dirname $(find / -name $app*.rpm))
cd $dir
rpm -ivh $app*.rpm &> /dev/null
cd
if [ $? -eq 0 ] ;then echo " 程序:$app 已经安装"
else echo "程序安装error"
fi
else echo "error"
fi
for i in $name
do
ssh $name >/dev/null 2>&1 <<eeooff
app="wget vim net-tools createrepo"
if [ $? -eq 0 ] ; then
dir=$(dirname $(find / -name $app*.rpm))
cd dir
rpm -ivh $app*.rpm &> /dev/null
cd
if [ $? -eq 0 ] ;then echo " 程序:$app 已经安装"
else echo "程序安装error"
fi
else echo "error"
fi
exit
eeooff
done
}
#yum源的配置
function Createyum()
{
mkdir /var/www/html/cdh_local_repo
mkdir /var/www/html/parcels
cat >> /etc/yum.repos.d/yum.repo <<EOF
[yum]
name=yum
baseurl=http://$ips/cdh_local_repo
gpgcheck=0
enabled=1
EOF
createrepo /var/www/html/cdh_local_repo
yum clean all && yum makecache
for i in $name
do
ssh $name >/dev/null 2>&1 <<eeooff
yum clean all && yum makecache
exit
eeooff
done
}
#mysql
function Mysql_rpm()
{
app="jdk mysql-community-release mysql-community-server"
if [ $? -eq 0 ] ; then
dir=$(dirname $(find / -name $app*.rpm))
cd $dir
rpm -ivh $app*.rpm &> /dev/null
cd
if [ $? -eq 0 ] ;then echo " 程序:$app 已经安装"
else echo "程序安装error"
fi
else echo "error"
fi
systemctl start mysqld
systemctl enable mysqld
mkdir -p /usr/share/java/
a="mysql-connector-java"
dr=$(dirname $(find / -name $a*.gz))
cd $d
tar -xvf $a*.gz
cp mysql-connector-java-5.1.46/mysql-connector-java-5.1.46-bin.jar /usr/share/java/mysql-connector-java.jar
cd
mysql <<EOF
use mysql;
grant all privileges on *.* to root@'node' identified by "cqie" with grant option;
grant all privileges on *.* to root@'localhost' identified by "cqie" with grant option;
grant all privileges on *.* to root@'%' identified by "cqie" with grant option;
flush privileges;
create database scm default character set utf8;
create database metastore default character set utf8;
create database hue default character set utf8;
flush privileges;
EOF
for i in $name
do
ssh $name >/dev/null 2>&1 <<eeooff
drs=$(dirname $(find / -name jdk*.rpm))
cd $drs
rpm -ivh jdk*.rpm &> /dev/null
cd
exit
eeooff
done
}
#cloudera服务
function Clouderas()
{
app="cloudera-manager-agnet cloudera-manager-server"
if [ $? -eq 0 ] ; then
dir=$(dirname $(find / -name $app*.rpm|head -n 1))
cd $dir
rpm -ivh $app*.rpm &> /dev/null
cd
if [ $? -eq 0 ] ;then echo " 程序:$app 已经安装"
else echo "程序安装error"
fi
else echo "error"
fi
sed -i "s/server_host=localhost/server_host=$master_ip/g" /etc/cloudera-scm-agent/config.ini
/opt/cloudera/cm/schema/scm_prepare_database.sh mysql scm root cqie
systemctl start cloudera-scm-agent
systemctl enable cloudera-scm-agent
systemctl start cloudera-scm-server
systemctl enable cloudera-scm-server
for i in $name
do
ssh $name >/dev/null 2>&1 <<eeooff
dir=$(dirname $(find / -name cloudera-manager-agent*.rpm|head -n 1))
cd $dir
rpm -ivh cloudera-manager-agent*.rpm
cd
sed -i "s/server_host=localhost/server_host=$master_ip/g" /etc/cloudera-scm-agent/config.ini
systemctl start cloudera-scm-agent
systemctl enable cloudera-scm-agent
exit
eeooff
done
}
#调用函数
function main()
{
Hosts
Close
Install_rpm
Createyum
Mysql_rpm
Clouderas
}
main