From 95aba9a28ec62e4472abf577f8ba530f56dee7b5 Mon Sep 17 00:00:00 2001 From: colin Date: Wed, 24 Jul 2024 16:28:21 +0000 Subject: [PATCH] Update src/dockerutils.sh --- src/dockerutils.sh | 86 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 21 deletions(-) diff --git a/src/dockerutils.sh b/src/dockerutils.sh index 52d63d2..59d395b 100644 --- a/src/dockerutils.sh +++ b/src/dockerutils.sh @@ -5,7 +5,20 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then exit 1 fi -check_swarm_status() { +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 " + echo " remove_stack - Remove a Docker stack. Usage: remove_stack " + echo " find_container - Find a container by name pattern. Usage: find_container " + echo " find_service_node - Find the nodes where services matching a pattern are running. Usage: find_service_node " + echo " exec_remote - SSH into the node where a container is running and execute a command inside it. Usage: exec_remote " +} + +swarm_status() { SWARM_STATUS=$(docker info --format '{{.Swarm.LocalNodeState}}') if [[ "$SWARM_STATUS" == "inactive" ]]; then echo "Node is not in a swarm. Ready to join." @@ -20,7 +33,7 @@ check_swarm_status() { fi } -check_node_role() { +node_role() { NODE_ROLE=$(docker info --format '{{.Swarm.ControlAvailable}}') if [[ "$NODE_ROLE" == "true" ]]; then echo "Node is a manager." @@ -33,13 +46,13 @@ check_node_role() { fi } -list_docker_volumes() { - volumes=$(docker volume ls --format '{{.Name}}') - if [[ -z "$volumes" ]]; then +volumes() { + volume_list=$(docker volume ls --format '{{.Name}}') + if [[ -z "$volume_list" ]]; then echo "No Docker volumes found." else echo "Available Docker volumes:" - echo "$volumes" + echo "$volume_list" fi if [ $? -ne 0 ]; then echo "Usage: Ensure Docker is running and you have the correct permissions." @@ -47,13 +60,13 @@ list_docker_volumes() { fi } -list_docker_overlay_networks() { - networks=$(docker network ls --filter driver=overlay --format '{{.Name}}') - if [[ -z "$networks" ]]; then +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 "$networks" + echo "$network_list" fi if [ $? -ne 0 ]; then echo "Usage: Ensure Docker is running and you have the correct permissions." @@ -61,9 +74,9 @@ list_docker_overlay_networks() { fi } -dup() { +deploy_stack() { if [ -z "$1" ]; then - echo "Usage: dup " + echo "Usage: deploy_stack " return 1 fi docker stack deploy --with-registry-auth -c ~/"$1"/"$1".yml "$1" @@ -73,9 +86,9 @@ dup() { fi } -ddn() { +remove_stack() { if [ -z "$1" ]; then - echo "Usage: ddn " + echo "Usage: remove_stack " return 1 fi docker stack rm "$1" @@ -85,16 +98,47 @@ ddn() { fi } -cid() { +find_container() { if [ -z "$1" ]; then - echo "Usage: cid " + echo "Usage: find_container " 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." + 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_id" + echo "$container_info" +} + +find_service_node() { + if [ -z "$1" ]; then + echo "Usage: find_service_node " + 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 " + 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" }