CentOS6でJenkins(Master/Node)を構築するshell

# Jenkins Master

```bash
#!/usr/bin/env bash
# Fail on unset variables and command errors
set -ex -o pipefail
# Prevent commands misbehaving due to locale differences
export LC_ALL=C

JESNKINS_BACKUP_STORE_DIR=/var/lib/jenkins/backup
TOTAL_NODE_NUM=1
JDK_VERSION="8u91-b14" # [Java Version]-[Build Number]
JENKINS_PLUGINS=(
  # maintenance plugins
  "thinBackup"
  # security plugins
  # "active-directory"
  # view plugins
  # "greenballs"
  # "delivery-pipeline-plugin"
  # "extra-columns"
  # "dashboard-view"
  # "sectioned-view"
  # job plugins
  # "git"
  # "gitlab-hook"
  # "job-import-plugin"
  # "purge-job-history"
  # "jobConfigHistory"
  # "nodelabelparameter"
  # "parameterized-trigger"
  # "multiple-scms"
  # "uno-choice"
  # "email-ext"
  # "plot"
  # "ws-cleanup"
  # "rebuild"
  # "timestamper"
)

PROXY_HOST=192.168.56.102
PROXY_PORT=3128
NO_PROXY=localhost,127.0.0.1
SMTP_HOTS=localhost
SMTP_PORT=9090
export http_proxy="http://${PROXY_HOST}:${PROXY_PORT}"
export https_proxy="http://${PROXY_HOST}:${PROXY_PORT}"
export no_proxy="${NO_PROXY}"

# initialize package repositories
rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpm

# install packages
yum -y install git

# install oracle-jdk
curl -s -LO -b "oraclelicense=accept-securebackup-cookie" \
               "http://download.oracle.com/otn-pub/java/jdk/${JDK_VERSION}/jdk-${JDK_VERSION:0:4}-linux-x64.rpm"
rpm -ivh jdk-${JDK_VERSION:0:4}-linux-x64.rpm

# install jenkins (latest)
curl -o /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
yum -y install jenkins

# disable jenkins setupwizard
sed -i -e 's/^JENKINS_JAVA_OPTIONS=.*/JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Djenkins.install.runSetupWizard=false"/' /etc/sysconfig/jenkins

# init jenkins
chkconfig jenkins on #[CentOS6]
service jenkins start #[CentOS6]

# wait until jenkins is ready
while [ `curl -LI http://localhost:8080 -o /dev/null -w '%{http_code}\n' -s` -ne "200" ]; do sleep 5; done

service jenkins stop #[CentOS6]

# configure proxy
cat << EOS >> /var/lib/jenkins/proxy.xml
<?xml version='1.0' encoding='UTF-8'?>
<proxy>
  <name>${PROXY_HOST}</name>
  <port>${PROXY_PORT}</port>
</proxy>
EOS

# configure thinBackup
cat << EOS >> /var/lib/jenkins/thinBackup.xml
<?xml version='1.0' encoding='UTF-8'?>
<org.jvnet.hudson.plugins.thinbackup.ThinBackupPluginImpl plugin="thinBackup@1.7.4">
  <fullBackupSchedule></fullBackupSchedule>
  <diffBackupSchedule></diffBackupSchedule>
  <backupPath>${JESNKINS_BACKUP_STORE_DIR}</backupPath>
  <nrMaxStoredFull>5</nrMaxStoredFull>
  <excludedFilesRegex></excludedFilesRegex>
  <waitForIdle>true</waitForIdle>
  <forceQuietModeTimeout>120</forceQuietModeTimeout>
  <cleanupDiff>false</cleanupDiff>
  <moveOldBackupsToZipFile>true</moveOldBackupsToZipFile>
  <backupBuildResults>true</backupBuildResults>
  <backupBuildArchive>false</backupBuildArchive>
  <backupUserContents>true</backupUserContents>
  <backupNextBuildNumber>true</backupNextBuildNumber>
  <backupBuildsToKeepOnly>false</backupBuildsToKeepOnly>
</org.jvnet.hudson.plugins.thinbackup.ThinBackupPluginImpl>
EOS

# start jenkins
service jenkins start #[CentOS6]

# wait until jenkins is ready
while [ `curl -LI http://localhost:8080 -o /dev/null -w '%{http_code}\n' -s` -ne "200" ]; do sleep 5; done

