feat: Enhance package detection and installation process with improved parsing and validation

This commit is contained in:
Peter Wood
2025-05-29 07:31:59 -04:00
parent d75c7c5a94
commit ed50395490
9 changed files with 460 additions and 105 deletions

View File

@@ -11,10 +11,20 @@ ENV DEBIAN_FRONTEND=noninteractive
ARG APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=1
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
&& apt-get update && apt-get install -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" -y curl git sudo wget
&& apt-get update && apt-get install -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" -y \
curl git sudo wget
# Pre-install cowsay and lolcat packages for testing
RUN apt-get update && apt-get install -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" -y cowsay lolcat
# Pre-install essential packages from packages.list for faster testing
RUN apt-get update && apt-get install -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" -y \
python3 \
bat \
cowsay \
lolcat \
fzf \
zsh \
nala \
fd-find \
eza
# Create logs directory with proper permissions
RUN mkdir -p /logs && chmod -R 777 /logs
@@ -28,15 +38,21 @@ WORKDIR /home/testuser
# Create directory structure for shell setup
RUN mkdir -p /home/testuser/shell/setup
RUN mkdir -p /home/testuser/shell/dotfiles
# Copy test script, startup script, and packages.list
COPY --chown=testuser:testuser test-setup.sh /home/testuser/
# Copy all necessary setup files
COPY --chown=testuser:testuser test-setup.sh /home/testuser/shell/setup/
COPY --chown=testuser:testuser startup.sh /home/testuser/
COPY --chown=testuser:testuser packages.list /home/testuser/shell/
COPY --chown=testuser:testuser setup.sh /home/testuser/shell/setup/
COPY --chown=testuser:testuser bootstrap.sh /home/testuser/shell/setup/
COPY --chown=testuser:testuser packages.list /home/testuser/shell/setup/
COPY --chown=testuser:testuser my-aliases.zsh.template /home/testuser/shell/dotfiles/
# Make scripts executable
RUN chmod +x /home/testuser/test-setup.sh
RUN chmod +x /home/testuser/shell/setup/test-setup.sh
RUN chmod +x /home/testuser/startup.sh
RUN chmod +x /home/testuser/shell/setup/setup.sh
RUN chmod +x /home/testuser/shell/setup/bootstrap.sh
CMD ["/bin/bash", "-c", "./startup.sh"]
@@ -67,14 +83,20 @@ WORKDIR /home/testuser
# Create directory structure for shell setup
RUN mkdir -p /home/testuser/shell/setup
RUN mkdir -p /home/testuser/shell/dotfiles
# Copy test script, startup script, and packages.list
COPY --chown=testuser:testuser test-setup.sh /home/testuser/
COPY --chown=testuser:testuser startup.sh /home/testuser/
COPY --chown=testuser:testuser packages.list /home/testuser/shell/
# Copy all necessary setup files
COPY --chown=testuser:testuser test-setup.sh /home/testuser/shell/setup/
COPY --chown=testuser:testuser startup.sh /home/testuser/
COPY --chown=testuser:testuser setup.sh /home/testuser/shell/setup/
COPY --chown=testuser:testuser bootstrap.sh /home/testuser/shell/setup/
COPY --chown=testuser:testuser packages.list /home/testuser/shell/setup/
COPY --chown=testuser:testuser my-aliases.zsh.template /home/testuser/shell/dotfiles/
# Make scripts executable
RUN chmod +x /home/testuser/test-setup.sh
RUN chmod +x /home/testuser/shell/setup/test-setup.sh
RUN chmod +x /home/testuser/startup.sh
RUN chmod +x /home/testuser/shell/setup/setup.sh
RUN chmod +x /home/testuser/shell/setup/bootstrap.sh
CMD ["/bin/bash", "-c", "./startup.sh"]

View File

View File

@@ -1,14 +1,23 @@
// Essential packages for shell setup
// Cross-platform package list with fallbacks handled in setup scripts
// Core tools
git
python3
wget
curl
bat
cowsay
lolcat
fzf
zsh
nala
fd-find
lazygit
lazydocker
eza
// Enhanced shell tools
bat // Modern cat alternative (available as 'batcat' on Ubuntu/Debian)
cowsay // Fun ASCII art
lolcat // Colorful text output
fzf // Fuzzy finder
zsh // Z shell
nala // Modern apt frontend
fd-find // Modern find alternative (available as 'fd' or 'fdfind')
eza // Modern ls alternative
// Note: lazygit and lazydocker require special installation (snap/GitHub releases)
// These are handled separately in the setup script
// lazygit
// lazydocker

View File

@@ -151,7 +151,7 @@ PKG_MANAGER=$(determine_pkg_manager)
echo -e "${GREEN}Using package manager: $PKG_MANAGER${NC}"
# Load packages from package list
mapfile -t pkgs < <(grep -v '^//' "$SCRIPT_DIR/packages.list" | grep -v -e '^$')
mapfile -t pkgs < <(grep -v '^//' "$SCRIPT_DIR/packages.list" | grep -v -e '^$' | sed 's|//.*||' | awk '{print $1}' | grep -v '^$')
# Map Debian/Ubuntu package names to Fedora equivalents if needed
declare -A fedora_pkg_map
@@ -375,10 +375,10 @@ ALIASES_FILE="$ZSH_CUSTOM/aliases.zsh"
mkdir -p "$ZSH_CUSTOM"
# Create a copy of the original aliases file for backup
cp "$DOTFILES_SUBDIR/my-aliases.zsh" "$ALIASES_FILE.bak"
cp "$DOTFILES_SUBDIR/my-aliases.zsh.original" "$ALIASES_FILE.bak"
# First, copy all general aliases except those we'll modify based on OS and available commands
grep -v "^alias cat=" "$DOTFILES_SUBDIR/my-aliases.zsh" | \
grep -v "^alias cat=" "$DOTFILES_SUBDIR/my-aliases.zsh.original" | \
grep -v "^alias fd=" | \
grep -v "^alias fzf=" | \
grep -v "^alias ls=" | \
@@ -457,13 +457,11 @@ alias ll="ls -laFh --group-directories-first --color=auto"
EOF
fi
# Also create a symlink from the custom aliases file back to the dotfiles directory for persistence
# This allows changes made to aliases.zsh to be tracked in the dotfiles repo
echo -e "${YELLOW}Creating symlink to save customized aliases back to dotfiles...${NC}"
# Save the customized aliases to the dotfiles directory for reference
echo -e "${YELLOW}Saving customized aliases to dotfiles directory...${NC}"
if [ -f "$ALIASES_FILE" ]; then
# Save a copy of the original for reference
cp "$DOTFILES_SUBDIR/my-aliases.zsh" "$DOTFILES_SUBDIR/my-aliases.zsh.original" 2>/dev/null || true
# Replace the my-aliases.zsh with the new customized one
# Copy the customized aliases to the dotfiles directory as my-aliases.zsh
# This file will be ignored by git but serves as a local reference
cp "$ALIASES_FILE" "$DOTFILES_SUBDIR/my-aliases.zsh"
fi

View File

@@ -25,7 +25,7 @@ echo -e "${BLUE}Checking for packages.list:${NC}"
if [ -f "$HOME/shell/setup/packages.list" ]; then
echo -e "- packages.list: ${GREEN}Found${NC}"
# Count packages in list (excluding comments and empty lines)
pkg_count=$(grep -v '^//' "$HOME/shell/setup/packages.list" | grep -v -e '^$' | wc -l)
pkg_count=$(grep -v '^//' "$HOME/shell/setup/packages.list" | grep -v -e '^$' | sed 's|//.*||' | awk '{print $1}' | grep -v '^$' | wc -l)
echo -e "- Package count: ${GREEN}$pkg_count packages${NC}"
else
echo -e "- packages.list: ${RED}Not found${NC}"
@@ -36,19 +36,19 @@ fi
echo -e "${BLUE}Setting up logs directory:${NC}"
if [ -d "/logs" ]; then
echo -e "- Logs directory: ${GREEN}Found${NC}"
# Check ownership and permissions
logs_owner=$(stat -c '%U:%G' /logs)
echo -e "- Current ownership: $logs_owner"
echo "- Setting permissions on /logs directory..."
sudo chown -R $(whoami):$(whoami) /logs 2>/dev/null || echo -e "${YELLOW}Failed to set ownership${NC}"
sudo chmod -R 777 /logs 2>/dev/null || echo -e "${YELLOW}Failed to set permissions${NC}"
# Verify permissions are correct
if [ -w "/logs" ]; then
echo -e "- Write permission: ${GREEN}OK${NC}"
# Create a test file to really verify we can write
if touch "/logs/test_file" 2>/dev/null; then
echo -e "- Test write: ${GREEN}Succeeded${NC}"

View File

