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() {
|
check_swarm_status() {
|
||||||
SWARM_STATUS=$(docker info --format '{{.Swarm.LocalNodeState}}')
|
SWARM_STATUS=$(docker info --format '{{.Swarm.LocalNodeState}}')
|
||||||
if [ "$SWARM_STATUS" == "inactive" ]; then
|
if [[ "$SWARM_STATUS" == "inactive" ]]; then
|
||||||
echo "Node is not in a swarm. Ready to join."
|
echo "Node is not in a swarm. Ready to join."
|
||||||
elif [ "$SWARM_STATUS" == "pending" ]; then
|
elif [[ "$SWARM_STATUS" == "pending" ]]; then
|
||||||
echo "Node is in a pending state."
|
echo "Node is in a pending state."
|
||||||
elif [ "$SWARM_STATUS" == "active" ]; then
|
elif [[ "$SWARM_STATUS" == "active" ]]; then
|
||||||
echo "Node is in an active swarm."
|
echo "Node is in an active swarm."
|
||||||
else
|
else
|
||||||
echo "Node swarm status is unknown: $SWARM_STATUS"
|
echo "Node swarm status is unknown: $SWARM_STATUS"
|
||||||
echo "Usage: Ensure Docker is running and the node is part of a swarm."
|
echo "Usage: Ensure Docker is running and the node is part of a swarm."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to check node role
|
|
||||||
check_node_role() {
|
check_node_role() {
|
||||||
NODE_ROLE=$(docker info --format '{{.Swarm.ControlAvailable}}')
|
NODE_ROLE=$(docker info --format '{{.Swarm.ControlAvailable}}')
|
||||||
if [ "$NODE_ROLE" == "true" ]; then
|
if [[ "$NODE_ROLE" == "true" ]]; then
|
||||||
echo "Node is a manager."
|
echo "Node is a manager."
|
||||||
else
|
else
|
||||||
echo "Node is a worker."
|
echo "Node is a worker."
|
||||||
fi
|
fi
|
||||||
if [ -z "$NODE_ROLE" ]; then
|
if [[ -z "$NODE_ROLE" ]]; then
|
||||||
echo "Usage: Ensure Docker is running and the node is part of a swarm."
|
echo "Usage: Ensure Docker is running and the node is part of a swarm."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to list Docker volumes
|
|
||||||
list_docker_volumes() {
|
list_docker_volumes() {
|
||||||
volumes=$(docker volume ls --format '{{.Name}}')
|
volumes=$(docker volume ls --format '{{.Name}}')
|
||||||
if [ -z "$volumes" ]; then
|
if [[ -z "$volumes" ]]; then
|
||||||
echo "No Docker volumes found."
|
echo "No Docker volumes found."
|
||||||
else
|
else
|
||||||
echo "Available Docker volumes:"
|
echo "Available Docker volumes:"
|
||||||
echo "$volumes"
|
echo "$volumes"
|
||||||
fi
|
fi
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "Usage: Ensure Docker is running and you have the correct permissions."
|
echo "Usage: Ensure Docker is running and you have the correct permissions."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to list Docker overlay networks
|
|
||||||
list_docker_overlay_networks() {
|
list_docker_overlay_networks() {
|
||||||
networks=$(docker network ls --filter driver=overlay --format '{{.Name}}')
|
networks=$(docker network ls --filter driver=overlay --format '{{.Name}}')
|
||||||
if [ -z "$networks" ]; then
|
if [[ -z "$networks" ]]; then
|
||||||
echo "No Docker overlay networks found."
|
echo "No Docker overlay networks found."
|
||||||
else
|
else
|
||||||
echo "Docker overlay networks:"
|
echo "Docker overlay networks:"
|
||||||
echo "$networks"
|
echo "$networks"
|
||||||
fi
|
fi
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "Usage: Ensure Docker is running and you have the correct permissions."
|
echo "Usage: Ensure Docker is running and you have the correct permissions."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to deploy a Docker stack with registry auth
|
|
||||||
dup() {
|
dup() {
|
||||||
if [ -z "$1" ]; then
|
if [ -z "$1" ]; then
|
||||||
echo "Usage: dup <stack-name>"
|
echo "Usage: dup <stack-name>"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
docker stack deploy --with-registry-auth -c ~/"$1"/"$1".yml "$1"
|
docker stack deploy --with-registry-auth -c ~/"$1"/"$1".yml "$1"
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "Usage: Ensure the stack name is correct and the Docker Compose file exists in the specified location."
|
echo "Usage: Ensure the stack name is correct and the Docker Compose file exists in the specified location."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to remove a Docker stack
|
|
||||||
ddn() {
|
ddn() {
|
||||||
if [ -z "$1" ]; then
|
if [ -z "$1" ]; then
|
||||||
echo "Usage: ddn <stack-name>"
|
echo "Usage: ddn <stack-name>"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
docker stack rm "$1"
|
docker stack rm "$1"
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "Usage: Ensure the stack name is correct and the Docker stack is currently deployed."
|
echo "Usage: Ensure the stack name is correct and the Docker stack is currently deployed."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to get container ID by name
|
|
||||||
cid() {
|
cid() {
|
||||||
if [ -z "$1" ]; then
|
if [ -z "$1" ]; then
|
||||||
echo "Usage: cid <container-name>"
|
echo "Usage: cid <container-name>"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
container_id=$(docker ps -qf "name=^$1" 2>/dev/null)
|
container_id=$(docker ps -qf "name=^$1$" 2>/dev/null)
|
||||||
if [ -z "$container_id" ]; then
|
if [ -z "$container_id" ]; then
|
||||||
echo "No container found with the name: $1"
|
echo "No container found with the name: $1"
|
||||||
echo "Usage: Ensure the container name is correct and the container is running."
|
echo "Usage: Ensure the container name is correct and the container is running."
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
echo "$container_id"
|
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