Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate docker installation method #136

Merged
merged 27 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
998c994
migration script template
pablomendezroyo Jul 21, 2023
cb09dd9
remove legacy docker
pablomendezroyo Jul 26, 2023
a8b9bb8
use nsenter
pablomendezroyo Jul 26, 2023
b51592d
fix quoting
pablomendezroyo Jul 26, 2023
29fe908
remove comments
pablomendezroyo Jul 27, 2023
94c9a65
add initial checker
pablomendezroyo Jul 27, 2023
b6acdb5
fix typo log
pablomendezroyo Jul 27, 2023
e6c1c76
bump upgrade comment
pablomendezroyo Jul 31, 2023
ab92152
add removing log
pablomendezroyo Sep 15, 2023
87f4f60
add redirection
pablomendezroyo Sep 15, 2023
207ab6c
add unattended upgrades
pablomendezroyo Sep 15, 2023
fd95c71
add more logs
pablomendezroyo Sep 15, 2023
a57b40b
add yes to command gpg
pablomendezroyo Sep 15, 2023
73cb04e
add checker for unattended upgrades
pablomendezroyo Sep 15, 2023
1259d7e
add docker to unattended upgrades
pablomendezroyo Sep 15, 2023
26aadd0
use distro_codename
pablomendezroyo Sep 15, 2023
f1e06d0
add exit 0 to script
pablomendezroyo Sep 15, 2023
ac48318
fix initial check
pablomendezroyo Sep 15, 2023
33de243
Merge branch 'master' into pablo/docker-apt
pablomendezroyo Sep 18, 2023
abd4b17
Renamed migration from 14 to 15
dsimog01 Sep 18, 2023
600fa4d
minor fixes
pablomendezroyo Oct 9, 2023
1c7dead
add add_docker_to_unattended_upgrades function
pablomendezroyo Oct 9, 2023
00c8f88
add default yes
pablomendezroyo Oct 9, 2023
4286ec5
remove unnecesary log
pablomendezroyo Oct 9, 2023
e807b58
Merge branch 'master' into pablo/docker-apt
pablomendezroyo Oct 23, 2023
05c8fd1
Merge branch 'master' into pablo/docker-apt
pablomendezroyo Oct 23, 2023
93731a1
add finished log
pablomendezroyo Oct 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions src/scripts/docker_migration_apt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#!/bin/bash

# Set environment variables to avoid interactive prompts
export DEBIAN_FRONTEND=noninteractive
DOCKER_DOWNLOAD_ORIGINS="Docker:\${distro_codename}"
UNATTENDED_UPGRADES_FILE="/etc/apt/apt.conf.d/50unattended-upgrades"
LOG_FILE="/usr/src/dappnode/logs/upgrade_014.log"

log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') [upgrade_014]: $1" | tee -a ${LOG_FILE}
}

add_docker_to_unattended_upgrades() {
# Check that the UNATTENDED_upgrades_file exists if so, check that the file does not already contain the DOCKER_DOWNLOAD_ORIGINS, if not then modify it to include in the section Unattended-Upgrade::Allowed-Origins the docker download origins
if ! grep -q "${DOCKER_DOWNLOAD_ORIGINS}" "${UNATTENDED_UPGRADES_FILE}"; then
log "Add docker download origins to unattended-upgrades"
sed -i "/Unattended-Upgrade::Allowed-Origins {/a \"${DOCKER_DOWNLOAD_ORIGINS}\";" "${UNATTENDED_UPGRADES_FILE}" 2>&1 | tee -a ${LOG_FILE}
fi
}

log "Starting docker install migration from pkg to apt"

# Only update docker if unattended upgrades is enabled,
# otherwise the update might crash due to updating docker from docker.
if [ ! -f "${UNATTENDED_UPGRADES_FILE}" ]; then
log "WARNING: Unattended upgrades is not enabled, skipping..."
exit 0
fi

# Check if docker is installed via apt
# The docker.list file is created by the docker installation script
if [ -f /etc/apt/sources.list.d/docker.list ]; then
log "Docker is already installed via apt, skipping upgrade"
add_docker_to_unattended_upgrades
exit 0
pablomendezroyo marked this conversation as resolved.
Show resolved Hide resolved
fi

# Get docker version
DOCKER_VERSION=$(docker version --format '{{.Server.Version}}')

# Check the OS
if [ -f /etc/os-release ]; then
source /etc/os-release
OS=$NAME
VERSION=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
OS=$(lsb_release -si)
VERSION=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
source /etc/lsb-release
OS=$DISTRIB_ID
VERSION=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
OS=Debian
VERSION=$(cat /etc/debian_version)
else
OS=$(uname -s)
VERSION=$(uname -r)
fi

