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

Commit fed2a2a

Browse files
authored
Merge pull request #29 from jdeathe/issue/15
CLOSES #15: Adds improved bootstrap / wrapper scripts.
2 parents c907518 + 17c3571 commit fed2a2a

File tree

4 files changed

+317
-75
lines changed

4 files changed

+317
-75
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CentOS-6 6.10 x86_64 - Redis 3.2.
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: 252 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,170 @@
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]*(kb?|mb?|gb?|KB?|MB?|GB?)$'
8+
local -r value="${1}"
9+
if [[ ${value} =~ ${memory_units} ]]
10+
then
11+
return 0
12+
fi
13+
14+
return 1
15+
}
16+
17+
function __is_valid_redis_maxmemory_policy ()
18+
{
19+
local -r redis_policies='^(allkeys-lru|volatile-lru|volatile-random|allkeys-random|volatile-ttl|noeviction)$'
20+
local -r redis_policies_40='^(allkeys-lru|volatile-lru|volatile-lfu|allkeys-lfu|volatile-random|allkeys-random|volatile-ttl|noeviction)$'
21+
local -r redis_version="$(
22+
__redis_version
23+
)"
24+
local -r redis_version_gte_40='^[4]\.[0-9]+'
25+
local -r value="${1}"
26+
27+
if [[ ${value} =~ ${redis_policies} ]]
28+
then
29+
return 0
30+
elif [[ ${redis_version} =~ ${redis_version_gte_40} ]] \
31+
&& [[ ${value} =~ ${redis_policies_40} ]]
32+
then
33+
return 0
34+
fi
35+
36+
return 1
37+
}
38+
39+
function __is_valid_redis_maxmemory_samples ()
40+
{
41+
local -r non_zero_integer='^[1-9][0-9]*$'
42+
local -r value="${1}"
43+
44+
if [[ ${value} =~ ${non_zero_integer} ]]
45+
then
46+
return 0
47+
fi
48+
49+
return 1
50+
}
51+
52+
function __is_valid_redis_tcp_backlog ()
53+
{
54+
local -r non_zero_integer='^[1-9][0-9]*$'
55+
local -r value="${1}"
56+
57+
if [[ ${value} =~ ${non_zero_integer} ]]
58+
then
59+
return 0
60+
fi
61+
62+
return 1
63+
}
64+
65+
function __get_redis_options ()
66+
{
67+
printf -- '%s' "${REDIS_OPTIONS}"
68+
}
69+
70+
function __get_redis_maxmemory ()
71+
{
72+
local -r default_value="${1:-64mb}"
73+
74+
local value="${REDIS_MAXMEMORY}"
75+
76+
if ! __is_valid_redis_maxmemory "${value}"
77+
then
78+
value="${default_value}"
79+
fi
80+
81+
printf -- '%s' "${value}"
82+
}
83+
84+
function __get_redis_maxmemory_policy ()
85+
{
86+
local -r default_value="${1:-allkeys-lru}"
87+
88+
local value="${REDIS_MAXMEMORY_POLICY}"
89+
90+
if ! __is_valid_redis_maxmemory_policy "${value}"
91+
then
92+
value="${default_value}"
93+
fi
94+
95+
printf -- '%s' "${value}"
96+
}
97+
98+
function __get_redis_maxmemory_samples ()
99+
{
100+
local -r default_value="${1:-10}"
101+
102+
local value="${REDIS_MAXMEMORY_SAMPLES}"
103+
104+
if ! __is_valid_redis_maxmemory_samples "${value}"
105+
then
106+
value="${default_value}"
107+
fi
108+
109+
printf -- '%s' "${value}"
110+
}
111+
112+
function __get_redis_tcp_backlog ()
113+
{
114+
local -r default_value="${1:-1024}"
115+
116+
local value="${REDIS_TCP_BACKLOG}"
117+
118+
if ! __is_valid_redis_tcp_backlog "${value}"
119+
then
120+
value="${default_value}"
121+
fi
122+
123+
printf -- '%s' "${value}"
124+
}
125+
126+
function __load_config ()
127+
{
128+
local -r file_path="${1:-}"
129+
local -r maxmemory="${2:-"$(
130+
__get_redis_maxmemory
131+
)"}"
132+
local -r maxmemory_policy="${3:-"$(
133+
__get_redis_maxmemory_policy
134+
)"}"
135+
local -r maxmemory_samples="${4:-"$(
136+
__get_redis_maxmemory_samples
137+
)"}"
138+
local -r tcp_backlog="${5:-"$(
139+
__get_redis_tcp_backlog
140+
)"}"
20141

21142
if [[ -n ${file_path} ]] \
22143
&& [[ -s ${file_path} ]]
23144
then
24145
# Replace placeholders with environment variables
25146
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}~" \
147+
-e "s~{{REDIS_MAXMEMORY}}~${maxmemory}~" \
148+
-e "s~{{REDIS_MAXMEMORY_POLICY}}~${maxmemory_policy}~" \
149+
-e "s~{{REDIS_MAXMEMORY_SAMPLES}}~${maxmemory_samples}~" \
150+
-e "s~{{REDIS_TCP_BACKLOG}}~${tcp_backlog}~" \
30151
"${file_path}"
31152
fi
32153
}
33154

