Skip to content

Commit f1c09b3

Browse files
authored
Merge maxscale custom config with default config (#20)
Signed-off-by: Rabbani Islam Refat <[email protected]>
1 parent 4c87b6c commit f1c09b3

File tree

1 file changed

+89
-23
lines changed

1 file changed

+89
-23
lines changed

scripts/maxscale.sh

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
#!/bin/sh
22

33
args="$@"
4-
echo "INFO" "Storing default config into /etc/maxscale/maxscale.cnf"
4+
echo "INFO:" "Storing default config into /etc/maxscale/maxscale.cnf"
55

6-
mkdir -p /etc/maxscale/maxscale.cnf.d
7-
cat >/etc/maxscale/maxscale.cnf <<EOL
6+
#mkdir -p /etc/maxscale/maxscale.cnf.d
7+
8+
#=====================[maxscale] section started ===============================
9+
cat > /etc/maxscale/maxscale.cnf <<EOL
810
[maxscale]
911
threads=1
1012
log_debug=1
13+
persist_runtime_changes=false
14+
load_persisted_configs=false
15+
EOL
16+
17+
if [[ "${UI:-}" == "true" ]]; then
18+
cat >>/etc/maxscale/maxscale.cnf <<EOL
19+
20+
admin_secure_gui=false
21+
# this enables external access to the REST API outside of localhost
22+
# review / modify for any public / non development environments
23+
admin_host=0.0.0.0
1124
EOL
25+
else
26+
echo "UI is not set to true or does not exist."
27+
fi
28+
#=====================[maxscale] section end ====================================
29+
1230
#TODO: configuration sync: among maxscale nodes, when something done in a specific maxscale
1331
#https://mariadb.com/kb/en/mariadb-maxscale-2402-maxscale-2402-mariadb-maxscale-configuration-guide/#runtime-configuration-changes
1432
#if [ "${MAXSCALE_CLUSTER:-}" == "true" ];then
@@ -19,14 +37,14 @@ EOL
1937
#EOL
2038
#fi
2139

22-
cat >/etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
40+
cat >> /etc/maxscale/maxscale.cnf <<EOL
2341
# Auto-generated server list from environment
2442
EOL
2543

2644
serverList=""
2745
# Split HOST_LIST into an array
2846
for ((i=1; i<=REPLICAS; i++)); do
29-
cat >> /etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
47+
cat >> /etc/maxscale/maxscale.cnf <<EOL
3048
3149
[server$i]
3250
type=server
@@ -35,7 +53,7 @@ port=3306
3553
protocol=MariaDBBackend
3654
EOL
3755
if [[ "${REQUIRE_SSL:-}" == "TRUE" ]]; then
38-
cat >>/etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
56+
cat >>/etc/maxscale/maxscale.cnf <<EOL
3957
ssl=true
4058
ssl_ca=/etc/ssl/maxscale/ca.crt
4159
ssl_cert=/etc/ssl/maxscale/tls.crt
@@ -49,19 +67,7 @@ EOL
4967
serverList+="server$i"
5068
done
5169

52-
if [[ "${UI:-}" == "true" ]]; then
53-
cat >>/etc/maxscale/maxscale.cnf <<EOL
54-
55-
admin_secure_gui=false
56-
# this enables external access to the REST API outside of localhost
57-
# review / modify for any public / non development environments
58-
admin_host=0.0.0.0
59-
EOL
60-
else
61-
echo "UI is not set to true or does not exist."
62-
fi
63-
64-
cat >>/etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
70+
cat >>/etc/maxscale/maxscale.cnf <<EOL
6571
6672
[ReplicationMonitor]
6773
type=monitor
@@ -78,11 +84,11 @@ replication_password='$MYSQL_ROOT_PASSWORD'
7884
EOL
7985

8086
if [ "${MAXSCALE_CLUSTER:-}" == "true" ];then
81-
cat >>/etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
87+
cat >>/etc/maxscale/maxscale.cnf <<EOL
8288
cooperative_monitoring_locks=majority_of_running
8389
EOL
8490
fi
85-
cat >>/etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
91+
cat >>/etc/maxscale/maxscale.cnf <<EOL
8692
8793
[RW-Split-Router]
8894
type=service
@@ -98,7 +104,7 @@ master_accept_reads=true
98104
enable_root_user=true
99105
EOL
100106

101-
cat >>/etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
107+
cat >>/etc/maxscale/maxscale.cnf <<EOL
102108
103109
[RW-Split-Listener]
104110
type=listener
@@ -107,7 +113,7 @@ protocol=MariaDBClient
107113
port=3306
108114
EOL
109115
if [[ "${REQUIRE_SSL:-}" == "TRUE" ]]; then
110-
cat >>/etc/maxscale/maxscale.cnf.d/maxscale.cnf <<EOL
116+
cat >>/etc/maxscale/maxscale.cnf <<EOL
111117
ssl=true
112118
ssl_ca=/etc/ssl/maxscale/ca.crt
113119
ssl_cert=/etc/ssl/maxscale/tls.crt
@@ -116,6 +122,66 @@ EOL
116122
fi
117123

118124
echo "INFO: MaxScale configuration files have been successfully created."
125+
126+
127+
# Merge File1 with File2 and store it in File1
128+
function merge() {
129+
local tempFile=etc/maxscale/temp.cnf
130+
touch "$tempFile"
131+
# Match [section] headers in the first block
132+
# Match key=value pairs in the second block
133+
# Ignore all other lines
134+
# Finally print merged configuration in the third block
135+
awk '/^\[.*\]$/ {
136+
section = $0
137+
if (seen[section] == 0) {
138+
seq[++n] = section
139+
seen[section] = 1
140+
}
141+
next
142+
}
143+
/^[^=]+=[^=]+$/ {
144+
split($0,kv,"=")
145+
key = kv[1]
146+
val = kv[2]
147+
a[section, key] = val
148+
}
149+
END {
150+
for (i = 1; i <= n; i++){
151+
section = seq[i]
152+
print section
153+
for (k in a){
154+
split(k, parts, SUBSEP)
155+
if (parts[1] == section){
156+
print parts[2] "=" a[k]
157+
}
158+
}
159+
if (i < n) print ""
160+
}
161+
}' $1 $2 > $tempFile
162+
163+
mv "$tempFile" "$1"
164+
165+
echo "INFO: $1 merged with $2"
166+
}
167+
168+
function mergeCustomConfig() {
169+
defaultConfig=/etc/maxscale/maxscale.cnf
170+
customConfig=(/etc/maxscale/maxscale.custom.d/*.cnf)
171+
172+
# Check if any files are found
173+
if [ -e "${customConfig[0]}" ]; then
174+
echo "INFO: Found custom config files"
175+
for file in "${customConfig[@]}"; do
176+
merge $defaultConfig $file
177+
done
178+
else
179+
echo "INFO: No custom config found"
180+
fi
181+
}
182+
183+
mergeCustomConfig
184+
119185
IFS=' '
120186
set -- $args
121187
docker-entrypoint.sh maxscale "$@"

0 commit comments

Comments
 (0)