shell-utils/src/dockerutils.sh

165 lines
5.3 KiB
Bash

# /etc/profile.d/dockerutils.sh
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "This script should be sourced, not executed."
exit 1
fi
dockerutils() {
echo "Available Docker Utility Functions:"
echo " swarm_status - Check the status of the Docker swarm."
echo " node_role - Check the role of the current node in the swarm."
echo " volumes - List Docker volumes."
echo " networks - List Docker overlay networks."
echo " deploy_stack - Deploy a Docker stack. Usage: deploy_stack <stack-name>"
echo " remove_stack - Remove a Docker stack. Usage: remove_stack <stack-name>"
echo " find_container - Find a container by name pattern. Usage: find_container <container-name-pattern>"
echo " find_service_node - Find the nodes where services matching a pattern are running. Usage: find_service_node <service-name-pattern>"
echo " exec_remote - SSH into the node where a container is running and execute a command inside it. Usage: exec_remote <container-name-pattern> <command>"
echo " deploy_stack_ng - Build, push, and deploy a stack. Usage: deploy_stack_ng <stack-name>"
}
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
}
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
}
volumes() {
volume_list=$(docker volume ls --format '{{.Name}}')
if [[ -z "$volume_list" ]]; then
echo "No Docker volumes found."
else
echo "Available Docker volumes:"
echo "$volume_list"
fi
if [ $? -ne 0 ]; then
echo "Usage: Ensure Docker is running and you have the correct permissions."
return 1
fi
}
networks() {
network_list=$(docker network ls --filter driver=overlay --format '{{.Name}}')
if [[ -z "$network_list" ]]; then
echo "No Docker overlay networks found."
else
echo "Docker overlay networks:"
echo "$network_list"
fi
if [ $? -ne 0 ]; then
echo "Usage: Ensure Docker is running and you have the correct permissions."
return 1
fi
}
deploy_stack() {
if [ -z "$1" ]; then
echo "Usage: deploy_stack <stack-name>"
return 1
fi
docker stack deploy --with-registry-auth -c ~/"$1"/stack.production.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
}
remove_stack() {
if [ -z "$1" ]; then
echo "Usage: remove_stack <stack-name>"
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
}
find_container() {
if [ -z "$1" ]; then
echo "Usage: find_container <container-name-pattern>"
return 1
fi
container_info=$(docker ps --filter "name=$1" --format "{{.ID}} {{.Names}} {{.Node}}")
if [ -z "$container_info" ]; then
echo "No container found with the pattern: $1"
echo "Usage: Ensure the container name pattern is correct and the container is running."
return 1
fi
echo "$container_info"
}
find_service_node() {
if [ -z "$1" ]; then
echo "Usage: find_service_node <service-name-pattern>"
return 1
fi
service_info=$(docker service ps --filter "desired-state=running" --format "{{.Name}} {{.Node}}" | grep "$1")
if [ -z "$service_info" ]; then
echo "No service found matching pattern: $1"
return 1
fi
echo "$service_info"
}
exec_remote() {
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: exec_remote <container-name-pattern> <command>"
return 1
fi
container_info=$(find_container "$1")
if [ $? -ne 0 ]; then
return 1
fi
container_id=$(echo "$container_info" | awk '{print $1}')
container_node=$(echo "$container_info" | awk '{print $3}')
echo "Connecting to node $container_node to execute command in container $container_id"
ssh "$container_node" "docker exec -it $container_id $2"
}
deploy_stack_ng() {
if [ -z "$1" ]; then
echo "Usage: deploy_stack_ng <stack-name>"
return 1
fi
ssh "$1" "docker compose -f ~/stackname/docker-compose.staging.yml build && docker compose -f ~/stackname/docker-compose.staging.yml push && docker compose -f ~/stackname/docker-compose.production.yml build && docker compose -f ~/stackname/docker-compose.production.yml push && docker stack deploy --with-registry-auth -c ~/stackname/stack.production.yml $1"
}
# Aliases for backward compatibility
alias check_swarm_status='swarm_status'
alias check_node_role='node_role'
alias list_docker_volumes='volumes'
alias list_docker_overlay_networks='networks'
alias dup='deploy_stack'
alias ddn='remove_stack'
alias cid='find_container'
alias service_node='find_service_node'
alias dup_ng='deploy_stack_ng'