#!/usr/bin/env bash set -Eeuo pipefail if [[ "${EUID}" -eq 0 ]]; then sudo_command=() else sudo_command=(sudo) fi if [[ ! -r /etc/os-release ]]; then printf '/etc/os-release is unavailable.\n' >&2 exit 1 fi source /etc/os-release if [[ "${ID:-}" != "ubuntu" || "${VERSION_ID:-}" != "22.04" ]]; then printf 'This script requires Ubuntu 22.04; found %s %s.\n' "${ID:-unknown}" "${VERSION_ID:-unknown}" >&2 exit 1 fi timezone_name="${TIMEZONE_NAME:-Asia/Shanghai}" deploy_user="${DEPLOY_USER:-${SUDO_USER:-}}" docker_repository_url="${DOCKER_REPOSITORY_URL:-https://download.docker.com/linux/ubuntu}" docker_gpg_url="${DOCKER_GPG_URL:-${docker_repository_url}/gpg}" docker_gpg_fingerprint="9DC858229FC7DD38854AE2D88D81803C0EBFCD88" docker_force_ipv4="${DOCKER_FORCE_IPV4:-1}" docker_apt_force_ipv4="${DOCKER_APT_FORCE_IPV4:-0}" "${sudo_command[@]}" timedatectl set-timezone "$timezone_name" "${sudo_command[@]}" apt-get update "${sudo_command[@]}" apt-get install -y ca-certificates curl gnupg git jq rsync tmux htop sysstat iotop nvme-cli smartmontools unzip if [[ "${RUN_UPGRADE:-0}" == "1" ]]; then "${sudo_command[@]}" apt-get upgrade -y fi "${sudo_command[@]}" install -m 0755 -d /etc/apt/keyrings docker_curl_options=( --fail --silent --show-error --location --retry 5 --retry-delay 2 --retry-all-errors --connect-timeout 10 --max-time 90 ) docker_apt_options=() if [[ "$docker_force_ipv4" == "1" ]]; then docker_curl_options+=(--ipv4) fi if [[ "$docker_apt_force_ipv4" == "1" ]]; then docker_apt_options+=(-o Acquire::ForceIPv4=true) fi docker_key_file="$(mktemp)" trap 'rm -f "$docker_key_file"' EXIT curl "${docker_curl_options[@]}" "$docker_gpg_url" --output "$docker_key_file" downloaded_fingerprint="$( gpg --batch --show-keys --with-colons "$docker_key_file" 2>/dev/null \ | awk -F: '$1 == "fpr" { print $10; exit }' )" if [[ "$downloaded_fingerprint" != "$docker_gpg_fingerprint" ]]; then printf 'Docker GPG fingerprint mismatch: expected %s, found %s.\n' \ "$docker_gpg_fingerprint" "${downloaded_fingerprint:-unavailable}" >&2 exit 1 fi "${sudo_command[@]}" gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg "$docker_key_file" "${sudo_command[@]}" chmod a+r /etc/apt/keyrings/docker.gpg architecture="$(dpkg --print-architecture)" codename="${VERSION_CODENAME}" printf 'deb [arch=%s signed-by=/etc/apt/keyrings/docker.gpg] %s %s stable\n' \ "$architecture" "$docker_repository_url" "$codename" \ | "${sudo_command[@]}" tee /etc/apt/sources.list.d/docker.list >/dev/null "${sudo_command[@]}" apt-get "${docker_apt_options[@]}" update "${sudo_command[@]}" apt-get "${docker_apt_options[@]}" install -y \ docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin "${sudo_command[@]}" systemctl enable --now docker if [[ -n "$deploy_user" && "$deploy_user" != "root" ]]; then "${sudo_command[@]}" usermod -aG docker "$deploy_user" printf 'Added %s to the docker group; start a new SSH session before using docker without sudo.\n' "$deploy_user" fi "${sudo_command[@]}" docker version "${sudo_command[@]}" docker compose version "${sudo_command[@]}" systemctl is-active docker if [[ -e /var/run/reboot-required ]]; then printf 'A reboot is required. Keep the current SSH session until key login and the reserved address are rechecked.\n' fi