Tomcat Multi Instance로 설정 하려면, 기동관련을 위해서 Shell도 만져야 하고, server.xml 도 만져야 한다.


그것을 Script에서만 하면 좋을것 같아서 고민 중, Tomcat에 적용해 보았다.


[env.sh]

#!/bin/sh

DATE=`date +%Y%m%d_%H%M%S`

export JAVA_HOME=/usr/local/java8
export PATH=$PATH:$JAVA_HOME/bin

# Apache Balance Workers 값(WAS별 유니크)
HTTPD_LB_WORKER_NM=testcluster
HTTPD_WORKER_NM=$HTTPD_LB_WORKER_NM'11'
PROJECT_NM_PREPIX=
PROJECT_NM=test-cluster
WAR_PATH=/usr/local/tomcat/webroot/$PROJECT_NM_PREPIX$PROJECT_NM
WEB_ROOT=$WAR_PATH/webapp

export HTTPD_WORKER_NM
export PROJECT_NM
export WAR_PATH
export WEB_ROOT

# 동일 머신에서 중복되지 않게(01~99: 최대 90개)
export PORT_OFFSET=1

let SHUTDOWN_PORT=9100+$PORT_OFFSET
let HTTP_PORT=9200+$PORT_OFFSET
let HTTPS_PORT=9300+$PORT_OFFSET
let AJP_PORT=9400+$PORT_OFFSET

export SHUTDOWN_PORT
export HTTP_PORT
export HTTPS_PORT
export AJP_PORT


export CATALINA_HOME=/usr/local/tomcat/apache-tomcat
export CATALINA_BASE=/usr/local/tomcat/domains/$HTTPD_LB_WORKER_NM

# JVM Options : Server
export JAVA_OPTS="-server $JAVA_OPTS"

# JVM Options : Memory
export JAVA_OPTS="$JAVA_OPTS -Xms2048m -Xmx2048m -Xss256k"
# jdk7 이하
#export JAVA_OPTS="$JAVA_OPTS -XX:PermSize=256m -XX:MaxPermSize=256m"

export JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCTimeStamps "
export JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails "
export JAVA_OPTS="$JAVA_OPTS -Xloggc:$CATALINA_BASE/logs/gc_$DATE.log "
export JAVA_OPTS="$JAVA_OPTS -XX:+UseParallelGC "
export JAVA_OPTS="$JAVA_OPTS -XX:+UseParallelOldGC "

export JAVA_OPTS="$JAVA_OPTS -XX:+ExplicitGCInvokesConcurrent "
export JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError "
export JAVA_OPTS="$JAVA_OPTS -XX:HeapDumpPath=$CATALINA_BASE/logs/heapDump-$DATE.hprof "

export JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true"
export JAVA_OPTS="$JAVA_OPTS -Dsun.rmi.dgc.client.gcInterval=0x7FFFFFFFFFFFFFFE"
export JAVA_OPTS="$JAVA_OPTS -Dsun.rmi.dgc.server.gcInterval=0x7FFFFFFFFFFFFFFE"
export JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true"



export JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF-8"
export JAVA_OPTS="$JAVA_OPTS -DjvmRoute=$HTTPD_WORKER_NM"
export JAVA_OPTS="$JAVA_OPTS -Dtomcat.web.root=$WEB_ROOT"
export JAVA_OPTS="$JAVA_OPTS -Dtomcat.port.shutdown=$SHUTDOWN_PORT"
export JAVA_OPTS="$JAVA_OPTS -Dtomcat.port.http=$HTTP_PORT"
export JAVA_OPTS="$JAVA_OPTS -Dtomcat.port.https=$HTTPS_PORT"
export JAVA_OPTS="$JAVA_OPTS -Dtomcat.port.ajp=$AJP_PORT"

# WEB JK_MOD 정보
export HTTPD_HOSTS=( "192.0.0.100" )
export LB_WORKER=$HTTPD_LB_WORKER_NM
export WORKER=$HTTPD_WORKER_NM

