11#! /bin/sh
22
33args=" $@ "
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]
911threads=1
1012log_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
1124EOL
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
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
2442EOL
2543
2644serverList=" "
2745# Split HOST_LIST into an array
2846for (( 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 ]
3250type=server
@@ -35,7 +53,7 @@ port=3306
3553protocol=MariaDBBackend
3654EOL
3755 if [[ " ${REQUIRE_SSL:- } " == " TRUE" ]]; then
38- cat >> /etc/maxscale/maxscale.cnf.d/maxscale.cnf << EOL
56+ cat >> /etc/maxscale/maxscale.cnf << EOL
3957ssl=true
4058ssl_ca=/etc/ssl/maxscale/ca.crt
4159ssl_cert=/etc/ssl/maxscale/tls.crt
4967 serverList+=" server$i "
5068done
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]
6773type=monitor
@@ -78,11 +84,11 @@ replication_password='$MYSQL_ROOT_PASSWORD'
7884EOL
7985
8086if [ " ${MAXSCALE_CLUSTER:- } " == " true" ]; then
81- cat >> /etc/maxscale/maxscale.cnf.d/maxscale.cnf << EOL
87+ cat >> /etc/maxscale/maxscale.cnf << EOL
8288cooperative_monitoring_locks=majority_of_running
8389EOL
8490fi
85- cat >> /etc/maxscale/maxscale.cnf.d/maxscale.cnf << EOL
91+ cat >> /etc/maxscale/maxscale.cnf << EOL
8692
8793[RW-Split-Router]
8894type=service
@@ -98,7 +104,7 @@ master_accept_reads=true
98104enable_root_user=true
99105EOL
100106
101- cat >> /etc/maxscale/maxscale.cnf.d/maxscale.cnf << EOL
107+ cat >> /etc/maxscale/maxscale.cnf << EOL
102108
103109[RW-Split-Listener]
104110type=listener
@@ -107,7 +113,7 @@ protocol=MariaDBClient
107113port=3306
108114EOL
109115if [[ " ${REQUIRE_SSL:- } " == " TRUE" ]]; then
110- cat >> /etc/maxscale/maxscale.cnf.d/maxscale.cnf << EOL
116+ cat >> /etc/maxscale/maxscale.cnf << EOL
111117ssl=true
112118ssl_ca=/etc/ssl/maxscale/ca.crt
113119ssl_cert=/etc/ssl/maxscale/tls.crt
116122fi
117123
118124echo " 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+
119185IFS=' '
120186set -- $args
121187docker-entrypoint.sh maxscale " $@ "
0 commit comments