-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdocker_retry.sh
executable file
·93 lines (80 loc) · 2.03 KB
/
docker_retry.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
RETRIES=5
WAIT_TIME=5
DOCKER_CMD="$1"
shift
DOCKER_ARGS="$@"
# Function to check if Docker is installed
check_docker_installed() {
if ! command -v docker &> /dev/null; then
echo "Error: Docker is not installed. Please install Docker to continue."
exit 1
fi
}
# Function to check if Docker daemon is running
check_docker_daemon() {
if ! docker info &> /dev/null; then
echo "Error: Docker daemon is not running or not reachable. Please start Docker."
exit 1
fi
}
# Function to check if Docker is logged into any ECR registries
check_registry_login() {
if ! command -v jq &> /dev/null; then
echo "Error: jq is not installed. Required to check Docker ECR login."
exit 1
fi
# Retrieve the list of authenticated Docker registries
local auths
auths=$(jq -r '.auths | keys[]' ~/.docker/config.json 2>/dev/null)
if [ -z "$auths" ]; then
echo "No Docker registries found in Docker configuration. Please login to a registry to continue"
exit 1
fi
}
# Function to handle retries for Docker image-related commands
retry_docker_image_command() {
local cmd="$1"
local retries="$2"
local wait_time="$3"
for ((i=1; i<=retries; i++)); do
if eval "$cmd"; then
return 0
else
echo "Retrying in $wait_time seconds..."
sleep "$wait_time"
fi
done
echo "failed after $retries attempts."
return 1
}
# Function to determine if the command is Docker image-related
is_docker_image_command() {
local cmd="$1"
case "$cmd" in
*push*)
return 0
;;
*)
return 1
;;
esac
}
if [ -z "$DOCKER_CMD" ]; then
echo "Usage: $0 <docker-command> [options...]"
exit 1
fi
FULL_CMD="docker $DOCKER_CMD $DOCKER_ARGS"
# Execute checks
check_docker_installed
check_docker_daemon
check_registry_login
# Determine if the command is Docker image push related and apply retry logic accordingly
if is_docker_image_command "$FULL_CMD"; then
retry_docker_image_command "$FULL_CMD" "$RETRIES" "$WAIT_TIME"
else
eval "$FULL_CMD"
if [ $? -ne 0 ]; then
exit 1
fi
fi