JJB: Include perf analysis per job
[ci-management.git] / docker / scripts / lib_apt.sh
1 # lib_apt.sh - Docker build script apt library.
2 #              For import only.
3
4 # Copyright (c) 2021 Cisco and/or its affiliates.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Don't import more than once.
18 if [ -n "$(alias lib_apt_imported 2> /dev/null)" ] ; then
19     return 0
20 fi
21 alias lib_apt_imported=true
22
23 export CIMAN_DOCKER_SCRIPTS=${CIMAN_DOCKER_SCRIPTS:-"$(dirname $BASH_SOURCE)"}
24 . "$CIMAN_DOCKER_SCRIPTS/lib_common.sh"
25 . "$CIMAN_DOCKER_SCRIPTS/lib_csit.sh"
26
27 dump_apt_package_list() {
28     branchname="$(echo $branch | sed -e 's,/,_,')"
29     dpkg -l > \
30          "$DOCKER_BUILD_LOG_DIR/$FDIOTOOLS_IMAGENAME-$branchname-apt-packages.log"
31 }
32
33 apt_install_packages() {
34     apt-get install -y --allow-downgrades --allow-remove-essential \
35             --allow-change-held-packages $@
36 }
37
38 # Used for older OS distro's which are incompatible
39 # with modern distro cmake vesrion
40 apt_override_cmake_install_with_pip3_version() {
41     local os_cmake="/usr/bin/cmake"
42     local os_cmake_ver="$($os_cmake --version | head -1)"
43     local pip3_cmake="/usr/local/bin/cmake"
44
45     python3 -m pip --disable-pip-version-check install cmake || true
46     local pip3_cmake_ver="$($pip3_cmake --version | head -1)"
47     echo_log "Overriding $OS_NAME '$os_cmake_ver' with '$pip3_cmake_ver'!"
48     apt-get remove -y cmake --autoremove || true
49     update-alternatives --quiet --remove-all cmake || true
50     update-alternatives --quiet --install "$os_cmake" cmake "$pip3_cmake" 100
51     echo_log "Default cmake ($(which cmake)) version: '$(cmake --version | head -1)'!"
52 }
53
54 generate_apt_dockerfile_common() {
55     local executor_class="$1"
56     local executor_image="$2"
57         debian_docker_inst_sed="| sed -e 's/has_rootless_extras="1"//g' | sh
58 "
59     cat <<EOF >>"$DOCKERFILE"
60
61 # Create download dir to cache external tarballs
62 WORKDIR $DOCKER_DOWNLOADS_DIR
63
64 # Copy-in temporary build tree containing
65 # ci-management, vpp, & csit git repos
66 WORKDIR $DOCKER_BUILD_DIR
67 COPY . .
68
69 # Build Environment Variables
70 ENV DEBIAN_FRONTEND="noninteractive"
71 ENV FDIOTOOLS_IMAGE="$executor_image"
72 ENV FDIOTOOLS_EXECUTOR_CLASS="$executor_class"
73 ENV CIMAN_ROOT="$DOCKER_CIMAN_ROOT"
74 ENV PATH="\$PATH:$DOCKER_CIMAN_ROOT/docker/scripts"
75
76 # Configure locales
77 RUN apt-get update -qq \\
78   && apt-get install -y \\
79         apt-utils \\
80         locales \\
81   && sed -i 's/# \(en_US\.UTF-8 .*\)/\1/' /etc/locale.gen \\
82   && locale-gen en_US.UTF-8 \\
83   && dpkg-reconfigure --frontend=noninteractive locales \\
84   && update-locale LANG=en_US.UTF-8 \\
85   && TZ=Etc/UTC && ln -snf /usr/share/zoneinfo/\$TZ /etc/localtime && echo \$TZ > /etc/timezone \\
86   && rm -r /var/lib/apt/lists/*
87 ENV LANG="en_US.UTF-8" LANGUAGE="en_US" LC_ALL="en_US.UTF-8"
88
89 # Install baseline packages (minimum build & utils).
90 #
91 # ci-management global-jjb requirements:
92 #        facter
93 #        python3-pip
94 #        python3-venv
95 #    for lftools:
96 #        xmlstarlet
97 #        libxml2-dev
98 #        libxslt-dev
99 #   from packer/provision/baseline.sh:
100 #        unzip
101 #        xz-utils
102 #        git
103 #        git-review
104 #        libxml2-dev
105 #        libxml-xpath-perl
106 #        libxslt-dev
107 #        make
108 #        wget
109 #        jq
110 #
111 # Python build from source requirements:
112 #        build-essential
113 #
114 # TODO:  Fix broken project requirement install targets
115 #        graphviz         for 'make bootstrap-doxygen' (VPP)
116 #        doxygen          for 'make doxygen' (VPP)
117 #        enchant          for 'make docs' (VPP)
118 #        libffi-dev       for python cffi install (Ubuntu20.04/VPP/aarch64)
119 #        liblapack-dev    for python numpy/scipy (CSIT/aarch64)
120 #        libopenblas-dev  for python numpy/scipy (CSIT/aarch64)
121 #        libpcap-dev      for python pypcap install (CSIT)
122 #        sshpass          for CSIT jobs
123 #
124 #        From .../csit/resources/tools/presentation/run_report_*.sh:
125 #        libxml2
126 #        libxml2-dev
127 #        libxslt-dev
128 #        build-essential
129 #        zlib1g-dev
130 #        unzip
131 #        xvrb
132 #        texlive-latex-recommended
133 #        texlive-fonts-recommended
134 #        texlive-fonts-extra
135 #        texlive-latex-extra
136 #        latexmk
137 #        wkhtmltopdf
138 #        inkscape
139 #
140 RUN apt-get update -qq \\
141   && apt-get install -y \\
142              apt-transport-https \\
143              curl \\
144              ca-certificates \\
145              default-jdk \\
146              default-jre \\
147              dnsutils \\
148              doxygen \\
149              enchant \\
150              emacs \\
151              facter \\
152              gawk \\
153              gdb \\
154              gfortran \\
155              git \\
156              git-review \\
157              gnupg-agent \\
158              graphviz \\
159              inkscape \\
160              iproute2 \\
161              iputils-clockdiff \\
162              iputils-ping \\
163              iputils-tracepath \\
164              jq \\
165              latexmk \\
166              libffi-dev \\
167              liblapack-dev \\
168              libopenblas-dev \\
169              libpcap-dev \\
170              libxml2 \\
171              libxml2-dev \\
172              libxml-xpath-perl \\
173              libxslt-dev \\
174              make \\
175              python3-pip \\
176              python3-venv \\
177              rsync \\
178              ruby-dev \\
179              software-properties-common \\
180              sshpass \\
181              sysstat \\
182              sudo \\
183              texlive-fonts-extra \\
184              texlive-fonts-recommended \\
185              texlive-latex-extra \\
186              texlive-latex-recommended \\
187              traceroute \\
188              tree \\
189              unzip \\
190              vim \\
191              wget \\
192              wkhtmltopdf \\
193              xmlstarlet \\
194              xvfb \\
195              xz-utils \\
196              zlib1g-dev \\
197   && curl -L https://packagecloud.io/fdio/master/gpgkey | apt-key add - \\
198   && curl -s https://packagecloud.io/install/repositories/fdio/master/script.deb.sh | bash \\
199 EOF
200     # Hack to prevent failure on debian-9 build
201     head $DOCKERFILE
202     if grep -qe 'debian:9' "$DOCKERFILE" ; then
203         echo "  && curl -fsSL https://get.docker.com | sed -e 's/has_rootless_extras=\"1\"//g' | sh \ " >>"$DOCKERFILE"
204     else
205         echo "  && curl -fsSL https://get.docker.com | sh \ " >>"$DOCKERFILE"
206     fi
207
208     cat <<EOF >>"$DOCKERFILE"
209   && rm -r /var/lib/apt/lists/*
210
211 # Install packages for all project branches
212 #
213 RUN apt-get update -qq \\
214   && dbld_vpp_install_packages.sh \\
215   && dbld_csit_install_packages.sh \\
216   && rm -r /var/lib/apt/lists/*
217 EOF
218 }
219
220 generate_apt_dockerfile_clean() {
221     cat <<EOF >>"$DOCKERFILE"
222
223 # Clean up copy-in build tree
224 RUN dbld_dump_build_logs.sh \\
225   && rm -rf "/tmp/*" "$DOCKER_BUILD_FILES_DIR" "/root/.ccache"
226 EOF
227 }
228
229 # Generate 'builder' class apt dockerfile
230 builder_generate_apt_dockerfile() {
231     local executor_class="$1"
232     local executor_os_name="$2"
233     local executor_image="$3"
234     local vpp_install_skip_sysctl_envvar="";
235
236     if grep -q "debian-9"  <<< "$executor_os_name" ; then
237         # Workaround to VPP package installation failure on debian-9
238         vpp_install_skip_sysctl_envvar="ENV VPP_INSTALL_SKIP_SYSCTL=1"
239     fi
240     generate_apt_dockerfile_common $executor_class $executor_image
241     csit_builder_generate_docker_build_files
242     cat <<EOF >>"$DOCKERFILE"
243
244 # Install LF-IT requirements
245 ENV LF_VENV="/root/lf-venv"
246 RUN apt-get update -qq \\
247   && dbld_lfit_requirements.sh \\
248   && rm -r /var/lib/apt/lists/*
249
250 # Install packagecloud requirements
251 RUN gem install rake package_cloud \\
252   && curl -s https://packagecloud.io/install/repositories/fdio/master/script.deb.sh | bash
253
254 # Install CSIT ssh requirements
255 # TODO: Verify why badkey is required & figure out how to avoid it.
256 COPY files/badkey /root/.ssh/id_rsa
257 COPY files/sshconfig /root/.ssh/config
258
259 # CI Runtime Environment
260 WORKDIR /
261 $vpp_install_skip_sysctl_envvar
262 ENV VPP_ZOMBIE_NOCHECK="1"
263 ENV CCACHE_DIR="/scratch/ccache"
264 ENV CCACHE_MAXSIZE="10G"
265 EOF
266     generate_apt_dockerfile_clean
267 }
268
269 # Generate 'csit_dut' class apt dockerfile
270 csit_dut_generate_apt_dockerfile() {
271     local executor_class="$1"
272     local executor_os_name="$2"
273     local executor_image="$3"
274
275     csit_dut_generate_docker_build_files
276     generate_apt_dockerfile_common "$executor_class" "$executor_image"
277     cat <<EOF >>"$DOCKERFILE"
278
279 # Install csit_dut specific packages
280 RUN apt-get update -qq \\
281   && apt-get install -y \\
282              net-tools \\
283              openssh-server \\
284              pciutils \\
285              rsyslog \\
286              supervisor \\
287   && rm -r /var/lib/apt/lists/*
288
289 # Fix permissions
290 RUN chown root:syslog /var/log \\
291   && chmod 755 /etc/default
292
293 # Create directory structure
294 RUN mkdir -p /var/run/sshd
295
296 # SSH settings
297 RUN echo 'root:Csit1234' | chpasswd \\
298   && sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config \\
299   && sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
300
301 EXPOSE 2222
302
303 COPY files/supervisord.conf /etc/supervisor/supervisord.conf
304
305 CMD ["sh", "-c", "rm -f /dev/shm/db /dev/shm/global_vm /dev/shm/vpe-api; /usr/bin/supervisord -c /etc/supervisor/supervisord.conf; /usr/sbin/sshd -D -p 2222"]
306 EOF
307     generate_apt_dockerfile_clean
308 }
309
310 # Generate 'csit_shim' class apt dockerfile
311 csit_shim_generate_apt_dockerfile() {
312     local executor_class="$1"
313     local executor_os_name="$2"
314     local executor_image="$3"
315
316     csit_shim_generate_docker_build_files
317     cat <<EOF >>"$DOCKERFILE"
318
319 # Copy-in temporary build tree containing
320 # ci-management, vpp, & csit git repos
321 WORKDIR $DOCKER_BUILD_DIR
322 COPY . .
323
324 # Build Environment Variables
325 ENV DEBIAN_FRONTEND="noninteractive"
326 ENV FDIOTOOLS_IMAGE="$executor_image"
327 ENV FDIOTOOLS_EXECUTOR_CLASS="$executor_class"
328 ENV CIMAN_ROOT="$DOCKER_CIMAN_ROOT"
329 ENV PATH="\$PATH:$DOCKER_CIMAN_ROOT/docker/scripts"
330
331 # Configure locales & timezone
332 RUN apt-get update -qq \\
333   && apt-get install -y \\
334              apt-utils \\
335              locales \\
336   && sed -i 's/# \(en_US\.UTF-8 .*\)/\1/' /etc/locale.gen \\
337   && locale-gen en_US.UTF-8 \\
338   && dpkg-reconfigure --frontend=noninteractive locales \\
339   && update-locale LANG=en_US.UTF-8 \\
340   && TZ=Etc/UTC && ln -snf /usr/share/zoneinfo/\$TZ /etc/localtime && echo \$TZ > /etc/timezone \\
341   && rm -r /var/lib/apt/lists/*
342 ENV LANG=en_US.UTF-8 LANGUAGE=en_US LC_ALL=en_US.UTF-8
343
344 COPY files/wrapdocker /usr/local/bin/wrapdocker
345 RUN chmod +x /usr/local/bin/wrapdocker
346
347 # Install packages and Docker
348 RUN apt-get update -qq \\
349   && apt-get install -y  \\
350              bash \\
351              curl \\
352              iproute2 \\
353              locales \\
354              ssh \\
355              sudo \\
356              tzdata \\
357              uuid-runtime \\
358   && curl -fsSL https://get.docker.com | sh \\
359   && rm -rf /var/lib/apt/lists/*
360
361 RUN mkdir /var/run/sshd
362 RUN echo 'root:Csit1234' | chpasswd
363 RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
364
365 # SSH login fix. Otherwise user is kicked off after login
366 RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
367
368 # Need volume for sidecar docker launches
369 VOLUME /var/lib/docker
370
371 # SSH to listen on port 6022 in shim
372 RUN echo 'Port 6022' >>/etc/ssh/sshd_config
373 RUN echo 'Port 6023' >>/etc/ssh/sshd_config
374
375 # TODO: Verify why badkeypub is required & figure out how to avoid it.
376 COPY files/badkeypub /root/.ssh/authorized_keys
377 COPY files/sshconfig /root/.ssh/config
378
379 # Clean up copy-in build tree
380 RUN rm -rf /tmp/* $DOCKER_BUILD_FILES_DIR
381
382 # Start sshd by default
383 EXPOSE 22
384 CMD ["/usr/sbin/sshd", "-D"]
385 EOF
386 }
387
388 generate_apt_dockerfile() {
389     local executor_class="$1"
390     local executor_os_name="$2"
391     local from_image="$3"
392     local executor_image="$4"
393
394     cat <<EOF  >"$DOCKERIGNOREFILE"
395 **/__pycache__
396 *.pyc
397 EOF
398     cat <<EOF  >"$DOCKERFILE"
399 FROM $from_image AS ${executor_class}-executor-image
400 LABEL Description="FD.io CI '$executor_class' executor docker image for $executor_os_name/$OS_ARCH"
401 LABEL Vendor="fd.io"
402 LABEL Version="$DOCKER_TAG"
403 EOF
404     ${executor_class}_generate_apt_dockerfile "$executor_class" \
405         "$executor_os_name" "$executor_image"
406 }