Add logging for installation processes and enhance UFW setup in install script

This commit is contained in:
2025-03-13 16:10:03 +00:00
parent 009360f6e4
commit 7dac92429b
3 changed files with 129 additions and 82 deletions

2
.gitignore vendored
View File

@@ -5,3 +5,5 @@ BrewFile.lock.json
.vscode .vscode
/nix-shells/macos-vm/ /nix-shells/macos-vm/
logs/

View File

@@ -9,100 +9,127 @@ PURPLE='\033[0;35m'
CYAN='\033[0;36m' CYAN='\033[0;36m'
NC='\033[0m' # No Color NC='\033[0m' # No Color
mkdir -p "$HOME/dotfiles/logs"
# Clone Dotfiles if not already present # Clone Dotfiles if not already present
cd $HOME/dotfiles cd "$HOME/dotfiles" || exit
if [ $(pwd) != "$HOME/dotfiles" ]; then if [ "$(pwd)" != "$HOME/dotfiles" ]; then
echo -e "${YELLOW}[+] Cloning dotfiles repository...${NC}" printf "${YELLOW}[+] Cloning dotfiles repository...${NC}\n"
git clone https://github.com/TrudeEH/dotfiles --depth 1 git clone https://github.com/TrudeEH/dotfiles --depth 1
if [ $? -ne 0 ]; then 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 exit 2
fi fi
cd dotfiles cd dotfiles || exit
echo -e "${GREEN}[I] dotfiles repository cloned successfully.${NC}" printf "${GREEN}[I] dotfiles repository cloned successfully.${NC}\n"
else else
echo -e "${GREEN}[I] dotfiles repository already present.${NC}" printf "${GREEN}[I] dotfiles repository already present.${NC}\n"
fi fi
source ./scripts/p.sh source ./scripts/p.sh
packageManagers=($(pcheck))
echo -e "${CYAN}" printf "${CYAN}\n"
echo "####################" printf "####################\n"
echo -n "#" printf "#"
echo -e "${PURPLE} Trude's Dotfiles${CYAN} #" printf "${PURPLE} Trude's Dotfiles${CYAN} #\n"
echo "####################" printf "####################\n"
echo -e "${CYAN}Running on: ${PURPLE}$OSTYPE${NC}" printf "${CYAN}Running on: ${PURPLE}%s${NC}\n" "$OSTYPE"
echo -e "${CYAN}Package managers: ${PURPLE}${packageManagers[@]}${NC}" printf "\n"
echo
# Install Programs # Install Programs
programs=(neovim curl git tmux htop fzf gcc make tldr s-tui pass ufw) programs=(neovim curl git tmux htop fzf gcc make tldr pass lynis)
p i ${programs[@]}
if [[ "$OSTYPE" != "darwin"* ]]; then
programs+=(ufw s-tui)
fi
p i "${programs[@]}"
# Copy files # Copy files
echo -e "${YELLOW}[+] Installing Dotfiles...${NC}" printf "${YELLOW}[+] Installing Dotfiles...${NC}\n"
cp -r $HOME/dotfiles/home/. $HOME cp -r "$HOME/dotfiles/home/." "$HOME"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo -e "${RED}[E] Error copying Dotfiles.${NC}" printf "${RED}[E] Error copying Dotfiles.${NC}\n"
else else
echo -e "${GREEN}[I] Dotfiles installed successfully.${NC}" printf "${GREEN}[I] Dotfiles installed successfully.${NC}\n"
fi fi
# Copy scripts # Copy scripts
echo -e "${YELLOW}[+] Installing Scripts...${NC}" printf "${YELLOW}[+] Installing Scripts...${NC}\n"
mkdir -p $HOME/.local/bin mkdir -p "$HOME/.local/bin"
cp -r $HOME/dotfiles/scripts/. $HOME/.local/bin/ cp -r "$HOME/dotfiles/scripts/." "$HOME/.local/bin/"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo -e "${RED}[E] Error copying Scripts.${NC}" printf "${RED}[E] Error copying Scripts.${NC}\n"
else else
echo -e "${GREEN}[I] Scripts installed successfully.${NC}" printf "${GREEN}[I] Scripts installed successfully.${NC}\n"
fi fi
# Install fonts # Install fonts
echo -e "${YELLOW}[+] Installing fonts...${NC}" printf "${YELLOW}[+] Installing fonts...${NC}\n"
if [[ "$OSTYPE" == "darwin"* ]]; then if [[ "$OSTYPE" == "darwin"* ]]; then
cp -rf $HOME/dotfiles/fonts/* $HOME/Library/Fonts/ cp -rf "$HOME/dotfiles/fonts/"* "$HOME/Library/Fonts/"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo -e "${RED}[E] Error installing fonts.${NC}" printf "${RED}[E] Error installing fonts.${NC}\n"
else else
echo -e "${GREEN}[I] Fonts installed successfully.${NC}" printf "${GREEN}[I] Fonts installed successfully.${NC}\n"
fi fi
else else
mkdir -p $HOME/.local/share/fonts mkdir -p "$HOME/.local/share/fonts"
cp -rf $HOME/dotfiles/fonts/* $HOME/.local/share/fonts/ cp -rf "$HOME/dotfiles/fonts/"* "$HOME/.local/share/fonts/"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo -e "${RED}[E] Error installing fonts.${NC}" printf "${RED}[E] Error installing fonts.${NC}\n"
else else
fc-cache -fv $HOME/.local/share/fonts fc-cache -fv "$HOME/.local/share/fonts" >"$HOME/dotfiles/logs/font_install.log"
echo -e "${GREEN}[I] Fonts installed successfully.${NC}" printf "${GREEN}[I] Fonts installed successfully.${NC}\n"
fi fi
fi fi
# Load Dconf (GNOME settings) # Load Dconf (GNOME settings)
if [[ "$OSTYPE" != "darwin"* ]]; then if [[ "$OSTYPE" != "darwin"* ]]; then
echo -e "${YELLOW}[+] Loading Dconf settings...${NC}" printf "${YELLOW}[+] Loading Dconf settings...${NC}\n"
dconf load / <$HOME/dotfiles/dconf-settings.ini dconf load / <"$HOME/dotfiles/dconf-settings.ini"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo -e "${RED}[E] Error loading Dconf settings.${NC}" printf "${RED}[E] Error loading Dconf settings.${NC}\n"
else else
echo -e "${GREEN}[I] Dconf settings loaded successfully.${NC}" printf "${GREEN}[I] Dconf settings loaded successfully.${NC}\n"
fi fi
fi fi
# UFW Firewall # UFW Firewall
echo -e "${YELLOW}[+] Setting up UFW...${NC}" if [[ "$OSTYPE" != "darwin"* ]]; then
sudo ufw default deny incoming printf "${YELLOW}[+] Setting up UFW...${NC}\n"
sudo ufw default allow outgoing sudo ufw default deny incoming
if systemctl is-active --quiet sshd; then sudo ufw default allow outgoing
echo -e "${YELLOW}[+] SSH Server detected; Enabling SSH rule...${NC}" if systemctl is-active --quiet sshd; then
sudo ufw limit 22/tcp printf "${YELLOW}[+] SSH Server detected; Enabling SSH rule...${NC}\n"
sudo ufw limit ssh sudo ufw limit 22/tcp
fi fi
sudo ufw enable sudo ufw enable
sudo ufw status numbered sudo ufw status numbered | tee "$HOME/dotfiles/logs/ufw_status.log"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo -e "${RED}[E] Error setting up UFW.${NC}" printf "${RED}[E] Error setting up UFW.${NC}\n"
else
printf "${GREEN}[I] UFW setup successfully.${NC}\n"
fi
else 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 fi

View File

@@ -97,20 +97,38 @@ p() (
echo $flatpak_apps | grep -iq $app_name echo $flatpak_apps | grep -iq $app_name
flatpak_success=$? flatpak_success=$?
if [[ $flatpak_success == 0 ]]; then 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
fi fi
# Some package names are different from the command name # Some package names are different from the command name
declare -A altNames=(["neovim"]="nvim" ["python"]="python3" ["nodejs"]="node" ["docker-compose"]="docker compose" ["pip"]="pip3") case "$app_name" in
commandName="${altNames[$app_name]:-$app_name}" 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 which "$commandName" &>/dev/null
distro_success=$? distro_success=$?
if [[ $distro_success == 0 ]]; then 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 fi
if [[ $flatpak_success != 0 && $nix_success != 0 && $brew_success != 0 && $distro_success != 0 ]]; then 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 return 1
fi fi
} }
@@ -118,108 +136,108 @@ p() (
installP() { installP() {
checkP $1 checkP $1
if [[ $? != 1 ]]; then if [[ $? != 1 ]]; then
echo -e "${GREEN}$1 is already installed.${ENDCOLOR}" printf "%b\n" "${GREEN}$1 is already installed.${ENDCOLOR}"
return 0 return 0
fi fi
if [[ ${packageManagers[@]} =~ "nix" ]]; then if [[ ${packageManagers[@]} =~ "nix" ]]; then
echo -e "${YELLOW}Attempting nix install...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting nix install...${ENDCOLOR}"
nix-env -iA nixpkgs.$1 nix-env -iA nixpkgs.$1
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
return 0 return 0
fi fi
fi fi
if [[ ${packageManagers[@]} =~ "brew" ]]; then if [[ ${packageManagers[@]} =~ "brew" ]]; then
echo -e "${YELLOW}Attempting brew install...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting brew install...${ENDCOLOR}"
brew install $1 brew install $1
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
return 0 return 0
fi fi
fi fi
if [[ ${packageManagers[@]} =~ "apt" ]]; then if [[ ${packageManagers[@]} =~ "apt" ]]; then
echo -e "${YELLOW}Attempting apt install...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting apt install...${ENDCOLOR}"
sudo apt install $1 sudo apt install $1
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
return 0 return 0
fi fi
elif [[ ${packageManagers[@]} =~ "paru" ]]; then elif [[ ${packageManagers[@]} =~ "paru" ]]; then
echo -e "${YELLOW}Attempting paru install...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting paru install...${ENDCOLOR}"
paru -Sy $1 paru -Sy $1
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
return 0 return 0
fi fi
elif [[ ${packageManagers[@]} =~ "pacman" ]]; then elif [[ ${packageManagers[@]} =~ "pacman" ]]; then
echo -e "${YELLOW}Attempting pacman install...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting pacman install...${ENDCOLOR}"
sudo pacman -Sy $1 sudo pacman -Sy $1
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
return 0 return 0
fi fi
elif [[ ${packageManagers[@]} =~ "dnf" ]]; then elif [[ ${packageManagers[@]} =~ "dnf" ]]; then
echo -e "${YELLOW}Attempting dnf install...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting dnf install...${ENDCOLOR}"
sudo dnf install $1 sudo dnf install $1
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
return 0 return 0
fi fi
fi fi
if [[ ${packageManagers[@]} =~ "flatpak" ]]; then if [[ ${packageManagers[@]} =~ "flatpak" ]]; then
echo -e "${YELLOW}Attempting flatpak install...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting flatpak install...${ENDCOLOR}"
flatpak install $1 flatpak install $1
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
return 0 return 0
fi fi
fi fi
echo -e "${RED}ERROR - $1 not found.${ENDCOLOR}" printf "%b\n" "${RED}ERROR - $1 not found.${ENDCOLOR}"
return 1 return 1
} }
removeP() { removeP() {
checkP $1 checkP $1
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
echo -e "${YELLOW}$1 is not installed.${ENDCOLOR}" printf "%b\n" "${YELLOW}$1 is not installed.${ENDCOLOR}"
return 0 return 0
fi fi
if [[ ${packageManagers[@]} =~ "flatpak" ]]; then if [[ ${packageManagers[@]} =~ "flatpak" ]]; then
echo -e "${YELLOW}Attempting flatpak uninstall...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting flatpak uninstall...${ENDCOLOR}"
flatpak uninstall $1 flatpak uninstall $1
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
return 0 return 0
fi fi
fi fi
if [[ ${packageManagers[@]} =~ "nix" ]]; then if [[ ${packageManagers[@]} =~ "nix" ]]; then
echo -e "${YELLOW}Attempting nix uninstall...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting nix uninstall...${ENDCOLOR}"
nix-env --uninstall $1 nix-env --uninstall $1
fi fi
if [[ ${packageManagers[@]} =~ "brew" ]]; then if [[ ${packageManagers[@]} =~ "brew" ]]; then
echo -e "${YELLOW}Attempting brew uninstall...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting brew uninstall...${ENDCOLOR}"
brew uninstall $1 brew uninstall $1
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
return 0 return 0
fi fi
fi fi
if [[ ${packageManagers[@]} =~ "apt" ]]; then if [[ ${packageManagers[@]} =~ "apt" ]]; then
echo -e "${YELLOW}Attempting apt uninstall...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting apt uninstall...${ENDCOLOR}"
sudo apt remove $1 sudo apt remove $1
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
return 0 return 0
fi fi
elif [[ ${packageManagers[@]} =~ "pacman" ]]; then elif [[ ${packageManagers[@]} =~ "pacman" ]]; then
echo -e "${YELLOW}Attempting pacman uninstall...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting pacman uninstall...${ENDCOLOR}"
sudo pacman -Rs $1 sudo pacman -Rs $1
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
return 0 return 0
fi fi
elif [[ ${packageManagers[@]} =~ "dnf" ]]; then elif [[ ${packageManagers[@]} =~ "dnf" ]]; then
echo -e "${YELLOW}Attempting dnf uninstall...${ENDCOLOR}" printf "%b\n" "${YELLOW}Attempting dnf uninstall...${ENDCOLOR}"
sudo dnf remove $1 sudo dnf remove $1
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
return 0 return 0
fi fi
fi fi
echo -e "${RED}ERROR - Failed to uninstall $1.${ENDCOLOR}" printf "%b\n" "${RED}ERROR - Failed to uninstall $1.${ENDCOLOR}"
return 1 return 1
} }
# If no parameter or u # 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 if [ -z $1 ] || [ $1 = "u" ]; then
updateP updateP
return 0 return 0
@@ -239,12 +257,12 @@ p() (
checkP $package checkP $package
done done
else else
echo -e "${YELLOW}${UNDERLINE}[i] Usage:${ENDCOLOR}" printf "%b\n" "${YELLOW}${UNDERLINE}[i] Usage:${ENDCOLOR}"
echo -e "p (u) ${FAINT}- update os${ENDCOLOR}" printf "%b\n" "p (u) ${FAINT}- update os${ENDCOLOR}"
echo -e "p i package ${FAINT}- install package${ENDCOLOR}" printf "%b\n" "p i package ${FAINT}- install package${ENDCOLOR}"
echo -e "p r package ${FAINT}- remove package${ENDCOLOR}" printf "%b\n" "p r package ${FAINT}- remove package${ENDCOLOR}"
echo -e "p c package ${FAINT}- check if package is installed${ENDCOLOR}" printf "%b\n" "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" "${FAINT}Supported package managers: flatpak, nix, brew, apt, paru, pacman, dnf${ENDCOLOR}"
return 1 return 1
fi fi
) )