基于Gitlab+Jenkins+Ansible控制JavaTomcat多版本环境部署

简单说明:


本博文实现CentOS6模板机克隆和JavaTomcat多版本运行环境部署的Jenkins构建实施

依据《基于Gitlab+Jenkins的测试环境自动构建和生产多环境手动发布方案》部署构建环境,部署完成Nginx环境搭建
将需要同步的安装包存放在jenkins的nginx服务对应目录/usr/local/nginx/html之下
浏览器登陆gitlab,创建devops项目,设置分支保护,只允许merge请求来合并master分支,并且只允许master角色确认merge请求
依据《CentOS7 python3.6 ansible2.5 源码安装》在jenkins主机的deploy用户下部署ansible
依据《CentOS6重置网卡克隆后的自适应脚本》对CentOS6模板机进行改造,将可用vlan的IP设置为192.168.1.250/192.168.77.250

Jenkins 主机操作:


yum -y install sshpass
su - deploy

cd ~
git clone [email protected]:root/devops.git
git config --global user.name "deploy"
git config --global user.email "deploy@jeninks"
cd devops
mkdir -pv centos6_java_tomcat_init
mkdir -pv centos6_java_tomcat_init/{inventory,roles}

cat >centos6_java_tomcat_init/inventory/proenv.source<<EOF
CloneIPValue
[Server]
CloneIPValue
[Server:vars]
Host_name=Host_nameValue
Host_IP=Host_IPValue
Ethernet=EthernetValue
ZabbixServer=ZabbixServerValue
NginxServer=192.168.77.130
ZabbixAgentRpm=zabbix-agent-3.0.23-1.el6.x86_64.rpm
jre16=jre-6u45-linux-x64.bin
jre17=server-jre-7u80-linux-x64.tar.gz
jre18=server-jre-8u172-linux-x64.tar.gz
tomcat6=apache-tomcat-6.0.53.tar.gz
tomcat7=apache-tomcat-7.0.86.tar.gz
tomcat8=apache-tomcat-8.0.51.tar.gz
EOF

mkdir -pv centos6_java_tomcat_init/roles/os/{files,tasks,templates}
cat >centos6_java_tomcat_init/roles/os/templates/os.sh.j2<<EOF
#!/bin/bash
source ~/.bash_profile
hostname {{ Host_name }}
echo "\$(grep -E '127|::1' /etc/hosts)">/etc/hosts
echo "{{ Host_IP }} {{ Host_name }}">>/etc/hosts
sed -i "s/^HOSTNAME.*$/HOSTNAME={{ Host_name }}/g" /etc/sysconfig/network
sed -i 's/\(IPADDR=\).*/\1{{ Host_IP }}/g' /etc/sysconfig/network-scripts/ifcfg-{{ Ethernet }}
yum -y localinstall /tmp/{{ ZabbixAgentRpm }}
sed -i 's/^Server=127.0.0.1/Server={{ ZabbixServer }}/g' /etc/zabbix/zabbix_agentd.conf
sed -i 's/^ServerActive=127.0.0.1/ServerActive={{ ZabbixServer }}:10051/g' /etc/zabbix/zabbix_agentd.conf
sed -i "s/^\(Hostname=\).*/\1{{ Host_name }}/g" /etc/zabbix/zabbix_agentd.conf
chkconfig zabbix-agent on
/etc/init.d/zabbix-agent stop
EOF

cat >centos6_java_tomcat_init/roles/os/tasks/main.yml<<EOF
- name: rsync os.sh
  template: src=roles/os/templates/os.sh.j2 dest=/tmp/os.sh
- name: wget zabbix agent rpm
  get_url: "url=http://{{ NginxServer }}/zabbix/{{ ZabbixAgentRpm }} dest=/tmp group=root owner=root mode=644"
- name: setup hosts
  shell: "bash /tmp/os.sh"
- name: update os
  shell: "yum -y update warn=False"
- name: reboot linux
  shell: "reboot"
EOF

cat >centos6_java_tomcat_init/deployOs.yml<<EOF
- hosts: "Server"
  gather_facts: true
  remote_user: root
  roles:
    - os
EOF

