[COMMON] Fix some array bashisms
[oom.git] / kubernetes / common / mariadb-init / resources / config / db_init.sh
1 #!/bin/sh
2
3 {{/*
4 # Copyright © 2019 Orange
5 # Copyright © 2020 Samsung Electronics
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #       http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 */}}
19
20 # make sure the script fails if any of commands failed
21 set -e
22
23 while read DB ; do
24     USER_VAR="MYSQL_USER_$(echo $DB | tr '[:lower:]' '[:upper:]')"
25     PASS_VAR="MYSQL_PASSWORD_$(echo $DB | tr '[:lower:]' '[:upper:]')"
26 {{/*
27     # USER=${!USER_VAR}
28     # PASS=`echo -n ${!PASS_VAR} | sed -e "s/'/''/g"`
29     # eval replacement of the bashism equivalents above might present a security issue here
30     # since it reads content from DB values filled by helm at the end of the script.
31     # These possible values has to be constrainted and/or limited by helm for a safe use of eval.
32 */}}
33     eval USER=\$$USER_VAR
34     PASS=$(eval echo -n \$$PASS_VAR | sed -e "s/'/''/g")
35     MYSQL_OPTS=" -h "${DB_HOST}" -P "${DB_PORT}" -uroot -p"${MYSQL_ROOT_PASSWORD}
36
37     echo "Creating database ${DB} and user ${USER}..."
38
39     mysql $MYSQL_OPTS -e "CREATE OR REPLACE USER '${USER}'@'%' IDENTIFIED BY '${PASS}'"
40     mysql $MYSQL_OPTS -e "CREATE DATABASE IF NOT EXISTS ${DB}"
41     mysql $MYSQL_OPTS -e "GRANT ALL PRIVILEGES ON ${DB}.* TO '${USER}'@'%'"
42
43     echo "Created database ${DB} and user ${USER}."
44 done <<EOF
45 {{ .Values.config.mysqlDatabase }}
46 {{- range $db, $_value := .Values.config.mysqlAdditionalDatabases }}
47 {{ $db }}
48 {{- end }}
49 EOF