@@ -6,8 +6,9 @@ SHUTDOWN_LIST="shutdowned.txt"
6
6
7
7
log_message () {
8
8
local message=" $1 "
9
+ local level=" ${2:- INFO} " # Default level is INFO
9
10
local timestamp=$( date ' +%Y-%m-%d %H:%M:%S' )
10
- echo " [$timestamp ] $message " | tee -a " $LOG_FILE "
11
+ echo " [$timestamp ] [ $level ] $message " | tee -a " $LOG_FILE "
11
12
}
12
13
13
14
# Function to check container status
@@ -25,6 +26,19 @@ check_container() {
25
26
fi
26
27
}
27
28
29
+ # Function to get container details
30
+ get_container_details () {
31
+ local name=$1
32
+ local details=$( docker inspect " $name " 2> /dev/null)
33
+ if [ $? -eq 0 ]; then
34
+ local ip=$( echo " $details " | grep -m 1 ' "IPAddress":' | cut -d ' "' -f 4)
35
+ local created=$( echo " $details " | grep -m 1 ' "Created":' | cut -d ' "' -f 4)
36
+ echo " IP: $ip , Created: $created "
37
+ else
38
+ echo " No details available"
39
+ fi
40
+ }
41
+
28
42
# Function to reverse file content (compatible with both macOS and Linux)
29
43
reverse_file () {
30
44
if command -v tac > /dev/null 2>&1 ; then
@@ -36,66 +50,57 @@ reverse_file() {
36
50
37
51
# Check if shutdown list exists
38
52
if [ ! -f " $SHUTDOWN_LIST " ]; then
39
- log_message " No shutdown list found at $SHUTDOWN_LIST . Nothing to start."
53
+ log_message " No shutdown list found at $SHUTDOWN_LIST . Nothing to start." " WARNING "
40
54
exit 1
41
55
fi
42
56
43
- log_message " Starting Docker containers from shutdown list..."
57
+ log_message " Starting Docker containers from shutdown list..." " INFO "
44
58
45
59
# Read the shutdown list in reverse order (to respect dependencies)
46
60
while IFS=' |' read -r name image cmd ports networks; do
47
- log_message " Processing container: $name "
61
+ log_message " Processing container: $name " " INFO "
48
62
49
63
# Check container status
50
64
container_status=$( check_container " $name " )
51
65
52
- case $container_status in
66
+ case " $container_status " in
53
67
" running" )
54
- log_message " Container $name is already running, skipping..."
55
- continue
68
+ log_message " Container $name is already running" " INFO"
69
+ details=$( get_container_details " $name " )
70
+ log_message " Container details - $details " " INFO"
56
71
;;
57
72
" stopped" )
58
- log_message " Container $name exists but is stopped, starting it... "
73
+ log_message " Starting stopped container: $name " " INFO "
59
74
if docker start " $name " ; then
60
- log_message " Successfully started existing container: $name "
75
+ details=$( get_container_details " $name " )
76
+ log_message " Successfully started container $name " " SUCCESS"
77
+ log_message " Container details - $details " " INFO"
61
78
else
62
- log_message " Failed to start existing container: $name "
79
+ log_message " Failed to start container $name " " ERROR "
63
80
fi
64
- continue
65
81
;;
66
82
" none" )
67
- # Process port mappings
68
- port_args=" "
69
- if [ ! -z " $ports " ]; then
70
- # Convert semicolon-separated port mappings into multiple -p arguments
71
- for port_mapping in ${ports// ;/ } ; do
72
- if [ ! -z " $port_mapping " ]; then
73
- port_args=" $port_args -p $port_mapping "
74
- fi
75
- done
76
- fi
77
-
78
- # Process network
79
- network_arg=" "
80
- if [ ! -z " $networks " ]; then
81
- # Get first network (before semicolon)
82
- network=$( echo " $networks " | cut -d' ;' -f1)
83
- if [ ! -z " $network " ]; then
84
- network_arg=" --network $network "
85
- fi
86
- fi
87
-
83
+ log_message " Creating new container: $name " " INFO"
88
84
# Create and start the container
89
- docker_cmd=" docker run -d --name ${name} ${port_args} ${network_arg} ${image} ${cmd} "
90
- log_message " Creating new container: $docker_cmd "
91
-
92
- if eval " $docker_cmd " ; then
93
- log_message " Successfully created and started container: $name "
85
+ if [ -z " $networks " ]; then
86
+ if docker run -d --name " $name " $ports " $image " $cmd ; then
87
+ details=$( get_container_details " $name " )
88
+ log_message " Successfully created and started container $name " " SUCCESS"
89
+ log_message " Container details - $details " " INFO"
90
+ else
91
+ log_message " Failed to create container $name " " ERROR"
92
+ fi
94
93
else
95
- log_message " Failed to create and start container: $name "
94
+ if docker run -d --name " $name " $ports --network " $networks " " $image " $cmd ; then
95
+ details=$( get_container_details " $name " )
96
+ log_message " Successfully created and started container $name with network $networks " " SUCCESS"
97
+ log_message " Container details - $details " " INFO"
98
+ else
99
+ log_message " Failed to create container $name with network $networks " " ERROR"
100
+ fi
96
101
fi
97
102
;;
98
103
esac
99
104
done < <( reverse_file " $SHUTDOWN_LIST " )
100
105
101
- log_message " Finished starting all containers from shutdown list. "
106
+ log_message " Container startup process completed " " INFO "
0 commit comments