summaryrefslogtreecommitdiffstats
path: root/ci/jenkins-slave.init
blob: 35dc2fd629f64833d05f37a72caebd9afbc171f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
##############################################################################
# Copyright (c) 2015 Huawei Technologies Co.,Ltd.
# meimei@huawei.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0#
##############################################################################
set -o errexit
set -o nounset
set -o pipefail

SLAVE_CONF="/etc/default/jenkins-slave"

if [ -f ${SLAVE_CONF} ]
then
    . ${SLAVE_CONF}
else
    echo "Error : Configuration file ($SLAVE_CONF) does not exist, details please refer to README."
    exit
fi

if [ -z "${SLAVENAME}" ] || [ -z "${TOKEN}" ]
then
    echo "Error : Please define SLAVENAME and TOKEN values in the Configuration file ($SLAVE_CONF),  details please refer to README."
    exit
fi

SLAVE_JNLP="-jnlpUrl https://build.opnfv.org/ci/computer/${SLAVENAME}/slave-agent.jnlp"
JENKINS_HOME="/home/jenkins"
SLAVE_JAR="${JENKINS_HOME}/slave.jar"
JENKINS_SLAVE_ROOTDIR="${JENKINS_HOME}/jenkins-slave"
PID_FILE="${JENKINS_SLAVE_ROOTDIR}/slave.pid"
SLAVE_LOG="${JENKINS_SLAVE_ROOTDIR}/slave.log"
USER="jenkins"
GROUP="jenkins"
JAVA_BIN="/usr/bin/java"

if [ ! -f ${SLAVE_JAR} ]
then
    echo "Error : ${SLAVE_JAR} dosenot exist, details please refer to README."
    exit
fi

[ -e ${JENKINS_SLAVE_ROOTDIR} ] || { mkdir ${JENKINS_SLAVE_ROOTDIR}; chown ${USER}:${GROUP} ${JENKINS_SLAVE_ROOTDIR};};
if [ ! -d ${JENKINS_SLAVE_ROOTDIR} ]
then
    echo "Error : ${JENKINS_SLAVE_ROOTDIR} can not be created! Maybe there is a file with the same name of 'jenkins-slave'."
    exit
fi

usage ()
{
cat<<EOF
Usage: jenkins-slave {start|stop|restart}
EOF
}

start()
{
    echo "Starting jenkins slave(${SLAVENAME})"
    if ! start-stop-daemon --start -m -b -c ${USER} -p ${PID_FILE} \
        --startas /bin/bash -- -c \
        "exec ${JAVA_BIN}  -jar ${SLAVE_JAR} ${SLAVE_JNLP} -secret ${TOKEN} >>${SLAVE_LOG} 2>&1"
    then
        echo "Start jenkins slave failed!"
    else
        echo "DONE."
    fi
}

stop()
{
    echo "Stopping jenkins slave(${SLAVENAME})"
    if ! start-stop-daemon --stop -p ${PID_FILE}
    then
        echo "Stop jenkins slave failed!"
    else
        echo "DONE."
    fi
    rm -f ${PID_FILE}
}

status()
{
    set +e
    pid=`ps -ef | grep "${SLAVE_JAR}" | grep -v 'grep' |  awk '{print $2}'`
    set -e
    if [ -z $pid ]
    then
        echo "jenkins slave is not running!"
    else
        echo "jenkins slave is running, pid is $pid"
    fi
}

if [ $# -gt 0 ]
then
    OPTION=$1
else
    echo "Please input your argument!"
    usage
    exit 1
fi

case "$OPTION" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        sleep 2
        start
    ;;
    status)
        status
    ;;
    *)
        echo "Error: $OPTION is not a valid argument"
        usage
        exit 1
    ;;
esac