Debian Codegraph @

test/lib/ansible_test/_util/target/setup/bootstrap.sh

raw
1 # shellcheck shell=sh
2
3 set -eu
4
5 retry_init()
6 {
7 attempt=0
8 }
9
10 retry_or_fail()
11 {
12 attempt=$((attempt + 1))
13
14 if [ $attempt -gt 5 ]; then
15 echo "Failed to install packages. Giving up."
16 exit 1
17 fi
18
19 echo "Failed to install packages. Sleeping before trying again..."
20 sleep 10
21 }
22
23 remove_externally_managed_marker()
24 {
25 "${python_interpreter}" -c '
26 import pathlib
27 import sysconfig
28 path = pathlib.Path(sysconfig.get_path("stdlib")) / "EXTERNALLY-MANAGED"
29 path.unlink(missing_ok=True)
30 '
31 }
32
33 install_ssh_keys()
34 {
35 if [ ! -f "${ssh_private_key_path}" ]; then
36 # write public/private ssh key pair
37 public_key_path="${ssh_private_key_path}.pub"
38
39 # shellcheck disable=SC2174
40 mkdir -m 0700 -p "${ssh_path}"
41 touch "${public_key_path}" "${ssh_private_key_path}"
42 chmod 0600 "${public_key_path}" "${ssh_private_key_path}"
43 echo "${ssh_public_key}" > "${public_key_path}"
44 echo "${ssh_private_key}" > "${ssh_private_key_path}"
45
46 # add public key to authorized_keys
47 authorized_keys_path="${HOME}/.ssh/authorized_keys"
48
49 # the existing file is overwritten to avoid conflicts (ex: RHEL on EC2 blocks root login)
50 cat "${public_key_path}" > "${authorized_keys_path}"
51 chmod 0600 "${authorized_keys_path}"
52
53 # add localhost server keys to known_hosts
54 known_hosts_path="${HOME}/.ssh/known_hosts"
55
56 for key in /etc/ssh/ssh_host_*_key.pub; do
57 echo "localhost $(cat "${key}")" >> "${known_hosts_path}"
58 done
59 fi
60 }
61
62 customize_bashrc()
63 {
64 true > ~/.bashrc
65
66 # Show color `ls` results when available.
67 if ls --color > /dev/null 2>&1; then
68 echo "alias ls='ls --color'" >> ~/.bashrc
69 elif ls -G > /dev/null 2>&1; then
70 echo "alias ls='ls -G'" >> ~/.bashrc
71 fi
72
73 # Improve shell prompts for interactive use.
74 echo "export PS1='"'\[\e]0;\u@\h: \w\a\]\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '"'" >> ~/.bashrc
75 }
76
77 install_pip() {
78 if ! "${python_interpreter}" -m pip.__main__ --version --disable-pip-version-check 2>/dev/null; then
79 case "${python_version}" in
80 *)
81 pip_bootstrap_url="https://ci-files.testing.ansible.com/ansible-test/get-pip-24.0.py"
82 ;;
83 esac
84
85 retry_init
86 while true; do
87 curl --silent --show-error "${pip_bootstrap_url}" -o /tmp/get-pip.py && \
88 "${python_interpreter}" /tmp/get-pip.py --disable-pip-version-check --quiet && \
89 rm /tmp/get-pip.py \
90 && break
91 retry_or_fail
92 done
93 fi
94 }
95
96 install_powershell() {
97 if [ ! "${powershell_versions}" ]; then
98 return
99 fi
100
101 for powershell_version in ${powershell_versions}; do
102 install_powershell_version
103 done
104 }
105
106 install_powershell_version() {
107 version="${powershell_version}"
108 major_version="$(echo "${version}" | cut -f 1 -d .)"
109 minor_version="$(echo "${version}" | cut -f 2 -d .)"
110 major_minor_version="${major_version}.${minor_version}"
111 install_dir="/opt/microsoft/powershell/${major_minor_version}"
112 pwsh_installed_bin="${install_dir}/pwsh"
113 pwsh_versioned_bin="/usr/local/bin/pwsh${major_minor_version}"
114
115 if [ -x "${pwsh_versioned_bin}" ]; then
116 echo "Detected PowerShell ${major_minor_version} at: ${pwsh_versioned_bin}"
117 return
118 fi
119
120 echo "Bootstrapping PowerShell ${major_minor_version} at: ${pwsh_versioned_bin}"
121
122 system="$("${python_interpreter}" -c 'import platform; print(platform.system());')"
123
124 case "${system}" in
125 Linux)
126 if ! "${python_interpreter}" -c "import os; os.confstr('CS_GNU_LIBC_VERSION');" 2>/dev/null; then
127 echo "Only glibc-based Linux distributions are supported."
128 exit 1
129 fi
130
131 system="linux"
132 ;;
133 Darwin)
134 system="osx"
135 ;;
136 *)
137 echo "Unsupported system: ${system}"
138 exit 1
139 ;;
140 esac
141
142 arch="$("${python_interpreter}" -c 'import platform; print(platform.machine());')"
143
144 case "${arch}" in
145 x86_64)
146 arch="x64"
147 ;;
148 aarch64)
149 arch="arm64"
150 ;;
151 arm64)
152 arch="arm64"
153 ;;
154 *)
155 echo "Unsupported architecture: ${arch}"
156 exit 1
157 ;;
158 esac
159
160 url="https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-${system}-${arch}.tar.gz"
161 tmp_file="/tmp/powershell-${major_minor_version}.tgz"
162
163 retry_init
164 while true; do
165 curl --silent --show-error --location "${url}" -o "${tmp_file}" \
166 && break
167 retry_or_fail
168 done
169
170 mkdir -p "${install_dir}"
171 tar zxf "${tmp_file}" --no-same-owner --no-same-permissions -C "${install_dir}"
172 rm "${tmp_file}"
173 find "${install_dir}" -type f -exec chmod -x "{}" ";"
174 chmod +x "${pwsh_installed_bin}"
175 ln -s "${pwsh_installed_bin}" "${pwsh_versioned_bin}"
176 }
177
178 optimize_dnf()
179 {
180 if ! grep ansible-test /etc/dnf/dnf.conf > /dev/null; then
181 cat >> /etc/dnf/dnf.conf <<EOF
182 # ansible-test
183 minrate=1M
184 timeout=15
185 retries=5
186 max_parallel_downloads=10
187 zchunk=0
188 deltarpm=0
189 EOF
190 fi
191 }
192
193 bootstrap_remote_alpine()
194 {
195 py_pkg_prefix="py3"
196
197 packages="
198 acl
199 bash
200 gcc
201 python3-dev
202 ${py_pkg_prefix}-pip
203 sudo
204 "
205
206 if [ "${controller}" ]; then
207 packages="
208 ${packages}
209 ${py_pkg_prefix}-cryptography
210 ${py_pkg_prefix}-packaging
211 ${py_pkg_prefix}-yaml
212 ${py_pkg_prefix}-jinja2
213 ${py_pkg_prefix}-resolvelib
214 "
215 fi
216
217 retry_init
218 while true; do
219 # shellcheck disable=SC2086
220 apk add -q ${packages} \
221 && break
222 retry_or_fail
223 done
224
225 # Upgrade the `libexpat` package to ensure that an upgraded Python (`pyexpat`) continues to work.
226 retry_init
227 while true; do
228 # shellcheck disable=SC2086
229 apk upgrade -q libexpat \
230 && break
231 retry_or_fail
232 done
233
234 # Allow root to use sudo to facilitate become testing as is done in other test environments.
235 echo "root ALL=(ALL) ALL" > /etc/sudoers.d/root
236 }
237
238 bootstrap_remote_fedora()
239 {
240 py_pkg_prefix="python3"
241
242 packages="
243 acl
244 gcc
245 ${py_pkg_prefix}-devel
246 which
247 "
248
249 if [ "${controller}" ]; then
250 packages="
251 ${packages}
252 ${py_pkg_prefix}-cryptography
253 ${py_pkg_prefix}-jinja2
254 ${py_pkg_prefix}-packaging
255 ${py_pkg_prefix}-pyyaml
256 ${py_pkg_prefix}-resolvelib
257 "
258 fi
259
260 retry_init
261 while true; do
262 # shellcheck disable=SC2086
263 dnf install -q -y ${packages} \
264 && break
265 retry_or_fail
266 done
267
268 packages="
269 expat
270 "
271
272 retry_init
273 while true; do
274 # shellcheck disable=SC2086
275 dnf update -q -y ${packages} \
276 && break
277 retry_or_fail
278 done
279 }
280
281 bootstrap_remote_freebsd()
282 {
283 packages="
284 python${python_package_version}
285 py${python_package_version}-sqlite3
286 py${python_package_version}-setuptools
287 bash
288 ca_root_nss
289 curl
290 gtar
291 sudo
292 "
293
294 if [ "${controller}" ]; then
295 jinja2_pkg="py${python_package_version}-jinja2"
296 cryptography_pkg="py${python_package_version}-cryptography"
297 pyyaml_pkg="py${python_package_version}-pyyaml"
298 packaging_pkg="py${python_package_version}-packaging"
299
300 # Declare platform/python version combinations which do not have supporting OS packages available.
301 # For these combinations ansible-test will use pip to install the requirements instead.
302 case "${platform_version}/${python_version}" in
303 "14.5/3.14")
304 jinja2_pkg="" # not available
305 cryptography_pkg="" # not available
306 pyyaml_pkg="" # not available
307 ;;
308 "15.1/3.14")
309 jinja2_pkg="" # not available
310 cryptography_pkg="" # not available
311 pyyaml_pkg="" # not available
312 ;;
313 *)
314 # just assume nothing is available
315 jinja2_pkg="" # not available
316 cryptography_pkg="" # not available
317 pyyaml_pkg="" # not available
318 packaging_pkg="" # not available
319 ;;
320 esac
321
322 packages="
323 ${packages}
324 ${packaging_pkg}
325 libyaml
326 ${pyyaml_pkg}
327 ${jinja2_pkg}
328 ${cryptography_pkg}
329 "
330 fi
331
332 retry_init
333 while true; do
334 # shellcheck disable=SC2086
335 env ASSUME_ALWAYS_YES=YES pkg bootstrap && \
336 pkg install -q -y ${packages} \
337 && break
338 retry_or_fail
339 done
340
341 install_pip
342
343 if ! grep '^PermitRootLogin yes$' /etc/ssh/sshd_config > /dev/null; then
344 sed -i '' 's/^# *PermitRootLogin.*$/PermitRootLogin yes/;' /etc/ssh/sshd_config
345 service sshd restart
346 fi
347
348 # make additional wheels available for packages which lack them for this platform
349 echo "# generated by ansible-test
350 [global]
351 extra-index-url = https://spare-tire.core.ansible.com/simple/
352 prefer-binary = yes
353 " > /etc/pip.conf
354
355 # enable ACL support on the root filesystem (required for become between unprivileged users)
356 fs_path="/"
357 fs_device="$(mount -v "${fs_path}" | cut -w -f 1)"
358 # shellcheck disable=SC2001
359 fs_device_escaped=$(echo "${fs_device}" | sed 's|/|\\/|g')
360
361 mount -o acls "${fs_device}" "${fs_path}"
362 awk 'BEGIN{FS=" "}; /'"${fs_device_escaped}"'/ {gsub(/^rw$/,"rw,acls", $4); print; next} // {print}' /etc/fstab > /etc/fstab.new
363 mv /etc/fstab.new /etc/fstab
364
365 # enable sudo without a password for the wheel group, allowing ansible to use the sudo become plugin
366 echo '%wheel ALL=(ALL:ALL) NOPASSWD: ALL' > /usr/local/etc/sudoers.d/ansible-test
367 chmod 440 /usr/local/etc/sudoers.d/ansible-test
368 }
369
370 bootstrap_remote_macos()
371 {
372 # Silence macOS deprecation warning for bash.
373 echo "export BASH_SILENCE_DEPRECATION_WARNING=1" >> ~/.bashrc
374
375 # Make sure ~/ansible/ is the starting directory for interactive shells on the control node.
376 # The root home directory is under a symlink. Without this the real path will be displayed instead.
377 if [ "${controller}" ]; then
378 echo "cd ~/ansible/" >> ~/.bashrc
379 fi
380
381 # Make sure commands like 'brew' can be found.
382 # This affects users with the 'zsh' shell, as well as 'root' accessed using 'sudo' from a user with 'zsh' for a shell.
383 # shellcheck disable=SC2016
384 echo 'PATH="/usr/local/bin:$PATH"' > /etc/zshenv
385 }
386
387 bootstrap_remote_rhel_9()
388 {
389 optimize_dnf
390
391 if [ "${python_version}" = "3.9" ]; then
392 py_pkg_prefix="python3"
393 else
394 py_pkg_prefix="python${python_version}"
395 fi
396
397 packages="
398 gcc
399 ${py_pkg_prefix}-devel
400 ${py_pkg_prefix}-pip
401 "
402
403 # Jinja2 is not installed with an OS package since the provided version is too old.
404 # Instead, ansible-test will install it using pip.
405 # packaging and resolvelib are missing for controller supported Python versions, so we just
406 # skip them and let ansible-test install them from PyPI.
407 #
408 # sqlite-libs needs to be specified currently to get sqlite3 imports working
409 # https://redhat.atlassian.net/browse/RHEL-178008
410 if [ "${controller}" ]; then
411 packages="
412 ${packages}
413 sqlite-libs
414 ${py_pkg_prefix}-cryptography
415 ${py_pkg_prefix}-pyyaml
416 "
417 fi
418
419 retry_init
420 while true; do
421 # shellcheck disable=SC2086
422 dnf install -q -y ${packages} \
423 && break
424 retry_or_fail
425 done
426 }
427
428 bootstrap_remote_rhel_10()
429 {
430 optimize_dnf
431
432 if [ "${python_version}" = "3.12" ]; then
433 py_pkg_prefix="python3"
434 else
435 py_pkg_prefix="python${python_version}"
436 fi
437
438 packages="
439 gcc
440 python3-packaging
441 ${py_pkg_prefix}-devel
442 ${py_pkg_prefix}-pip
443 "
444
445 # Jinja2, packaging and resolvelib are missing for controller supported Python versions, so we just
446 # skip them and let ansible-test install them from PyPI.
447 if [ "${controller}" ]; then
448 packages="
449 ${packages}
450 ${py_pkg_prefix}-cryptography
451 ${py_pkg_prefix}-pyyaml
452 "
453 fi
454
455 retry_init
456 while true; do
457 # shellcheck disable=SC2086
458 dnf install -q -y ${packages} \
459 && break
460 retry_or_fail
461 done
462 }
463
464 bootstrap_remote_rhel()
465 {
466 case "${platform_version}" in
467 8.*) bootstrap_remote_rhel_9 ;;
468 9.*) bootstrap_remote_rhel_9 ;;
469 10.*) bootstrap_remote_rhel_10 ;;
470 esac
471 }
472
473 bootstrap_remote_ubuntu()
474 {
475 py_pkg_prefix="python3"
476
477 packages="
478 acl
479 gcc
480 python${python_version}-dev
481 python3-pip
482 python${python_version}-venv
483 "
484
485 if [ "${controller}" ]; then
486 cryptography_pkg="${py_pkg_prefix}-cryptography"
487 jinja2_pkg="${py_pkg_prefix}-jinja2"
488 packaging_pkg="${py_pkg_prefix}-packaging"
489 pyyaml_pkg="${py_pkg_prefix}-yaml"
490 resolvelib_pkg="${py_pkg_prefix}-resolvelib"
491
492 # Declare platforms which do not have supporting OS packages available.
493 # For these ansible-test will use pip to install the requirements instead.
494 # Only the platform is checked since Ubuntu shares Python packages across Python versions.
495 case "${platform_version}" in
496 esac
497
498 packages="
499 ${packages}
500 ${cryptography_pkg}
501 ${jinja2_pkg}
502 ${packaging_pkg}
503 ${pyyaml_pkg}
504 ${resolvelib_pkg}
505 "
506 fi
507
508 retry_init
509 while true; do
510 # shellcheck disable=SC2086
511 apt-get update -qq -y && \
512 DEBIAN_FRONTEND=noninteractive apt-get install -qq -y --no-install-recommends ${packages} \
513 && break
514 retry_or_fail
515 done
516 }
517
518 bootstrap_docker()
519 {
520 # Required for newer mysql-server packages to install/upgrade on Ubuntu 16.04.
521 rm -f /usr/sbin/policy-rc.d
522
523 for key_value in ${python_interpreters}; do
524 IFS=':' read -r python_version python_interpreter << EOF
525 ${key_value}
526 EOF
527
528 echo "Bootstrapping Python ${python_version} at: ${python_interpreter}"
529
530 remove_externally_managed_marker
531 done
532 }
533
534 bootstrap_remote()
535 {
536 for key_value in ${python_interpreters}; do
537 IFS=':' read -r python_version python_interpreter << EOF
538 ${key_value}
539 EOF
540
541 echo "Bootstrapping Python ${python_version} at: ${python_interpreter}"
542
543 python_package_version="$(echo "${python_version}" | tr -d '.')"
544
545 case "${platform}" in
546 "alpine") bootstrap_remote_alpine ;;
547 "fedora") bootstrap_remote_fedora ;;
548 "freebsd") bootstrap_remote_freebsd ;;
549 "macos") bootstrap_remote_macos ;;
550 "rhel") bootstrap_remote_rhel ;;
551 "ubuntu") bootstrap_remote_ubuntu ;;
552 esac
553
554 remove_externally_managed_marker
555 done
556 }
557
558 bootstrap()
559 {
560 ssh_path="${HOME}/.ssh"
561 ssh_private_key_path="${ssh_path}/id_${ssh_key_type}"
562
563 install_ssh_keys
564 customize_bashrc
565
566 # allow tests to detect ansible-test bootstrapped instances, as well as the bootstrap type
567 echo "${bootstrap_type}" > /etc/ansible-test.bootstrap
568
569 case "${bootstrap_type}" in
570 "docker") bootstrap_docker ;;
571 "remote") bootstrap_remote ;;
572 esac
573
574 install_powershell
575 }
576
577 # These variables will be templated before sending the script to the host.
578 # They are at the end of the script to maintain line numbers for debugging purposes.
579 bootstrap_type=#{bootstrap_type}
580 controller=#{controller}
581 platform=#{platform}
582 platform_version=#{platform_version}
583 python_interpreters=#{python_interpreters}
584 ssh_key_type=#{ssh_key_type}
585 ssh_private_key=#{ssh_private_key}
586 ssh_public_key=#{ssh_public_key}
587 powershell_versions=#{powershell_versions}
588
589 bootstrap