Launch slave of OPNFV Jenkins using JNLP 97/297/3
authorm00133142 <meimei@huawei.com>
Mon, 13 Apr 2015 09:44:26 +0000 (17:44 +0800)
committerm00133142 <meimei@huawei.com>
Fri, 17 Apr 2015 04:00:59 +0000 (12:00 +0800)
JIRA: OCTO-25

Change-Id: I56d08457976412daa50e4b87ab2629c36460bff6
Signed-off-by: m00133142 <meimei@huawei.com>
ci/README [new file with mode: 0755]
ci/jenkins-slave [new file with mode: 0755]
ci/jenkins-slave.init [new file with mode: 0755]

diff --git a/ci/README b/ci/README
new file mode 100755 (executable)
index 0000000..a830076
--- /dev/null
+++ b/ci/README
@@ -0,0 +1,24 @@
+##############################################################################
+# 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#
+##############################################################################
+Before use this script please make sure things below you have done:
+1.Install JDK on server
+2.Create local user on server: jenkins
+3.Create the local workspace configured in jenkins
+3.Download slave.jar and put it into $HOME directory of jenkins user
+4.Your slave has been configured in OPNFV Jenkins
+5.Obtain slave's token from Linux Foundation Helpdesk
+Consult wiki : https://wiki.opnfv.org/wiki/jenkins#how_to_connect_servers_from_labs_to_opnfv_jenkins
+
+Follow steps below to launch your slave to OPNFV Jenkins:
+1. cp jenkins-slave.init /etc/init.d/jenkins-slave
+2. chmod +x /etc/init.d/jenkins-slave
+3. define $SLAVENAME&$TOKEN in config file(jenkins-slave)
+4. cp jenkins-slave /etc/default/jenkins-slave
+5. service jenkins-slave start
+Finally, run "service jenkins-slave status" command to check the process is running, see the log(/home/jenkins/slave.log) to confirm the connect to  OPNFV Jenkins.
\ No newline at end of file
diff --git a/ci/jenkins-slave b/ci/jenkins-slave
new file mode 100755 (executable)
index 0000000..975f384
--- /dev/null
@@ -0,0 +1,2 @@
+SLAVENAME=[slave's name]
+TOKEN=[slave's secret token]
diff --git a/ci/jenkins-slave.init b/ci/jenkins-slave.init
new file mode 100755 (executable)
index 0000000..8402b67
--- /dev/null
@@ -0,0 +1,111 @@
+#!/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 "Fatal : Configuration file dosenot exist, details please refer to README."
+    exit
+fi
+
+if [ -z "${SLAVENAME}" ] || [ -z "${TOKEN}" ]
+then
+    echo "Fatal : Lack of slavename and token"
+    exit
+fi
+
+JENKINS_HOME="/home/jenkins"
+PID_FILE="${JENKINS_HOME}/slave.pid"
+SLAVE_JAR="${JENKINS_HOME}/slave.jar"
+SLAVE_JNLP="-jnlpUrl https://build.opnfv.org/ci/computer/${SLAVENAME}/slave-agent.jnlp"
+USER="jenkins"
+SLAVE_LOG="${JENKINS_HOME}/slave.log"
+JAVA_BIN="/usr/bin/java"
+
+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 "None valid argument!"
+    usage
+    exit 1
+fi
+
+case "$OPTION" in
+    start)
+        start
+    ;;
+    stop)
+        stop
+    ;;
+    restart)
+        stop
+        sleep 2
+        start
+    ;;
+    status)
+        status
+    ;;
+    *)
+        echo "$OPTION is not a valid argument!"
+    usage
+        exit 1
+    ;;
+esac
+