34-
function set_wrapper_execute_user ()
155+
function __redis_version ()
156+
{
157+
local -r version="$(
158+
redis-server -v \
159+
| grep -E -o 'v=[0-9\.]+' \
160+
| sed 's~^v=~~'
161+
)"
162+
163+
printf -- \
164+
'%s' \
165+
"${version}"
166+
}
167+
168+
function __set_wrapper_execute_user ()
35169
{
36170
local file_path="${1:-}"
37171
local user="${2:-}"
@@ -45,38 +179,102 @@ function set_wrapper_execute_user ()
45179
"${file_path}"
46180
}
47181

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
182+
function main ()
183+
{
184+
local -r config_path="/etc/redis.conf"
185+
local -r lock_file="/var/lock/subsys/redis-server-bootstrap"
186+
local -r timer_start="$(
187+
date +%s.%N
188+
)"
189+
local -r user="redis"
190+
local -r wrapper="/usr/sbin/redis-server-wrapper"
191+
192+
local redis_options
193+
local redis_maxmemory
194+
local redis_maxmemory_policy
195+
local redis_maxmemory_samples
196+
local redis_tcp_backlog
197+
local timer_total
198+
local verbose=false
199+
200+
# Create lock
201+
touch \
202+
"${lock_file}"
203+
204+
# Parse options
205+
while [[ "${#}" -gt 0 ]]
206+
do
207+
case "${1}" in
208+
-v|--verbose)
209+
verbose=true
210+
shift 1
211+
;;
212+
esac
213+
done
214+
215+
__set_wrapper_execute_user \
216+
"${wrapper}" \
217+
"${user}"
218+
219+
if [[ ${verbose} == true ]]
220+
then
221+
redis_options="$(
222+
__get_redis_options
223+
)"
224+
redis_maxmemory="$(
225+
__get_redis_maxmemory
226+
)"
227+
redis_maxmemory_policy="$(
228+
__get_redis_maxmemory_policy
229+
)"
230+
redis_maxmemory_samples="$(
231+
__get_redis_maxmemory_samples
232+
)"
233+
redis_tcp_backlog="$(
234+
__get_redis_tcp_backlog
235+
)"
236+
237+
__load_config \
238+
"${config_path}" \
239+
"${redis_maxmemory}" \
240+
"${redis_maxmemory_policy}" \
241+
"${redis_maxmemory_samples}" \
242+
"${redis_tcp_backlog}"
243+
244+
timer_total="$(
245+
awk \
246+
-v timer_end="$(
247+
date +%s.%N
248+
)" \
249+
-v timer_start="${timer_start}" \
250+
'BEGIN { print \
251+
timer_end - timer_start;
252+
}'
253+
)"
254+
255+
cat \
256+
<<-EOT
257+
258+
================================================================================
259+
Redis Details
260+
--------------------------------------------------------------------------------
261+
maxmemory : ${redis_maxmemory}
262+
maxmemory-policy: ${redis_maxmemory_policy}
263+
maxmemory-samples: ${redis_maxmemory_samples}
264+
tcp-backlog: ${redis_tcp_backlog}
265+
redis-server options: ${redis_options}
266+
--------------------------------------------------------------------------------
267+
${timer_total}
268+
269+
EOT
270+
else
271+
__load_config \
272+
"${config_path}"
273+
fi
274+
275+
# Release lock
276+
rm -f \
277+
"${lock_file}"
278+
}
279+
280+
main "${@}"

0 commit comments

Comments
 (0)