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