Update src/dockerutils.sh
This commit is contained in:
parent
f209d5c0c9
commit
63658f478d
|
@ -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 <stack-name>"
|
||||
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 <stack-name>"
|
||||
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 <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
|
||||
if [ -z "$1" ]; then
|
||||
echo "Usage: ddn <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
|
||||
}
|
||||
|
||||
# Function to get container ID by name
|
||||
cid() {
|
||||
if [ -z "$1" ]; then
|
||||
echo "Usage: cid <container-name>"
|
||||
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 <container-name>"
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue