152 lines
4.0 KiB
Bash
152 lines
4.0 KiB
Bash
#!/bin/bash
|
|
# =============================================================================
|
|
# Package management module
|
|
# =============================================================================
|
|
|
|
# Set script directory
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
# Source common functions and variables
|
|
source "./common.sh"
|
|
|
|
# Function to configure APT with proxy settings if needed
|
|
configure_apt_proxy() {
|
|
local proxy_file="/etc/apt/apt.conf.d/90proxy"
|
|
local proxy_doc="/root/apt-proxy-documentation.txt"
|
|
|
|
log_message "INFO" "Creating APT proxy documentation at $proxy_doc"
|
|
|
|
cat > "$proxy_doc" << EOF
|
|
# APT Proxy Configuration
|
|
# To configure APT to use a proxy, edit the file /etc/apt/apt.conf.d/90proxy
|
|
# and add one of the following configurations:
|
|
|
|
# HTTP proxy
|
|
Acquire::http::Proxy "http://username:password@proxy.example.com:8080/";
|
|
|
|
# HTTPS proxy
|
|
Acquire::https::Proxy "http://username:password@proxy.example.com:8080/";
|
|
|
|
# For APT to use the system's proxy settings
|
|
Acquire::http::Proxy "http://${http_proxy}";
|
|
Acquire::https::Proxy "http://${https_proxy}";
|
|
|
|
# To disable proxy for specific hosts
|
|
Acquire::http::Proxy::hostname.example.com "DIRECT";
|
|
EOF
|
|
|
|
log_message "SUCCESS" "APT proxy documentation created. Edit $proxy_file to configure proxies if needed"
|
|
}
|
|
|
|
# Function to update package lists and upgrade installed packages
|
|
update_upgrade_packages() {
|
|
log_message "INFO" "Updating package lists"
|
|
apt-get update
|
|
|
|
if [ $? -eq 0 ]; then
|
|
log_message "SUCCESS" "Package lists updated successfully"
|
|
else
|
|
log_message "ERROR" "Failed to update package lists"
|
|
return 1
|
|
fi
|
|
|
|
log_message "INFO" "Upgrading installed packages"
|
|
apt-get full-upgrade -y
|
|
|
|
if [ $? -eq 0 ]; then
|
|
log_message "SUCCESS" "Packages upgraded successfully"
|
|
else
|
|
log_message "ERROR" "Failed to upgrade packages"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to install essential security packages
|
|
install_essential_packages() {
|
|
local packages=(
|
|
"apt-transport-https"
|
|
"ca-certificates"
|
|
"gnupg"
|
|
"software-properties-common"
|
|
"curl"
|
|
"wget"
|
|
"ufw"
|
|
"unattended-upgrades"
|
|
"apt-listchanges"
|
|
)
|
|
|
|
log_message "INFO" "Installing essential security packages"
|
|
|
|
for package in "${packages[@]}"; do
|
|
if ! is_package_installed "$package"; then
|
|
log_message "INFO" "Installing $package"
|
|
apt-get install -y "$package"
|
|
|
|
if [ $? -eq 0 ]; then
|
|
log_message "SUCCESS" "Installed $package successfully"
|
|
else
|
|
log_message "ERROR" "Failed to install $package"
|
|
fi
|
|
else
|
|
log_message "INFO" "$package is already installed"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Create a new package installation module
|
|
cat > "$SCRIPT_DIR/install_packages.sh" << 'EOF'
|
|
#!/bin/bash
|
|
# =============================================================================
|
|
# New package installation module
|
|
# =============================================================================
|
|
|
|
# Set script directory
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
# Source common functions and variables
|
|
source "./common.sh"
|
|
|
|
# Function to install new packages safely
|
|
install_new_packages() {
|
|
if [ $# -eq 0 ]; then
|
|
log_message "ERROR" "No packages specified for installation"
|
|
return 1
|
|
fi
|
|
|
|
log_message "INFO" "Installing new packages: $*"
|
|
|
|
# Update package lists first
|
|
apt-get update
|
|
|
|
if [ $? -ne 0 ]; then
|
|
log_message "ERROR" "Failed to update package lists"
|
|
return 1
|
|
fi
|
|
|
|
# Install the specified packages
|
|
apt-get install -y "$@"
|
|
|
|
if [ $? -eq 0 ]; then
|
|
log_message "SUCCESS" "Installed packages successfully: $*"
|
|
return 0
|
|
else
|
|
log_message "ERROR" "Failed to install packages: $*"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Usage example:
|
|
# source "$SCRIPT_DIR/install_packages.sh"
|
|
# install_new_packages package1 package2 package3
|
|
EOF
|
|
|
|
chmod +x "$SCRIPT_DIR/install_packages.sh"
|
|
|
|
log_message "INFO" "Created new package installation module at $SCRIPT_DIR/install_packages.sh"
|
|
|
|
# Main execution for package management
|
|
configure_apt_proxy
|
|
update_upgrade_packages
|
|
install_essential_packages
|
|
|
|
log_message "SUCCESS" "Package management configuration completed" |