Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 94a3a33

Browse files
committed
#14: Adds improved bootstrap / wrapper scripts.
1 parent fdf94fd commit 94a3a33

File tree

4 files changed

+318
-75
lines changed

4 files changed

+318
-75
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CentOS-7 7.5.1804 x86_64 - Redis 4.0.
1818
- Adds port incrementation to Makefile's run template for container names with an instance suffix.
1919
- Adds docker-compose configuration example.
2020
- Adds improved logging output.
21+
- Adds improved bootstrap / wrapper scripts.
2122
- Removes use of `/etc/services-config` paths.
2223
- Removes X-Fleet section from etcd register template unit-file.
2324
- Removes the unused group element from the default container name.

src/etc/supervisord.d/redis-server-bootstrap.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[program:redis-server-bootstrap]
22
priority = 6
3-
command = /usr/sbin/redis-server-bootstrap
3+
command = /usr/sbin/redis-server-bootstrap --verbose
44
autostart = %(ENV_REDIS_AUTOSTART_REDIS_BOOTSTRAP)s
55
startsecs = 0
66
startretries = 0

src/usr/sbin/redis-server-bootstrap

Lines changed: 253 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,171 @@
22

33
set -e
44

5-
readonly CONFIG_PATH="/etc/redis.conf"
6-
readonly LOCK_FILE="/var/lock/subsys/redis-server-bootstrap"
7-
readonly TIMER_START="$(
8-
date +%s.%N
9-
)"
10-
readonly USER="redis"
11-
readonly WRAPPER="/usr/sbin/redis-server-wrapper"
12-
13-
# Create lock file
14-
touch \
15-
"${LOCK_FILE}"
16-
17-
function load_config ()
5+
function __is_valid_redis_maxmemory ()
186
{
19-
local file_path="${1:-}"
7+
local -r memory_units='^[1-9][0-9]*(?i)(?:kb?|mb?|gb?)$'
8+
local -r value="${1}"
9+
10+
if [[ ${value} =~ ${memory_units} ]]
11+
then
12+
return 0
13+
fi
14+
15+
return 1
16+
}
17+
18+
function __is_valid_redis_maxmemory_policy ()
19+
{
20+
local -r redis_policies='^(?:allkeys-lru|volatile-lru|volatile-random|allkeys-random|volatile-ttl|noeviction)$'
21+
local -r redis_policies_40='^(?:allkeys-lru|volatile-lru|volatile-lfu|allkeys-lfu|volatile-random|allkeys-random|volatile-ttl|noeviction)$'
22+
local -r redis_version="$(
23+
__redis_version
24+
)"
25+
local -r redis_version_gte_40='^[4]\.[0-9]+'
26+
local -r value="${1}"
27+
28+
if [[ ${value} =~ ${redis_policies} ]]
29+
then
30+
return 0
31+
elif [[ ${redis_version} =~ ${redis_version_gte_40} ]] \
32+
&& [[ ${value} =~ ${redis_policies_40} ]]
33+
then
34+
return 0
35+
fi
36+
37+
return 1
38+
}
39+
40+
function __is_valid_redis_maxmemory_samples ()
41+
{
42+
local -r non_zero_integer='^[1-9][0-9]*$'
43+
local -r value="${1}"
44+
45+
if [[ ${value} =~ ${non_zero_integer} ]]
46+
then
47+
return 0
48+
fi
49+
50+
return 1
51+
}
52+
53+
function __is_valid_redis_tcp_backlog ()
54+
{
55+
local -r non_zero_integer='^[1-9][0-9]*$'
56+
local -r value="${1}"
57+
58+
if [[ ${value} =~ ${non_zero_integer} ]]
59+
then
60+
return 0
61+
fi
62+
63+
return 1
64+
}
65+
66+
function __get_redis_options ()
67+
{
68+
printf -- '%s' "${REDIS_OPTIONS}"
69+
}
70+
71+
function __get_redis_maxmemory ()
72+
{
73+
local -r default_value="${1:-64mb}"
74+
75+
local value="${REDIS_MAXMEMORY}"
76+
77+
if ! __is_valid_redis_maxmemory "${value}"
78+
then
79+
value="${default_value}"
80+
fi
81+
82+
printf -- '%s' "${value}"
83+
}
84+
85+
function __get_redis_maxmemory_policy ()
86+
{
87+
local -r default_value="${1:-allkeys-lru}"
88+
89+
local value="${REDIS_MAXMEMORY_POLICY}"
90+
91+
if ! __is_valid_redis_maxmemory_policy "${value}"
92+
then
93+
value="${default_value}"
94+
fi
95+
96+
printf -- '%s' "${value}"
97+
}
98+
99+
function __get_redis_maxmemory_samples ()
100+
{
101+
local -r default_value="${1:-10}"
102+
103+
local value="${REDIS_MAXMEMORY_SAMPLES}"
104+
105+
if ! __is_valid_redis_maxmemory_samples "${value}"
106+
then
107+
value="${default_value}"
108+
fi
109+
110+
printf -- '%s' "${value}"
111+
}
112+
113+
function __get_redis_tcp_backlog ()
114+
{
115+
local -r default_value="${1:-1024}"
116+
117+
local value="${REDIS_TCP_BACKLOG}"
118+
119+
if ! __is_valid_redis_tcp_backlog "${value}"
120+
then
121+
value="${default_value}"
122+
fi
123+
124+
printf -- '%s' "${value}"
125+
}
126+
127+
function __load_config ()
128+
{
129+
local -r file_path="${1:-}"
130+
local -r maxmemory="${2:-"$(
131+
__get_redis_maxmemory
132+
)"}"
133+
local -r maxmemory_policy="${3:-"$(
134+
__get_redis_maxmemory_policy
135+
)"}"
136+
local -r maxmemory_samples="${4:-"$(
137+
__get_redis_maxmemory_samples
138+
)"}"
139+
local -r tcp_backlog="${5:-"$(
140+
__get_redis_tcp_backlog
141+
)"}"
20142

21143
if [[ -n ${file_path} ]] \
22144
&& [[ -s ${file_path} ]]
23145
then
24146
# Replace placeholders with environment variables
25147
sed -i \
26-
-e "s~{{REDIS_MAXMEMORY}}~${REDIS_MAXMEMORY:-64mb}~" \
27-
-e "s~{{REDIS_MAXMEMORY_POLICY}}~${REDIS_MAXMEMORY_POLICY:-allkeys-lru}~" \
28-
-e "s~{{REDIS_MAXMEMORY_SAMPLES}}~${REDIS_MAXMEMORY_SAMPLES:-10}~" \
29-
-e "s~{{REDIS_TCP_BACKLOG}}~${REDIS_TCP_BACKLOG:-1024}~" \
148+
-e "s~{{REDIS_MAXMEMORY}}~${maxmemory}~" \
149+
-e "s~{{REDIS_MAXMEMORY_POLICY}}~${maxmemory_policy}~" \
150+
-e "s~{{REDIS_MAXMEMORY_SAMPLES}}~${maxmemory_samples}~" \
151+
-e "s~{{REDIS_TCP_BACKLOG}}~${tcp_backlog}~" \
30152
"${file_path}"
31153
fi
32154
}
33155

