blob: 9d41bc910298b1eb2c54f8723a33d8f3db8ddb4d [file] [log] [blame]
Alanny Lopez620baa12017-10-10 11:50:03 -05001#!/bin/bash
2################################################################################
Alanny Lopeze08e8702018-02-24 18:07:13 -06003#
Alanny Lopez620baa12017-10-10 11:50:03 -05004# Script used to create a Jenkins master that can run amd64 or ppc64le. It can
5# be used to launch the Jenkins master as a Docker container locally or as a
6# Kubernetes Deployment in a Kubernetes cluster.
Alanny Lopeze08e8702018-02-24 18:07:13 -06007#
Alanny Lopez620baa12017-10-10 11:50:03 -05008################################################################################
Alanny Lopeze08e8702018-02-24 18:07:13 -06009#
10# Script Variables:
11# build_scripts_dir The path of the openbmc-build-scripts directory.
12# Default: The directory containing this script
13# workspace The directory that holds files used to build the Jenkins
14# master master image and volumes used to deploy it.
15# Default: "~/jenkins-build-${RANDOM}"
16#
17# Jenkins Dockerfile Variables:
18# agent_port The port used as the Jenkins slave agent port.
19# Default: "50000"
20# http_port The port used as Jenkins UI port.
21# Default: "8080"
22# img_tag The tag of the OpenJDK image used as the base image.
23# Default: "/8-jdk"
24# j_vrsn The version of the Jenkins war file you wish to use.
25# Default: "2.60.3"
26# j_user Username tag the container will use to run Jenkins.
27# Default: "jenkins"
28# j_group Group name tag the container will use to run Jenkins.
29# Default: "jenkins"
30# j_uid Jenkins user ID the container will use to run Jenkins.
31# Default: "1000"
32# j_gif Jenkins group ID the container will use to run Jenkins.
33# Default: "1000"
34# j_home Directory used as the Jenkins Home in the container.
35# Default: "/var/jenkins_home"
36# out_img The name given to the Docker image when it is built.
37# Default: "openbmc/jenkins-master-${ARCH}:${JENKINS_VRSN}"
38# tini_vrsn The version of Tini to use in the Dockerfile, 0.16.1 is
39# the first release with ppc64le release support.
40# Default: "0.16.1"
41#
42# Deployment Variables:
43# cont_import_mnt The directory on the container used to import extra files.
44# Default: "/mnt/jenkins_import", ignored if above not set
45# home_mnt The directory on the host used as the Jenkins home.
Alanny Lopez620baa12017-10-10 11:50:03 -050046# Default: "${WORKSPACE}/jenkins_home"
Alanny Lopeze08e8702018-02-24 18:07:13 -060047# host_import_mnt The directory on the host used to import extra files.
48# Default: "", import mount is ignored if not set
Alanny Lopez492c52c2017-11-10 15:06:26 -060049# jenkins_options What will be passed as the environment variable for the
Alanny Lopez29cc3b12017-10-18 15:18:00 -050050# JENKINS_OPTS environment variable.
51# Default: "--prefix=/jenkins"
Alanny Lopez492c52c2017-11-10 15:06:26 -060052# java_options What will be passed as the environment variable for the
53# JAVA_OPTS environment variable.
54# Default: "-Xmx4096m"
Alanny Lopeze08e8702018-02-24 18:07:13 -060055# launch docker|k8s
56# Method in which the container will be launched. Either as
57# a Docker container launched via Docker, or by using a
58# helper script to launch into a Kubernetes cluster.
59# Default: "docker"
Alanny Lopez620baa12017-10-10 11:50:03 -050060#
61################################################################################
Alanny Lopeze08e8702018-02-24 18:07:13 -060062build_scripts_dir=${build_scripts_dir:-"$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"}
Alanny Lopez620baa12017-10-10 11:50:03 -050063
64set -xeo pipefail
65ARCH=$(uname -m)
66
67# Launch Variables
68workspace=${workspace:-${HOME}/jenkins-build-${RANDOM}}
69launch=${launch:-docker}
70home_mnt=${home_mnt:-${workspace}/jenkins_home}
Alanny Lopez29cc3b12017-10-18 15:18:00 -050071host_import_mnt=${host_import_mnt:-}
Alanny Lopez620baa12017-10-10 11:50:03 -050072cont_import_mnt=${cont_import_mnt:-/mnt/jenkins_import}
Alanny Lopez492c52c2017-11-10 15:06:26 -060073jenkins_options=${jenkins_options:-"--prefix=/jenkins"}
74java_options=${java_options:-"-Xmx4096m"}
Alanny Lopez620baa12017-10-10 11:50:03 -050075
76# Dockerfile Variables
77img_tag=${img_tag:-8-jdk}
78tini_vrsn=${tini_vrsn:-0.16.1}
79j_vrsn=${j_vrsn:-2.60.3}
80j_user=${j_user:-jenkins}
81j_group=${j_group:-jenkins}
82j_uid=${j_uid:-1000}
83j_gid=${j_gid:-1000}
84j_home=${j_home:-/var/jenkins_home}
Alanny Lopez29cc3b12017-10-18 15:18:00 -050085http_port=${http_port:-8080}
Alanny Lopez620baa12017-10-10 11:50:03 -050086agent_port=${agent_port:-50000}
Alanny Lopez29cc3b12017-10-18 15:18:00 -050087out_img=${out_img:-openbmc/jenkins-master-${ARCH}:${j_vrsn}}
Alanny Lopez620baa12017-10-10 11:50:03 -050088
89# Save the Jenkins.war URL to a variable and SHA if we care about verification
90j_url=https://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/${j_vrsn}/jenkins-war-${j_vrsn}.war
91
92# Make or Clean WORKSPACE
93if [[ -d ${workspace} ]]; then
94 rm -rf ${workspace}/Dockerfile \
95 ${workspace}/docker-jenkins \
96 ${workspace}/plugins.* \
97 ${workspace}/install-plugins.sh \
98 ${workspace}/jenkins.sh \
99 ${workspace}/jenkins-support \
100 ${workspace}/init.groovy
101else
102 mkdir -p ${workspace}
103fi
104
105# Determine the prefix of the Dockerfile's base image
106case ${ARCH} in
107 "ppc64le")
108 docker_base="ppc64le/"
109 tini_arch="ppc64el"
110 ;;
111 "x86_64")
112 docker_base=""
113 tini_arch="amd64"
114 ;;
115 *)
116 echo "Unsupported system architecture(${ARCH}) found for docker image"
117 exit 1
118esac
119
120# Move Into the WORKSPACE
121cd ${workspace}
122
123# Make the Dockerfile
124################################################################################
125cat >> Dockerfile << EOF
126FROM ${docker_base}openjdk:${img_tag}
127
128RUN apt-get update && apt-get install -y git curl
129
130ENV JENKINS_HOME ${j_home}
131ENV JENKINS_SLAVE_AGENT_PORT ${agent_port}
132
Alanny Lopez492c52c2017-11-10 15:06:26 -0600133# Jenkins will default to run with user jenkins, uid = 1000
Alanny Lopez620baa12017-10-10 11:50:03 -0500134# If you bind mount a volume from the host or a data container,
135# ensure you use the same uid
136RUN groupadd -g ${j_gid} ${j_group} && \
137 useradd -d ${j_home} -u ${j_uid} -g ${j_gid} -m -s /bin/bash ${j_user}
138
139# Jenkins home directory is a volume, so configuration and build history
140# can be persisted and survive image upgrades
141VOLUME ${j_home}
142
Alanny Lopez492c52c2017-11-10 15:06:26 -0600143# /usr/share/jenkins/ref/ contains all reference configuration we want
Alanny Lopez620baa12017-10-10 11:50:03 -0500144# to set on a fresh new installation. Use it to bundle additional plugins
145# or config file with your custom jenkins Docker image.
146RUN mkdir -p /usr/share/jenkins/ref/init.groovy.d
147
148# Use tini as subreaper in Docker container to adopt zombie processes
149RUN curl -fsSL https://github.com/krallin/tini/releases/download/v${tini_vrsn}/tini-static-${tini_arch} \
150 -o /bin/tini && \
151 chmod +x /bin/tini
152
153COPY init.groovy /usr/share/jenkins/ref/init.groovy.d/tcp-slave-agent-port.groovy
154
155# could use ADD but this one does not check Last-Modified header neither does it allow to control checksum
156# see https://github.com/docker/docker/issues/8331
157RUN curl -fsSL ${j_url} -o /usr/share/jenkins/jenkins.war
158
159ENV JENKINS_UC https://updates.jenkins.io
160ENV JENKINS_UC_EXPERIMENTAL=https://updates.jenkins.io/experimental
161RUN chown -R ${j_user} ${j_home} /usr/share/jenkins/ref
162
163# for main web interface:
164EXPOSE ${http_port}
165
166# will be used by attached slave agents:
167EXPOSE ${agent_port}
168
169ENV COPY_REFERENCE_FILE_LOG ${j_home}/copy_reference_file.log
170USER ${j_user}
171
172COPY jenkins-support /usr/local/bin/jenkins-support
173COPY jenkins.sh /usr/local/bin/jenkins.sh
174ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/jenkins.sh"]
175
Alanny Lopez492c52c2017-11-10 15:06:26 -0600176# from a derived Dockerfile, can use RUN plugins.sh active.txt to setup /usr/share/jenkins/ref/plugins from a support bundle
Alanny Lopez620baa12017-10-10 11:50:03 -0500177COPY plugins.sh /usr/local/bin/plugins.sh
178COPY install-plugins.sh /usr/local/bin/install-plugins.sh
179
180# Install plugins.txt plugins
181COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
182RUN /usr/local/bin/install-plugins.sh < /usr/share/jenkins/ref/plugins.txt
183EOF
184################################################################################
185
186# Clone in the jenkinsci docker jenkins repo and copy some files into WORKSPACE
187git clone https://github.com/jenkinsci/docker.git docker-jenkins
188cp docker-jenkins/init.groovy .
189cp docker-jenkins/jenkins-support .
190cp docker-jenkins/jenkins.sh .
191cp docker-jenkins/plugins.sh .
192cp docker-jenkins/install-plugins.sh .
193
194# Generate Plugins.txt, the plugins you want installed automatically go here
195################################################################################
196cat >> plugins.txt << EOF
197kubernetes
198EOF
199################################################################################
200
201# Build the image
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500202docker build -t ${out_img} .
Alanny Lopez620baa12017-10-10 11:50:03 -0500203
204if [[ ${launch} == "docker" ]]; then
205
206 # Ensure directories that will be mounted exist
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500207 if [[ ! -z ${host_import_mnt} && ! -d ${host_import_mnt} ]]; then
208 mkdir -p ${host_import_mnt}
Alanny Lopez620baa12017-10-10 11:50:03 -0500209 fi
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500210
Alanny Lopez620baa12017-10-10 11:50:03 -0500211 if [[ ! -d ${home_mnt} ]]; then
212 mkdir -p ${home_mnt}
213 fi
214
215 # Ensure directories tht will be mounted are owned by the jenkins user
216 if [[ "$(id -u)" != 0 ]]; then
217 echo "Not running as root:"
218 echo "Checking if jgid and juid are the owners of mounted directories"
219 test1=$(ls -nd ${home_mnt} | awk '{print $3 " " $4}')
Alanny Lopez620baa12017-10-10 11:50:03 -0500220 if [[ "${test1}" != "${j_uid} ${j_gid}" ]]; then
221 echo "Owner of ${home_mnt} is not the jenkins user"
222 echo "${test1} != ${j_uid} ${j_gid}"
223 willfail=1
224 fi
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500225 if [[ ! -z "${host_import_mnt}" ]]; then
226 test2=$(ls -nd ${host_import_mnt} | awk '{print $3 " " $4}' )
227 if [[ "${test2}" != "${j_uid} ${j_gid}" ]]; then
228 echo "Owner of ${host_import_mnt} is not the jenkins user"
229 echo "${test2} != ${j_uid} ${j_gid}"
230 willfail=1
231 fi
Alanny Lopez620baa12017-10-10 11:50:03 -0500232 fi
233 if [[ "${willfail}" == 1 ]]; then
234 echo "Failing before attempting to launch container"
235 echo "Try again as root or use correct uid/gid pairs"
236 exit 1
237 fi
238 else
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500239 if [[ ! -z ${host_import_mnt} ]]; then
240 chown -R ${j_uid}:${j_gid} ${host_import_mnt}
241 fi
Alanny Lopez620baa12017-10-10 11:50:03 -0500242 chown -R ${j_uid}:${j_gid} ${home_mnt}
243 fi
244
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500245 #If we don't have import mount don't add to docker command
246 if [[ ! -z ${host_import_mnt} ]]; then
247 importvolcmd="-v ${host_import_mnt}:${cont_import_mnt}"
248 fi
Alanny Lopez620baa12017-10-10 11:50:03 -0500249 # Launch the jenkins image with Docker
250 docker run -d \
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500251 ${importvolcmd} \
Alanny Lopez620baa12017-10-10 11:50:03 -0500252 -v ${home_mnt}:${j_home} \
253 -p ${http_port}:8080 \
254 -p ${agent_port}:${agent_port} \
Alanny Lopez492c52c2017-11-10 15:06:26 -0600255 --env JAVA_OPTS=\"${java_options}\" \
256 --env JENKINS_OPTS=\"${jenkins_options}\" \
Alanny Lopez620baa12017-10-10 11:50:03 -0500257 ${out_img}
258
259elif [[ ${launch} == "k8s" ]]; then
260 # launch using the k8s template
261 echo "Not yet Implemented"
262 exit 1
Alanny Lopeze08e8702018-02-24 18:07:13 -0600263 source ${build_scripts_dir}/kubernetes/kubernetes-launch.sh Build-Jenkins false false
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500264fi