installs/docker.sh
2025-05-11 15:16:07 +02:00

139 lines
4.3 KiB
Bash

#!/usr/bin/env bash
# ─< Check if the given command exists silently >─────────────────────────────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}
# WHY:
# This import will give you the following variables:
# _sudo="sudo -E" <- only if non root user
# distro = <distro name, like 'arch', 'debian', 'fedora'..>
# arch = bool
# fedora = bool
# opensuse = bool....
# You can then use it for, `if $arch; then`
# Also this gives you the _install command, which installs a package pased on the packagemanager/distro used.
# CAUTION:
# This only wokrs for generic package names, like neovim, or vim, or tmux etc..
# not every package packagemanager has the same packagenames for their packages..
getImports() {
i="https://git.k4li.de/scripts/imports/raw/branch/main/distros.sh"
import="$(mktemp)"
echo "importing $i into $import"
if command_exists curl; then
curl -fsSL $i -o $import
elif command_exists wget; then
wget $i -o $import
else
echo "curl/wget is required, but missing.."
exit 1
fi
source "$import"
sleep 0.3
rm "$import" &&
echo_warning "cleaned $import"
}
init_docker() {
if command_exists docker; 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])
$_sudo usermod -aG docker "$(whoami)"
;;
esac
sleep 1
run $_sudo 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
}
_arch() {
echo_info "executing arch"
sleep 1.3
run _install docker docker-compose --noconfirm
}
_debian() {
if ! $trixie; then
echo_info "executing debian"
sleep 1.3
run $_sudo apt-get update &&
run $_sudo apt-get install -y ca-certificates curl &&
run $_sudo install -m 0755 -d /etc/apt/keyrings &&
run $_sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc &&
run $_sudo 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" | $_sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
echo_info "Addet repository. Updating and installing now.."
sleep 1
run $_sudo apt-get update
run checkAndInstall "docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin"
else
echo_info "executing trixie.."
run _install docker.io docker-compose
fi
}
_ubuntu() {
echo_info "executing ubuntu"
sleep 2
run $_sudo apt-get update &&
run $_sudo apt-get install -y ca-certificates curl &&
run $_sudo install -m 0755 -d /etc/apt/keyrings &&
run $_sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc &&
run $_sudo 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" | $_sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
echo_info "Addet repository. Updating and installing now.."
sleep 0.5
run $_sudo apt-get update
run checkAndInstall "docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin"
}
_fedora() {
echo_info "executing fedora"
sleep 2
run _install dnf-plugins-core
run $_sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
run checkAndInstall "docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin"
}
main() {
case "$distro" in
arch) _arch ;;
debian) _debian ;;
ubuntu) _ubuntu ;;
fedora) _fedora ;;
*) echo "$distro is not supported by this script" ;;
esac
}
if getImports; then
case "$1" in
--silent | -s)
silent=true
echo_warning "Running the script silently.."
;;
*)
silent=false
;;
esac
if main; then
init_docker
fi
fi