From f814f5aa290000e7e7c42ea86bc962f836617668 Mon Sep 17 00:00:00 2001 From: Tomas Mirchev Date: Mon, 24 Feb 2025 15:57:20 +0100 Subject: [PATCH] fix scripts with sudo --- config.json | 6 +++--- scripts/linux-setup_docker.sh | 35 +++++++++++++++------------------- scripts/linux-setup_sudoers.sh | 0 scripts/linux-setup_zsh.sh | 11 +++-------- scripts/setup_ssh_keys.sh | 6 +++--- setups/linux-vm.sh | 12 ++++++------ setups/macos.sh | 0 7 files changed, 30 insertions(+), 40 deletions(-) mode change 100644 => 100755 scripts/linux-setup_sudoers.sh mode change 100644 => 100755 setups/linux-vm.sh mode change 100644 => 100755 setups/macos.sh diff --git a/config.json b/config.json index 266df95..c2bc0ec 100644 --- a/config.json +++ b/config.json @@ -53,16 +53,16 @@ "git", { "package": "zsh", - "install": "sudo apt install zsh", + "install": "sudo apt install -y zsh", "post-link": "./scripts/linux-setup_zsh.sh" }, { "package": "tmux", - "install": "sudo apt install tmux" + "install": "sudo apt install -y tmux" }, { "package": "htop", - "install": "sudo apt install htop" + "install": "sudo apt install -y htop" } ], "linux-dev": [ diff --git a/scripts/linux-setup_docker.sh b/scripts/linux-setup_docker.sh index 48b8fe9..f785163 100755 --- a/scripts/linux-setup_docker.sh +++ b/scripts/linux-setup_docker.sh @@ -2,14 +2,9 @@ set -e -if [ "$EUID" -ne 0 ]; then - echo "Please run this script as root: sudo $0" - exit 1 -fi - echo "Removing old Docker versions..." for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do - apt-get remove -y $pkg || true + sudo apt-get remove -y $pkg || true done # Detect OS @@ -23,24 +18,24 @@ else fi echo "Updating package list and installing dependencies..." -apt-get update -apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release +sudo apt-get update +sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release echo "Setting up Docker repository..." # Add Docker's official GPG key -install -m 0755 -d /etc/apt/keyrings -curl -fsSL https://download.docker.com/linux/${DOCKER_OS}/gpg -o /etc/apt/keyrings/docker.asc -chmod a+r /etc/apt/keyrings/docker.asc +sudo install -m 0755 -d /etc/apt/keyrings +sudo curl -fsSL https://download.docker.com/linux/${DOCKER_OS}/gpg -o /etc/apt/keyrings/docker.asc +sudo chmod a+r /etc/apt/keyrings/docker.asc # Add the repository to Apt sources echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/${DOCKER_OS} \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ - tee /etc/apt/sources.list.d/docker.list > /dev/null + sudo tee /etc/apt/sources.list.d/docker.list > /dev/null echo "Installing Docker..." -apt-get update -apt-get install -y \ +sudo apt-get update +sudo apt-get install -y \ docker-ce \ docker-ce-cli \ containerd.io \ @@ -49,15 +44,15 @@ apt-get install -y \ # Verify installation echo "Verifying Docker installation..." -docker --version -containerd --version +sudo docker --version +sudo containerd --version echo "Configuring Docker permissions..." -groupadd docker 2>/dev/null || true -usermod -aG docker ${SUDO_USER:-$USER} +sudo groupadd docker 2>/dev/null || true +sudo usermod -aG docker ${SUDO_USER:-$USER} echo "Enabling and starting Docker services..." -systemctl enable --now docker.service -systemctl enable --now containerd.service +sudo systemctl enable --now docker.service +sudo systemctl enable --now containerd.service echo "Docker setup completed. Please log out and log back in for group changes to take effect." diff --git a/scripts/linux-setup_sudoers.sh b/scripts/linux-setup_sudoers.sh old mode 100644 new mode 100755 diff --git a/scripts/linux-setup_zsh.sh b/scripts/linux-setup_zsh.sh index e20eefa..9dd3182 100755 --- a/scripts/linux-setup_zsh.sh +++ b/scripts/linux-setup_zsh.sh @@ -2,20 +2,15 @@ set -e -if [ "$(id -u)" -ne 0 ]; then - echo "Please run this script as root: sudo $0" - exit 1 -fi - # Check if Zsh is already installed if command -v zsh &> /dev/null; then echo "Zsh is already installed. Skipping installation." else echo "Updating package list..." - apt-get update + sudo apt-get update echo "Installing Zsh..." - apt-get install -y zsh + sudo apt-get install -y zsh if ! command -v zsh &> /dev/null; then echo "Error: Zsh installation failed." @@ -25,6 +20,6 @@ fi echo "Changing default shell to Zsh for user ${SUDO_USER:-$USER}..." zsh_path=$(command -v zsh) -chsh -s "$zsh_path" "${SUDO_USER:-$USER}" +sudo chsh -s "$zsh_path" "${SUDO_USER:-$USER}" echo "Zsh installation and setup complete. Please log out and log back in for changes to take effect." diff --git a/scripts/setup_ssh_keys.sh b/scripts/setup_ssh_keys.sh index 59622ae..ea03647 100755 --- a/scripts/setup_ssh_keys.sh +++ b/scripts/setup_ssh_keys.sh @@ -46,12 +46,12 @@ chmod 700 "$SSH_DIR" # Generate SSH key if [ -f "$KEY_PATH" ]; then - echo "Error: Key file $KEY_PATH already exists." - exit 1 + echo "Skipping: Key file $KEY_PATH already exists." + exit 0 fi ssh-keygen -t ed25519 -C "$COMMENT" -f "$KEY_PATH" -N "" -echo "SSH key created: $KEY_PATH" +echo "SSH key created at: $KEY_PATH" echo "Public key:" cat "$KEY_PATH.pub" diff --git a/setups/linux-vm.sh b/setups/linux-vm.sh old mode 100644 new mode 100755 index 4b546cd..03ae7e3 --- a/setups/linux-vm.sh +++ b/setups/linux-vm.sh @@ -19,25 +19,25 @@ if ! sudo -v; then exit 1 fi -# /bin/bash ../scripts/linux-setup_sudoers.sh +# /bin/bash ./scripts/linux-setup_sudoers.sh -/bin/bash ../scripts/linux-change_hostname.sh "$NEW_HOSTNAME" +/bin/bash ./scripts/linux-change_hostname.sh "$NEW_HOSTNAME" echo "" echo "Installing docker..." -/bin/bash ../scripts/linux-setup_docker.sh +/bin/bash ./scripts/linux-setup_docker.sh echo "" echo "Creating internal SSH keys..." -/bin/bash ../scripts/setup_ssh_keys.sh --comment "${USER}@${NEW_HOSTNAME}" +/bin/bash ./scripts/setup_ssh_keys.sh --comment "${USER}@${NEW_HOSTNAME}" echo "" echo "Installing packages..." -python3 ../manage.py install linux-vm +python3 ./manage.py install linux-vm echo "" echo "Linking config..." -python3 ../manage.py link linux-vm --force +python3 ./manage.py link linux-vm --force echo "" echo "Updating git remote origin url..." diff --git a/setups/macos.sh b/setups/macos.sh old mode 100644 new mode 100755