From 63658f478d821b7dd60b78562fcbf65bfe1a3e22 Mon Sep 17 00:00:00 2001 From: colin Date: Wed, 24 Jul 2024 13:44:42 +0000 Subject: [PATCH] Update src/dockerutils.sh --- src/dockerutils.sh | 169 +++++++++++++++++++++------------------------ 1 file changed, 79 insertions(+), 90 deletions(-) diff --git a/src/dockerutils.sh b/src/dockerutils.sh index ff666ea..52d63d2 100644 --- a/src/dockerutils.sh +++ b/src/dockerutils.sh @@ -1,111 +1,100 @@ -#!/bin/bash +# /etc/profile.d/dockerutils.sh + +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + echo "This script should be sourced, not executed." + exit 1 +fi -# Function to check Docker swarm status check_swarm_status() { - SWARM_STATUS=$(docker info --format '{{.Swarm.LocalNodeState}}') - if [ "$SWARM_STATUS" == "inactive" ]; then - echo "Node is not in a swarm. Ready to join." - elif [ "$SWARM_STATUS" == "pending" ]; then - echo "Node is in a pending state." - elif [ "$SWARM_STATUS" == "active" ]; then - echo "Node is in an active swarm." - else - echo "Node swarm status is unknown: $SWARM_STATUS" - echo "Usage: Ensure Docker is running and the node is part of a swarm." - return 1 - fi + SWARM_STATUS=$(docker info --format '{{.Swarm.LocalNodeState}}') + if [[ "$SWARM_STATUS" == "inactive" ]]; then + echo "Node is not in a swarm. Ready to join." + elif [[ "$SWARM_STATUS" == "pending" ]]; then + echo "Node is in a pending state." + elif [[ "$SWARM_STATUS" == "active" ]]; then + echo "Node is in an active swarm." + else + echo "Node swarm status is unknown: $SWARM_STATUS" + echo "Usage: Ensure Docker is running and the node is part of a swarm." + return 1 + fi } -# Function to check node role check_node_role() { - NODE_ROLE=$(docker info --format '{{.Swarm.ControlAvailable}}') - if [ "$NODE_ROLE" == "true" ]; then - echo "Node is a manager." - else - echo "Node is a worker." - fi - if [ -z "$NODE_ROLE" ]; then - echo "Usage: Ensure Docker is running and the node is part of a swarm." - return 1 - fi + NODE_ROLE=$(docker info --format '{{.Swarm.ControlAvailable}}') + if [[ "$NODE_ROLE" == "true" ]]; then + echo "Node is a manager." + else + echo "Node is a worker." + fi + if [[ -z "$NODE_ROLE" ]]; then + echo "Usage: Ensure Docker is running and the node is part of a swarm." + return 1 + fi } -# Function to list Docker volumes list_docker_volumes() { - volumes=$(docker volume ls --format '{{.Name}}') - if [ -z "$volumes" ]; then - echo "No Docker volumes found." - else - echo "Available Docker volumes:" - echo "$volumes" - fi - if [ $? -ne 0 ]; then - echo "Usage: Ensure Docker is running and you have the correct permissions." - return 1 - fi + volumes=$(docker volume ls --format '{{.Name}}') + if [[ -z "$volumes" ]]; then + echo "No Docker volumes found." + else + echo "Available Docker volumes:" + echo "$volumes" + fi + if [ $? -ne 0 ]; then + echo "Usage: Ensure Docker is running and you have the correct permissions." + return 1 + fi } -# Function to list Docker overlay networks list_docker_overlay_networks() { - networks=$(docker network ls --filter driver=overlay --format '{{.Name}}') - if [ -z "$networks" ]; then - echo "No Docker overlay networks found." - else - echo "Docker overlay networks:" - echo "$networks" - fi - if [ $? -ne 0 ]; then - echo "Usage: Ensure Docker is running and you have the correct permissions." - return 1 - fi + networks=$(docker network ls --filter driver=overlay --format '{{.Name}}') + if [[ -z "$networks" ]]; then + echo "No Docker overlay networks found." + else + echo "Docker overlay networks:" + echo "$networks" + fi + if [ $? -ne 0 ]; then + echo "Usage: Ensure Docker is running and you have the correct permissions." + return 1 + fi } -# Function to deploy a Docker stack with registry auth dup() { - if [ -z "$1" ]; then - echo "Usage: dup " - return 1 - fi - docker stack deploy --with-registry-auth -c ~/"$1"/"$1".yml "$1" - if [ $? -ne 0 ]; then - echo "Usage: Ensure the stack name is correct and the Docker Compose file exists in the specified location." - return 1 - fi + if [ -z "$1" ]; then + echo "Usage: dup " + return 1 + fi + docker stack deploy --with-registry-auth -c ~/"$1"/"$1".yml "$1" + if [ $? -ne 0 ]; then + echo "Usage: Ensure the stack name is correct and the Docker Compose file exists in the specified location." + return 1 + fi } -# Function to remove a Docker stack ddn() { - if [ -z "$1" ]; then - echo "Usage: ddn " - return 1 - fi - docker stack rm "$1" - if [ $? -ne 0 ]; then - echo "Usage: Ensure the stack name is correct and the Docker stack is currently deployed." - return 1 - fi + if [ -z "$1" ]; then + echo "Usage: ddn " + return 1 + fi + docker stack rm "$1" + if [ $? -ne 0 ]; then + echo "Usage: Ensure the stack name is correct and the Docker stack is currently deployed." + return 1 + fi } -# Function to get container ID by name cid() { - if [ -z "$1" ]; then - echo "Usage: cid " - return 1 - fi - container_id=$(docker ps -qf "name=^$1" 2>/dev/null) - if [ -z "$container_id" ]; then - echo "No container found with the name: $1" - echo "Usage: Ensure the container name is correct and the container is running." - return 1 - fi - echo "$container_id" + if [ -z "$1" ]; then + echo "Usage: cid " + return 1 + fi + container_id=$(docker ps -qf "name=^$1$" 2>/dev/null) + if [ -z "$container_id" ]; then + echo "No container found with the name: $1" + echo "Usage: Ensure the container name is correct and the container is running." + return 1 + fi + echo "$container_id" } - -# Export functions as commands -export -f check_swarm_status -export -f check_node_role -export -f list_docker_volumes -export -f list_docker_overlay_networks -export -f dup -export -f ddn -export -f cid