fix(compute-images): wait for apt/dpkg locks to be released
[arvados.git] / tools / compute-images / scripts / base.sh
1 #!/bin/bash -euxo pipefail
2
3 # Copyright (C) The Arvados Authors. All rights reserved.
4 #
5 # SPDX-License-Identifier: Apache-2.0
6
7 SUDO=sudo
8
9 wait_for_apt_locks() {
10   while $SUDO fuser /var/{lib/{dpkg,apt/lists},cache/apt/archives}/lock >/dev/null 2>&1; do
11     echo "APT: Waiting for apt/dpkg locks to be released..."
12     sleep 1
13   done
14 }
15
16 # Run apt-get update
17 $SUDO DEBIAN_FRONTEND=noninteractive apt-get --yes update
18
19 # Install gnupg and dirmgr or gpg key checks will fail
20 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
21   gnupg \
22   dirmngr \
23   lsb-release
24
25 # For good measure, apt-get upgrade
26 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes upgrade
27
28 # Make sure cloud-init is installed
29 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install cloud-init
30 if [[ ! -d /var/lib/cloud/scripts/per-boot ]]; then
31   mkdir -p /var/lib/cloud/scripts/per-boot
32 fi
33
34 TMP_LSB=`/usr/bin/lsb_release -c -s`
35 LSB_RELEASE_CODENAME=${TMP_LSB//[$'\t\r\n ']}
36
37 # Add the arvados apt repository
38 echo "# apt.arvados.org" |$SUDO tee --append /etc/apt/sources.list.d/apt.arvados.org.list
39 echo "deb http://apt.arvados.org/$LSB_RELEASE_CODENAME $LSB_RELEASE_CODENAME${REPOSUFFIX} main" |$SUDO tee --append /etc/apt/sources.list.d/apt.arvados.org.list
40
41 # Add the arvados signing key
42 cat /tmp/1078ECD7.asc | $SUDO apt-key add -
43 # Add the debian keys
44 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get install --yes debian-keyring debian-archive-keyring
45
46 # Fix locale
47 $SUDO /bin/sed -ri 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
48 $SUDO /usr/sbin/locale-gen
49
50 # Install some packages we always need
51 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get --yes update
52 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
53   openssh-server \
54   apt-utils \
55   git \
56   curl \
57   libcurl3-gnutls \
58   libcurl4-openssl-dev \
59   lvm2 \
60   cryptsetup \
61   xfsprogs
62
63 # See if python3-distutils is installable, and if so install it. This is a
64 # temporary workaround for an Arvados packaging bug and should be removed once
65 # Arvados 2.0.4 or 2.1.0 is released, whichever comes first.
66 # See https://dev.arvados.org/issues/16611 for more information
67 if apt-cache -qq show python3-distutils >/dev/null 2>&1; then
68   wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install python3-distutils
69 fi
70
71 # Install the Arvados packages we need
72 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
73   python-arvados-fuse \
74   crunch-run \
75   arvados-docker-cleaner \
76   docker.io
77
78 # Remove unattended-upgrades if it is installed
79 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes remove unattended-upgrades --purge
80
81 # Configure arvados-docker-cleaner
82 $SUDO mkdir -p /etc/arvados/docker-cleaner
83 $SUDO echo -e "{\n  \"Quota\": \"10G\",\n  \"RemoveStoppedContainers\": \"always\"\n}" > /etc/arvados/docker-cleaner/docker-cleaner.json
84
85 # Enable cgroup accounting
86 $SUDO sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"/g' /etc/default/grub
87 $SUDO update-grub
88
89 # Set a higher ulimit for docker
90 $SUDO sed -i "s/ExecStart=\(.*\)/ExecStart=\1 --default-ulimit nofile=10000:10000 --dns ${RESOLVER}/g" /lib/systemd/system/docker.service
91 $SUDO systemctl daemon-reload
92
93 # Make sure user_allow_other is set in fuse.conf
94 $SUDO sed -i 's/#user_allow_other/user_allow_other/g' /etc/fuse.conf
95
96 # Add crunch user with sudo powers
97 $SUDO adduser --disabled-password --gecos "Crunch user,,,," crunch
98 # Do not require a password to sudo
99 echo -e "# for the crunch user\ncrunch ALL=(ALL) NOPASSWD:ALL" | $SUDO tee /etc/sudoers.d/91-crunch
100
101 # Set up the ssh public key for the crunch user
102 $SUDO mkdir /home/crunch/.ssh
103 $SUDO mv /tmp/crunch-authorized_keys /home/crunch/.ssh/authorized_keys
104 $SUDO chown -R crunch:crunch /home/crunch/.ssh
105 $SUDO chmod 600 /home/crunch/.ssh/authorized_keys
106 $SUDO chmod 700 /home/crunch/.ssh/
107
108 # Make sure we resolve via the provided resolver IP. Prepending is good enough because
109 # unless 'rotate' is set, the nameservers are queried in order (cf. man resolv.conf)
110 $SUDO sed -i "s/#prepend domain-name-servers 127.0.0.1;/prepend domain-name-servers ${RESOLVER};/" /etc/dhcp/dhclient.conf
111
112 # Set up the cloud-init script that will ensure encrypted disks
113 $SUDO mv /tmp/usr-local-bin-ensure-encrypted-partitions.sh /usr/local/bin/ensure-encrypted-partitions.sh
114 $SUDO chmod 755 /usr/local/bin/ensure-encrypted-partitions.sh
115 $SUDO chown root:root /usr/local/bin/ensure-encrypted-partitions.sh
116 $SUDO mv /tmp/etc-cloud-cloud.cfg.d-07_compute_arvados_dispatch_cloud.cfg /etc/cloud/cloud.cfg.d/07_compute_arvados_dispatch_cloud.cfg
117 $SUDO chown root:root /etc/cloud/cloud.cfg.d/07_compute_arvados_dispatch_cloud.cfg