forked from luckman666/mutual_trust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutual_trust.sh
273 lines (225 loc) · 6.34 KB
/
mutual_trust.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/bash
#source ./base.config
bash_path=$(cd "$(dirname "$0")";pwd)
source $bash_path/base.config
if [[ "$(whoami)" != "root" ]]; then
echo "please run this script as root ." >&2
exit 1
fi
#log="./setup.log" #操作日志存放路径
#fsize=2000000
#exec 2>>$log #如果执行过程中有错误信息均输出到日志文件中
echo -e "\033[31m 这个是互信脚本!欢迎关注我的个人公众号“波哥的IT人生”获得更多实用工具!Please continue to enter or ctrl+C to cancel \033[0m"
sleep 5
#yum update
yum_update(){
yum update -y
}
#configure yum source
yum_config(){
yum install wget epel-release -y
if [[ $aliyun == "1" ]];then
test -d /etc/yum.repos.d/bak/ || yum install wget epel-release -y && cd /etc/yum.repos.d/ && mkdir bak && mv -f *.repo bak/ && wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && yum clean all && yum makecache
fi
}
yum_init(){
num=0
while true ; do
let num+=1
yum -y install iotop sshpass iftop yum-utils net-tools rsync git lrzsz expect gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel bash-completion
if [[ $? -eq 0 ]] ; then
echo "初始化安装环境配置完成!!!"
break;
else
if [[ num -gt 3 ]];then
echo "你登录 "$masterip" 瞅瞅咋回事?一直无法yum包"
break
fi
echo "FK!~没成功?哥再来一次!!"
fi
done
}
#firewalld
firewalld_config(){
if [[ `ps -ef | grep firewalld |wc -l` -gt 1 ]];then
systemctl stop firewalld.service
systemctl disable firewalld.service
echo "防火墙我关了奥!!!"
fi
# iptables -P FORWARD ACCEPT
}
#system config
selinux_config(){
grep "SELINUX=disabled" /etc/selinux/config
if [[ $? -eq 0 ]];then
echo "SELINUX 已经禁用!!"
else
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
setenforce 0
echo "SELINUX 已经禁用!!"
fi
}
ntp_config(){
if [[ `ps -ef | grep chrony |wc -l` -eq 1 ]];then
timedatectl set-local-rtc 1 && timedatectl set-timezone Asia/Shanghai
yum -y install chrony && systemctl start chronyd.service && systemctl enable chronyd.service
systemctl restart chronyd.service
echo "时钟同步chrony服务安装完毕!!"
fi
}
ulimit_config(){
grep 'ulimit' /etc/rc.local
if [[ $? -eq 0 ]];then
echo "内核参数调整完毕!!!"
else
echo "ulimit -SHn 102400" >> /etc/rc.local
cat >> /etc/security/limits.conf << EOF
* soft nofile 102400
* hard nofile 102400
* soft nproc 102400
* hard nproc 102400
* soft memlock unlimited
* hard memlock unlimited
EOF
cat >> /etc/sysctl.conf << EOF
kernel.pid_max=4194303
vm.swappiness = 0
EOF
sysctl -p
echo "内核参数调整完毕!!!"
fi
}
ssh_config(){
grep 'UserKnownHostsFile' /etc/ssh/ssh_config
if [[ $? -eq 0 ]];then
echo "ssh参数配置完毕!!!"
else
sed -i "2i StrictHostKeyChecking no\nUserKnownHostsFile /dev/null" /etc/ssh/ssh_config
echo "ssh参数配置完毕!!!"
fi
}
get_localip(){
if [[ ! -n "$network_card" ]];then
ipaddr=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}' | grep $ip_segment)
else
ipaddr=$(ifconfig $network_card | awk '/inet/ {print $2}' | cut -f2 -d ":" |awk 'NR==1 {print $1}')
fi
echo "$ipaddr"
}
change_hosts(){
cd $bash_path
num=0
for host in ${hostip[@]}
do
grep "$host" /etc/hosts
if [[ $? -eq 0 ]];then
echo "hosts修改完毕!!!"
else
let num+=1
if [[ $host == `get_localip` ]];then
`hostnamectl set-hostname $hostname$num`
echo $host `hostname` >> /etc/hosts
else
echo $host $hostname$num >> /etc/hosts
fi
fi
done
}
#install docker
install_docker() {
test -d /etc/docker
if [[ $? -eq 0 ]];then
echo "docker已经安装完毕!!!"
else
mkdir -p /etc/docker
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
num=0
while true ; do
let num+=1
yum install -y --setopt=obsoletes=0 docker-ce-$dockerVersion
if [[ $? -eq 0 ]] ; then
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://gpkhi0nk.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl enable docker
systemctl restart docker
echo "docker 安装成功!!!"
break;
else
if [[ num -gt 3 ]];then
echo "docker 安装3次仍然失败,退出安装docker!"
break
fi
echo "docker 安装?哥再来一次!!"
fi
done
fi
}
# config docker
config_docker(){
grep "tcp://0.0.0.0:2375" /usr/lib/systemd/system/docker.service
if [[ $? -eq 0 ]];then
echo "docker API接口已经配置完毕"
else
sed -i "/^ExecStart/cExecStart=\/usr\/bin\/dockerd -H tcp:\/\/0\.0\.0\.0:2375 -H unix:\/\/\/var\/run\/docker.sock" /usr/lib/systemd/system/docker.service
systemctl daemon-reload
systemctl restart docker.service
echo "docker API接口已经配置完毕"
fi
}
pull_ceph_image(){
docker pull registry.cn-hangzhou.aliyuncs.com/yangb/ceph_luminous
echo "docker 镜像下载完毕"
}
rootssh_trust(){
yum -y install sshpass
cd $bash_path
num=0
for host in ${hostip[@]}
do
let num+=1
if [[ `get_localip` != $host ]];then
if [[ ! -f /root/.ssh/id_rsa.pub ]];then
echo '###########init'
expect ssh_trust_init.exp
sshpass -p $root_passwd ssh-copy-id $host
else
echo '###########add'
sshpass -p $root_passwd ssh-copy-id $host
fi
scp base.config mutual_trust_node.sh ssh_trust_init.exp root@$host:/root && scp /etc/hosts root@$host:/etc/hosts && ssh root@$host "hostnamectl set-hostname $hostname$num" && ssh root@$host /root/mutual_trust_node.sh && ssh root@$host "rm -rf mutual_trust_node.sh ssh_trust_init.exp"
if [[ $ntpConfig == "1" ]];then
scp hwclock_ntp.sh root@$host:/root && ssh root@$host /root/hwclock_ntp.sh && ssh root@$host "rm -rf /root/hwclock_ntp.sh /root/base.config"
fi
fi
done
}
main(){
yum_config
ssh_config
if [[ $firewalldClose == "1" ]];then
firewalld_config
fi
if [[ $selinuxClose == "1" ]];then
selinux_config
fi
if [[ $ntpConfig == "1" ]];then
ntp_config
fi
if [[ $ulimitConfig == "1" ]];then
ulimit_config
fi
change_hosts
if [[ $dockerInstall == "1" ]];then
install_docker
config_docker
fi
rootssh_trust
echo "互信完成,请登录相关服务器验收!"
}
#main > ./setup.log 2>&1
main