34-
function set_wrapper_execute_user ()
156+
function __redis_version ()
157+
{
158+
local -r version="$(
159+
redis-server -v \
160+
| grep -E -o 'v=[0-9\.]+' \
161+
| sed 's~^v=~~'
162+
)"
163+
164+
printf -- \
165+
'%s' \
166+
"${version}"
167+
}
168+
169+
function __set_wrapper_execute_user ()
35170
{
36171
local file_path="${1:-}"
37172
local user="${2:-}"
@@ -45,38 +180,102 @@ function set_wrapper_execute_user ()
45180
"${file_path}"
46181
}
47182

48-
set_wrapper_execute_user \
49-
"${WRAPPER}" \
50-
"${USER}"
51-
52-
load_config \
53-
"${CONFIG_PATH}"
54-
55-
TIMER_TOTAL="$(
56-
echo - | awk "\
57-
{ T1=\"${TIMER_START}\" } \
58-
{ T2=\"$(date +%s.%N)\" } \
59-
{ print T2 - T1; }"
60-
)"
61-
62-
cat \
63-
<<-EOT
64-
65-
================================================================================
66-
Redis Details
67-
--------------------------------------------------------------------------------
68-
maxmemory : ${REDIS_MAXMEMORY:-64mb}
69-
maxmemory-policy: ${REDIS_MAXMEMORY_POLICY:-allkeys-lru}
70-
maxmemory-samples: ${REDIS_MAXMEMORY_SAMPLES:-5}
71-
tcp-backlog: ${REDIS_TCP_BACKLOG:-1024}
72-
redis-server options: ${REDIS_OPTIONS:-N/A}
73-
--------------------------------------------------------------------------------
74-
${TIMER_TOTAL}
75-
76-
EOT
77-
78-
# Release lock file
79-
rm -f \
80-
"${LOCK_FILE}"
81-
82-
exit 0
183+
function main ()
184+
{
185+
local -r config_path="/etc/redis.conf"
186+
local -r lock_file="/var/lock/subsys/redis-server-bootstrap"
187+
local -r timer_start="$(
188+
date +%s.%N
189+
)"
190+
local -r user="redis"
191+
local -r wrapper="/usr/sbin/redis-server-wrapper"
192+
193+
local redis_options
194+
local redis_maxmemory
195+
local redis_maxmemory_policy
196+
local redis_maxmemory_samples
197+
local redis_tcp_backlog
198+
local timer_total
199+
local verbose=false
200+
201+
# Create lock
202+
touch \
203+
"${lock_file}"
204+
205+
# Parse options
206+
while [[ "${#}" -gt 0 ]]
207+
do
208+
case "${1}" in
209+
-v|--verbose)
210+
verbose=true
211+
shift 1
212+
;;
213+
esac
214+
done
215+
216+
__set_wrapper_execute_user \
217+
"${wrapper}" \
218+
"${user}"
219+
220+
if [[ ${verbose} == true ]]
221+
then
222+
redis_options="$(
223+
__get_redis_options
224+
)"
225+
redis_maxmemory="$(
226+
__get_redis_maxmemory
227+
)"
228+
redis_maxmemory_policy="$(
229+
__get_redis_maxmemory_policy
230+
)"
231+
redis_maxmemory_samples="$(
232+
__get_redis_maxmemory_samples
233+
)"
234+
redis_tcp_backlog="$(
235+
__get_redis_tcp_backlog
236+
)"
237+
238+
__load_config \
239+
"${config_path}" \
240+
"${redis_maxmemory}" \
241+
"${redis_maxmemory_policy}" \
242+
"${redis_maxmemory_samples}" \
243+
"${redis_tcp_backlog}"
244+
245+
timer_total="$(
246+
awk \
247+
-v timer_end="$(
248+
date +%s.%N
249+
)" \
250+
-v timer_start="${timer_start}" \
251+
'BEGIN { print \
252+
timer_end - timer_start;
253+
}'
254+
)"
255+
256+
cat \
257+
<<-EOT
258+
259+
================================================================================
260+
Redis Details
261+
--------------------------------------------------------------------------------
262+
maxmemory : ${redis_maxmemory}
263+
maxmemory-policy: ${redis_maxmemory_policy}
264+
maxmemory-samples: ${redis_maxmemory_samples}
265+
tcp-backlog: ${redis_tcp_backlog}
266+
redis-server options: ${redis_options}
267+
--------------------------------------------------------------------------------
268+
${timer_total}
269+
270+
EOT
271+
else
272+
__load_config \
273+
"${config_path}"
274+
fi
275+
276+
# Release lock
277+
rm -f \
278+
"${lock_file}"
279+
}
280+
281+
main "${@}"

0 commit comments

Comments
 (0)