# install jenkins plugins
JENKINS_CLI=/var/cache/jenkins/war/WEB-INF/jenkins-cli.jar
for ((i = 0; i < ${#JENKINS_PLUGINS[@]}; i++)) {
  java -jar ${JENKINS_CLI} -s http://localhost:8080 install-plugin ${JENKINS_PLUGINS[i]}
}

# wait until all nodes is ready
node_count=0
while [ ${node_count} -lt ${TOTAL_NODE_NUM} ]; do
  sleep 30
  curl -s -L -o nodes.xml http://localhost:8080/computer/api/xml
  node_count=`echo "xpath count(/computerSet/computer[not(displayName/text()='master')]/offline[text()='false'])" | xmllint --shell nodes.xml | sed 's/\/ > //g' | sed 's/Object is a number : //g'`
done

# reboot jenkins to apply changes
service jenkins restart #[CentOS6]

# wait until jenkins is ready
while [ `curl -LI http://localhost:8080 -o /dev/null -w '%{http_code}\n' -s` -ne "200" ]; do sleep 5; done
java -jar ${JENKINS_CLI} -s http://localhost:8080 list-plugins

```

# Jenkins Slave

```bash
#!/usr/bin/env bash
# Fail on unset variables and command errors
set -ex -o pipefail
# Prevent commands misbehaving due to locale differences
export LC_ALL=C

JENKINS_MASTER_HOST=192.168.100.7
JENKINS_MASTER_PORT=8080
JDK_VERSION="8u91-b14" # [Java Version]-[Build Number]
JENKINS_WORKDIR=/var/lib/jenkins
OS_SHORTNAME=cent6
NODE_ROLE=build
INSTANCE_ID=i-1234567890

# install oracle-jdk
mkdir -p ${JENKINS_WORKDIR}
curl -s -L -o ${JENKINS_WORKDIR}/jdk-${JDK_VERSION:0:4}-linux-x64.rpm \
           -b "oraclelicense=accept-securebackup-cookie" \
              "http://download.oracle.com/otn-pub/java/jdk/${JDK_VERSION}/jdk-${JDK_VERSION:0:4}-linux-x64.rpm"
rpm -ivh ${JENKINS_WORKDIR}/jdk-${JDK_VERSION:0:4}-linux-x64.rpm

# wait until master jenkins is ready
while [ `curl -LI http://${JENKINS_MASTER_HOST}:${JENKINS_MASTER_PORT} -o /dev/null -w '%{http_code}\n' -s` -ne "200" ]; do sleep 5; done

# download jenkins cli tools
curl -s -L -o ${JENKINS_WORKDIR}/jenkins-cli.jar http://${JENKINS_MASTER_HOST}:${JENKINS_MASTER_PORT}/jnlpJars/jenkins-cli.jar
curl -s -L -o ${JENKINS_WORKDIR}/slave.jar http://${JENKINS_MASTER_HOST}:${JENKINS_MASTER_PORT}/jnlpJars/slave.jar

# create jenkins user
useradd jenkins

# create node to master jenkins
mkdir -p ${JENKINS_WORKDIR}/jobs
NODE_NAME=${OS_SHORTNAME}_${NODE_ROLE}_${INSTANCE_ID}
cat <<EOS | java -jar ${JENKINS_WORKDIR}/jenkins-cli.jar -s http://${JENKINS_MASTER_HOST}:${JENKINS_MASTER_PORT} create-node ${NODE_NAME}
<slave>
<name>${NODE_NAME}</name>
<description/>
<remoteFS>${JENKINS_WORKDIR}/jobs</remoteFS>
<numExecutors>1</numExecutors>
<mode>EXCLUSIVE</mode>
<retentionStrategy class="hudson.slaves.RetentionStrategy\$Always"/>
<launcher class="hudson.slaves.JNLPLauncher"/>
<label>${OS_SHORTNAME} ${NODE_ROLE} ${OS_SHORTNAME}_${NODE_ROLE}</label>
<nodeProperties/>
<userId>anonymous</userId>
</slave>
EOS

# get node token
curl -s -L -o ${JENKINS_WORKDIR}/slave-agent.jnlp \
           http://${JENKINS_MASTER_HOST}:${JENKINS_MASTER_PORT}/computer/${NODE_NAME}/slave-agent.jnlp
secret=`echo "cat /jnlp/application-desc/argument[1]" | xmllint --shell ${JENKINS_WORKDIR}/slave-agent.jnlp | sed '/^\/ >/d' | sed 's/<[^>]*.//g'`

# register jenkins as a service
cat << EOS >> /etc/sysconfig/jenkins-slave
JENKINS_WORKDIR=${JENKINS_WORKDIR}
JENKINS_USER=jenkins
JENKINS_URL="http://${JENKINS_MASTER_HOST}:${JENKINS_MASTER_PORT}"
JENKINS_NODENAME=${NODE_NAME}
JENKINS_SECRET=${secret}
EOS
chmod 600 /etc/sysconfig/jenkins-slave
chown -R jenkins:jenkins ${JENKINS_WORKDIR}

cat << 'EOS' >> /etc/init.d/jenkins-slave
#!/bin/sh
#
# jenkins-slave:    Launch a Jenkins BuildSlave instance on this node
#
# chkconfig:    - 99 01
# description:  Enable this node to fulfill build jobs
#

# Source function library.
. /etc/rc.d/init.d/functions

[ -f /etc/sysconfig/jenkins-slave ] && . /etc/sysconfig/jenkins-slave

[ -n "$JENKINS_URL" ] || exit 0
[ -n "$JENKINS_WORKDIR" ] || exit 0
[ -n "$JENKINS_USER" ] || exit 0
[ -n "$JENKINS_NODENAME" ] || exit 0
[ -n "$JENKINS_SECRET" ] || exit 0
[ -x /usr/bin/java ] || exit 0

download_jar()
{
    curl -s -o slave.jar $JENKINS_URL/jnlpJars/slave.jar || exit 0
}

start()
{
  cd $JENKINS_WORKDIR
  [ -f slave.jar ] || download_jar

  echo -n $"Starting $prog: "

  su $JENKINS_USER sh -c "\
    java -jar slave.jar -jnlpUrl $JENKINS_URL/computer/$JENKINS_NODENAME/slave-agent.jnlp -secret $JENKINS_SECRET >slave.log 2>&1 &"

  if [ $? = 0 ]; then echo '[OK]'; else echo '[NG]'; fi
}

stop()
{
  echo -n $"Shutting down $prog: "

  PID=`ps -ef | grep '[j]ava -jar slave.jar' | awk '{print $2}'`
  [ -n "$PID" ] && kill $PID

  if [ $? = 0 ]; then echo '[OK]'; else echo '[NG]'; fi
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart|reload)
    stop
    start
    ;;
  status)
    status java
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|reload}"
    exit 1
esac

exit 0
EOS

chmod 755 /etc/init.d/jenkins-slave
chkconfig --add jenkins-slave
chkconfig jenkins-slave on

# start jenkins node
service jenkins-slave start

```