跳至主要内容

Jenkins

#!/bin/bash
# Author: Alex Tu, 2011
#


### BEGIN INIT INFO
# Provides:          gbv-hudson-integration
# Required-Start:    $local_fs $network
# Required-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Starts the hudson server for CI
# Description:       Starts Continuous Integration System for VZG SOA
### END INIT INFO

# to avoid conflict with functions local $1
ARG=$1

APP="jenkins"

HUDSON_USER=jenkins
HUDSON_GROUP=jenkins
JAVA=/usr/bin/java
export HOME=/opt/${HUDSON_USER}
export HUDSON_WAR=/opt/${HUDSON_USER}/jenkins.war

# include rc.status functions
test -s /etc/rc.status && . /etc/rc.status && rc_reset

# use test_for_app (x|f)
test_for_app ()
{
    app_found=0
    if [ "f" = "$2" ] && [ -f $1 ]; then
        app_found=1
    elif [ "x" = "$2" ] && [ -x $1 ]; then
        app_found=1
    fi
    if [ 0 = $app_found ]; then
        echo -n "Warning:  Couldn't find $1"
        if [ "$ARG" = "stop" ] ; then
            rc_failed 0
        else
            rc_failed 5
        fi
        rc_status -v
        rc_exit
    fi
}


HUDSON_CMD="${JAVA} -jar ${HUDSON_WAR}"

HUDSON_PIDFILE=/var/run/jenkins.pid
HUDSON_LOG=/var/log/jenkins

usage ()
{
    echo ""
    echo "Usage: $0 "
    echo ""
    echo "where is one of the following:"
    echo "    start - start $APP if not running"
    echo "    stop - stop $APP if running"
    echo "    status - report whether $APP is running"
    echo "    restart - stop and restart $APP"
    echo "    usage, help - print this message"
}

get_pid_for_cmd ()
{
    for pid in `ls /proc`; do
        if [ -d /proc/$pid ] && [ -f /proc/$pid/cmdline ]; then
            if [ "$1" = "$(        # return with current pid
                return
            fi
        fi
    done
    pid=0
}

# checkport
# returns port_used
check_port()
{
    port=$1
    if netstat -nlept 2>/dev/null | grep --regexp=":$port " &>/dev/null ; then
            port_used=1
        else
            port_used=0
        fi
}

# checks hudson, sets/returns status -
# 0 if pidfile same as pid, running
# 3 no pidfile, not running (what with running?)
# 2 pidfile, but no process (remove file ...), not running
# 1 pidfile != pid, but running
# 4 no pidfile, but running
#
# @todo: check port usage
check_hudson()
{
    get_pid_for_cmd `eval echo ${HUDSON_CMD} | sed -e 's/ //g'`

    if [ -f $HUDSON_PIDFILE ]; then
                chown jenkins:jenkins ${HUDSON_PIDFILE}
        pidfile_pid=`cat $HUDSON_PIDFILE`
        if [ 0 = $pid ]; then
            status=2
        elif [ "$pid" = "$pidfile_pid" ]; then
            status=0;
        else
            status=1;
        fi       
    else
        if [ 0 = $pid ]; then
            status=3;
        else
            status=4;
        fi
    fi

}


test_for_app ${JAVA} x
test_for_app ${HUDSON_WAR} f

case "$1" in
    start)
        check_hudson
        case "$status" in
            0)
                echo "${APP} already running"
        rc_failed 0
        rc_status -v1
               rc_exit
                ;;
        1|4)
        echo "${APP} seems running but pidfile is not up to date or doesn't exist, please check!"
        rc_failed
                rc_status -v1
                rc_exit
        ;;
            2)
                echo "Found stale pidfile for ${APP} - unclean shutdown?"
                rm ${HUDSON_PIDFILE}
                ;;
            3)
                # not running - ok
            check_port $CI_HTTP_PORT
        if [ $port_used = 1 ]; then
            echo "Port $CI_HTTP_PORT already in use, pleas check!"
                    rc_failed
                    rc_status -v1
                    rc_exit
        fi
                ;;
            *)
                echo "Check for ${APP} failed"
                rc_failed
                rc_status -v1
                rc_exit
        esac

        echo -n "Starting ${APP}"
        startproc -f -u $HUDSON_USER -g $HUDSON_GROUP -p ${HUDSON_PIDFILE} ${HUDSON_CMD} >> ${HUDSON_LOG} 2>&1
        if ! [ 0 = $? ]; then
            echo -n "(Error - "
            case $? in
                2)
                    echo -n "invalid arguments"
                    ;;
                4)
                    echo -n "insufficient permission"
                    ;;
                5)
                    echo -n "no such program"
                    ;;
                7)
                    echo -n "launch failure"
                    ;;
                *)
                    echo -n "unspecified error"
                    ;;
            esac
            echo -n ")"
            rc_failed
            rc_status -v1
            rc_exit
        fi
    # removing spaces from commandline to be able to compare with /proc//cmdline
        get_pid_for_cmd `eval echo ${HUDSON_CMD} | sed -e 's/ //g'`
        if [ 0 = $pid ]; then
            echo "Warning - Couldn't obtain PID for ${APP} "
        else
            echo $pid > ${HUDSON_PIDFILE}
        fi
        rc_status -v
        ;;
    stop)
        if [ $1 = "stop" ]; then
            check_hudson
        if ! [ $status = 0 ]; then
        echo "Warning - not running!"
        fi
            killproc -p ${HUDSON_PIDFILE} -t 10 ${JAVA}
            rc_status -v
        fi
        ;;
    status)
        check_hudson
    case "$status" in
        0)
            echo -n "(running)"
            rc_failed 0
            ;;
    1|4) 
        echo -n "(running)"
        echo -n  "Warning - PID corrupted"
            rc_failed 0
            ;;
        2)
            echo "(not running)"
            echo -n "Warning - PID file found"
            rc_failed 3
            ;;
        3)
            echo -n "(not running)"
            rc_failed 1
            ;;
        *)
            echo "(unknown)"
            echo "Warning - Couldn't get status"
            rc_failed 1
            rc_status -v1
            rc_exit
    esac
    rc_status -v
        ;;
    restart)
        $0 stop &>/dev/null
        $0 start &>/dev/null
        rc_status -v
        ;;
    usage|help)
        usage
        rc_exit
        ;;
    *)
        usage
        rc_failed 1
        rc_status -v
        rc_exit