pablomendezroyo marked this conversation as resolved.
Show resolved Hide resolved
if echo "$OS" | grep -Ei "(Debian)" >/dev/null 2>&1; then
DOWNLOAD_GPG_URL="https://download.docker.com/linux/debian/gpg"
DOWNLOAD_REPO_URL="https://download.docker.com/linux/debian"
elif echo "$OS" | grep -Ei "(Ubuntu)" >/dev/null 2>&1; then
DOWNLOAD_GPG_URL="https://download.docker.com/linux/ubuntu/gpg"
DOWNLOAD_REPO_URL="https://download.docker.com/linux/ubuntu"
elif echo "$OS" | grep -Ei "(Raspbian)" >/dev/null 2>&1; then
DOWNLOAD_GPG_URL="https://download.docker.com/linux/raspbian/gpg"
DOWNLOAD_REPO_URL="https://download.docker.com/linux/raspbian"
else
log "OS $OS is not supported, skipping upgrade"
exit 0
fi

# Print OS and version for the migration
log "OS: $OS ; Version: $VERSION ; Docker version: $DOCKER_VERSION"

# Remove legacy docker packages
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do
log "Removing $pkg"
apt-get remove -y $pkg 2>&1 | tee -a ${LOG_FILE}
done

# Set up the repository
log "Set up the repository"
# 1. Update the apt package index and install packages to allow apt to use a repository over HTTPS
log "Update the apt packages"
apt-get update 2>&1 | tee -a ${LOG_FILE}
if [ $? -ne 0 ]; then
log "Failed to update"
exit 1
fi
log "Install ca-certificates curl and gnupg"
apt-get install -y ca-certificates curl gnupg 2>&1 | tee -a ${LOG_FILE}
if [ $? -ne 0 ]; then
log "Failed to install ca-certofocates curl and gnupg."
exit 1
fi
# 2. Add Docker's official GPG key
log "Add Docker's official GPG key"
install -m 0755 -d /etc/apt/keyrings 2>&1 | tee -a ${LOG_FILE}
pablomendezroyo marked this conversation as resolved.
Show resolved Hide resolved
if [ $? -ne 0 ]; then
log "Failed to create /etc/apt/keyrings directory."
exit 1
fi
log "Download and install docker gpg key"
curl -fL "${DOWNLOAD_GPG_URL}" |
gpg --yes --dearmor -o /etc/apt/keyrings/docker.gpg 2>&1 | tee -a ${LOG_FILE}
if [ $? -ne 0 ]; then
log "Failed to download and install docker gpg key."
exit 1
fi
log "Change permissions for docker gpg key"
chmod a+r /etc/apt/keyrings/docker.gpg 2>&1 | tee -a ${LOG_FILE}
if [ $? -ne 0 ]; then
log "Failed to change permissions for docker gpg key."
exit 1
fi
# 3. Use the following command to set up the repository
log "Add docker repository"
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] ${DOWNLOAD_REPO_URL} \
$(source /etc/os-release && echo "$VERSION_CODENAME") stable" 2>&1 |
tee /etc/apt/sources.list.d/docker.list >/dev/null
if [ $? -ne 0 ]; then
log "Failed to add docker repository."
exit 1
fi

# 1. Update the apt package index:
log "Update the apt packages again"
apt-get update 2>&1 | tee -a ${LOG_FILE}
if [ $? -ne 0 ]; then
log "Failed to update"
exit 1
fi
# IMPORTANT: This step MUST be skipped so unattended-upgrades will upgrade docker later on
pablomendezroyo marked this conversation as resolved.
Show resolved Hide resolved
# 2. Install Docker Engine, containerd, and Docker Compose.
#log "Install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin"
#apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y 2>&1 | tee -a ${LOG_FILE}
#if [ $? -ne 0 ]; then
# log "Failed to install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin."
# exit 1
#fi
# 3. Verify that the Docker Engine installation is successful by running the hello-world image.
#docker run --rm hello-world && docker rmi hello-world

# Add docker to unattended-upgrades
add_docker_to_unattended_upgrades

# Add docker-compose alias of docker compose to the dappnode profile if is not already there and if docker compose is installed
if [ -x "$(command -v docker-compose)" ]; then
if ! grep -q "alias docker-compose='docker compose'" /usr/src/dappnode/DNCORE/.dappnode_profile; then
log "Adding docker-compose alias to the dappnode profile"
echo "alias docker-compose='docker compose'" >>/usr/src/dappnode/DNCORE/.dappnode_profile
source /usr/src/dappnode/DNCORE/.dappnode_profile | tee -a ${LOG_FILE}
if [ $? -ne 0 ]; then
log "Failed to source dappnode profile."
exit 1
fi
# TODO: consider removing old docker-compose binary
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right here. Do we know which of them will be used if both exist ( docker-compose alias and docker-compose old binary)?

fi
fi

# Remove legacy docker download path /usr/src/dappnode/bin/docker/ if exists
if [ -d /usr/src/dappnode/bin/docker/ ]; then
log "Removing legacy docker download path /usr/src/dappnode/bin/docker/"
rm -rf /usr/src/dappnode/bin/docker/
fi

log "Docker upgrade finished"

exit 0
8 changes: 8 additions & 0 deletions src/upgrades/upgrade_014.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

# Upgrade from 0.2.76 to 0.2.77

# Switch docker installtion method to use apt official repository

# Run script on the host with the nsenter tool
docker run --rm --privileged --pid=host -t alpine:3.8 nsenter -t 1 -m -u -n -i /usr/src/dappnode/DNCORE/scripts/upgrade/docker_migration_apt.sh
Loading