因某些原因 ,公司会经常新增服务器配置环境变量,故写了此自动化部署脚本。再把脚本放到jenkins服务器上,每次批量部署环境前,先修改新买服务器的主机名,再在jenkins hosts里配置好主机名,最后点击jenkins即可。
1 #/bin/bash 2 #先把相关软件复制到相关目录再执行本脚本 3 4 #set -e 5 #安装基本软件 6 yum install -y lrzsz dos2unix nginx 7 8 #新增账号 9 useradd dev && sed -n '1p' /data1/yunwei/passwd | passwd --stdin dev 10 useradd wuyun && sed -n '2p' /data1/yunwei/passwd | passwd --stdin wuyun 11 12 usermod -a -G dev wuyun 13 usermod -a -G dev nginx 14 15 #设置环境变量 16 mv /data1/yunwei/pack_tar/jdk-8u92-zhuanyong.tar.gz /usr/local/ 17 tar zxvf /usr/local/jdk-8u92-zhuanyong.tar.gz -C /usr/local/ 18 echo " 19 20 export HISTTIMEFORMAT="%Y-%m-%d:%H-%M-%S `whoami`: " 21 22 export JAVA_HOME=/usr/local/jdk1.8.0_92 23 export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 24 export PATH=$JAVA_HOME/bin:$PATH 25 " >> /etc/profile 26 source /etc/profile 27 28 #更改熵池随机数 29 function rng(){ 30 yum install -y rng-tools 31 cp /usr/lib/systemd/system/rngd.service /etc/systemd/system 32 sed -i '5s/$/& -r /dev/urandom/' /etc/systemd/system/rngd.service 33 systemctl daemon-reload 34 systemctl restart rngd 35 } 36 37 #安装zabbix-agent 38 rpm -ihv http://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/6/x86_64/zabbix-release-3.0-1.el6.noarch.rpm 39 yum install zabbix-agent -y 40 name=`hostname` 41 sed -i '95,136s/127.0.0.1/x.x.x.x/' /etc/zabbix/zabbix_agentd.conf 42 sed -i '147s/Zabbix Server/'$name'/' /etc/zabbix/zabbix_agentd.conf 43 systemctl start zabbix-agent 44 45 46 47 hostname=`hostname | cut -d - -f2` 48 49 #安装字体 50 yum -y install fontconfig 51 cp /data1/yunwei/font/alger.ttf /usr/share/fonts/ 52 fc-cache -f 53 54 55 function redis_stuep(){ 56 57 #安装redis伪集群脚本,先把redis-4.0.9.tar.gz redis-3.2.2.gem及启动脚本放在/data1/redis-cluster目录下,然后执行该脚本即可 58 set -e 59 #获取redis本机ip 60 ip=`ifconfig eth0 | grep inet | awk '{print $2}'` 61 62 #解压安装redis 63 cd /data1/redis-cluster 64 tar zxvf redis-4.0.9.tar.gz -C /data1/redis-cluster 65 cd /data1/redis-cluster/redis-4.0.9 66 make && make install PREFIX=/data1/redis-cluster 67 68 #配置redis 69 cd /data1/redis-cluster 70 mv bin redis-01 71 cp /data1/redis-cluster/redis-4.0.9/redis.conf redis-01 72 sed -i '69s/127.0.0.1/'$ip'/' redis-01/redis.conf 73 sed -i '92,158s/6379/7001/' redis-01/redis.conf 74 sed -i '136s/no/yes/' redis-01/redis.conf 75 sed -i '672s/no/yes/' redis-01/redis.conf 76 sed -i '814s/#//' redis-01/redis.conf 77 sed -i '822s/#//' redis-01/redis.conf 78 sed -i '828s/#//' redis-01/redis.conf 79 80 81 cp -r redis-01 redis-02 82 cp -r redis-01 redis-03 83 cp -r redis-01 redis-04 84 cp -r redis-01 redis-05 85 cp -r redis-01 redis-06 86 87 sed -i '92,158s/7001/7002/' redis-02/redis.conf 88 sed -i '92,158s/7001/7003/' redis-03/redis.conf 89 sed -i '92,158s/7001/7004/' redis-04/redis.conf 90 sed -i '92,158s/7001/7005/' redis-05/redis.conf 91 sed -i '92,158s/7001/7006/' redis-06/redis.conf 92 93 #启动redis 94 chmod 741 /data1/redis-cluster/*.sh 95 /data1/redis-cluster/all-start.sh 96 sleep 3 97 #安装redis-trib所需的 ruby脚本 98 cp /data1/redis-cluster/redis-4.0.9/src/redis-trib.rb /data1/redis-cluster/redis-trib.rb 99 yum install -y ruby rubygems 100 gem install redis-3.2.2.gem 101 102 yum install -y expect 103 #配置集群,自动交互 104 expect -c " 105 cd /data1/redis-cluster; 106 spawn ./redis-trib.rb create --replicas 1 $ip:7001 $ip:7002 $ip:7003 $ip:7004 $ip:7005 $ip:7006; 107 expect "configuration" { send "yes "; } 108 expect eof 109 " 110 cp /data1/redis-cluster/redis-01/redis-cli /usr/bin/redis-cli 111 echo "redis-cluster is ok " 112 113 } 114 115 if [ $hostname == "api" ];then 116 rng 117 mkdir -p /data1/www/{download/apps,gateway} 118 chown -R dev.dev /mnt 119 if [ `hostname | cut -d - -f3` -eq 03 ];then 120 cp -r /data1/yunwei/pack_tar/tomcat/app-03/application.zip /data1/ 121 mv /data1/yunwei/systemctl-shell/ysc/* /lib/systemd/system/ 122 mv /data1/yunwei/nginx/*test.conf /etc/nginx/conf.d/ 123 unzip -o /data1/application.zip -d /data1/ 124 chown -R dev.dev /data1/ 125 find /data1/application/* -name "*.sh" | xargs chmod 741 126 systemctl start tomcat-1 127 systemctl start tomcat-2 128 systemctl start tomcat-3 129 systemctl enable tomcat-1 130 systemctl enable tomcat-2 131 systemctl enable tomcat-3 132 133 else 134 mkdir -p /data1/application/sms/{logs,bak} 135 mkdir -p /data1/bak/api 136 cp -r /data1/yunwei/pack_tar/tomcat/app-01/application.zip /data1/ 137 mv /data1/yunwei/systemctl-shell/tomcat.service /lib/systemd/system/ 138 mv /data1/yunwei/nginx/api.conf /etc/nginx/conf.d/api.conf 139 unzip -o /data1/application.zip -d /data1/ 140 find /data1/application/* -name "*.sh" | xargs chmod 741 141 chown -R dev.dev /data1/ 142 systemctl start tomcat 143 systemctl enable tomcat 144 fi 145 systemctl start nginx 146 systemctl enable nginx 147 rm -rf /data1/application.zip 148 elif [ $hostname == "redis" ];then 149 mkdir -p /data1/redis-cluster 150 mv /data1/yunwei/redis/* /data1/redis-cluster 151 yum -y remove nginx 152 redis_stuep 153 else 154 rng 155 cp -r /data1/yunwei/pack_tar/tomcat/cs-server/application.zip /data1/ 156 mv /data1/yunwei/systemctl-shell/tomcat-* /lib/systemd/system/ 157 mv /data1/yunwei/nginx/cs.conf /etc/nginx/conf.d/cs.conf 158 mv /data1/yunwei/nginx/server.conf /etc/nginx/conf.d/server.conf 159 unzip -o /data1/application.zip -d /data1/ 160 find /data1/application/* -name "*.sh" | xargs chmod 741 161 systemctl start nginx 162 systemctl enable nginx 163 mkdir -p /data1/bak/{cs/tomcat-1,server/tomcat-2} 164 chown -R dev.dev /data1/ 165 systemctl start tomcat-1 166 systemctl start tomcat-2 167 systemctl enable tomcat-1 168 systemctl enable tomcat-2 169 rm -rf /data1/application.zip 170 fi 171 172 rm -rf /data1/yunwei*