Skip to content

Instantly share code, notes, and snippets.

@luzfcb
Created July 1, 2024 01:30
Show Gist options
  • Save luzfcb/db9a8b54b04666b25a61f718d3abdab9 to your computer and use it in GitHub Desktop.
Save luzfcb/db9a8b54b04666b25a61f718d3abdab9 to your computer and use it in GitHub Desktop.
install extra debian packages on the docker-steam-headless . Simple create a extra_packages.txt on Download directory with the name of the packages
#!/bin/bash
# Define file and directory paths
BASE_HOME_DIR="/home/default"
PACKAGE_LIST="${BASE_HOME_DIR}/Downloads/extra_packages.txt"
TEMP_DIR="/tmp/extra_packages"
DEST_DIR="${BASE_HOME_DIR}/Downloads/extra_packages"
LOG_FILE="$TEMP_DIR/download.log"
# Check if the package list file exists
if [ ! -f "$PACKAGE_LIST" ]; then
echo "Package list file $PACKAGE_LIST does not exist. Exiting."
exit 0
fi
# Create necessary directories
echo "Creating temporary and destination directories..."
sudo mkdir -p "$TEMP_DIR"
sudo chown "$USER" -R "$TEMP_DIR"
mkdir -p "$DEST_DIR"
sudo chown "$USER" -R "$DEST_DIR"
# Read all package names from the file
packages_to_check=()
echo "Reading package names from $PACKAGE_LIST..."
while IFS= read -r package || [ -n "$package" ]; do
echo "Adding $package to the list of packages to check."
packages_to_check+=("$package")
done < "$PACKAGE_LIST"
# Exit if no packages need to be checked
if [ ${#packages_to_check[@]} -eq 0 ]; then
echo "No packages listed in $PACKAGE_LIST. Exiting."
exit 0
fi
echo "Packages to be checked: ${packages_to_check[*]}"
# Check for conflicts before downloading
echo "Checking for conflicts..."
if ! sudo apt-get -s install "${packages_to_check[@]}" > /dev/null 2>&1; then
echo "Conflict detected or some packages are not available. Exiting."
sudo apt-get -s install "${packages_to_check[@]}"
exit 1
fi
# Initialize an empty list for packages to install
packages_to_install=()
# Check if each package is already installed
for package in "${packages_to_check[@]}"; do
echo "Checking if $package is installed..."
if ! dpkg-query -W -f='${Status}' "$package" 2>/dev/null | grep -q "^install ok installed$"; then
echo "$package is not installed. Adding to list of packages to install."
packages_to_install+=("$package")
else
echo "$package is already installed. Skipping."
fi
done
# Exit if no packages need to be installed
if [ ${#packages_to_install[@]} -eq 0 ]; then
echo "All packages are already installed."
exit 0
fi
echo "Packages to be installed: ${packages_to_install[*]}"
# Download all .deb packages and dependencies at once to the temporary directory
echo "Downloading packages and their dependencies..."
while true; do
if sudo bash -c "apt-get install --download-only -y ${packages_to_install[*]} -o Dir::Cache=$TEMP_DIR >> $LOG_FILE 2>&1"; then
echo "Downloaded packages and their dependencies successfully."
break
else
echo "Download failed. Retrying..."
sleep 5
fi
done
echo "Moving downloaded files to $DEST_DIR..."
# Check if there are downloaded files to move
if ls "$TEMP_DIR"/archives/*.deb 1> /dev/null 2>&1; then
sudo mv "$TEMP_DIR"/archives/*.deb "$DEST_DIR/"
else
echo "No downloaded files to move."
exit 1
fi
# List the packages that will be installed
echo "The following packages will be installed:"
ls "$DEST_DIR"/*.deb
echo "Installing all packages..."
# Install all packages at once
if ! sudo dpkg -i "$DEST_DIR"/*.deb; then
echo "There were errors during the installation."
fi
# Clean up
echo "Cleaning up temporary files..."
sudo rm -rf "$TEMP_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment