Merge "Use Ubuntu-20.04 for vpp-csit-* job executor images"
[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
58     cat <<EOF >>"$DOCKERFILE"
59
60 # Create download dir to cache external tarballs
61 WORKDIR $DOCKER_DOWNLOADS_DIR
62
63 # Copy-in temporary build tree containing
64 # ci-management, vpp, & csit git repos
65 WORKDIR $DOCKER_BUILD_DIR
66 COPY . .
67
68 # Build Environment Variables
69 ENV DEBIAN_FRONTEND="noninteractive"
70 ENV FDIOTOOLS_IMAGE="$executor_image"
71 ENV FDIOTOOLS_EXECUTOR_CLASS="$executor_class"
72 ENV CIMAN_ROOT="$DOCKER_CIMAN_ROOT"
73 ENV PATH="\$PATH:$DOCKER_CIMAN_ROOT/docker/scripts"
74
75 # Configure locales
76 RUN apt-get update -qq \\
77   && apt-get install -y \\
78         apt-utils \\
79         locales \\
80   && sed -i 's/# \(en_US\.UTF-8 .*\)/\1/' /etc/locale.gen \\
81   && locale-gen en_US.UTF-8 \\
82   && dpkg-reconfigure --frontend=noninteractive locales \\
83   && update-locale LANG=en_US.UTF-8 \\
84   && TZ=Etc/UTC && ln -snf /usr/share/zoneinfo/\$TZ /etc/localtime && echo \$TZ > /etc/timezone \\
85   && rm -r /var/lib/apt/lists/*
86 ENV LANG="en_US.UTF-8" LANGUAGE="en_US" LC_ALL="en_US.UTF-8"
87
88 # Install baseline packages (minimum build & utils).
89 #
90 # ci-management global-jjb requirements:
91 #        facter
92 #        python3-pip
93 #        python3-venv
94 #    for lftools:
95 #        xmlstarlet
96 #        libxml2-dev
97 #        libxslt-dev
98 #   from packer/provision/baseline.sh:
99 #        unzip
100 #        xz-utils
101 #        git
102 #        git-review
103 #        libxml2-dev
104 #        libxml-xpath-perl
105 #        libxslt-dev
106 #        make
107 #        wget
108 #        jq
109 #
110 # Python build from source requirements:
111 #        build-essential
112 #
113 # TODO:  Fix broken project requirement install targets
114 #        graphviz         for 'make bootstrap-doxygen' (VPP)
115 #        doxygen          for 'make doxygen' (VPP)
116 #        enchant          for 'make docs' (VPP)
117 #        libffi-dev       for python cffi install (Ubuntu20.04/VPP/aarch64)
118 #        liblapack-dev    for python numpy/scipy (CSIT/aarch64)
119 #        libopenblas-dev  for python numpy/scipy (CSIT/aarch64)
120 #        libpcap-dev      for python pypcap install (CSIT)
121 #        sshpass          for CSIT jobs
122 #
123 #        From .../csit/resources/tools/presentation/run_report_*.sh:
124 #        libxml2
125 #        libxml2-dev
126 #        libxslt-dev
127 #        build-essential
128 #        zlib1g-dev
129 #        unzip
130 #        xvrb
131 #        texlive-latex-recommended
132 #        texlive-fonts-recommended
133 #        texlive-fonts-extra
134 #        texlive-latex-extra
135 #        latexmk
136 #        wkhtmltopdf
137 #        inkscape
138 #
139 RUN apt-get update -qq \\
140   && apt-get install -y \\
141              apt-transport-https \\
142              curl \\
143              ca-certificates \\
144              default-jdk \\
145              default-jre \\
146              dnsutils \\
147              doxygen \\
148              enchant \\
149              emacs \\
150              facter \\
151              gawk \\
152              gdb \\
153              gfortran \\
154              git \\
155              git-review \\
156              gnupg-agent \\
157              graphviz \\
158              inkscape \\
159              iproute2 \\
160              iputils-clockdiff \\
161              iputils-ping \\
162              iputils-tracepath \\
163              jq \\
164              latexmk \\
165              libffi-dev \\
166              liblapack-dev \\
167              libopenblas-dev \\
168              libpcap-dev \\
169              libxml2 \\
170              libxml2-dev \\
171              libxml-xpath-perl \\
172              libxslt-dev \\
173              make \\
174              python3-pip \\
175              python3-venv \\
176              rsync \\
177              ruby-dev \\
178              software-properties-common \\
179              sshpass \\
180              sudo \\
181              texlive-fonts-extra \\
182              texlive-fonts-recommended \\
183              texlive-latex-extra \\
184              texlive-latex-recommended \\
185              traceroute \\
186              tree \\
187              unzip \\
188              vim \\
189              wget \\
190              wkhtmltopdf \\
191              xmlstarlet \\
192              xvfb \\
193              xz-utils \\
194              zlib1g-dev \\
195   && curl -L https://packagecloud.io/fdio/master/gpgkey | apt-key add - \\
196   && curl -s https://packagecloud.io/install/repositories/fdio/master/script.deb.sh | bash \\
197   && curl -fsSL https://get.docker.com | sh \\
198   && rm -r /var/lib/apt/lists/*
199
200 # Install packages for all project branches
201 #
202 RUN apt-get update -qq \\
203   && dbld_vpp_install_packages.sh \\
204   && dbld_csit_install_packages.sh \\
205   && rm -r /var/lib/apt/lists/*
206 EOF
207 }
208
209 generate_apt_dockerfile_clean() {
210     cat <<EOF >>"$DOCKERFILE"
211
212 # Clean up copy-in build tree
213 RUN dbld_dump_build_logs.sh \\
214   && rm -rf "/tmp/*" "$DOCKER_BUILD_FILES_DIR"
215 EOF
216 }
217
218 # Generate 'builder' class apt dockerfile
219 builder_generate_apt_dockerfile() {
220     local executor_class="$1"
221     local executor_os_name="$2"
222     local executor_image="$3"
223     local vpp_install_skip_sysctl_envvar="";
224
225     if grep -q "debian-9"  <<< "$executor_os_name" ; then
226         # Workaround to VPP package installation failure on debian-9
227         vpp_install_skip_sysctl_envvar="ENV VPP_INSTALL_SKIP_SYSCTL=1"
228     fi
229     generate_apt_dockerfile_common $executor_class $executor_image
230     csit_builder_generate_docker_build_files
231     cat <<EOF >>"$DOCKERFILE"
232
233 # Install LF-IT requirements
234 ENV LF_VENV="/root/lf-venv"
235 RUN apt-get update -qq \\
236   && dbld_lfit_requirements.sh \\
237   && rm -r /var/lib/apt/lists/*
238
239 # Install packagecloud requirements
240 RUN gem install rake package_cloud \\
241   && curl -s https://packagecloud.io/install/repositories/fdio/master/script.deb.sh | bash
242
243 # Install CSIT ssh requirements
244 # TODO: Verify why badkey is required & figure out how to avoid it.
245 COPY files/badkey /root/.ssh/id_rsa
246 COPY files/sshconfig /root/.ssh/config
247
248 # CI Runtime Environment
249 WORKDIR /
250 $vpp_install_skip_sysctl_envvar
251 ENV VPP_ZOMBIE_NOCHECK="1"
252 # TODO: Mount ccache volume into docker container, then remove this.
253 ENV CCACHE_DISABLE="1"
254 EOF
255     generate_apt_dockerfile_clean
256 }
257
258 # Generate 'csit_dut' class apt dockerfile
259 csit_dut_generate_apt_dockerfile() {
260     local executor_class="$1"
261     local executor_os_name="$2"
262     local executor_image="$3"
263
264     csit_dut_generate_docker_build_files
265     generate_apt_dockerfile_common "$executor_class" "$executor_image"
266     cat <<EOF >>"$DOCKERFILE"
267
268 # Install csit_dut specific packages
269 RUN apt-get update -qq \\
270   && apt-get install -y \\
271              net-tools \\
272              openssh-server \\
273              pciutils \\
274              rsyslog \\
275              supervisor \\
276   && rm -r /var/lib/apt/lists/*
277
278 # Fix permissions
279 RUN chown root:syslog /var/log \\
280   && chmod 755 /etc/default
281
282 # Create directory structure
283 RUN mkdir -p /var/run/sshd
284
285 # SSH settings
286 RUN echo 'root:Csit1234' | chpasswd \\
287   && sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config \\
288   && sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
289
290 EXPOSE 2222
291
292 COPY files/supervisord.conf /etc/supervisor/supervisord.conf
293
294 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"]
295 EOF
296     generate_apt_dockerfile_clean
297 }
298
299 # Generate 'csit_shim' class apt dockerfile
300 csit_shim_generate_apt_dockerfile() {
301     local executor_class="$1"
302     local executor_os_name="$2"
303     local executor_image="$3"
304
305     csit_shim_generate_docker_build_files
306     cat <<EOF >>"$DOCKERFILE"
307
308 # Copy-in temporary build tree containing
309 # ci-management, vpp, & csit git repos
310 WORKDIR $DOCKER_BUILD_DIR
311 COPY . .
312
313 # Build Environment Variables
314 ENV DEBIAN_FRONTEND="noninteractive"
315 ENV FDIOTOOLS_IMAGE="$executor_image"
316 ENV FDIOTOOLS_EXECUTOR_CLASS="$executor_class"
317 ENV CIMAN_ROOT="$DOCKER_CIMAN_ROOT"
318 ENV PATH="\$PATH:$DOCKER_CIMAN_ROOT/docker/scripts"
319
320 # Configure locales & timezone
321 RUN apt-get update -qq \\
322   && apt-get install -y \\
323              apt-utils \\
324              locales \\
325   && sed -i 's/# \(en_US\.UTF-8 .*\)/\1/' /etc/locale.gen \\
326   && locale-gen en_US.UTF-8 \\
327   && dpkg-reconfigure --frontend=noninteractive locales \\
328   && update-locale LANG=en_US.UTF-8 \\
329   && TZ=Etc/UTC && ln -snf /usr/share/zoneinfo/\$TZ /etc/localtime && echo \$TZ > /etc/timezone \\
330   && rm -r /var/lib/apt/lists/*
331 ENV LANG=en_US.UTF-8 LANGUAGE=en_US LC_ALL=en_US.UTF-8
332
333 COPY files/wrapdocker /usr/local/bin/wrapdocker
334 RUN chmod +x /usr/local/bin/wrapdocker
335
336 # Install packages and Docker
337 RUN apt-get update -qq \\
338   && apt-get install -y  \\
339              bash \\
340              curl \\
341              iproute2 \\
342              locales \\
343              ssh \\
344              sudo \\
345              tzdata \\
346              uuid-runtime \\
347   && curl -fsSL https://get.docker.com | sh \\
348   && rm -rf /var/lib/apt/lists/*
349
350 RUN mkdir /var/run/sshd
351 RUN echo 'root:Csit1234' | chpasswd
352 RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
353
354 # SSH login fix. Otherwise user is kicked off after login
355 RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
356
357 # Need volume for sidecar docker launches
358 VOLUME /var/lib/docker
359
360 # SSH to listen on port 6022 in shim
361 RUN echo 'Port 6022' >>/etc/ssh/sshd_config
362 RUN echo 'Port 6023' >>/etc/ssh/sshd_config
363
364 # TODO: Verify why badkeypub is required & figure out how to avoid it.
365 COPY files/badkeypub /root/.ssh/authorized_keys
366 COPY files/sshconfig /root/.ssh/config
367
368 # Clean up copy-in build tree
369 RUN rm -rf /tmp/* $DOCKER_BUILD_FILES_DIR
370
371 # Start sshd by default
372 EXPOSE 22
373 CMD ["/usr/sbin/sshd", "-D"]
374 EOF
375 }
376
377 generate_apt_dockerfile() {
378     local executor_class="$1"
379     local executor_os_name="$2"
380     local from_image="$3"
381     local executor_image="$4"
382
383     cat <<EOF  >"$DOCKERIGNOREFILE"
384 **/__pycache__
385 *.pyc
386 EOF
387     cat <<EOF  >"$DOCKERFILE"
388 FROM $from_image AS ${executor_class}-executor-image
389 LABEL Description="FD.io CI '$executor_class' executor docker image for $executor_os_name/$OS_ARCH"
390 LABEL Vendor="fd.io"
391 LABEL Version="$DOCKER_TAG"
392 EOF
393     ${executor_class}_generate_apt_dockerfile "$executor_class" \
394         "$executor_os_name" "$executor_image"
395 }