Merge "Fix hicn-docs job to use correct label."
[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 ENV CCACHE_DIR="/scratch/ccache"
253 EOF
254     generate_apt_dockerfile_clean
255 }
256
257 # Generate 'csit_dut' class apt dockerfile
258 csit_dut_generate_apt_dockerfile() {
259     local executor_class="$1"
260     local executor_os_name="$2"
261     local executor_image="$3"
262
263     csit_dut_generate_docker_build_files
264     generate_apt_dockerfile_common "$executor_class" "$executor_image"
265     cat <<EOF >>"$DOCKERFILE"
266
267 # Install csit_dut specific packages
268 RUN apt-get update -qq \\
269   && apt-get install -y \\
270              net-tools \\
271              openssh-server \\
272              pciutils \\
273              rsyslog \\
274              supervisor \\
275   && rm -r /var/lib/apt/lists/*
276
277 # Fix permissions
278 RUN chown root:syslog /var/log \\
279   && chmod 755 /etc/default
280
281 # Create directory structure
282 RUN mkdir -p /var/run/sshd
283
284 # SSH settings
285 RUN echo 'root:Csit1234' | chpasswd \\
286   && sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config \\
287   && sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
288
289 EXPOSE 2222
290
291 COPY files/supervisord.conf /etc/supervisor/supervisord.conf
292
293 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"]
294 EOF
295     generate_apt_dockerfile_clean
296 }
297
298 # Generate 'csit_shim' class apt dockerfile
299 csit_shim_generate_apt_dockerfile() {
300     local executor_class="$1"
301     local executor_os_name="$2"
302     local executor_image="$3"
303
304     csit_shim_generate_docker_build_files
305     cat <<EOF >>"$DOCKERFILE"
306
307 # Copy-in temporary build tree containing
308 # ci-management, vpp, & csit git repos
309 WORKDIR $DOCKER_BUILD_DIR
310 COPY . .
311
312 # Build Environment Variables
313 ENV DEBIAN_FRONTEND="noninteractive"
314 ENV FDIOTOOLS_IMAGE="$executor_image"
315 ENV FDIOTOOLS_EXECUTOR_CLASS="$executor_class"
316 ENV CIMAN_ROOT="$DOCKER_CIMAN_ROOT"
317 ENV PATH="\$PATH:$DOCKER_CIMAN_ROOT/docker/scripts"
318
319 # Configure locales & timezone
320 RUN apt-get update -qq \\
321   && apt-get install -y \\
322              apt-utils \\
323              locales \\
324   && sed -i 's/# \(en_US\.UTF-8 .*\)/\1/' /etc/locale.gen \\
325   && locale-gen en_US.UTF-8 \\
326   && dpkg-reconfigure --frontend=noninteractive locales \\
327   && update-locale LANG=en_US.UTF-8 \\
328   && TZ=Etc/UTC && ln -snf /usr/share/zoneinfo/\$TZ /etc/localtime && echo \$TZ > /etc/timezone \\
329   && rm -r /var/lib/apt/lists/*
330 ENV LANG=en_US.UTF-8 LANGUAGE=en_US LC_ALL=en_US.UTF-8
331
332 COPY files/wrapdocker /usr/local/bin/wrapdocker
333 RUN chmod +x /usr/local/bin/wrapdocker
334
335 # Install packages and Docker
336 RUN apt-get update -qq \\
337   && apt-get install -y  \\
338              bash \\
339              curl \\
340              iproute2 \\
341              locales \\
342              ssh \\
343              sudo \\
344              tzdata \\
345              uuid-runtime \\
346   && curl -fsSL https://get.docker.com | sh \\
347   && rm -rf /var/lib/apt/lists/*
348
349 RUN mkdir /var/run/sshd
350 RUN echo 'root:Csit1234' | chpasswd
351 RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
352
353 # SSH login fix. Otherwise user is kicked off after login
354 RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
355
356 # Need volume for sidecar docker launches
357 VOLUME /var/lib/docker
358
359 # SSH to listen on port 6022 in shim
360 RUN echo 'Port 6022' >>/etc/ssh/sshd_config
361 RUN echo 'Port 6023' >>/etc/ssh/sshd_config
362
363 # TODO: Verify why badkeypub is required & figure out how to avoid it.
364 COPY files/badkeypub /root/.ssh/authorized_keys
365 COPY files/sshconfig /root/.ssh/config
366
367 # Clean up copy-in build tree
368 RUN rm -rf /tmp/* $DOCKER_BUILD_FILES_DIR
369
370 # Start sshd by default
371 EXPOSE 22
372 CMD ["/usr/sbin/sshd", "-D"]
373 EOF
374 }
375
376 generate_apt_dockerfile() {
377     local executor_class="$1"
378     local executor_os_name="$2"
379     local from_image="$3"
380     local executor_image="$4"
381
382     cat <<EOF  >"$DOCKERIGNOREFILE"
383 **/__pycache__
384 *.pyc
385 EOF
386     cat <<EOF  >"$DOCKERFILE"
387 FROM $from_image AS ${executor_class}-executor-image
388 LABEL Description="FD.io CI '$executor_class' executor docker image for $executor_os_name/$OS_ARCH"
389 LABEL Vendor="fd.io"
390 LABEL Version="$DOCKER_TAG"
391 EOF
392     ${executor_class}_generate_apt_dockerfile "$executor_class" \
393         "$executor_os_name" "$executor_image"
394 }