touch wso2esb.sh && chmod +x wso2esb.sh
#!/bin/bash # 安装路径 DIR_NAME=wso2esb-5.0.0 DATADIR=/app/$DIR_NAME # 日志文件 LOG_FILE=/tmp/wso2esb.log # 安装包 ZIP_FILE=/root/wso2esb-5.0.0.zip # wso2的用户名 WSO2_USER=wso2 # wso2监听端口 WSO2_PORT=9443 #网卡名 NETWORK_CARD=ens33 check_err() { if [ $? -ne 0 ]; then echo "$1" exit 1 else echo "$2" fi } start() { netstat -tln | grep ":$WSO2_PORT" CHECK_RESULT=$? #取当前用户的变量值,$的前面要加反斜杠 if [ $CHECK_RESULT -eq 0 ];then echo "端口 $WSO2_PORT 正在监听,请确定应用是否启动或者是否是其他应用占用了端口!" exit 1 fi echo "正在启动 WSO2 ESB....." cat /etc/passwd | grep -w $WSO2_USER if [ $? -ne 0 ]; then echo "不存在 $WSO2_USER 用户,请执行 install 命令!" exit 1 fi if [ ! -d "$DATADIR" ];then echo "安装目录 $DATADIR 不存在,请执行 install 命令!" exit 1 fi if [ ! -f "$LOG_FILE" ];then echo "日志文件 $LOG_FILE 不存在,请执行 install 命令!" exit 1 fi IP_ADDR=`ifconfig $NETWORK_CARD | grep 'inet ' | sed 's/^.*inet //g' | sed 's/netmask.*$//g' | sed 's/ //g'` echo "当前服务器 IP 地址为:$IP_ADDR。" #替换HostName HOSTNAME_ROW=`cat $DATADIR/repository/conf/carbon.xml | grep -n -w "HostName" | awk -F ":" '{print $1}'` # sed 替换可以通过 / # _ @ * 进行分割,xml标签中是含有反斜线/的,这个在替换的时候会出现异常,这里使用 @ 来替换 / sed -i "${HOSTNAME_ROW}"'s@.*@<HostName>'"$IP_ADDR"'</HostName>@g' $DATADIR/repository/conf/carbon.xml #替换MgtHostName MGTHOSTNAME_ROW=`cat $DATADIR/repository/conf/carbon.xml | grep -n -w "MgtHostName" | awk -F ":" '{print $1}'` sed -i "${MGTHOSTNAME_ROW}"'s@.*@<MgtHostName>'"$IP_ADDR"'</MgtHostName>@g' $DATADIR/repository/conf/carbon.xml #替换http transportReceiver TRANSPORTRECESBVER_ROW=`cat $DATADIR/repository/conf/axis2/axis2.xml | grep -n -w "PassThroughHttpListener" | awk -F ":" '{print $1}'` ROWE=$(($TRANSPORTRECESBVER_ROW+4)) sed -i "${ROWE}"'s@.*@<parameter name="WSDLEPRPrefix" locked="false">http://'"$IP_ADDR"':8280</parameter>@g' $DATADIR/repository/conf/axis2/axis2.xml #替换https transportReceiver TRANSPORTRECESBVERS_ROW=`cat $DATADIR/repository/conf/axis2/axis2.xml | grep -n -w "PassThroughHttpSSLListener" | awk -F ":" '{print $1}'` ROWES=$(($TRANSPORTRECESBVERS_ROW+4)) sed -i "${ROWES}"'s@.*@<parameter name="WSDLEPRPrefix" locked="false">https://'"$IP_ADDR"':8280</parameter>@g' $DATADIR/repository/conf/axis2/axis2.xml chown -R $WSO2_USER:$WSO2_USER $LOG_FILE chmod 755 $LOG_FILE chown -R $WSO2_USER:$WSO2_USER $DATADIR chmod 755 $DATADIR chmod +x $DATADIR/bin/wso2server.sh # 切换为es用户 su $WSO2_USER << EOF # 不加反斜杠,取的值是切换用户前的 nohup $DATADIR/bin/wso2server.sh >>$LOG_FILE 2>&1 & #每5s检查一次监听端口是否启动,总共检查60次共10分钟,超时未启动则退出 CHECK_RESULT=1 CHECK_COUNT=60 while [ \$CHECK_RESULT -ne 0 ];do if [ \$CHECK_COUNT -gt 0 ];then echo "等待监听端口启动..." sleep 5 netstat -tln | grep ":$WSO2_PORT" CHECK_RESULT=\$? CHECK_COUNT=\$((\$CHECK_COUNT-1)) else echo "ERROR: 启动$WSO2_PORT端口超时,请检查!" exit 1 fi done echo "WSO2 ESB 启动成功!" EOF #检查并关闭selinux CHECK_SELINUX=`getenforce` if [[ $CHECK_SELINUX != "Disabled" ]];then #临时关闭selinux setenforce 0 #配置文件关闭selinux,下次服务器重启后生效 sed -i '/^SELINUX=/s@.*@SELINUX=disabled@' /etc/selinux/config fi #检查防火墙是否已启动,如果启动则开放端口 systemctl status firewalld > /dev/null if [ $? -eq 0 ];then firewall-cmd --add-port=8243/tcp --permanent firewall-cmd --add-port=8280/tcp --permanent firewall-cmd --add-port=9443/tcp --permanent firewall-cmd --add-port=9763/tcp --permanent firewall-cmd --reload fi } stop() { netstat -tln | grep ":$WSO2_PORT" check_err "WSO2 ESB 未启动!" "WSO2 ESB 正在运行,开始停止应用....." su - $WSO2_USER << EOF PROCESS=`ps aux | grep $DATADIR | grep -v "grep" | grep -v "su $WSO2_USER" | grep -v "wso2server.sh" | awk '{print $2}'` for i in \${PROCESS} do echo "停止 WSO2 ESB 相关进程:[ \$i ]" kill -9 \$i done EOF CHECK_RESULT=0 CHECK_COUNT=60 while [ $CHECK_RESULT -eq 0 ];do if [ $CHECK_COUNT -gt 0 ];then echo "等待监听端口关闭..." sleep 5 netstat -tln | grep ":$WSO2_PORT" CHECK_RESULT=$? CHECK_COUNT=$(($CHECK_COUNT-1)) else echo "ERROR: 关闭$WSO2_PORT端口超时,请检查!" exit 1 fi done echo "关闭 WSO2 ESB 成功!" } status() { su $WSO2_USER -c "ps aux | grep $DATADIR | grep -v \"grep\" | grep -v \"su $WSO2_USER\" | grep -v \"wso2server.sh\"" } restart() { stop start } remove(){ netstat -tln | grep ":$WSO2_PORT" if [ $? -eq 0 ]; then stop fi if [ -f "$LOG_FILE" ];then DATE_TIME=`date "+%Y-%m-%d_%H-%M-%S"` mv -f $LOG_FILE $LOG_FILE.$DATE_TIME.bak fi if [ -d "$DATADIR" ];then rm -rf $DATADIR fi cat /etc/passwd | grep $WSO2_USER if [ $? -eq 0 ]; then userdel -r $WSO2_USER fi } install(){ #检查是否存在netstat命令,没有则安装 rpm -q net-tools if [ $? -ne 0 ]; then yum install net-tools -y fi rpm -q unzip if [ $? -ne 0 ]; then yum unzip -y fi netstat -tln | grep ":$WSO2_PORT" CHECK_RESULT=$? #取当前用户的变量值,$的前面要加反斜杠 if [ $CHECK_RESULT -eq 0 ];then echo "端口 $WSO2_PORT 正在监听,请确定应用是否启动或者是否是其他应用占用了端口!" exit 1 fi if [ ! -f "$ZIP_FILE" ];then echo "安装包 $ZIP_FILE 不存在!退出安装!" exit 1 fi if [ ! -f "$LOG_FILE" ];then touch $LOG_FILE fi if [ ! -d "$LOG_FILE" ];then mkdir -p $DATADIR fi if [ -d "$DATADIR" ];then rm -rf $DATADIR fi unzip -q $ZIP_FILE -d /tmp mv -f /tmp/$DIR_NAME $DATADIR #创建用户和组 cat /etc/group | grep -w $WSO2_USER if [ $? -ne 0 ]; then groupadd -g 10000 $WSO2_USER fi cat /etc/passwd | grep -w $WSO2_USER if [ $? -ne 0 ]; then useradd -u 10001 -g $WSO2_USER -d /home/$WSO2_USER -m $WSO2_USER fi chown -R $WSO2_USER:$WSO2_USER $LOG_FILE chmod 755 $LOG_FILE chown -R $WSO2_USER:$WSO2_USER $DATADIR chmod 755 $DATADIR start } usage () { echo " " echo "请输入 start 启动 wso2ei。" echo "请输入 stop 停止 wso2ei。" echo "请输入 status 获取 wso2ei 运行状态。" echo "请输入 restart 重启 wso2ei。" echo "请输入 install 安装 wso2ei。" echo "请输入 remove 卸载 wso2ei。" echo " " } INPUT_ACTIVE=$1 ACTIVE=${INPUT_ACTIVE:=usage} case ${ACTIVE} in start) start ;; stop) stop ;; status) status ;; restart) restart ;; install) install ;; remove) remove ;; *) usage ;; esac