@@ -31,7 +31,8 @@ LOGS_DIR="/tmp"
if [ ! -d "$LOGS_DIR" ]; then
mkdir -p "$LOGS_DIR"
fi
firectory if it doesn't exist and is writable
# Try creating the logs directory if it doesn't exist and is writable
# First, try creating the logs directory in case it doesn't exist
if [ -d "/logs" ] || mkdir -p /logs 2>/dev/null; then
if [ -w "/logs" ]; then
@@ -132,6 +133,20 @@ check_command() {
command -v "$1" &> /dev/null
}
# Refresh the environment after package installation
refresh_environment() {
# Refresh package database
hash -r 2>/dev/null || true
# Update PATH to include common installation directories
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/games:/usr/local/games:$HOME/.local/bin:$PATH"
# Source common profile files if they exist
[ -f /etc/profile ] && source /etc/profile 2>/dev/null || true
[ -f "$HOME/.profile" ] && source "$HOME/.profile" 2>/dev/null || true
[ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc" 2>/dev/null || true
}
test_package() {
local pkg=$1
local cmd=${2:-$1}
@@ -139,57 +154,94 @@ test_package() {
echo -en "Testing if $pkg is installed... "
# Special case for cowsay and lolcat which might be in different paths
if [ "$pkg" = "cowsay" ]; then
if check_command "$cmd"; then
echo -e "${GREEN}${NC}"
echo "$pkg: Installed (found in PATH as $cmd)" >> "$LOG_FILE"
return 0
elif [ -x "/usr/games/cowsay" ]; then
echo -e "${GREEN}${NC} (in /usr/games)"
echo "$pkg: Installed (found in /usr/games/)" >> "$LOG_FILE"
# Create a symlink to make it available in PATH for other scripts
if [ ! -e "$HOME/.local/bin" ]; then
mkdir -p "$HOME/.local/bin"
# Special case handling for different packages
case "$pkg" in
"python3")
if check_command "python3"; then
echo -e "${GREEN}${NC}"
echo "$pkg: Installed (as python3)" >> "$LOG_FILE"
return 0
fi
if [ ! -e "$HOME/.local/bin/cowsay" ] && [ -w "$HOME/.local/bin" ]; then
ln -sf /usr/games/cowsay "$HOME/.local/bin/cowsay"
echo "Created symlink for cowsay in $HOME/.local/bin" >> "$LOG_FILE"
;;
"bat")
if check_command "bat"; then
echo -e "${GREEN}${NC}"
echo "$pkg: Installed (as bat)" >> "$LOG_FILE"
return 0
elif check_command "batcat"; then
echo -e "${GREEN}${NC} (as batcat)"
echo "$pkg: Installed (as batcat)" >> "$LOG_FILE"
return 0
fi
return 0
fi
elif [ "$pkg" = "lolcat" ]; then
if check_command "$cmd"; then
echo -e "${GREEN}${NC}"
echo "$pkg: Installed (found in PATH as $cmd)" >> "$LOG_FILE"
return 0
elif [ -x "/usr/games/lolcat" ]; then
echo -e "${GREEN}${NC} (in /usr/games)"
echo "$pkg: Installed (found in /usr/games/)" >> "$LOG_FILE"
# Create a symlink to make it available in PATH for other scripts
if [ ! -e "$HOME/.local/bin" ]; then
mkdir -p "$HOME/.local/bin"
;;
"fd-find")
if check_command "fd" || check_command "fdfind"; then
echo -e "${GREEN}${NC}"
echo "$pkg: Installed (as fd/fdfind)" >> "$LOG_FILE"
return 0
fi
if [ ! -e "$HOME/.local/bin/lolcat" ] && [ -w "$HOME/.local/bin" ]; then
ln -sf /usr/games/lolcat "$HOME/.local/bin/lolcat"
echo "Created symlink for lolcat in $HOME/.local/bin" >> "$LOG_FILE"
;;
"eza")
if check_command "eza" || check_command "exa"; then
echo -e "${GREEN}${NC}"
echo "$pkg: Installed" >> "$LOG_FILE"
return 0
fi
return 0
fi
elif check_command "$cmd"; then
echo -e "${GREEN}${NC}"
echo "$pkg: Installed (as $cmd)" >> "$LOG_FILE"
return 0
elif [ -n "$alt_cmd" ] && check_command "$alt_cmd"; then
echo -e "${GREEN}${NC} (as $alt_cmd)"
echo "$pkg: Installed (as $alt_cmd)" >> "$LOG_FILE"
return 0
else
echo -e "${RED}${NC}"
echo "$pkg: Missing" >> "$LOG_FILE"
return 1
fi
# Note: The calling code will handle the failure and continue testing
;;
"cowsay")
if check_command "$cmd"; then
echo -e "${GREEN}${NC}"
echo "$pkg: Installed (found in PATH as $cmd)" >> "$LOG_FILE"
return 0
elif [ -x "/usr/games/cowsay" ]; then
echo -e "${GREEN}${NC} (in /usr/games)"
echo "$pkg: Installed (found in /usr/games/)" >> "$LOG_FILE"
# Create a symlink to make it available in PATH for other scripts
if [ ! -e "$HOME/.local/bin" ]; then
mkdir -p "$HOME/.local/bin"
fi
if [ ! -e "$HOME/.local/bin/cowsay" ] && [ -w "$HOME/.local/bin" ]; then
ln -sf /usr/games/cowsay "$HOME/.local/bin/cowsay"
echo "Created symlink for cowsay in $HOME/.local/bin" >> "$LOG_FILE"
fi
return 0
fi
;;
"lolcat")
if check_command "$cmd"; then
echo -e "${GREEN}${NC}"
echo "$pkg: Installed (found in PATH as $cmd)" >> "$LOG_FILE"
return 0
elif [ -x "/usr/games/lolcat" ]; then
echo -e "${GREEN}${NC} (in /usr/games)"
echo "$pkg: Installed (found in /usr/games/)" >> "$LOG_FILE"
# Create a symlink to make it available in PATH for other scripts
if [ ! -e "$HOME/.local/bin" ]; then
mkdir -p "$HOME/.local/bin"
fi
if [ ! -e "$HOME/.local/bin/lolcat" ] && [ -w "$HOME/.local/bin" ]; then
ln -sf /usr/games/lolcat "$HOME/.local/bin/lolcat"
echo "Created symlink for lolcat in $HOME/.local/bin" >> "$LOG_FILE"
fi
return 0
fi
;;
*)
if check_command "$cmd"; then
echo -e "${GREEN}${NC}"
echo "$pkg: Installed (as $cmd)" >> "$LOG_FILE"
return 0
elif [ -n "$alt_cmd" ] && check_command "$alt_cmd"; then
echo -e "${GREEN}${NC} (as $alt_cmd)"
echo "$pkg: Installed (as $alt_cmd)" >> "$LOG_FILE"
return 0
fi
;;
esac
echo -e "${RED}${NC}"
echo "$pkg: Missing" >> "$LOG_FILE"
return 1
}
test_file_exists() {
@@ -244,12 +296,12 @@ install_missing_packages() {
# Determine the best installation command
if check_command nala; then
install_cmd=(sudo DEBIAN_FRONTEND=noninteractive nala install -y)
install_cmd=(sudo env DEBIAN_FRONTEND=noninteractive nala install -y)
install_cmd_name="nala"
echo -e "${GREEN}Using nala for package installation${NC}"
echo "Using nala for package installation" >> "$LOG_FILE"
else
install_cmd=(sudo DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" install -y)
install_cmd=(sudo env DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" install -y)
install_cmd_name="apt-get"
echo -e "${YELLOW}Using apt-get for package installation${NC}"
echo "Using apt-get for package installation" >> "$LOG_FILE"
@@ -266,11 +318,11 @@ install_missing_packages() {
echo -e "${YELLOW}Updating package lists...${NC}"
echo "Updating package lists" >> "$LOG_FILE"
if check_command nala; then
echo -e "${GREEN}Running: sudo DEBIAN_FRONTEND=noninteractive nala update${NC}"
sudo DEBIAN_FRONTEND=noninteractive nala update | tee -a "$LOG_FILE"
echo -e "${GREEN}Running: sudo env DEBIAN_FRONTEND=noninteractive nala update${NC}"
sudo env DEBIAN_FRONTEND=noninteractive nala update | tee -a "$LOG_FILE"
else
echo -e "${GREEN}Running: sudo DEBIAN_FRONTEND=noninteractive apt-get update${NC}"
sudo DEBIAN_FRONTEND=noninteractive apt-get update | tee -a "$LOG_FILE"
echo -e "${GREEN}Running: sudo env DEBIAN_FRONTEND=noninteractive apt-get update${NC}"
sudo env DEBIAN_FRONTEND=noninteractive apt-get update | tee -a "$LOG_FILE"
fi
# Install packages
@@ -281,7 +333,17 @@ install_missing_packages() {
echo -e "${BLUE}Running: ${install_cmd[*]} ${install_list[*]}${NC}"
# Execute the install command with the package list
if ! "${install_cmd[@]}" "${install_list[@]}" 2>&1 | tee -a "$LOG_FILE"; then
# Use PIPESTATUS to catch apt-get failures even when piped through tee
if bash -c "set -o pipefail; \"${install_cmd[@]}\" \"${install_list[@]}\" 2>&1 | tee -a \"$LOG_FILE\""; then
echo -e "${GREEN}Successfully installed all packages!${NC}"
echo "Successfully installed all packages" >> "$LOG_FILE"
installed_this_round=("${packages[@]}")
# Refresh environment after installation
refresh_environment
return 0
else
echo -e "${RED}Failed to install some packages. Check the log for details.${NC}"
echo "Failed to install some packages" >> "$LOG_FILE"
@@ -301,6 +363,9 @@ install_missing_packages() {
echo -e "${GREEN}${NC}"
echo "$pkg: Installed successfully" >> "$LOG_FILE"
installed_this_round+=("$pkg")
# Refresh environment after each successful install
refresh_environment
else
echo -e "${RED}${NC}"
echo "$pkg: Installation failed" >> "$LOG_FILE"
@@ -310,11 +375,6 @@ install_missing_packages() {
failed_packages=("${failed_this_round[@]}")
return 1
else
echo -e "${GREEN}Successfully installed all packages!${NC}"
echo "Successfully installed all packages" >> "$LOG_FILE"
installed_this_round=("${packages[@]}")
return 0
fi
}
@@ -394,8 +454,8 @@ if [ -f "$HOME/shell/setup/packages.list" ]; then
echo -e "${YELLOW}Testing package availability in repositories:${NC}"
echo "Testing package availability:" >> "$LOG_FILE"
# Exclude commented lines and empty lines
packages=$(grep -v '^//' "$HOME/shell/setup/packages.list" | grep -v -e '^$')
# Exclude commented lines and empty lines, and strip inline comments
packages=$(grep -v '^//' "$HOME/shell/setup/packages.list" | grep -v -e '^$' | sed 's|//.*||' | awk '{print $1}' | grep -v '^$')
for pkg in $packages; do
echo -en "Checking if $pkg is available in repos... "
@@ -441,8 +501,8 @@ while [ $CURRENT_ATTEMPT -le $MAX_INSTALL_ATTEMPTS ]; do
echo "Testing packages listed in packages.list:" >> "$LOG_FILE"
if [ -f "$HOME/shell/setup/packages.list" ]; then
# Exclude commented lines and empty lines
packages=$(grep -v '^//' "$HOME/shell/setup/packages.list" | grep -v -e '^$')
# Exclude commented lines and empty lines, and strip inline comments
packages=$(grep -v '^//' "$HOME/shell/setup/packages.list" | grep -v -e '^$' | sed 's|//.*||' | awk '{print $1}' | grep -v '^$')
for pkg in $packages; do
case "$pkg" in
@@ -511,7 +571,7 @@ while [ $CURRENT_ATTEMPT -le $MAX_INSTALL_ATTEMPTS ]; do
done
# Count installed vs. total packages
total_pkgs=$(grep -v '^//' "$HOME/shell/setup/packages.list" | grep -v -e '^$' | wc -l)
total_pkgs=$(grep -v '^//' "$HOME/shell/setup/packages.list" | grep -v -e '^$' | sed 's|//.*||' | awk '{print $1}' | grep -v '^$' | wc -l)
installed_pkgs=$((total_pkgs - ${#missing_packages[@]}))
echo -e "${GREEN}$installed_pkgs of $total_pkgs packages installed${NC} (${YELLOW}${#missing_packages[@]} missing${NC})"
echo "$installed_pkgs of $total_pkgs packages installed (${#missing_packages[@]} missing)" >> "$LOG_FILE"
@@ -543,7 +603,7 @@ while [ $CURRENT_ATTEMPT -le $MAX_INSTALL_ATTEMPTS ]; do
continue
fi
else
total_pkgs=$(grep -v '^//' "$HOME/shell/setup/packages.list" | grep -v -e '^$' | wc -l)
total_pkgs=$(grep -v '^//' "$HOME/shell/setup/packages.list" | grep -v -e '^$' | sed 's|//.*||' | awk '{print $1}' | grep -v '^$' | wc -l)
echo -e "\n${GREEN}All $total_pkgs packages are installed!${NC}"
echo "All $total_pkgs packages are installed" >> "$LOG_FILE"
break
@@ -732,7 +792,7 @@ check_bootstrapped_environment() {
echo "Packages list: Present" >> "$LOG_FILE"
# Count packages in list
pkg_count=$(grep -v '^//' "$HOME/shell/setup/packages.list" | grep -v -e '^$' | wc -l)
pkg_count=$(grep -v '^//' "$HOME/shell/setup/packages.list" | grep -v -e '^$' | sed 's|//.*||' | awk '{print $1}' | grep -v '^$' | wc -l)
echo -e " - ${GREEN}Package list contains $pkg_count packages${NC}"
echo "Package list count: $pkg_count" >> "$LOG_FILE"
else