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