From e24d9fa729af621c5c226fa103710936f040013f Mon Sep 17 00:00:00 2001 From: nikurasu Date: Thu, 30 May 2024 22:57:42 +0200 Subject: [PATCH] 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 --- shell/scripts/prepareServer.sh | 108 +++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 shell/scripts/prepareServer.sh diff --git a/shell/scripts/prepareServer.sh b/shell/scripts/prepareServer.sh new file mode 100644 index 0000000..b3e925a --- /dev/null +++ b/shell/scripts/prepareServer.sh @@ -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 \ No newline at end of file