functions
This commit is contained in:
		
							parent
							
								
									a0cfa1751a
								
							
						
					
					
						commit
						1c7ed5c2b7
					
				
							
								
								
									
										119
									
								
								Init-Server.sh
									
									
									
									
									
								
							
							
						
						
									
										119
									
								
								Init-Server.sh
									
									
									
									
									
								
							@ -11,7 +11,23 @@ log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
 | 
				
			|||||||
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
 | 
					log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
 | 
				
			||||||
log_skip() { echo -e "${YELLOW}[SKIPPED]${NC} $1"; }
 | 
					log_skip() { echo -e "${YELLOW}[SKIPPED]${NC} $1"; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Überprüfen, ob der .ssh-Ordner existiert, wenn nicht, erstelle ihn
 | 
					# Function to check if a command exists
 | 
				
			||||||
 | 
					command_exists() {
 | 
				
			||||||
 | 
					    command -v "$1" &> /dev/null
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 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]$ ]]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Function to setup SSH directory and keys
 | 
				
			||||||
 | 
					setup_ssh() {
 | 
				
			||||||
 | 
					    # Create .ssh directory if it doesn't exist
 | 
				
			||||||
    if [ ! -d "$HOME/.ssh" ]; then
 | 
					    if [ ! -d "$HOME/.ssh" ]; then
 | 
				
			||||||
        mkdir -m 700 "$HOME/.ssh"
 | 
					        mkdir -m 700 "$HOME/.ssh"
 | 
				
			||||||
        log_success ".ssh-Verzeichnis wurde erstellt."
 | 
					        log_success ".ssh-Verzeichnis wurde erstellt."
 | 
				
			||||||
@ -19,73 +35,62 @@ else
 | 
				
			|||||||
        log_info ".ssh-Verzeichnis existiert bereits."
 | 
					        log_info ".ssh-Verzeichnis existiert bereits."
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Benutzer fragen, ob ein SSH-Key hinzugefügt werden soll
 | 
					    # Ask for SSH public key
 | 
				
			||||||
    log_info "SSH-Public-Key eingeben (Leerlassen zum Überspringen)"
 | 
					    log_info "SSH-Public-Key eingeben (Leerlassen zum Überspringen)"
 | 
				
			||||||
    read -p "> " user_input
 | 
					    read -p "> " user_input
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Nur fortfahren, wenn der Benutzer etwas eingegeben hat
 | 
					 | 
				
			||||||
    if [ ! -z "$user_input" ]; then
 | 
					    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
 | 
					        if grep -q "$user_input" "$HOME/.ssh/authorized_keys"; then
 | 
				
			||||||
            log_info "Der SSH-Public-Key ist bereits vorhanden."
 | 
					            log_info "Der SSH-Public-Key ist bereits vorhanden."
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
    # Den eingegebenen Public Key in die authorized_keys-Datei speichern
 | 
					 | 
				
			||||||
            echo "$user_input" >> "$HOME/.ssh/authorized_keys"
 | 
					            echo "$user_input" >> "$HOME/.ssh/authorized_keys"
 | 
				
			||||||
            log_success "SSH-Public-Key wurde hinzugefügt."
 | 
					            log_success "SSH-Public-Key wurde hinzugefügt."
 | 
				
			||||||
        fi
 | 
					        fi
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
        log_skip "SSH-Key Installation übersprungen."
 | 
					        log_skip "SSH-Key Installation übersprungen."
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Fragen, ob das System aktualisiert werden soll
 | 
					# Function to update system packages
 | 
				
			||||||
log_info "System aktualisieren? (Y/n)"
 | 
					update_system() {
 | 
				
			||||||
read -p "> " update_input
 | 
					    if get_user_confirmation "System aktualisieren?"; then
 | 
				
			||||||
 | 
					 | 
				
			||||||
# Ü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..."
 | 
					        log_info "System wird aktualisiert..."
 | 
				
			||||||
        apt update && apt upgrade -y
 | 
					        apt update && apt upgrade -y
 | 
				
			||||||
        log_success "System wurde erfolgreich aktualisiert."
 | 
					        log_success "System wurde erfolgreich aktualisiert."
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
        log_skip "System-Aktualisierung übersprungen."
 | 
					        log_skip "System-Aktualisierung übersprungen."
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Fragen, ob Docker installiert werden soll
 | 
					# Function to install Docker and Dockge
 | 
				
			||||||
log_info "Docker installieren? (Y/n)"
 | 
					install_docker() {
 | 
				
			||||||
read -p "> " docker_input
 | 
					    if get_user_confirmation "Docker installieren?"; then
 | 
				
			||||||
 | 
					        if command_exists docker; then
 | 
				
			||||||
# Ü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."
 | 
					            log_info "Docker ist bereits installiert."
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
            log_info "Docker wird installiert..."
 | 
					            log_info "Docker wird installiert..."
 | 
				
			||||||
        # Installiere benötigte Pakete
 | 
					            # Install required packages
 | 
				
			||||||
            apt install -y apt-transport-https ca-certificates curl software-properties-common
 | 
					            apt install -y apt-transport-https ca-certificates curl software-properties-common
 | 
				
			||||||
        # Füge Docker's offiziellen GPG-Schlüssel hinzu
 | 
					            
 | 
				
			||||||
 | 
					            # 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
 | 
					            curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
 | 
				
			||||||
        # Füge Docker Repository hinzu
 | 
					            
 | 
				
			||||||
 | 
					            # 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
 | 
					            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
 | 
					            
 | 
				
			||||||
 | 
					            # Update and install Docker
 | 
				
			||||||
            apt update
 | 
					            apt update
 | 
				
			||||||
            apt install -y docker-ce docker-ce-cli containerd.io
 | 
					            apt install -y docker-ce docker-ce-cli containerd.io
 | 
				
			||||||
        # Starte und aktiviere Docker
 | 
					            
 | 
				
			||||||
 | 
					            # Start and enable Docker
 | 
				
			||||||
            systemctl start docker
 | 
					            systemctl start docker
 | 
				
			||||||
            systemctl enable docker
 | 
					            systemctl enable docker
 | 
				
			||||||
            log_success "Docker wurde erfolgreich installiert."
 | 
					            log_success "Docker wurde erfolgreich installiert."
 | 
				
			||||||
        fi
 | 
					        fi
 | 
				
			||||||
else
 | 
					 | 
				
			||||||
    log_skip "Docker-Installation übersprungen."
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Fragen, ob Dockge installiert werden soll (nur wenn Docker installiert ist)
 | 
					        # Install Dockge if Docker is available
 | 
				
			||||||
if command -v docker &> /dev/null; then
 | 
					        if command_exists docker; then
 | 
				
			||||||
    log_info "Dockge (Docker Compose Manager) installieren? (Y/n)"
 | 
					            if get_user_confirmation "Dockge (Docker Compose Manager) installieren?"; then
 | 
				
			||||||
    read -p "> " dockge_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..."
 | 
					                log_info "Dockge wird installiert..."
 | 
				
			||||||
                docker run -d --name=dockge -p 5001:5001 -v /var/run/docker.sock:/var/run/docker.sock louislam/dockge
 | 
					                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."
 | 
					                log_success "Dockge wurde erfolgreich installiert und läuft auf Port 5001."
 | 
				
			||||||
@ -93,45 +98,49 @@ if command -v docker &> /dev/null; then
 | 
				
			|||||||
                log_skip "Dockge-Installation übersprungen."
 | 
					                log_skip "Dockge-Installation übersprungen."
 | 
				
			||||||
            fi
 | 
					            fi
 | 
				
			||||||
        fi
 | 
					        fi
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					        log_skip "Docker-Installation übersprungen."
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Fragen, ob Node.js installiert werden soll
 | 
					# Function to install Node.js
 | 
				
			||||||
log_info "Node.js installieren? (Y/n)"
 | 
					install_nodejs() {
 | 
				
			||||||
read -p "> " nodejs_input
 | 
					    if get_user_confirmation "Node.js installieren?"; then
 | 
				
			||||||
 | 
					        if command_exists node; then
 | 
				
			||||||
# Ü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."
 | 
					            log_info "Node.js ist bereits installiert."
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
            log_info "Node.js wird installiert..."
 | 
					            log_info "Node.js wird installiert..."
 | 
				
			||||||
        # Installiere curl falls noch nicht vorhanden
 | 
					 | 
				
			||||||
            apt install -y curl
 | 
					            apt install -y curl
 | 
				
			||||||
        # Füge NodeSource Repository hinzu
 | 
					 | 
				
			||||||
            curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
 | 
					            curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
 | 
				
			||||||
        # Installiere Node.js
 | 
					 | 
				
			||||||
            apt install -y nodejs
 | 
					            apt install -y nodejs
 | 
				
			||||||
            log_success "Node.js wurde erfolgreich installiert."
 | 
					            log_success "Node.js wurde erfolgreich installiert."
 | 
				
			||||||
        fi
 | 
					        fi
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
        log_skip "Node.js-Installation übersprungen."
 | 
					        log_skip "Node.js-Installation übersprungen."
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Installiere und konfiguriere UFW
 | 
					# Function to setup UFW firewall
 | 
				
			||||||
 | 
					setup_ufw() {
 | 
				
			||||||
    log_info "UFW (Uncomplicated Firewall) wird installiert und konfiguriert..."
 | 
					    log_info "UFW (Uncomplicated Firewall) wird installiert und konfiguriert..."
 | 
				
			||||||
 | 
					    if command_exists ufw; then
 | 
				
			||||||
# Überprüfen, ob UFW bereits installiert ist
 | 
					 | 
				
			||||||
if command -v ufw &> /dev/null; then
 | 
					 | 
				
			||||||
        log_info "UFW ist bereits installiert."
 | 
					        log_info "UFW ist bereits installiert."
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    # Installiere UFW
 | 
					 | 
				
			||||||
        apt install -y ufw
 | 
					        apt install -y ufw
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    # Erlaube SSH-Verbindungen
 | 
					 | 
				
			||||||
        ufw allow ssh
 | 
					        ufw allow ssh
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    # Aktiviere UFW
 | 
					 | 
				
			||||||
        echo "y" | ufw enable
 | 
					        echo "y" | ufw enable
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
        log_success "UFW wurde erfolgreich installiert und konfiguriert. SSH-Verbindungen sind erlaubt."
 | 
					        log_success "UFW wurde erfolgreich installiert und konfiguriert. SSH-Verbindungen sind erlaubt."
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Main execution
 | 
				
			||||||
 | 
					main() {
 | 
				
			||||||
 | 
					    setup_ssh
 | 
				
			||||||
 | 
					    update_system
 | 
				
			||||||
 | 
					    install_docker
 | 
				
			||||||
 | 
					    install_nodejs
 | 
				
			||||||
 | 
					    setup_ufw
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Run main function
 | 
				
			||||||
 | 
					main
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user