Last active
December 25, 2024 02:13
-
-
Save FlyingFathead/bbe9a7a90da469486c1edcaa49ba033d to your computer and use it in GitHub Desktop.
Enable or disable docker services quickly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script Name: manage_docker_services.sh | |
# Description: Enables or disables Docker-related services on Ubuntu 24.04 LTS. | |
# Usage: | |
# - To disable Docker services: | |
# sudo ./manage_docker_services.sh --disable | |
# - To enable Docker services: | |
# sudo ./manage_docker_services.sh --enable | |
# - To be prompted for action: | |
# sudo ./manage_docker_services.sh | |
# | |
# By FlyingFathead | |
# (digital ghostcode by ChaosWhisperer) | |
# Date: 2024-04-27 | |
# Exit immediately if a command exits with a non-zero status | |
set -e | |
# Function to print stylish horizontal lines using ANSI color gradients | |
viivo() { | |
local GRADIENT=('124' '160' '196' '202' '208' '214' '220' '226' '190' '154' '118' '82' '46' '47' '48' '49' '50' '51' '45' '39' '33' '27' '21' '57' '93' '129' '165' '201' '200' '199' '198' '197' '196' '160' '124') | |
local cols="${COLUMNS:-$(tput cols)}" | |
local color_count=${#GRADIENT[@]} | |
for ((i=0; i<cols; i++)); do | |
local color="${GRADIENT[i%color_count]}" | |
printf "\e[38;5;%sm─" "$color" | |
done | |
printf "\e[0m\n" | |
} | |
# Function to check if the script is run as root | |
check_root() { | |
if [[ "$EUID" -ne 0 ]]; then | |
echo "Error: This script must be run with root privileges. Use sudo." | |
exit 1 | |
fi | |
} | |
# Function to stop, disable, and mask a given service | |
disable_service() { | |
local service_name=$1 | |
echo -e "\n$(viivo)" | |
echo "Disabling service: $service_name" | |
# Stop, disable, and mask the service if it exists | |
if systemctl list-unit-files | grep -q "^${service_name}"; then | |
echo "Stopping ${service_name}..." | |
systemctl stop "${service_name}" || echo "Failed to stop ${service_name}, continuing." | |
echo "Disabling ${service_name}..." | |
systemctl disable "${service_name}" || echo "Failed to disable ${service_name}, continuing." | |
echo "Masking ${service_name}..." | |
systemctl mask "${service_name}" || echo "Failed to mask ${service_name}, continuing." | |
else | |
echo "Service ${service_name} does not exist. Skipping." | |
fi | |
} | |
# Function to unmask, enable, and start a given service | |
enable_service() { | |
local service_name=$1 | |
echo -e "\n$(viivo)" | |
echo "Enabling service: $service_name" | |
# Unmask, enable, and start the service if it exists | |
if systemctl list-unit-files | grep -q "^${service_name}"; then | |
echo "Unmasking ${service_name}..." | |
systemctl unmask "${service_name}" || echo "Failed to unmask ${service_name}, continuing." | |
echo "Enabling ${service_name}..." | |
systemctl enable "${service_name}" || echo "Failed to enable ${service_name}, continuing." | |
echo "Starting ${service_name}..." | |
systemctl start "${service_name}" || echo "Failed to start ${service_name}, continuing." | |
else | |
echo "Service ${service_name} does not exist. Skipping." | |
fi | |
} | |
# Function to verify the status of a given service | |
verify_service() { | |
local service_name=$1 | |
echo -e "\n$(viivo)" | |
echo "Verifying status of ${service_name}..." | |
systemctl status "${service_name}" --no-pager || echo "Service ${service_name} is not active." | |
} | |
# Function to display usage information | |
usage() { | |
echo "Usage: sudo $0 [--enable | --disable]" | |
echo " --enable Enable Docker-related services." | |
echo " --disable Disable Docker-related services." | |
echo " If no option is provided, the script will prompt for action." | |
exit 1 | |
} | |
# Function to prompt the user for action if no flags are provided | |
prompt_action() { | |
echo -e "\n$(viivo)" | |
echo "No action flag provided. Please choose an action:" | |
echo "1) Enable Docker services" | |
echo "2) Disable Docker services" | |
echo -e "$(viivo)" | |
read -rp "Enter 1 or 2: " choice | |
case "$choice" in | |
1) | |
ACTION="enable" | |
;; | |
2) | |
ACTION="disable" | |
;; | |
*) | |
echo "Invalid choice. Exiting." | |
exit 1 | |
;; | |
esac | |
} | |
# Main Script Execution | |
main() { | |
check_root | |
# Parse command-line arguments | |
if [[ $# -gt 1 ]]; then | |
echo "Error: Too many arguments." | |
usage | |
fi | |
case "$1" in | |
--enable) | |
ACTION="enable" | |
;; | |
--disable) | |
ACTION="disable" | |
;; | |
--help|-h) | |
usage | |
;; | |
"") | |
prompt_action | |
;; | |
*) | |
echo "Error: Unknown option '$1'" | |
usage | |
;; | |
esac | |
echo "Starting Docker services management script..." | |
viivo | |
# List of Docker-related services to manage | |
SERVICES=( | |
"docker.service" | |
"docker.socket" | |
"containerd.service" | |
) | |
# Unmask all services first before performing actions | |
if [[ "$ACTION" == "enable" ]]; then | |
for service in "${SERVICES[@]}"; do | |
echo "Unmasking ${service} before proceeding..." | |
systemctl unmask "${service}" || echo "Failed to unmask ${service}, continuing." | |
done | |
fi | |
# Iterate over each service and perform the chosen action | |
for service in "${SERVICES[@]}"; do | |
if [[ "$ACTION" == "disable" ]]; then | |
disable_service "${service}" | |
elif [[ "$ACTION" == "enable" ]]; then | |
enable_service "${service}" | |
fi | |
done | |
echo -e "\n$(viivo)" | |
if [[ "$ACTION" == "disable" ]]; then | |
echo "All specified Docker services have been stopped, disabled, and masked." | |
elif [[ "$ACTION" == "enable" ]]; then | |
echo "All specified Docker services have been unmasked, enabled, and started." | |
fi | |
viivo | |
echo "Verifying the status of Docker services..." | |
viivo | |
# Verify the status of each service | |
for service in "${SERVICES[@]}"; do | |
verify_service "${service}" | |
done | |
echo -e "\n$(viivo)" | |
echo "Docker services management script completed successfully." | |
} | |
# Invoke the main function | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment