blob: 5eecd068b996fc75a1535135915ec949e83916cb [file] [log] [blame]
Alanny Lopez620baa12017-10-10 11:50:03 -05001#!/bin/bash
2################################################################################
3# Script used to create a Jenkins master that can run amd64 or ppc64le. It can
4# be used to launch the Jenkins master as a Docker container locally or as a
5# Kubernetes Deployment in a Kubernetes cluster.
6################################################################################
7# Launch Variables:
8# These variables are used to determine how the master will be launched
9# workspace The directory that hold files used to deploy the Jenkins
10# master
11# Default: "${HOME}/jenkins-build-${RANDOM}"
12# launch Method in which the container will be launched, either as
13# a Docker container launched via Docker or by using a
14# helper script to launch into Kubernetes (docker or k8s)
15# Default: "docker"
16# home_mnt The directory on the host used as the Jenkins home
17# Default: "${WORKSPACE}/jenkins_home"
18# host_import_mnt The directory on the host used to import extra files
Alanny Lopez29cc3b12017-10-18 15:18:00 -050019# Default: "", variable ignored by default
Alanny Lopez620baa12017-10-10 11:50:03 -050020# cont_import_mnt The directory on the container used to import extra files
Alanny Lopez29cc3b12017-10-18 15:18:00 -050021# Default: "/mnt/jenkins_import", will be ignored by default
22# options What will be passed as the environment variable for the
23# JENKINS_OPTS environment variable.
24# Default: "--prefix=/jenkins"
Alanny Lopez620baa12017-10-10 11:50:03 -050025#
26# Build Variables:
27# img_tag The tag for the OpenJDK image used to build the Dockerfile
28# Default: "/8-jdk"
29# tini_vrsn The version of Tini to use in the dockerfile, 0.16.1 is
30# the first release with ppc64le release support
31# Default: "0.16.1"
32# j_vrsn The version of the Jenkins war file you wish to use
33# Default: "2.60.3"
34# j_user Username tag the container will use to run Jenkins
35# Default: "jenkins"
36# j_group Group name tag the container will use to run Jenkins
37# Default: "jenkins"
38# j_uid Jenkins user ID the container will use to run Jenkins
39# Default: "1000"
40# j_gif Jenkins group ID the container will use to run Jenkins
41# Default: "1000"
42# j_home Directory used as the Jenkins Home in the container
Alanny Lopez29cc3b12017-10-18 15:18:00 -050043# Default: "/var/jenkins_home"
Alanny Lopez620baa12017-10-10 11:50:03 -050044# http_port The port used as Jenkins UI port
45# Default: "8080"
46# agent_port The port used as the Jenkins slave agent port
47# Default: "50000"
48# out_img The name given to the Docker image when it is built
49# Default: "openbmc/jenkins-master-${ARCH}:${JENKINS_VRSN}"
50#
51################################################################################
52
53set -xeo pipefail
54ARCH=$(uname -m)
55
56# Launch Variables
57workspace=${workspace:-${HOME}/jenkins-build-${RANDOM}}
58launch=${launch:-docker}
59home_mnt=${home_mnt:-${workspace}/jenkins_home}
Alanny Lopez29cc3b12017-10-18 15:18:00 -050060host_import_mnt=${host_import_mnt:-}
Alanny Lopez620baa12017-10-10 11:50:03 -050061cont_import_mnt=${cont_import_mnt:-/mnt/jenkins_import}
Alanny Lopez29cc3b12017-10-18 15:18:00 -050062options="--prefix=/jenkins"
Alanny Lopez620baa12017-10-10 11:50:03 -050063
64# Dockerfile Variables
65img_tag=${img_tag:-8-jdk}
66tini_vrsn=${tini_vrsn:-0.16.1}
67j_vrsn=${j_vrsn:-2.60.3}
68j_user=${j_user:-jenkins}
69j_group=${j_group:-jenkins}
70j_uid=${j_uid:-1000}
71j_gid=${j_gid:-1000}
72j_home=${j_home:-/var/jenkins_home}
Alanny Lopez29cc3b12017-10-18 15:18:00 -050073http_port=${http_port:-8080}
Alanny Lopez620baa12017-10-10 11:50:03 -050074agent_port=${agent_port:-50000}
Alanny Lopez29cc3b12017-10-18 15:18:00 -050075out_img=${out_img:-openbmc/jenkins-master-${ARCH}:${j_vrsn}}
Alanny Lopez620baa12017-10-10 11:50:03 -050076
77# Save the Jenkins.war URL to a variable and SHA if we care about verification
78j_url=https://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/${j_vrsn}/jenkins-war-${j_vrsn}.war
79
80# Make or Clean WORKSPACE
81if [[ -d ${workspace} ]]; then
82 rm -rf ${workspace}/Dockerfile \
83 ${workspace}/docker-jenkins \
84 ${workspace}/plugins.* \
85 ${workspace}/install-plugins.sh \
86 ${workspace}/jenkins.sh \
87 ${workspace}/jenkins-support \
88 ${workspace}/init.groovy
89else
90 mkdir -p ${workspace}
91fi
92
93# Determine the prefix of the Dockerfile's base image
94case ${ARCH} in
95 "ppc64le")
96 docker_base="ppc64le/"
97 tini_arch="ppc64el"
98 ;;
99 "x86_64")
100 docker_base=""
101 tini_arch="amd64"
102 ;;
103 *)
104 echo "Unsupported system architecture(${ARCH}) found for docker image"
105 exit 1
106esac
107
108# Move Into the WORKSPACE
109cd ${workspace}
110
111# Make the Dockerfile
112################################################################################
113cat >> Dockerfile << EOF
114FROM ${docker_base}openjdk:${img_tag}
115
116RUN apt-get update && apt-get install -y git curl
117
118ENV JENKINS_HOME ${j_home}
119ENV JENKINS_SLAVE_AGENT_PORT ${agent_port}
120
121# Jenkins will default to run with user `jenkins`, uid = 1000
122# If you bind mount a volume from the host or a data container,
123# ensure you use the same uid
124RUN groupadd -g ${j_gid} ${j_group} && \
125 useradd -d ${j_home} -u ${j_uid} -g ${j_gid} -m -s /bin/bash ${j_user}
126
127# Jenkins home directory is a volume, so configuration and build history
128# can be persisted and survive image upgrades
129VOLUME ${j_home}
130
131# `/usr/share/jenkins/ref/` contains all reference configuration we want
132# to set on a fresh new installation. Use it to bundle additional plugins
133# or config file with your custom jenkins Docker image.
134RUN mkdir -p /usr/share/jenkins/ref/init.groovy.d
135
136# Use tini as subreaper in Docker container to adopt zombie processes
137RUN curl -fsSL https://github.com/krallin/tini/releases/download/v${tini_vrsn}/tini-static-${tini_arch} \
138 -o /bin/tini && \
139 chmod +x /bin/tini
140
141COPY init.groovy /usr/share/jenkins/ref/init.groovy.d/tcp-slave-agent-port.groovy
142
143# could use ADD but this one does not check Last-Modified header neither does it allow to control checksum
144# see https://github.com/docker/docker/issues/8331
145RUN curl -fsSL ${j_url} -o /usr/share/jenkins/jenkins.war
146
147ENV JENKINS_UC https://updates.jenkins.io
148ENV JENKINS_UC_EXPERIMENTAL=https://updates.jenkins.io/experimental
149RUN chown -R ${j_user} ${j_home} /usr/share/jenkins/ref
150
151# for main web interface:
152EXPOSE ${http_port}
153
154# will be used by attached slave agents:
155EXPOSE ${agent_port}
156
157ENV COPY_REFERENCE_FILE_LOG ${j_home}/copy_reference_file.log
158USER ${j_user}
159
160COPY jenkins-support /usr/local/bin/jenkins-support
161COPY jenkins.sh /usr/local/bin/jenkins.sh
162ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/jenkins.sh"]
163
164# from a derived Dockerfile, can use `RUN plugins.sh active.txt` to setup /usr/share/jenkins/ref/plugins from a support bundle
165COPY plugins.sh /usr/local/bin/plugins.sh
166COPY install-plugins.sh /usr/local/bin/install-plugins.sh
167
168# Install plugins.txt plugins
169COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
170RUN /usr/local/bin/install-plugins.sh < /usr/share/jenkins/ref/plugins.txt
171EOF
172################################################################################
173
174# Clone in the jenkinsci docker jenkins repo and copy some files into WORKSPACE
175git clone https://github.com/jenkinsci/docker.git docker-jenkins
176cp docker-jenkins/init.groovy .
177cp docker-jenkins/jenkins-support .
178cp docker-jenkins/jenkins.sh .
179cp docker-jenkins/plugins.sh .
180cp docker-jenkins/install-plugins.sh .
181
182# Generate Plugins.txt, the plugins you want installed automatically go here
183################################################################################
184cat >> plugins.txt << EOF
185kubernetes
186EOF
187################################################################################
188
189# Build the image
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500190docker build -t ${out_img} .
Alanny Lopez620baa12017-10-10 11:50:03 -0500191
192if [[ ${launch} == "docker" ]]; then
193
194 # Ensure directories that will be mounted exist
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500195 if [[ ! -z ${host_import_mnt} && ! -d ${host_import_mnt} ]]; then
196 mkdir -p ${host_import_mnt}
Alanny Lopez620baa12017-10-10 11:50:03 -0500197 fi
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500198
Alanny Lopez620baa12017-10-10 11:50:03 -0500199 if [[ ! -d ${home_mnt} ]]; then
200 mkdir -p ${home_mnt}
201 fi
202
203 # Ensure directories tht will be mounted are owned by the jenkins user
204 if [[ "$(id -u)" != 0 ]]; then
205 echo "Not running as root:"
206 echo "Checking if jgid and juid are the owners of mounted directories"
207 test1=$(ls -nd ${home_mnt} | awk '{print $3 " " $4}')
Alanny Lopez620baa12017-10-10 11:50:03 -0500208 if [[ "${test1}" != "${j_uid} ${j_gid}" ]]; then
209 echo "Owner of ${home_mnt} is not the jenkins user"
210 echo "${test1} != ${j_uid} ${j_gid}"
211 willfail=1
212 fi
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500213 if [[ ! -z "${host_import_mnt}" ]]; then
214 test2=$(ls -nd ${host_import_mnt} | awk '{print $3 " " $4}' )
215 if [[ "${test2}" != "${j_uid} ${j_gid}" ]]; then
216 echo "Owner of ${host_import_mnt} is not the jenkins user"
217 echo "${test2} != ${j_uid} ${j_gid}"
218 willfail=1
219 fi
Alanny Lopez620baa12017-10-10 11:50:03 -0500220 fi
221 if [[ "${willfail}" == 1 ]]; then
222 echo "Failing before attempting to launch container"
223 echo "Try again as root or use correct uid/gid pairs"
224 exit 1
225 fi
226 else
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500227 if [[ ! -z ${host_import_mnt} ]]; then
228 chown -R ${j_uid}:${j_gid} ${host_import_mnt}
229 fi
Alanny Lopez620baa12017-10-10 11:50:03 -0500230 chown -R ${j_uid}:${j_gid} ${home_mnt}
231 fi
232
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500233 #If we don't have import mount don't add to docker command
234 if [[ ! -z ${host_import_mnt} ]]; then
235 importvolcmd="-v ${host_import_mnt}:${cont_import_mnt}"
236 fi
Alanny Lopez620baa12017-10-10 11:50:03 -0500237 # Launch the jenkins image with Docker
238 docker run -d \
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500239 ${importvolcmd} \
Alanny Lopez620baa12017-10-10 11:50:03 -0500240 -v ${home_mnt}:${j_home} \
241 -p ${http_port}:8080 \
242 -p ${agent_port}:${agent_port} \
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500243 --env JENKINS_OPTS=\"${options}\" \
Alanny Lopez620baa12017-10-10 11:50:03 -0500244 ${out_img}
245
246elif [[ ${launch} == "k8s" ]]; then
247 # launch using the k8s template
248 echo "Not yet Implemented"
249 exit 1
250 source ./kubernetes/kubernetes-launch.sh Build-Jenkins false false
Alanny Lopez29cc3b12017-10-18 15:18:00 -0500251fi