guillaume.lambert | fb9dc87 | 2021-12-08 11:20:38 +0100 | [diff] [blame^] | 1 | #!/bin/sh |
Guillaume Lambert | f345486 | 2021-03-10 16:09:31 +0100 | [diff] [blame] | 2 | |
Jakub Latusek | 2eea149 | 2020-10-21 13:36:29 +0200 | [diff] [blame] | 3 | {{/* |
Sylvain Desbureaux | 1d061a4 | 2019-11-08 15:27:20 +0100 | [diff] [blame] | 4 | # Copyright © 2019 Orange |
Krzysztof Opasiak | f54d668 | 2020-01-08 18:35:44 +0100 | [diff] [blame] | 5 | # Copyright © 2020 Samsung Electronics |
Sylvain Desbureaux | 1d061a4 | 2019-11-08 15:27:20 +0100 | [diff] [blame] | 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. |
Jakub Latusek | 2eea149 | 2020-10-21 13:36:29 +0200 | [diff] [blame] | 18 | */}} |
Sylvain Desbureaux | 1d061a4 | 2019-11-08 15:27:20 +0100 | [diff] [blame] | 19 | |
Krzysztof Opasiak | c5dac87 | 2020-07-02 20:10:04 +0200 | [diff] [blame] | 20 | # make sure the script fails if any of commands failed |
| 21 | set -e |
| 22 | |
Krzysztof Opasiak | f54d668 | 2020-01-08 18:35:44 +0100 | [diff] [blame] | 23 | while read DB ; do |
Guillaume Lambert | 05c791d | 2021-03-12 10:59:34 +0100 | [diff] [blame] | 24 | USER_VAR="MYSQL_USER_$(echo $DB | tr '[:lower:]' '[:upper:]')" |
| 25 | PASS_VAR="MYSQL_PASSWORD_$(echo $DB | tr '[:lower:]' '[:upper:]')" |
Guillaume Lambert | f345486 | 2021-03-10 16:09:31 +0100 | [diff] [blame] | 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") |
guillaume.lambert | fb9dc87 | 2021-12-08 11:20:38 +0100 | [diff] [blame^] | 35 | MYSQL_OPTS=" -h "${DB_HOST}" -P "${DB_PORT}" -uroot -p"${MYSQL_ROOT_PASSWORD} |
Sylvain Desbureaux | 1d061a4 | 2019-11-08 15:27:20 +0100 | [diff] [blame] | 36 | |
Krzysztof Opasiak | f54d668 | 2020-01-08 18:35:44 +0100 | [diff] [blame] | 37 | echo "Creating database ${DB} and user ${USER}..." |
Sylvain Desbureaux | 1d061a4 | 2019-11-08 15:27:20 +0100 | [diff] [blame] | 38 | |
guillaume.lambert | fb9dc87 | 2021-12-08 11:20:38 +0100 | [diff] [blame^] | 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}'@'%'" |
Sylvain Desbureaux | 1d061a4 | 2019-11-08 15:27:20 +0100 | [diff] [blame] | 42 | |
Krzysztof Opasiak | f54d668 | 2020-01-08 18:35:44 +0100 | [diff] [blame] | 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 |