跳至主要内容

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

评论

此博客中的热门博文

4 steps to delete account in Gerrit DB

4 steps to delete account in DB. Delete from accounts where preferred_email=’’; delete from account_ssh_keys where account_id=''; delete from account_external_ids where external_id='gerrit:*’; delete from account_external_ids where external_id='username:*’; whatever it was in H2 database and postgres db . H2: ssh -p 24198 localhost gerrit gsql Postgres: psql

vmware esxi 安装

需求来自于save cost. 开始有用户需求部署vmware esxi为架构的虚拟机, 由于目前没有server来测试,用户自己找的pavilion的家用机系列也跑不了。只得从onsite那里借来一台xw4400. 这机器吧,挺好,xw4400开跑的64位虚拟支持的话,先得在Blos上设置下, 因为没有这方面经验,以为机器都能直接判断,但是从安装实录上里讲,需要在blos里这是,对于xw4400的设置是,security-os security- enable virtualization. 然后重启。顺便丢入光盘。   iso其实很小大概只有200多M,是一个linux kernel的东西。里面从个人观点来看,带有web接口。和管理接口。 安装程序会自动检测你机器的配置是否符合安装要求,家用额pavilion就没检测过,看网上资料,初步认定是网卡没有不办法兼容。 一路按照提示点F2和enter就成。由于是基于网络的,请注意ip的分配。 安装完成之后请拿出光盘,因为我之前就是这样犯了个错误,导致重复安装一次:(   重启之后,会有一个界面告诉你这个管理的 http://ip 是啥, 然后你可以通过windows client段去下载客户端管理工具,ci. 里面按f2是管理配置界面,可以配置root密码,ip地址等等,详细可以参考 文章 .   点击后的下载ci客户端,点击,输入控制端ip/用户名/密码。 然后就是如vmware的配置一般。 有一点奇怪的是,他竟然没有办法认出host上的光驱。好在有网络以后,他可以mount本机和网络上的盘,当然我指的是client上的物理光驱以及网络上的虚拟iso. 选择client的话,然后用物理光驱要选emulate cd. 安装非常的方便。 然后装完两个32bit and 64bit的suse. 用户开始test -- Alex Tu ----------------------------------------------------------- ShangHai,China