mkdir -pv centos6_java_tomcat_init/roles/java/{files,templates,tasks}
cat >centos6_java_tomcat_init/roles/java/templates/java.sh.j2<<EOFALL
#!/bin/bash
source ~/.bash_profile
# chattr -i -R /usr/local/java &>/dev/null
rm -rf /usr/local/java
mkdir -p /usr/local/java
cd /usr/local/java
bash /tmp/{{ jre16 }}
find . -name "jre1.6*" -exec ln -s {} java_1.6 \;
tar -xf /tmp/{{ jre17 }}
find . -name "jdk1.7*" -exec ln -s {} java_1.7 \;
tar -xf /tmp/{{ jre18 }}
find . -name "jdk1.8*" -exec ln -s {} java_1.8 \;
chown root: -R .
cat >java_1.6_env<$JAVA_HOME/lib:\$JAVA_HOME/jre/lib
export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH
EOF
chmod 444 java_1.6_env
cat >java_1.7_env<$JAVA_HOME/lib:\$JAVA_HOME/jre/lib
export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH
EOF
chmod 444 java_1.7_env
cat >java_1.8_env<$JAVA_HOME/lib:\$JAVA_HOME/jre/lib
export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH
EOF
chmod 444 java_1.8_env
# chattr +i -R /usr/local/java &>/dev/null
EOFALL
sed -i 's/\$/\\$/g' centos6_java_tomcat_init/roles/java/templates/java.sh.j2

cat >centos6_java_tomcat_init/roles/java/tasks/main.yml<<EOF
- name: wget java16
  get_url: "url=http://{{ NginxServer }}/jre/{{ jre16 }} dest=/tmp group=root owner=root mode=644"
- name: wget java17
  get_url: "url=http://{{ NginxServer }}/jre/{{ jre17 }} dest=/tmp group=root owner=root mode=644"
- name: wget java18
  get_url: "url=http://{{ NginxServer }}/jre/{{ jre18 }} dest=/tmp group=root owner=root mode=644"
- name: rsync java.sh
  template: src=roles/java/templates/java.sh.j2 dest=/tmp/java.sh
- name: setup java environment
  command: "bash /tmp/java.sh"
EOF

mkdir -pv centos6_java_tomcat_init/roles/tomcat/{files,templates,tasks}
cat >centos6_java_tomcat_init/roles/tomcat/templates/tomcat_deploy.sh.j2<<EOFALL
#!/bin/bash
source ~/.bash_profile
chattr -i -R /usr/local/tomcat
rm -rf /usr/local/tomcat
mkdir -pv /usr/local/tomcat
cd /usr/local/tomcat
tar -xf /tmp/{{ tomcat6 }}
find . -name "apache-tomcat-6*" -exec ln -s {} tomcat6 \;
tar -xf /tmp/{{ tomcat7 }}
find . -name "apache-tomcat-7*" -exec ln -s {} tomcat7 \;
tar -xf /tmp/{{ tomcat8 }}
find . -name "apache-tomcat-8*" -exec ln -s {} tomcat8 \;
chown root: -R .
unset OPTS1 OPTS2 OPTS3 OPTS4
OPTS1="-Djava.security.egd=file:/dev/./urandom -Xms1256m -Xmx1512m"
OPTS2=\${OPTS1}" -XX:PermSize=164m -XX:MaxPermSize=228m"
OPTS3=\${OPTS2}" -XX:-UseGCOverheadLimit"
OPTS4=\${OPTS3}" -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp"
sed -i "s|cygwin=false|JAVA_OPTS=\"\${OPTS3}\"\n&|g" tomcat*/bin/catalina.sh
sed -i "s|cygwin=false|# JAVA_OPTS=\"\${OPTS4}\"\n&|g" tomcat*/bin/catalina.sh
sed -i 's/8005/18080/g' tomcat*/conf/server.xml
sed -i 's/8443/9443/g' tomcat*/conf/server.xml
sed -i 's/8009/28080/g' tomcat*/conf/server.xml
sed -i 's/Connector port="8080" protocol="HTTP\/1.1"/&\
               maxThreads="800" acceptCount="1000"\
               compression="on"\
               compressionMinSize="2048"\
               noCompressionUserAgents="gozilla,traviata"\
               compressableMimeType="text\/html,text\/xml,text\/javascript,text\/css,text\/plain"/g' tomcat*/conf/server.xml
sed -i 's|appBase="webapps"|appBase="/web/project/tomcat6"|g' tomcat6/conf/server.xml
sed -i 's|appBase="webapps"|appBase="/web/project/tomcat7"|g' tomcat7/conf/server.xml
sed -i 's|appBase="webapps"|appBase="/web/project/tomcat8"|g' tomcat8/conf/server.xml
sed -i 's|
                    

你可能感兴趣的:(DevOps和生产中间件)