Files
cloudron-box/scripts/installer.sh
T

208 lines
8.3 KiB
Bash
Raw Normal View History

2015-08-04 16:29:49 -07:00
#!/bin/bash
2018-10-26 10:13:27 -07:00
# This script is run before the box code is switched. This means that we can
# put network related/curl downloads here. If the script fails, the old code
# will continue to run
2015-08-04 16:29:49 -07:00
set -eu -o pipefail
2016-11-01 15:13:20 +01:00
if [[ ${EUID} -ne 0 ]]; then
echo "This script should be run as root." > /dev/stderr
exit 1
fi
2021-03-02 23:05:41 -08:00
function log() {
echo -e "$(date +'%Y-%m-%dT%H:%M:%S')" "==> installer: $1"
}
export DEBIAN_FRONTEND=noninteractive
apt_ready="no"
function prepare_apt_once() {
[[ "${apt_ready}" == "yes" ]] && return
log "Making sure apt is in a good state"
log "Waiting for all dpkg tasks to finish..."
while fuser /var/lib/dpkg/lock; do
sleep 1
done
# it's unclear what needs to be run first or whether both these command should be run. so keep trying both
for count in {1..3}; do
# alternative to apt-install -y --fix-missing ?
if ! dpkg --force-confold --configure -a; then
log "dpkg reconfigure failed (try $count)"
dpkg_configure="no"
else
dpkg_configure="yes"
fi
if ! apt update -y; then
log "apt update failed (try $count)"
apt_update="no"
else
apt_update="yes"
fi
[[ "${dpkg_configure}" == "yes" && "${apt_update}" == "yes" ]] && break
sleep 1
done
apt_ready="yes"
if [[ "${dpkg_configure}" == "yes" && "${apt_update}" == "yes" ]]; then
log "apt is ready"
else
log "apt is not ready but proceeding anyway"
fi
}
2020-05-17 21:34:39 -07:00
readonly user=yellowtent
readonly box_src_dir=/home/${user}/box
2015-08-04 16:29:49 -07:00
2017-04-18 11:17:27 -07:00
readonly curl="curl --fail --connect-timeout 20 --retry 10 --retry-delay 2 --max-time 2400"
2015-08-04 16:29:49 -07:00
readonly script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2025-06-15 17:38:39 +02:00
readonly box_src_next_dir="$(realpath ${script_dir}/..)"
2015-08-04 16:29:49 -07:00
2019-02-21 13:36:46 -08:00
readonly ubuntu_version=$(lsb_release -rs)
readonly ubuntu_codename=$(lsb_release -cs)
2021-03-04 23:14:00 -08:00
readonly is_update=$(systemctl is-active -q box && echo "yes" || echo "no")
2015-08-04 16:29:49 -07:00
2025-06-15 17:38:39 +02:00
log "Updating from $(cat $box_src_dir/VERSION 2>/dev/null) to $(cat $box_src_next_dir/VERSION 2>/dev/null)"
2020-05-17 21:34:39 -07:00
if [[ "${ubuntu_version}" == "18.04" ]]; then
log "This Cloudron version requires atleast Ubuntu 20.04. Please upgrade following https://docs.cloudron.io/guides/upgrade-ubuntu-20/"
exit 2
fi
# switch over to resolved and uninstall resolvconf
if dpkg -s resolvconf 2>/dev/null >/dev/null; then
2024-05-04 17:36:26 +02:00
vendor=$(cat /sys/devices/virtual/dmi/id/sys_vendor || true)
prepare_apt_once # do this first before DNS goes away intermittently
log "disabling unbound"
systemctl disable unbound || true
systemctl stop unbound || true
2024-05-04 17:36:26 +02:00
log "enabling systemd-resolved"
systemctl enable --now systemd-resolved
2024-05-04 17:36:26 +02:00
log "removing resolvconf"
2024-05-04 17:36:26 +02:00
[[ "${vendor}" == "netcup" && -f /etc/resolvconf/resolv.conf.d/original ]] && cp /etc/resolvconf/resolv.conf.d/original /tmp/resolv.conf.original # stash before purging
apt -y --purge remove resolvconf # purge required for dpkg -s to return error code
2024-05-04 17:36:26 +02:00
if [[ "${vendor}" == "netcup" && -f /tmp/resolv.conf.original ]]; then
log "Fix netcup DNS setup"
nameservers=$(sed -ne 's/nameserver \(.*\)/"\1"/p' /tmp/resolv.conf.original | paste -sd "," -) # json array
netplan set --origin-hint 50-cloud-init "ethernets.eth0.nameservers.addresses=[${nameservers}]"
netplan apply # generates /run/systemd/resolve/resolv.conf
systemctl restart systemd-resolved
fi
fi
2024-04-28 11:18:37 +02:00
# https://docs.docker.com/engine/installation/linux/ubuntulinux/
2024-11-23 20:30:12 +05:30
# https://download.docker.com/linux/ubuntu/dists/noble/pool/stable/amd64/
2025-09-11 09:18:01 +02:00
# this is the highest on Ubuntu 20.04 focal
readonly docker_version="28.1.1"
2025-06-25 11:16:27 +02:00
readonly containerd_version="1.7.27"
if ! which docker 2>/dev/null || [[ $(docker version --format {{.Client.Version}}) != "${docker_version}" ]]; then
log "installing/updating docker"
# create systemd drop-in file already to make sure images are with correct driver
mkdir -p /etc/systemd/system/docker.service.d
echo -e "[Service]\nExecStart=\nExecStart=/usr/bin/dockerd -H fd:// --log-driver=journald --exec-opt native.cgroupdriver=cgroupfs --storage-driver=overlay2 --experimental --ip6tables" > /etc/systemd/system/docker.service.d/cloudron.conf
2022-11-24 11:26:03 +01:00
# there are 3 packages for docker - containerd, CLI and the daemon (https://download.docker.com/linux/ubuntu/dists/jammy/pool/stable/amd64/)
2024-11-23 20:30:12 +05:30
$curl -sL "https://download.docker.com/linux/ubuntu/dists/${ubuntu_codename}/pool/stable/amd64/containerd.io_${containerd_version}-1_amd64.deb" -o /tmp/containerd.deb
2023-05-10 10:21:04 +02:00
$curl -sL "https://download.docker.com/linux/ubuntu/dists/${ubuntu_codename}/pool/stable/amd64/docker-ce-cli_${docker_version}-1~ubuntu.${ubuntu_version}~${ubuntu_codename}_amd64.deb" -o /tmp/docker-ce-cli.deb
$curl -sL "https://download.docker.com/linux/ubuntu/dists/${ubuntu_codename}/pool/stable/amd64/docker-ce_${docker_version}-1~ubuntu.${ubuntu_version}~${ubuntu_codename}_amd64.deb" -o /tmp/docker.deb
2017-11-10 18:23:22 -08:00
2022-01-13 11:04:43 -08:00
log "installing docker"
prepare_apt_once
2022-01-13 11:04:43 -08:00
apt install -y /tmp/containerd.deb /tmp/docker-ce-cli.deb /tmp/docker.deb
2019-02-21 13:36:46 -08:00
rm /tmp/containerd.deb /tmp/docker-ce-cli.deb /tmp/docker.deb
2017-11-10 18:23:22 -08:00
fi
2025-02-05 14:45:06 +01:00
readonly old_node_version=20.18.0
2025-10-02 08:31:19 +02:00
readonly node_version=22.20.0
if ! which node 2>/dev/null || [[ "$(node --version)" != "v${node_version}" ]]; then
log "installing/updating node ${node_version}"
2021-02-04 10:41:47 -08:00
mkdir -p /usr/local/node-${node_version}
2023-03-16 12:03:46 +01:00
$curl -sL https://nodejs.org/dist/v${node_version}/node-v${node_version}-linux-x64.tar.gz -o /tmp/node.tar.gz
2024-04-29 14:14:36 +02:00
tar zxf /tmp/node.tar.gz --strip-components=1 -C /usr/local/node-${node_version}
2023-03-16 12:03:46 +01:00
rm /tmp/node.tar.gz
2021-02-04 10:41:47 -08:00
ln -sf /usr/local/node-${node_version}/bin/node /usr/bin/node
ln -sf /usr/local/node-${node_version}/bin/npm /usr/bin/npm
2023-05-11 08:16:40 +02:00
rm -rf /usr/local/node-${old_node_version}
2017-07-13 08:51:34 -05:00
fi
# note that rebuild requires the above node
2016-11-01 17:01:16 +01:00
for try in `seq 1 10`; do
2016-01-14 10:48:44 -08:00
# for reasons unknown, the dtrace package will fail. but rebuilding second time will work
2016-11-01 16:57:43 +01:00
# We need --unsafe-perm as we run as root and the folder is owned by root,
# however by default npm drops privileges for npm rebuild
# https://docs.npmjs.com/misc/config#unsafe-perm
2025-06-15 17:38:39 +02:00
if cd "${box_src_next_dir}" && npm rebuild --unsafe-perm; then break; fi
2021-03-02 23:05:41 -08:00
log "Failed to rebuild, trying again"
2016-01-14 10:48:44 -08:00
sleep 5
done
2015-08-04 16:29:49 -07:00
2025-02-10 16:16:07 +01:00
# update TLDs rules.json (https://github.com/thom4parisot/tld.js?tab=readme-ov-file#updating-the-tlds-list)
2025-06-15 17:38:39 +02:00
if ! node "${box_src_next_dir}/node_modules/tldjs/bin/update.js"; then
2025-02-10 16:16:07 +01:00
log "Failed to update PSL, continue anyway"
fi
2016-11-01 17:01:16 +01:00
if [[ ${try} -eq 10 ]]; then
2021-03-02 23:05:41 -08:00
log "npm rebuild failed, giving up"
2016-11-01 17:01:16 +01:00
exit 4
fi
2021-03-02 23:05:41 -08:00
log "downloading new addon images"
2025-06-15 17:38:39 +02:00
images=$(node -e "const i = require('${box_src_next_dir}/src/infra_version.js'); console.log(Object.keys(i.images).map(x => i.images[x]).join(' '));")
2018-10-26 15:32:34 -07:00
2024-12-14 17:05:13 +01:00
# docker hub only uses first 64 bits for ipv6 addressing. this causes many ipv6 rate limit errors
# https://www.docker.com/blog/beta-ipv6-support-on-docker-hub-registry/
2021-03-02 23:05:41 -08:00
log "\tPulling docker images: ${images}"
2024-12-14 17:05:13 +01:00
for image_ref in ${images}; do
ipv4_image_ref="${image_ref/registry.docker.com/registry.ipv4.docker.com}"
ipv6_image_ref="${image_ref/registry.docker.com/registry.ipv6.docker.com}"
# on some machines, ipv6 pull just hangs
2024-12-14 17:05:13 +01:00
while true; do
if timeout --kill-after=10s 300s docker pull "${ipv4_image_ref}"; then # this pulls the image untagged using the sha256 but doesn't tag it!
2024-12-14 17:05:13 +01:00
docker tag "${ipv4_image_ref}" "${image_ref%@sha256:*}" # this will tag the image for readability
docker rmi "${ipv4_image_ref}"
break
fi
log "Could not pull ${ipv4_image_ref} , trying IPv6"
if timeout --kill-after=10s 300s docker pull "${ipv6_image_ref}"; then # this pulls the image untagged using the sha256 but doesn't tag it!
2024-12-14 17:05:13 +01:00
docker tag "${ipv6_image_ref}" "${image_ref%@sha256:*}" # this will tag the image for readability
docker rmi "${ipv6_image_ref}"
break
fi
log "Could not pull ${ipv6_image_ref} either, waiting for 10s"
sleep 10
2021-03-03 21:54:08 -08:00
done
2018-10-26 15:32:34 -07:00
done
if [[ "${is_update}" == "yes" ]]; then
2021-03-02 23:05:41 -08:00
log "stop box service for update"
2025-06-15 17:40:00 +02:00
systemctl stop box
2015-08-04 16:29:49 -07:00
fi
# ensure we are not inside the source directory, which we will remove now
cd /root
2021-03-02 23:05:41 -08:00
log "switching the box code"
2020-05-17 21:34:39 -07:00
rm -rf "${box_src_dir}"
2025-06-15 17:38:39 +02:00
mv "${box_src_next_dir}" "${box_src_dir}"
2020-05-17 21:34:39 -07:00
chown -R "${user}:${user}" "${box_src_dir}"
2015-08-04 16:29:49 -07:00
2021-03-02 23:05:41 -08:00
log "calling box setup script"
2020-05-17 21:34:39 -07:00
"${box_src_dir}/setup/start.sh"