#HTTPD_HOSTS_STR=""
for i in "${HTTPD_HOSTS[@]}"
do
        HTTPD_HOSTS_STR="${HTTPD_HOSTS_STR}${i},"
done

echo "============================================================="
echo "   Apache Tomcat 8.x                     ddakker@naver.com   "
echo "-------------------------------------------------------------"
$CATALINA_HOME/bin/./version.sh
echo "-------------------------------------------------------------"
echo "PROJECT_NM=$PROJECT_NM"
echo "WORKER_NM=$HTTPD_WORKER_NM"
echo "WEB_ROOT=$WEB_ROOT"
echo "SHUTDOWN_PORT=$SHUTDOWN_PORT"
echo "HTTP_PORT=$HTTP_PORT"
echo "HTTPS_PORT=$HTTPS_PORT"
echo "AJP_PORT=$AJP_PORT"
echo "HTTPD_HOSTS=$HTTPD_HOSTS_STR"
echo "============================================================="


[start.sh]

#!/bin/sh


. ./env.sh

# 임시 파일 삭제
rm -Rf $CATALINA_BASE/temp/*
# 로그 파일 관리
rm -Rf $CATALINA_BASE/logs/catalina.out


if [ "`ps -eaf | grep java | grep "$CATALINA_BASE "`" != "" ]; then
        echo "Tomcat SERVER - $PROJECT_NM is already RUNNING..."
        exit;
fi

if [ ! -d "$WEB_ROOT" ];
then
        mkdir -p $WAR_PATH
                mkdir -p $WEB_ROOT
                mkdir -p $WAR_PATH/../logs/$HTTPD_LB_WORKER_NM
fi


cd $CATALINA_HOME/bin

./startup.sh


if [ -z "`ps -eaf | grep java | grep "$CATALINA_BASE "`" ]; then
        echo "Error!!! Cannot start tomcat server."
fi


IS_WAIT=$1
IS_JK_CTL=$2

if [ "$IS_WAIT" == "wait" ]; then
        while [ `netstat -an | grep :$HTTP_PORT | grep LISTEN | wc | awk '{print $1}'` != 1 ]; do
                echo -ne "."
                sleep 1
        done

        echo "Check WAS Context Ready http://localhost:$HTTP_PORT ..."

        until [ "`curl --silent -o --show-error --connect-timeout 1 -w "%{http_code}\n" http://localhost:$HTTP_PORT | egrep '200|301|302|500'`" != "" ];
        do
                echo -ne "."
                sleep 1
        done
fi



if [ "$IS_JK_CTL" == "update" ]; then
        for HTTPD_HOST in "${HTTPD_HOSTS[@]}"
        do
                echo "<<< Turn-ON Worker $WORKER in $LB_WORKER [$HTTPD_HOST]"
                if [ "`curl --silent --show-error --connect-timeout 1 -I \"http://$HTTPD_HOST/jkstatus/?cmd=update&w=$LB_WORKER&sw=$WORKER&vwa=0\" | egrep '404|500|503'`" != "" ]
                then
                        echo "<<< Error"
                else
                        echo "<<< Done"
                fi
        done
fi


[shutdown.sh]

#!/bin/sh
 
 . ./env.sh

IS_WAIT=$1
IS_JK_CTL=$2
JK_CTL_AFTER_DELAY=$3
PID=`ps -ef | grep java | grep "$CATALINA_BASE " | awk '{print $2}'`

if [ e$PID == "e" ]
then
        echo "NOT RUNNING!!!"
        exit;
fi

if [ "$JK_CTL_AFTER_DELAY" == "" ]; then
        JK_CTL_AFTER_DELAY=0
fi

if [ "$IS_JK_CTL" == "update" ]; then
        for HTTPD_HOST in "${HTTPD_HOSTS[@]}"
        do
                echo "<<< Turn-OFF Worker $WORKER in $LB_WORKER [$HTTPD_HOST]"
                #if [ "`curl --silent --show-error --connect-timeout 1 -I \"http://$HTTPD_HOST/jkstatus/?cmd=update&w=$LB_WORKER&sw=$WORKER&vwa=1\" | egrep '403|404|500|503'`" != "" ]
                if [ "`curl --silent --show-error --connect-timeout 1 -I \"http://$HTTPD_HOST/jkstatus/?cmd=update&w=$LB_WORKER&sw=$WORKER&vwa=2\" | egrep '403|404|500|503'`" != "" ]
                then
                        echo "<<< Error"
                else
                        echo "<<< Done"
                fi
        done

        echo "+sleep $JK_CTL_AFTER_DELAY"
        sleep $JK_CTL_AFTER_DELAY
fi


$CATALINA_HOME/bin/./shutdown.sh
 
 
if [ "$IS_WAIT" == "wait" ]; then
        I=0
        until [ "`ps -eaf | grep java | grep "$CATALINA_BASE "`" == "" ];
        do
                if [ $I == 120 ]; then
                        ps -ef | grep java | grep "$CATALINA_BASE " | awk {'print "kill -9 " $2'} | bash -x
                        break;
                fi
 
                let I=$I+1
 
                echo -ne "."
                sleep 1
        done
fi


echo "*** Tomcat process ($PID) received SHUTDOWN signal ***" >> $CATALINA_BASE/logs/catalina.out


[setting.sh]

#!/bin/sh

. ./env.sh

if [ ! -d "$WEB_ROOT" ];
then
        mkdir -p $WAR_PATH
                mkdir -p $WEB_ROOT
                mkdir -p $WAR_PATH/../logs/$HTTPD_LB_WORKER_NM
fi


cd $CATALINA_BASE
find ./ -type d -exec chmod 750 {} \;
find ./ -type f -exec chmod 740 {} \;

chmod -Rf 640 $CATALINA_BASE/logs/*

cd $WEB_ROOT
find ./ -type d -exec chmod 755 {} \;
find ./ -type f -exec chmod 640 {} \;


if [ ! -e "$WEB_ROOT/index.html" ];
then
        echo "Hello World" >> $WEB_ROOT/index.html
fi
if [ ! -e "$WEB_ROOT/info.jsp" ];
then
        echo "<%@ page language=\"java\" contentType=\"text/html; charset=EUC-KR\" pageEncoding=\"EUC-KR\"%><%@page import=\"java.net.InetAddress\"%><%String serverIP = \"\";try{serverIP = InetAddress.getLocalHost().getHostAddress();serverIP = serverIP.substring(serverIP.lastIndexOf(\".\")+1);}catch(Exception e){}String info = request.getServerName() + \", \" + serverIP + \", \" + System.getProperty(\"jvmRoute\");out.print(info);// test%>" >> $WEB_ROOT/info.jsp
fi

if [ $LANG = "ko_KR.eucKR" ]; then
        cd $CATALINA_BASE
        FILE_ENC=`file -i env.sh `

        UTF8_IDX=`awk -v a="$FILE_ENC" -v b="utf-8" 'BEGIN{print index(a,b)}' `

        if [ $UTF8_IDX -gt 0 ]; then
                cd $CATALINA_BASE
                echo "----- encoding convertor -----"
                find . -name '*.sh' -exec iconv -f utf-8 -t euc-kr {} -o {} \;
        fi


[top.sh]

#!/bin/sh


. ./env.sh

PID=`ps -ef | grep java | grep "$CATALINA_BASE " | awk '{print $2}'`

if [ e$PID == "e" ]
then
        echo "NOT RUNNING!!!"
        exit;
fi

$CATALINA_BASE/../monitor_agent/./jvmtop.sh $PID


[dump.sh]

#!/bin/sh

. ./env.sh

for count in 1 2 3 4 5; do
    echo "Thread Dump : $count"
    for i in `ps -ef | grep java | grep "base=$CATALINA_BASE " | awk '{print $2}'`;do
        date
        #echo "+kill -3 $i"
        echo "+jstack -l $i >> $CATALINA_BASE/logs/thread_dump.$i"
        #kill -3 $i
        jstack -l $i >> $CATALINA_BASE/logs/thread_dump.$i
        #echo "sleep 1 sec"
        #sleep 1
    done
    echo "done"
    sleep 3
done


[dump_cpu.sh]

#!/bin/sh

. ./env.sh

for count in 1 2 3 4 5; do
    echo "Thread Dump : $count"
    for i in `ps -ef | grep java | grep "base=$CATALINA_BASE " | awk '{print $2}'`;do
        top -H -b -n1 >> $CATALINA_BASE/logs/dump_high_cpu_$i.txt
        date
        #kill -3 $i
        echo "+jstack -l $i >> $CATALINA_BASE/logs/dump_high_cpu_$i.txt"
        jstack -l $i >> $CATALINA_BASE/logs/dump_high_cpu_$i.txt
        echo "cpu snapshot and thread dump done. #"
    done
    echo "done"
    sleep 3
done


[httpd.sh]

#!/bin/sh


. ./env.sh



if [ "$1" == "stop" ]; then
        for HTTPD_HOST in "${HTTPD_HOSTS[@]}"
        do
                echo "<<< Turn-OFF Worker $WORKER in $LB_WORKER [$HTTPD_HOST]"
                if [ "`curl --silent --show-error --connect-timeout 1 -I \"http://$HTTPD_HOST/jkstatus/?cmd=update&w=$LB_WORKER&sw=$WORKER&vwa=2\" | egrep '403|404|500|503'`" != "" ]
                then
                        echo "<<< Error"
                else
                        echo "<<< Done"
                fi
        done
fi

if [ "$1" == "start" ]; then
        for HTTPD_HOST in "${HTTPD_HOSTS[@]}"
        do
                echo "<<< Turn-ON Worker $WORKER in $LB_WORKER [$HTTPD_HOST]"
                if [ "`curl --silent --show-error --connect-timeout 1 -I \"http://$HTTPD_HOST/jkstatus/?cmd=update&w=$LB_WORKER&sw=$WORKER&vwa=0\" | egrep '404|500|503'`" != "" ]
                then
                        echo "<<< Error"
                else
                        echo "<<< Done"
                fi
        done
fi


[server.xml]



  
  
  
  
  
  
  
  
    
    
    
    
 
      
        
      
      
                
        

      
    
  


[참고] http://snippets.khromov.se/subversion-1-8-centos-6/



wget http://snippets.khromov.se/wp-content/uploads/2013/09/svninstall.sh_.txt
mv svninstall.sh_.txt svninstall.sh
chmod 755 svninstall.sh
sh svninstall.sh


svninstall.sh_.txt


언제나 잊어버리는..


~]#  ifup eth0

~]#  vi /etc/sysconfig/network-scripts/ifcfg-eth0

ONBOOT=no -> yes


jUnit으로 HTTP기반 테스트 케이스를 작성 후 성공여부를 모니터링 하고 싶었다...



#!/bin/sh

LOG_FILE=../shell/TestCase2.log
LOG_FILE_FINISH=../shell/log/TestCase2.log

if [ -f $LOG_FILE ]; then
        echo "TestCase 현재 실행중임!"
        exit 1;
fi

java -classpath ...... org.junit.runner.JUnitCore com.test.TestCase2 > $LOG_FILE

/bin/rm -f $LOG_FILE_FINISH


FAILE_CNT_CNT=0
LOG_FILES=`cat $LOG_FILE`

for log_file in $LOG_FILES
do      
        tempS=`echo $log_file | grep "FAILURES!!!"`

        if [ "$tempS" != "" ]; then
                FAILE_CNT=1
                /bin/cp $LOG_FILE $LOG_FILE_FINISH$(date +%Y%m%d%H%M%S)   
                /bin/cp $LOG_FILE $LOG_FILE_FINISH
                break
        fi
done

if [ $FAILE_CNT = 0 ]; then
        /bin/rm -f $LOG_FILE_FINISH
fi

/bin/rm -f $LOG_FILE

# LOG_FILE_FINISH 경로에 파일이 존재 한다면 실패했다고 보면 됨...
  1. OpenSSL 설치되어 있다고 가정!!
  2. 키 생성
    • # openssl genrsa 1024 > key.pem
      • 여러가지 키생성 방법이 있으나 패스워드가 필요 없는 케이스
  3. 서명 생성
    • # openssl req -new -key key.pem > csr.pem
      • 다른 값들은 아무치나.. 아래것만 잘!!
        • Common Name (eg, your name or your server's hostname) []: 접속 도메인 주소(a.abc.com)
  4. 인증서 생성
    • # openssl req -key key.pem -x509 -nodes -sha1 -days 365 -in csr.pem -out crt.pem
  5. 키 저장소 생성
    • openssl pkcs12 -export -in crt.pem -inkey key.pem -out .keystore -name tomcat
      • WAS 에서 사용할 비밀번호 설정
  6. Tomcat server.xml 설정
    
    
  7. Tomcat 기동
  8. 환경
    • CentOs 6.4
    • Tomcat 5.5.36

[참고] http://dimdim.tistory.com/entry/openssl%EC%9D%84-%EC%9D%B4%EC%9A%A9%ED%95%9C-Tomcat-HTTPS-%EC%84%A4%EC%A0%95-PKCS12-%ED%8F%AC%EB%A7%B7

Oracle에서 JDK를 다운로드 받으려면 라인센스 동의를 클릭하고 이루어진다.


그런데 Linux Console에서 받으려면 해당 이벤트를 발생시킬수 없으므로 어떻게 받아야 하나.. 미러 싸이트를 찾았는데 이런기능이 있군요.. ㅎㅎ


wget --no-cookies --no-check-certificate  --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com" "http://download.oracle.com/otn-pub/java/jdk/7u51-b13/jdk-7u51-linux-i586.tar.gz"


wget --no-cookies --no-check-certificate  --header "Cookie: oraclelicense=accept-securebackup-cookie; s_cc=true; s_nr=1401757717323; gpw_e24=no%20value; s_sq=%5B%5BB%5D%5D" "http://download.oracle.com/otn-pub/java/jdk/7u60-b19/jdk-7u60-linux-i586.tar.gz"


Cookie: 부분을 브라우저에서 다운받을때 생성되는 정보를 입력한다.

Tomcat 7.x + Jenkins ver. 1.500


[Maven tomcat plugin을 이용하여 deploy -> reload 방법]

tomcat/conf/tomcat-users.xml







pom.xml


	org.codehaus.mojo
	tomcat-maven-plugin
	1.1
	
		UTF-8
		war
		http://localhost/manager/text
		
		admin
		admin
	



젠킨스 프로젝트 설정 Build 부분 명령어에 tomcat:redeploy tomcat:reload


[Shell 실행하여 Tomcat 재시작 방법]


jenkins에서 빌드 스크립트 종료 후 아래 shell 실행하면 정상 start 후 Process를 종료해버려서 기껏 올라간 Tomcat이 종료되는 문제가 발생한다.

그럴 경우 해당 프로젝트 설정에서 "This build is parameterized" 체크 하고, "Add Parameter" 에서 BUILD_ID 에다가 아무 값이나 셋팅!!

#!/bin/bash

echo "Shutdown tomcat now...."
bash /usr/local/tomcat/bin/shutdown.sh
echo "tomcat is now terminated..."
sleep 10
if [ -z "`ps -eaf | grep java | grep /usr/local/tomcat/bin`" ]; then
        echo "Tomcat was terminated successfully."
else
        ps -eaf | grep java | grep /usr/local/tomcat/bin | awk '{print $2}' |
        while read PID
                do
                echo "Killing $PID... "
                kill -9 $PID
                echo "Tomcat is being shutdowned"
                done
fi

echo "tomcat restart now"
/usr/local/tomcat/bin/./startup.sh
AA=`ps -eaf | grep java | grep /usr/local/tomcat/bin`
echo "AA: $AA"
if [ -z "`ps -eaf | grep java | grep /usr/local/tomcat/bin`" ]; then
        echo "Error!!! Cannot start tomcat server."
else
        echo "Tomcat restart succcessfully. "
fi
echo "Job finished"

[참조 shell] http://ausgang.egloos.com/2947190

startup.sh

#!/bin/sh


. ./env.sh

export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.0.9
export CATALINA_BASE=/usr/local/tomcat/domains/$PROJECT_NM
export JAVA_OPTS="-Djava.awt.headless=true -server -Xms512m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:+DisableExplicitGC"
export CATALINA_OPTS=$CATALINA_OPTS" -Dfile.encoding=UTF-8"

cd $CATALINA_HOME/bin

./startup.sh


AA=`ps -eaf | grep java | grep $WAS_PATH`
echo "AA: $AA"
if [ -z "`ps -eaf | grep java | grep $WAS_PATH`" ]; then
        echo "Error!!! Cannot start tomcat server."
fi

while [ `netstat -an | grep :$WAS_PORT | grep LISTEN | wc | awk '{print $1}'` != 1 ]; do
        echo -ne "."
        sleep 1
done

echo "Check WAS Context Ready http://localhost:$WAS_PORT ..."

until [ "`curl --silent --show-error --connect-timeout 1 -I http://localhost:$WAS_PORT | grep 'Coyote'`" != "" ];
do
        echo -ne "."
        sleep 1
done

echo "Start WAS And Context $PROJECT_NM Tomcat Succcess"

shutdown.sh

#!/bin/sh


. ./env.sh

export CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.0.9
export CATALINA_BASE=/usr/local/tomcat/domains/$PROJECT_NM

$CATALINA_HOME/bin/./shutdown.sh

I=0

until [ "`ps -eaf | grep java | grep $WAS_PATH`" == "" ];
do
        if [ $I == 5 ]; then
		ps -ef | grep java | grep "$WAS_PATH " | awk {'print "kill -9 " $2'} | sh -x
                break;
        fi

        let I=$I+1

        echo Stop WAS Finish? Sleeping for 2 seconds
        sleep 2
done


	Order deny,allow
	Deny from all
	Allow from 1.1.1.  # 1~255 활성

svn hook 기능을 이용하여 사용자별 커밋 리스트를 저장

post-commit 에 정의


#사용자 아이디를 구한다.(커밋정보=사용자, 시간 등)

INFO=$(svnlook info -r $REV $REPOS)

#IFS=-e "\n"

IFS=$"\n"

set -- $INFO

array=( $@ )


#현재 날짜를 구한다. YYYYMMDD

DATE_STRING=$(date +%Y%m%d)


#리비전 번호에 대한 커밋리스트를 구해 log파일에 Append한다.

svnlook changed -r $REV $REPOS >> "/home/svn/repos/hooks/svn_commit_${array[0]}_$DATE_STRING.log"

SVN 사용 중에.. "Head... 뭐라뭐라" 메시지 나오면서 커밋 등등 작동 안 하는 문제가 발생할때가 있다..(아주 간혹)

- 메시지를 적어놨어야 하는데.. 안 적어놨다..


이런 경우 최종 Commit 할때 무언가 꼬인 경우기 때문에 최종 이전 버전으로 되돌려 놓으면 해결 된다.


1. ../../"svn home"/"Repositories Name"/db/current     - 현재 최종 버전 정보를 가지고 있는 설정 파일

2. ../../"svn home"/"Repositories Name"/db/revs         - 하위 디렉토리에 Revision 정보들이 파일로 존재


2번 디렉토리에서 제일큰 Revision파일(가장 최근 파일)이 문제 시 되는 Revision 입니다.(예를 들어 227)

- 227 이라는 파일을 열어 보시면 대충 어디에서 커밋하다가 꼬인건지 파악 됩니다.

- 227 파일을 삭제(혹시 모르니 mv) 해놓으시고, 1번의 current 파일을 vi로 열어보시면 "227 ??? ???" 이런식으로 227이라는 문자열을 보실 수 있습니다.

이 227이라는 문자열을 그 이전 정보 226으로 고치고, SVN서버 재 시작 하시면 잘 될 것 입니다.

+ Recent posts