From 5d4478e4b1496eccb3951a1be37597d116804899 Mon Sep 17 00:00:00 2001 From: TrudeEH Date: Thu, 13 Mar 2025 16:10:03 +0000 Subject: [PATCH] Add logging for installation processes and enhance UFW setup in install script --- .gitignore | 2 + install.sh | 133 +++++++++++++++++++++++++++++++-------------------- scripts/p.sh | 76 ++++++++++++++++++----------- 3 files changed, 129 insertions(+), 82 deletions(-) diff --git a/.gitignore b/.gitignore index 2452b06f..e4eaa201 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ BrewFile.lock.json .vscode /nix-shells/macos-vm/ + +logs/ diff --git a/install.sh b/install.sh index c1636764..da9cbd1a 100755 --- a/install.sh +++ b/install.sh @@ -9,100 +9,127 @@ PURPLE='\033[0;35m' CYAN='\033[0;36m' NC='\033[0m' # No Color +mkdir -p "$HOME/dotfiles/logs" + # Clone Dotfiles if not already present -cd $HOME/dotfiles -if [ $(pwd) != "$HOME/dotfiles" ]; then - echo -e "${YELLOW}[+] Cloning dotfiles repository...${NC}" +cd "$HOME/dotfiles" || exit +if [ "$(pwd)" != "$HOME/dotfiles" ]; then + printf "${YELLOW}[+] Cloning dotfiles repository...${NC}\n" git clone https://github.com/TrudeEH/dotfiles --depth 1 if [ $? -ne 0 ]; then - echo -e "${RED}[E] Error cloning dotfiles repository. Exiting...${NC}" + printf "${RED}[E] Error cloning dotfiles repository. Exiting...${NC}\n" exit 2 fi - cd dotfiles - echo -e "${GREEN}[I] dotfiles repository cloned successfully.${NC}" + cd dotfiles || exit + printf "${GREEN}[I] dotfiles repository cloned successfully.${NC}\n" else - echo -e "${GREEN}[I] dotfiles repository already present.${NC}" + printf "${GREEN}[I] dotfiles repository already present.${NC}\n" fi source ./scripts/p.sh -packageManagers=($(pcheck)) -echo -e "${CYAN}" -echo "####################" -echo -n "#" -echo -e "${PURPLE} Trude's Dotfiles${CYAN} #" -echo "####################" -echo -e "${CYAN}Running on: ${PURPLE}$OSTYPE${NC}" -echo -e "${CYAN}Package managers: ${PURPLE}${packageManagers[@]}${NC}" -echo +printf "${CYAN}\n" +printf "####################\n" +printf "#" +printf "${PURPLE} Trude's Dotfiles${CYAN} #\n" +printf "####################\n" +printf "${CYAN}Running on: ${PURPLE}%s${NC}\n" "$OSTYPE" +printf "\n" # Install Programs -programs=(neovim curl git tmux htop fzf gcc make tldr s-tui pass ufw) -p i ${programs[@]} +programs=(neovim curl git tmux htop fzf gcc make tldr pass lynis) + +if [[ "$OSTYPE" != "darwin"* ]]; then + programs+=(ufw s-tui) +fi + +p i "${programs[@]}" # Copy files -echo -e "${YELLOW}[+] Installing Dotfiles...${NC}" -cp -r $HOME/dotfiles/home/. $HOME +printf "${YELLOW}[+] Installing Dotfiles...${NC}\n" +cp -r "$HOME/dotfiles/home/." "$HOME" if [ $? -ne 0 ]; then - echo -e "${RED}[E] Error copying Dotfiles.${NC}" + printf "${RED}[E] Error copying Dotfiles.${NC}\n" else - echo -e "${GREEN}[I] Dotfiles installed successfully.${NC}" + printf "${GREEN}[I] Dotfiles installed successfully.${NC}\n" fi # Copy scripts -echo -e "${YELLOW}[+] Installing Scripts...${NC}" -mkdir -p $HOME/.local/bin -cp -r $HOME/dotfiles/scripts/. $HOME/.local/bin/ +printf "${YELLOW}[+] Installing Scripts...${NC}\n" +mkdir -p "$HOME/.local/bin" +cp -r "$HOME/dotfiles/scripts/." "$HOME/.local/bin/" if [ $? -ne 0 ]; then - echo -e "${RED}[E] Error copying Scripts.${NC}" + printf "${RED}[E] Error copying Scripts.${NC}\n" else - echo -e "${GREEN}[I] Scripts installed successfully.${NC}" + printf "${GREEN}[I] Scripts installed successfully.${NC}\n" fi # Install fonts -echo -e "${YELLOW}[+] Installing fonts...${NC}" +printf "${YELLOW}[+] Installing fonts...${NC}\n" if [[ "$OSTYPE" == "darwin"* ]]; then - cp -rf $HOME/dotfiles/fonts/* $HOME/Library/Fonts/ + cp -rf "$HOME/dotfiles/fonts/"* "$HOME/Library/Fonts/" if [ $? -ne 0 ]; then - echo -e "${RED}[E] Error installing fonts.${NC}" + printf "${RED}[E] Error installing fonts.${NC}\n" else - echo -e "${GREEN}[I] Fonts installed successfully.${NC}" + printf "${GREEN}[I] Fonts installed successfully.${NC}\n" fi else - mkdir -p $HOME/.local/share/fonts - cp -rf $HOME/dotfiles/fonts/* $HOME/.local/share/fonts/ + mkdir -p "$HOME/.local/share/fonts" + cp -rf "$HOME/dotfiles/fonts/"* "$HOME/.local/share/fonts/" if [ $? -ne 0 ]; then - echo -e "${RED}[E] Error installing fonts.${NC}" + printf "${RED}[E] Error installing fonts.${NC}\n" else - fc-cache -fv $HOME/.local/share/fonts - echo -e "${GREEN}[I] Fonts installed successfully.${NC}" + fc-cache -fv "$HOME/.local/share/fonts" >"$HOME/dotfiles/logs/font_install.log" + printf "${GREEN}[I] Fonts installed successfully.${NC}\n" fi fi # Load Dconf (GNOME settings) if [[ "$OSTYPE" != "darwin"* ]]; then - echo -e "${YELLOW}[+] Loading Dconf settings...${NC}" - dconf load / <$HOME/dotfiles/dconf-settings.ini + printf "${YELLOW}[+] Loading Dconf settings...${NC}\n" + dconf load / <"$HOME/dotfiles/dconf-settings.ini" if [ $? -ne 0 ]; then - echo -e "${RED}[E] Error loading Dconf settings.${NC}" + printf "${RED}[E] Error loading Dconf settings.${NC}\n" else - echo -e "${GREEN}[I] Dconf settings loaded successfully.${NC}" + printf "${GREEN}[I] Dconf settings loaded successfully.${NC}\n" fi fi # UFW Firewall -echo -e "${YELLOW}[+] Setting up UFW...${NC}" -sudo ufw default deny incoming -sudo ufw default allow outgoing -if systemctl is-active --quiet sshd; then - echo -e "${YELLOW}[+] SSH Server detected; Enabling SSH rule...${NC}" - sudo ufw limit 22/tcp - sudo ufw limit ssh -fi -sudo ufw enable -sudo ufw status numbered -if [ $? -ne 0 ]; then - echo -e "${RED}[E] Error setting up UFW.${NC}" +if [[ "$OSTYPE" != "darwin"* ]]; then + printf "${YELLOW}[+] Setting up UFW...${NC}\n" + sudo ufw default deny incoming + sudo ufw default allow outgoing + if systemctl is-active --quiet sshd; then + printf "${YELLOW}[+] SSH Server detected; Enabling SSH rule...${NC}\n" + sudo ufw limit 22/tcp + fi + sudo ufw enable + sudo ufw status numbered | tee "$HOME/dotfiles/logs/ufw_status.log" + if [ $? -ne 0 ]; then + printf "${RED}[E] Error setting up UFW.${NC}\n" + else + printf "${GREEN}[I] UFW setup successfully.${NC}\n" + fi else - echo -e "${GREEN}[I] UFW setup successfully.${NC}" + printf "${YELLOW}[+] Enabling macOS Firewall...${NC}\n" + sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate on + if [ $? -ne 0 ]; then + printf "${RED}[E] Error enabling Firewall.${NC}\n" + else + printf "${GREEN}[I] Firewall enabled successfully.${NC}\n" + fi +fi + +# Security Scan +if [ ! -f "$HOME/dotfiles/logs/lynis_scan.log" ]; then + printf "${YELLOW}[+] Running Lynis Security Scan...${NC}\n" + sudo lynis audit system | tee "$HOME/dotfiles/logs/lynis_scan.log" + if [ $? -ne 0 ]; then + printf "${RED}[E] Error running Lynis.${NC}\n" + else + printf "${GREEN}[I] Lynis scan completed.${NC}\n" + fi +else + printf "${CYAN}[I] Previous Lynis scan detected, read the log @ $HOME/dotfiles/logs/lynis_scan.log.${NC}\n" fi diff --git a/scripts/p.sh b/scripts/p.sh index b133d947..7fab7dad 100755 --- a/scripts/p.sh +++ b/scripts/p.sh @@ -97,20 +97,38 @@ p() ( echo $flatpak_apps | grep -iq $app_name flatpak_success=$? if [[ $flatpak_success == 0 ]]; then - echo -e "${GREEN}${BOLD}Flatpak:${ENDCOLOR}${GREEN} $(echo $flatpak_apps | tr ' ' '\n' | grep -i $app_name)${ENDCOLOR}" + printf "%b\n" "${GREEN}${BOLD}Flatpak:${ENDCOLOR}${GREEN} $(echo "$flatpak_apps" | tr ' ' '\n' | grep -i "$app_name")${ENDCOLOR}" fi fi # Some package names are different from the command name - declare -A altNames=(["neovim"]="nvim" ["python"]="python3" ["nodejs"]="node" ["docker-compose"]="docker compose" ["pip"]="pip3") - commandName="${altNames[$app_name]:-$app_name}" + case "$app_name" in + neovim) + commandName="nvim" + ;; + python) + commandName="python3" + ;; + nodejs) + commandName="node" + ;; + docker-compose) + commandName="docker compose" + ;; + pip) + commandName="pip3" + ;; + *) + commandName="$app_name" + ;; + esac which "$commandName" &>/dev/null distro_success=$? if [[ $distro_success == 0 ]]; then - echo -e "${GREEN}${BOLD}Distro:${ENDCOLOR}${GREEN} $app_name is installed.${ENDCOLOR}" + printf "%b\n" "${GREEN}${BOLD}Distro:${ENDCOLOR}${GREEN} $app_name is installed.${ENDCOLOR}" fi if [[ $flatpak_success != 0 && $nix_success != 0 && $brew_success != 0 && $distro_success != 0 ]]; then - echo -e "${YELLOW}$app_name not installed.${ENDCOLOR}" + printf "%b\n" "${YELLOW}$app_name not installed.${ENDCOLOR}" return 1 fi } @@ -118,108 +136,108 @@ p() ( installP() { checkP $1 if [[ $? != 1 ]]; then - echo -e "${GREEN}$1 is already installed.${ENDCOLOR}" + printf "%b\n" "${GREEN}$1 is already installed.${ENDCOLOR}" return 0 fi if [[ ${packageManagers[@]} =~ "nix" ]]; then - echo -e "${YELLOW}Attempting nix install...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting nix install...${ENDCOLOR}" nix-env -iA nixpkgs.$1 if [[ $? == 0 ]]; then return 0 fi fi if [[ ${packageManagers[@]} =~ "brew" ]]; then - echo -e "${YELLOW}Attempting brew install...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting brew install...${ENDCOLOR}" brew install $1 if [[ $? == 0 ]]; then return 0 fi fi if [[ ${packageManagers[@]} =~ "apt" ]]; then - echo -e "${YELLOW}Attempting apt install...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting apt install...${ENDCOLOR}" sudo apt install $1 if [[ $? == 0 ]]; then return 0 fi elif [[ ${packageManagers[@]} =~ "paru" ]]; then - echo -e "${YELLOW}Attempting paru install...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting paru install...${ENDCOLOR}" paru -Sy $1 if [[ $? == 0 ]]; then return 0 fi elif [[ ${packageManagers[@]} =~ "pacman" ]]; then - echo -e "${YELLOW}Attempting pacman install...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting pacman install...${ENDCOLOR}" sudo pacman -Sy $1 if [[ $? == 0 ]]; then return 0 fi elif [[ ${packageManagers[@]} =~ "dnf" ]]; then - echo -e "${YELLOW}Attempting dnf install...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting dnf install...${ENDCOLOR}" sudo dnf install $1 if [[ $? == 0 ]]; then return 0 fi fi if [[ ${packageManagers[@]} =~ "flatpak" ]]; then - echo -e "${YELLOW}Attempting flatpak install...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting flatpak install...${ENDCOLOR}" flatpak install $1 if [[ $? == 0 ]]; then return 0 fi fi - echo -e "${RED}ERROR - $1 not found.${ENDCOLOR}" + printf "%b\n" "${RED}ERROR - $1 not found.${ENDCOLOR}" return 1 } removeP() { checkP $1 if [[ $? != 0 ]]; then - echo -e "${YELLOW}$1 is not installed.${ENDCOLOR}" + printf "%b\n" "${YELLOW}$1 is not installed.${ENDCOLOR}" return 0 fi if [[ ${packageManagers[@]} =~ "flatpak" ]]; then - echo -e "${YELLOW}Attempting flatpak uninstall...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting flatpak uninstall...${ENDCOLOR}" flatpak uninstall $1 if [[ $? == 0 ]]; then return 0 fi fi if [[ ${packageManagers[@]} =~ "nix" ]]; then - echo -e "${YELLOW}Attempting nix uninstall...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting nix uninstall...${ENDCOLOR}" nix-env --uninstall $1 fi if [[ ${packageManagers[@]} =~ "brew" ]]; then - echo -e "${YELLOW}Attempting brew uninstall...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting brew uninstall...${ENDCOLOR}" brew uninstall $1 if [[ $? == 0 ]]; then return 0 fi fi if [[ ${packageManagers[@]} =~ "apt" ]]; then - echo -e "${YELLOW}Attempting apt uninstall...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting apt uninstall...${ENDCOLOR}" sudo apt remove $1 if [[ $? == 0 ]]; then return 0 fi elif [[ ${packageManagers[@]} =~ "pacman" ]]; then - echo -e "${YELLOW}Attempting pacman uninstall...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting pacman uninstall...${ENDCOLOR}" sudo pacman -Rs $1 if [[ $? == 0 ]]; then return 0 fi elif [[ ${packageManagers[@]} =~ "dnf" ]]; then - echo -e "${YELLOW}Attempting dnf uninstall...${ENDCOLOR}" + printf "%b\n" "${YELLOW}Attempting dnf uninstall...${ENDCOLOR}" sudo dnf remove $1 if [[ $? == 0 ]]; then return 0 fi fi - echo -e "${RED}ERROR - Failed to uninstall $1.${ENDCOLOR}" + printf "%b\n" "${RED}ERROR - Failed to uninstall $1.${ENDCOLOR}" return 1 } # If no parameter or u - echo "Available package managers: ${packageManagers[@]}" + printf "%b\n" "${CYAN}Available package managers: ${MAGENTA}${packageManagers[@]}${ENDCOLOR}" if [ -z $1 ] || [ $1 = "u" ]; then updateP return 0 @@ -239,12 +257,12 @@ p() ( checkP $package done else - echo -e "${YELLOW}${UNDERLINE}[i] Usage:${ENDCOLOR}" - echo -e "p (u) ${FAINT}- update os${ENDCOLOR}" - echo -e "p i package ${FAINT}- install package${ENDCOLOR}" - echo -e "p r package ${FAINT}- remove package${ENDCOLOR}" - echo -e "p c package ${FAINT}- check if package is installed${ENDCOLOR}" - echo -e "${FAINT}Supported package managers: flatpak, nix, brew, apt, paru, pacman, dnf${ENDCOLOR}" + printf "%b\n" "${YELLOW}${UNDERLINE}[i] Usage:${ENDCOLOR}" + printf "%b\n" "p (u) ${FAINT}- update os${ENDCOLOR}" + printf "%b\n" "p i package ${FAINT}- install package${ENDCOLOR}" + printf "%b\n" "p r package ${FAINT}- remove package${ENDCOLOR}" + printf "%b\n" "p c package ${FAINT}- check if package is installed${ENDCOLOR}" + printf "%b\n" "${FAINT}Supported package managers: flatpak, nix, brew, apt, paru, pacman, dnf${ENDCOLOR}" return 1 fi )