forked from frappe/frappe_docker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontainer_optimize.sh
executable file
·183 lines (153 loc) · 5.3 KB
/
container_optimize.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/bash
# Function to show usage
show_usage() {
echo "Usage: $0 [apply|remove|status|rebuild]"
echo " apply - Apply container optimizations"
echo " remove - Remove container optimizations"
echo " status - Show current optimization status"
echo " rebuild - Rebuild and restart containers"
exit 1
}
# Function to check if docker is installed
check_docker() {
if ! command -v docker &> /dev/null; then
echo "Docker is not installed. Please install Docker first."
exit 1
fi
}
# Function to check if docker-compose is installed
check_compose() {
if ! command -v docker compose &> /dev/null; then
echo "Docker Compose is not installed. Please install Docker Compose first."
exit 1
fi
}
# Function to check if exp.yaml exists
check_config() {
if [ ! -f "exp.yaml" ]; then
echo "exp.yaml not found in current directory"
exit 1
fi
}
# Function to rebuild containers
rebuild_containers() {
check_docker
check_compose
check_config
echo "Rebuilding containers using exp.yaml configuration..."
# Stop containers
echo "Stopping containers..."
docker compose --project-name exp -f exp.yaml down
# Pull latest images
echo "Pulling latest images..."
docker compose --project-name exp -f exp.yaml pull
# Start containers
echo "Starting containers..."
docker compose --project-name exp -f exp.yaml up -d
# Wait for containers to be healthy
echo "Waiting for containers to be ready..."
sleep 10
echo "Rebuild complete. Current container status:"
docker compose --project-name exp -f exp.yaml ps
}
# Function to get current status
get_status() {
check_config
echo "=== Current Container Settings ==="
echo -e "\nContainer Status:"
docker compose --project-name exp -f exp.yaml ps
echo -e "\nRedis Cache Settings:"
docker exec -i exp-redis-cache-1 redis-cli CONFIG GET maxmemory 2>/dev/null || echo "Redis cache container not running"
docker exec -i exp-redis-cache-1 redis-cli CONFIG GET maxmemory-policy 2>/dev/null || echo "Redis cache container not running"
echo -e "\nRedis Queue Settings:"
docker exec -i exp-redis-queue-1 redis-cli CONFIG GET maxmemory 2>/dev/null || echo "Redis queue container not running"
docker exec -i exp-redis-queue-1 redis-cli CONFIG GET maxmemory-policy 2>/dev/null || echo "Redis queue container not running"
}
# Function to optimize Redis settings
optimize_redis() {
local container=$1
local maxmem=$2
local policy=$3
echo "Optimizing Redis container: $container"
# Create Redis config with settings
docker exec -i "$container" /bin/sh -c "echo 'maxmemory ${maxmem}' > /etc/redis.conf"
docker exec -i "$container" /bin/sh -c "echo 'maxmemory-policy ${policy}' >> /etc/redis.conf"
# Apply settings immediately
docker exec -i "$container" redis-cli CONFIG SET maxmemory "$maxmem"
docker exec -i "$container" redis-cli CONFIG SET maxmemory-policy "$policy"
# Verify settings
echo "Verifying settings for $container:"
docker exec -i "$container" redis-cli CONFIG GET maxmemory
docker exec -i "$container" redis-cli CONFIG GET maxmemory-policy
}
# Function to apply optimizations
apply_optimizations() {
check_docker
check_compose
check_config
echo "Applying container optimizations..."
# Get total system memory in MB
total_memory_mb=$(free -m | awk '/^Mem:/{print $2}')
# Calculate Redis memory limits (30% for cache, 20% for queue)
cache_memory="${total_memory_mb}/3"
queue_memory="${total_memory_mb}/5"
# Optimize Redis Cache (using volatile-lru policy)
if docker ps | grep -q exp-redis-cache; then
optimize_redis "exp-redis-cache-1" "${cache_memory}mb" "volatile-lru"
fi
# Optimize Redis Queue (using noeviction policy)
if docker ps | grep -q exp-redis-queue; then
optimize_redis "exp-redis-queue-1" "${queue_memory}mb" "noeviction"
fi
echo "Container optimizations have been applied"
echo "Would you like to rebuild the containers now? (y/n)"
read -r answer
if [ "$answer" = "y" ]; then
rebuild_containers
else
echo "Remember to rebuild containers later for changes to take effect"
fi
get_status
}
# Function to remove optimizations
remove_optimizations() {
check_docker
check_compose
check_config
echo "Removing container optimizations..."
# Reset Redis Cache
if docker ps | grep -q exp-redis-cache; then
optimize_redis "exp-redis-cache-1" "0" "volatile-lru"
fi
# Reset Redis Queue
if docker ps | grep -q exp-redis-queue; then
optimize_redis "exp-redis-queue-1" "0" "noeviction"
fi
echo "Container optimizations have been removed"
echo "Would you like to rebuild the containers now? (y/n)"
read -r answer
if [ "$answer" = "y" ]; then
rebuild_containers
else
echo "Remember to rebuild containers later for changes to take effect"
fi
get_status
}
# Main script
case "$1" in
apply)
apply_optimizations
;;
remove)
remove_optimizations
;;
status)
get_status
;;
rebuild)
rebuild_containers
;;
*)
show_usage
;;
esac