This commit is contained in:
hhftechnologies 2024-11-27 18:29:48 +05:30
parent dbf509646d
commit 25f8fd6b86

View file

@ -86,12 +86,12 @@ check_os_version() {
esac
}
# Function to install dependencies
install_dependencies() {
local install_output
# Function to install Docker
install_docker() {
local update_output
local install_output
# Update package lists with error handling
# Update package lists
echo "Updating package lists..."
update_output=$(sudo apt-get update 2>&1)
if [ $? -ne 0 ]; then
@ -100,111 +100,27 @@ install_dependencies() {
return 1
fi
# Check if packages are already installed
local missing_packages=()
local packages=("ca-certificates" "curl" "gnupg" "apt-transport-https" "software-properties-common")
for pkg in "${packages[@]}"; do
if ! dpkg -l | grep -q "^ii $pkg "; then
missing_packages+=("$pkg")
fi
done
# If all packages are installed, return success
if [ ${#missing_packages[@]} -eq 0 ]; then
echo -e "All required packages are already installed $CHECK_MARK"
return 0
fi
# Install missing packages
echo "Installing missing packages: ${missing_packages[*]}"
install_output=$(sudo apt-get install -y "${missing_packages[@]}" 2>&1)
# Install Docker
echo "Installing Docker..."
install_output=$(sudo apt-get install -y docker.io 2>&1)
if [ $? -ne 0 ]; then
echo -e "Failed to install dependencies $CROSS_MARK"
echo -e "Failed to install Docker $CROSS_MARK"
echo "Error output: $install_output"
return 1
fi
# Verify installation
local failed_packages=()
for pkg in "${missing_packages[@]}"; do
if ! dpkg -l | grep -q "^ii $pkg "; then
failed_packages+=("$pkg")
fi
done
if [ ${#failed_packages[@]} -gt 0 ]; then
echo -e "Failed to install some packages: ${failed_packages[*]} $CROSS_MARK"
return 1
fi
echo -e "Dependencies installed successfully $CHECK_MARK"
echo -e "Docker installed successfully $CHECK_MARK"
return 0
}
# Function to setup Docker repository
setup_docker_repo() {
# Create directory for keyrings if it doesn't exist
sudo install -m 0755 -d /etc/apt/keyrings
# Download Docker's official GPG key with error handling
if ! curl -fsSL "https://download.docker.com/linux/$ID/gpg" -o /tmp/docker.gpg; then
echo -e "Failed to download Docker GPG key $CROSS_MARK"
return 1
fi
# Dearmor and store the GPG key with error handling
if ! sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg /tmp/docker.gpg; then
echo -e "Failed to dearmor GPG key $CROSS_MARK"
rm -f /tmp/docker.gpg
return 1
fi
rm -f /tmp/docker.gpg
# Set proper permissions
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# Remove any existing Docker repository file
sudo rm -f /etc/apt/sources.list.d/docker.list
# Create the repository configuration with error handling
local repo_config="deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/${ID} ${VERSION_CODENAME} stable"
if ! echo "$repo_config" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null; then
echo -e "Failed to create repository configuration $CROSS_MARK"
return 1
fi
# Display the repository configuration for verification
echo "Created repository configuration:"
cat /etc/apt/sources.list.d/docker.list
# Verify the repository file exists and has content
if [ ! -s /etc/apt/sources.list.d/docker.list ]; then
echo -e "Repository configuration file is empty or missing $CROSS_MARK"
return 1
fi
echo -e "Docker repository setup completed $CHECK_MARK"
return 0
}
# Function to install Docker
install_docker() {
sudo apt-get update > /dev/null 2>&1
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo -e "Docker installation completed $CHECK_MARK"
return 0
else
echo -e "Docker installation failed $CROSS_MARK"
return 1
fi
}
# Function to verify Docker installation
verify_docker() {
# Check Docker service status
if ! systemctl is-active --quiet docker; then
echo "Starting Docker service..."
sudo systemctl start docker
fi
if sudo docker run hello-world > /dev/null 2>&1; then
echo -e "Docker verification successful $CHECK_MARK"
return 0
@ -219,6 +135,7 @@ setup_docker_user() {
sudo usermod -aG docker $USER_TO_RUN_AS
if [ $? -eq 0 ]; then
echo -e "User added to docker group $CHECK_MARK"
echo "Note: You may need to log out and back in for group changes to take effect"
return 0
else
echo -e "Failed to add user to docker group $CROSS_MARK"
@ -226,14 +143,6 @@ setup_docker_user() {
fi
}
# Function to clean up in case of failure
cleanup_repo() {
echo "Cleaning up repository files..."
sudo rm -f /etc/apt/sources.list.d/docker.list
sudo rm -f /etc/apt/keyrings/docker.gpg
echo -e "Repository cleanup completed $CHECK_MARK"
}
# Remove the script itself
remove_script() {
if [ -f "$0" ]; then
@ -273,17 +182,6 @@ if ! format_output check_os_version "Checking OS Version"; then
exit $success
fi
if ! format_output install_dependencies "Installing Dependencies"; then
success=1
exit $success
fi
if ! format_output setup_docker_repo "Setting up Docker Repository"; then
format_output cleanup_repo "Cleaning up failed repository setup"
success=1
exit $success
fi
if ! format_output install_docker "Installing Docker"; then
success=1
exit $success