diff --git a/Init-Server.sh b/Init-Server.sh index 6f9861b..14b0f85 100644 --- a/Init-Server.sh +++ b/Init-Server.sh @@ -11,127 +11,136 @@ log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; } log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_skip() { echo -e "${YELLOW}[SKIPPED]${NC} $1"; } -# Überprüfen, ob der .ssh-Ordner existiert, wenn nicht, erstelle ihn -if [ ! -d "$HOME/.ssh" ]; then - mkdir -m 700 "$HOME/.ssh" - log_success ".ssh-Verzeichnis wurde erstellt." -else - log_info ".ssh-Verzeichnis existiert bereits." -fi +# Function to check if a command exists +command_exists() { + command -v "$1" &> /dev/null +} -# Benutzer fragen, ob ein SSH-Key hinzugefügt werden soll -log_info "SSH-Public-Key eingeben (Leerlassen zum Überspringen)" -read -p "> " user_input +# Function to handle user input with Y/n prompt +get_user_confirmation() { + local prompt="$1" + local input + log_info "$prompt (Y/n)" + read -p "> " input + [[ -z "$input" || "$input" =~ ^[Yy]$ ]] +} -# Nur fortfahren, wenn der Benutzer etwas eingegeben hat -if [ ! -z "$user_input" ]; then - # Überprüfen, ob der Key bereits in authorized_keys vorhanden ist - if grep -q "$user_input" "$HOME/.ssh/authorized_keys"; then - log_info "Der SSH-Public-Key ist bereits vorhanden." - else - # Den eingegebenen Public Key in die authorized_keys-Datei speichern - echo "$user_input" >> "$HOME/.ssh/authorized_keys" - log_success "SSH-Public-Key wurde hinzugefügt." - fi -else - log_skip "SSH-Key Installation übersprungen." -fi - -# Fragen, ob das System aktualisiert werden soll -log_info "System aktualisieren? (Y/n)" -read -p "> " update_input - -# Überprüfen der Eingabe - Standardmäßig 'Y' bei Enter, sonst nur bei 'y' oder 'Y' -if [[ -z "$update_input" || "$update_input" =~ ^[Yy]$ ]]; then - log_info "System wird aktualisiert..." - apt update && apt upgrade -y - log_success "System wurde erfolgreich aktualisiert." -else - log_skip "System-Aktualisierung übersprungen." -fi - -# Fragen, ob Docker installiert werden soll -log_info "Docker installieren? (Y/n)" -read -p "> " docker_input - -# Überprüfen der Eingabe - Standardmäßig 'Y' bei Enter, sonst nur bei 'y' oder 'Y' -if [[ -z "$docker_input" || "$docker_input" =~ ^[Yy]$ ]]; then - # Überprüfen, ob Docker bereits installiert ist - if command -v docker &> /dev/null; then - log_info "Docker ist bereits installiert." +# Function to setup SSH directory and keys +setup_ssh() { + # Create .ssh directory if it doesn't exist + if [ ! -d "$HOME/.ssh" ]; then + mkdir -m 700 "$HOME/.ssh" + log_success ".ssh-Verzeichnis wurde erstellt." else - log_info "Docker wird installiert..." - # Installiere benötigte Pakete - apt install -y apt-transport-https ca-certificates curl software-properties-common - # Füge Docker's offiziellen GPG-Schlüssel hinzu - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg - # Füge Docker Repository hinzu - echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null - # Aktualisiere Paketliste und installiere Docker - apt update - apt install -y docker-ce docker-ce-cli containerd.io - # Starte und aktiviere Docker - systemctl start docker - systemctl enable docker - log_success "Docker wurde erfolgreich installiert." + log_info ".ssh-Verzeichnis existiert bereits." fi -else - log_skip "Docker-Installation übersprungen." -fi -# Fragen, ob Dockge installiert werden soll (nur wenn Docker installiert ist) -if command -v docker &> /dev/null; then - log_info "Dockge (Docker Compose Manager) installieren? (Y/n)" - read -p "> " dockge_input + # Ask for SSH public key + log_info "SSH-Public-Key eingeben (Leerlassen zum Überspringen)" + read -p "> " user_input - # Überprüfen der Eingabe - Standardmäßig 'Y' bei Enter, sonst nur bei 'y' oder 'Y' - if [[ -z "$dockge_input" || "$dockge_input" =~ ^[Yy]$ ]]; then - log_info "Dockge wird installiert..." - docker run -d --name=dockge -p 5001:5001 -v /var/run/docker.sock:/var/run/docker.sock louislam/dockge - log_success "Dockge wurde erfolgreich installiert und läuft auf Port 5001." + if [ ! -z "$user_input" ]; then + if grep -q "$user_input" "$HOME/.ssh/authorized_keys"; then + log_info "Der SSH-Public-Key ist bereits vorhanden." + else + echo "$user_input" >> "$HOME/.ssh/authorized_keys" + log_success "SSH-Public-Key wurde hinzugefügt." + fi else - log_skip "Dockge-Installation übersprungen." + log_skip "SSH-Key Installation übersprungen." fi -fi +} -# Fragen, ob Node.js installiert werden soll -log_info "Node.js installieren? (Y/n)" -read -p "> " nodejs_input - -# Überprüfen der Eingabe - Standardmäßig 'Y' bei Enter, sonst nur bei 'y' oder 'Y' -if [[ -z "$nodejs_input" || "$nodejs_input" =~ ^[Yy]$ ]]; then - # Überprüfen, ob Node.js bereits installiert ist - if command -v node &> /dev/null; then - log_info "Node.js ist bereits installiert." +# Function to update system packages +update_system() { + if get_user_confirmation "System aktualisieren?"; then + log_info "System wird aktualisiert..." + apt update && apt upgrade -y + log_success "System wurde erfolgreich aktualisiert." else - log_info "Node.js wird installiert..." - # Installiere curl falls noch nicht vorhanden - apt install -y curl - # Füge NodeSource Repository hinzu - curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - - # Installiere Node.js - apt install -y nodejs - log_success "Node.js wurde erfolgreich installiert." + log_skip "System-Aktualisierung übersprungen." fi -else - log_skip "Node.js-Installation übersprungen." -fi +} -# Installiere und konfiguriere UFW -log_info "UFW (Uncomplicated Firewall) wird installiert und konfiguriert..." +# Function to install Docker and Dockge +install_docker() { + if get_user_confirmation "Docker installieren?"; then + if command_exists docker; then + log_info "Docker ist bereits installiert." + else + log_info "Docker wird installiert..." + # Install required packages + apt install -y apt-transport-https ca-certificates curl software-properties-common + + # Add Docker's official GPG key + curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg + + # Add Docker repository + echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null + + # Update and install Docker + apt update + apt install -y docker-ce docker-ce-cli containerd.io + + # Start and enable Docker + systemctl start docker + systemctl enable docker + log_success "Docker wurde erfolgreich installiert." + fi -# Überprüfen, ob UFW bereits installiert ist -if command -v ufw &> /dev/null; then - log_info "UFW ist bereits installiert." -else - # Installiere UFW - apt install -y ufw - - # Erlaube SSH-Verbindungen - ufw allow ssh - - # Aktiviere UFW - echo "y" | ufw enable - - log_success "UFW wurde erfolgreich installiert und konfiguriert. SSH-Verbindungen sind erlaubt." -fi + # Install Dockge if Docker is available + if command_exists docker; then + if get_user_confirmation "Dockge (Docker Compose Manager) installieren?"; then + log_info "Dockge wird installiert..." + docker run -d --name=dockge -p 5001:5001 -v /var/run/docker.sock:/var/run/docker.sock louislam/dockge + log_success "Dockge wurde erfolgreich installiert und läuft auf Port 5001." + else + log_skip "Dockge-Installation übersprungen." + fi + fi + else + log_skip "Docker-Installation übersprungen." + fi +} + +# Function to install Node.js +install_nodejs() { + if get_user_confirmation "Node.js installieren?"; then + if command_exists node; then + log_info "Node.js ist bereits installiert." + else + log_info "Node.js wird installiert..." + apt install -y curl + curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - + apt install -y nodejs + log_success "Node.js wurde erfolgreich installiert." + fi + else + log_skip "Node.js-Installation übersprungen." + fi +} + +# Function to setup UFW firewall +setup_ufw() { + log_info "UFW (Uncomplicated Firewall) wird installiert und konfiguriert..." + if command_exists ufw; then + log_info "UFW ist bereits installiert." + else + apt install -y ufw + ufw allow ssh + echo "y" | ufw enable + log_success "UFW wurde erfolgreich installiert und konfiguriert. SSH-Verbindungen sind erlaubt." + fi +} + +# Main execution +main() { + setup_ssh + update_system + install_docker + install_nodejs + setup_ufw +} + +# Run main function +main