esac

评论

此博客中的热门博文

Python学习笔记20100117

映射list   Dictionary 是用{}. list 是一那个[]. turple是用() 当你定义过dictionary后,你可以使用d.keys(), d.values(),d.items()将定义后的时候分别显示出来 当然可以将list里的值加减乘除,也可以如一般的定义直接重新复制这个list. 链接与分割字符串的 li=";", join(li) 则显示的就是 分隔符为;的数 如果使用li=li.split(";")则将刚刚;的分隔符删除,而split里也可定义域,如li.split(";",1)   自省 <---这是啥 之前在使用的时候发觉有些书本上的模块不能调用,很多是py脚本定义过的函数。 下载该脚本s,然后上传至指定位置。 >>>import sys >>>sys.path >>>sys.path.append("绝对位置")然后就能调用这些脚本和参数了。 删除 >>>sys.path.pop() 定义一个参数是 def info(test, test1=10.test2=12): info是函数名,test是必备参数,因为没有定义值,test1和test2是可选参数,定义了初始值   以上是外部函数的调用,下面转到内部函数 内部函数有type,str,dir及其他   ---type 返回任意字符的类型,模块也可以。types模块 >>> type(1) <type 'int'> >>> li=[] >>> type(li) <type 'list'> >>> import odbchelper >>> type(odbchelper) <type 'module'> >>> type(sys) <type 'module'> >>> import types ...

Python学习笔记20100128

methodList = [method for method in dir(object) if callable(getattr(object, method))] ###插一句, ifconfig pcn0 unplumb 去禁solaris的网卡plumb起网卡   and, or  已经and-or一起用。 and 两者为真,print第二个数,一个为假一个为真返回假,三者为真返回最后一个真。 or 两个为真,返回第一个, 一个为假一个为真返回真,三者为假返回最后一个假   lambda可快速定义最小值函数   g = lambda x:x*2 g(3) 6 (lambda x:x*2)(3)   doc string print getattr(object,mothod).__doc__   ljust ljust 用空格填充字符串以符合指定的长度。info 函数使用它生成了两列输出并将所有在第二列的 doc string 纵向对齐。 如果小于的话不会从中截断   -----面向对象 首先导入,你可以选择import module or from module import module 如果你要经常访问模块的属性和方法,且不想一遍又一遍地敲入模块名,使用 from module import。 如果你想要有选择地导入某些属性和方法,而不想要其它的,使用 from module import。 如果模块包含的属性和方法与你的某个模块同名,你必须使用 import module 来避免名字冲突   -----类的定义 class Lofs    pass   这个类的名字是 Loaf,它没有从其它类继承。 类名通常是第一个字母大写,如:EachWordLikeThis,但这只是一个习惯,不是一个必要条件。   这个类没有定义任何方法或属性,但是从语法上,需要在定义中有些东西,所以你使用 pass。这是一个 Python 保留字,仅仅表示 “向前走,不要往这看”。它是一条什么都不做的语句,当你删空函数或类时,它是一个很好的占位符。   你可能猜...

Back to Blog again

     My last blog was almost 6 years again.  lots of things happening those years. like marriage, and move to another country, simply relocation, something is very big in my life.      Back to now,  I guess it is a very good time to start with the blog again. no matter daily life or the tech parts.      These blog may or will contains various languages, Chinese, English or Few Japanese. The first thing, I change the profile to Kamata life, Kamata is Japanese words. "鎌田"  as Kanji. why this change happened, due to we will move from Tamagawa "玉川" to Kamata very soon. I hope I can start to write this not daily, but I hope I can do this continuously.