scripts/installs/docker.sh
2025-04-14 09:22:15 +02:00

180 lines
5.5 KiB
Bash
Executable file

#!/bin/sh -e
# ─< ANSI color codes >───────────────────────────────────────────────────────────────────
RED='\033[0;31m'
CYAN='\033[0;36m'
YELLOW='\033[0;33m'
LIGHT_GREEN='\033[0;92m'
BOLD='\033[1m'
NC='\033[0m' # No Color
echo_error() {
printf "${BOLD}${RED}ERROR: ${NC}${RED}%s${NC}\n" "$1" >&2
}
echo_info() {
printf "${BOLD}${CYAN}INFO: ${NC}${CYAN}%s${NC}\n" "$1"
}
echo_warning() {
printf "${BOLD}${YELLOW}WARNING: ${NC}${YELLOW}%s${NC}\n" "$1"
}
echo_note() {
printf "${BOLD}${LIGHT_GREEN}NOTE: ${NC}${LIGHT_GREEN}%s${NC}\n" "$1"
}
check_sudo() {
# check for $su
if [ "$USER" != "root" ]; then
if command -v sudo >/dev/null 2>&1; then
su="sudo"
else
echo "Are you sure you can handle this? You're not root and sudo cannot be found.. [y/n]"
read -r sud </dev/tty
case $sud in
[Yy]) echo "All right, if anything breaks, it's on you" ;;
[Nn])
echo "All right, you're good. Try to install 'sudo'"
exit 1
;;
*)
echo "Invalid choice. Exiting."
exit 1
;;
esac
su=""
fi
else
su=""
fi
echo "--- check_sudo done --- echo '$su' |"
}
# ─< Distribution detection and installation >────────────────────────────────────────
check_dist() {
if [ -e /etc/os-release ]; then
echo_info "Detecting distribution..."
. /etc/os-release
# ─< Convert $ID and $ID_LIKE to lowercase >──────────────────────────────────────────────
ID=$(printf "%s" "$ID" | tr '[:upper:]' '[:lower:]')
ID_LIKE=$(printf "%s" "$ID_LIKE" | tr '[:upper:]' '[:lower:]')
case "$ID" in
ubuntu | pop) _ubuntu ;;
debian) _debian ;;
fedora) _fedora ;;
# alpine) _alpine ;;
arch | manjaro | garuda | endeavour) _arch ;;
opensuse*) inst_opensuse ;;
*)
if [ "${ID_LIKE#*debian}" != "$ID_LIKE" ]; then
_debian
elif [ "${ID_LIKE#*ubuntu}" != "$ID_LIKE" ]; then
_ubuntu
elif [ "${ID_LIKE#*arch}" != "$ID_LIKE" ]; then
_arch
elif [ "${ID_LIKE#*fedora}" != "$ID_LIKE" ]; then
_fedora
# elif [ "${ID_LIKE#*suse}" != "$ID_LIKE" ]; then
# _opensuse
else
echo_error "Unsupported distribution: $ID"
exit 1
fi
;;
esac
else
echo_error "Unable to detect distribution. /etc/os-release not found."
exit 1
fi
}
_debian() {
clear
echo_info "executing debian"
sleep 2
$su apt-get update &&
$su apt-get install -y ca-certificates curl &&
$su install -m 0755 -d /etc/apt/keyrings &&
$su curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc &&
$su chmod a+r /etc/apt/keyrings/docker.asc &&
sleep 0.5
if [ "$VERSION_CODENAME" == "trixie" ]; then
VERSION_CODENAME="bookworm"
fi
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | $su tee /etc/apt/sources.list.d/docker.list >/dev/null
clear &&
echo_info "Addet repository. Updating and installing now.."
sleep 1
$su apt-get update
$su apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
}
_ubuntu() {
clear
echo_info "executing ubuntu"
sleep 2
$su apt-get update &&
$su apt-get install -y ca-certificates curl &&
$su install -m 0755 -d /etc/apt/keyrings &&
$su curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc &&
$su chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | $su tee /etc/apt/sources.list.d/docker.list >/dev/null
clear &&
echo_info "Addet repository. Updating and installing now.."
sleep 0.5
$su apt-get update
$su apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
}
_fedora() {
clear
echo_info "executing fedora"
sleep 2
$su dnf -y install dnf-plugins-core
$su dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
$su dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
}
_arch() {
clear
echo_info "executing arch"
sleep 2
$su pacman -S docker docker-compose --noconfirm
}
init_docker() {
if command -v docker >/dev/null 2>&1; then
echo_info "Docker was installed correctly. Do you want to add $(whoami) to the docker group? (y/n)"
read -r dgroup </dev/tty
case "$dgroup" in
[Yy])
$su usermod -aG docker "$(whoami)"
;;
esac
sleep 1
$su systemctl enable --now docker
echo_info "$(whoami) is now part of the docker group. Restart your session to enable the changes. Also docker was addet as a service. Should autostart from now on."
else
echo_error "Something went wrong!"
fi
}
main() {
if check_sudo; then
# if check_dist; then
# init_docker
# else
# echo_error "No distro found.. At least thats what the error says.."
# fi
check_dist && init_docker
else
echo_error "Sudo check was failing hard..!"
fi
}
main </dev/tty