feat(shell): add prepare server script

begin with a new script for setup the basic structure of my servers, who tf needs ansible if we got sh
This commit is contained in:
nikurasu 2024-05-30 22:57:42 +02:00
parent 29c2dbcb86
commit e24d9fa729
Signed by: Nikurasu
GPG key ID: 9E7F14C03EF1F271

View file

@ -0,0 +1,108 @@
#! /usr/bin/env sh
while [ $# -gt 0 ]; do
case "$1" in
--dry-run)
DRY_RUN=1
shift
;;
--*)
echo "Illegal Operation"
;;
esac
done
is_dry_run() {
if [ -z "$DRY_RUN" ]; then
return 1
else
return 0
fi
}
command_exists() {
command -v "$@" > /dev/null 2>&1
}
check_distro() {
distro=""
if [ -r /etc/os-release ]; then
distro="$(. /etc/os-release && echo "$ID")"
fi
echo "$distro"
}
detect_debian_version() {
version=""
vers_num="$(sed 's/\/.*//' /etc/debian_version | sed 's/\..*//')"
case "$vers_num" in
12 | trixie)
version="bookworm"
;;
11)
version="bullseye"
;;
10)
version="buster"
;;
9)
version="stretch"
;;
8)
version="jessie"
;;
esac
echo "$version"
}
exec_command() {
uid="$(id -u 2>/dev/null || true)"
sh_c="sh -c"
if [ "$uid" != 0 ]; then
if command_exists sudo; then
sh_c="sudo -E sh -c"
elif command_exists su; then
sh_c="su -c"
else
cat >&2 <<-'EOF'
Error: no method for command elevation found, but thats needed for this setup script
EOF
exit 1
fi
fi
if is_dry_run; then
sh_c="echo"
fi
echo $sh_c
}
install_apt() {
pre_reqs="apt-transport-https ca-certificates curl"
apt_repo="deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] $download_url/linux/$distro $version $channel"
(
if ! is_dry_run; then
set -x
fi
$sh_c 'apt-get update -qq >/dev/null'
)
}
install() {
sh_c="$(exec_command)"
distro="$( check_distro )"
version=""
channel="stable"
download_url="https://download.docker.com"
case "$distro" in
debian)
version="$( detect_debian_version )"
install_apt
;;
*)
echo "Sorry currently only debian is supported"
esac
}
install