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