blob: df7c33640590ee6482abf68676bb74aa23a759a8 [file] [log] [blame]
Krzysztof Opasiakf54d6682020-01-08 18:35:44 +01001#!/bin/bash
Guillaume Lambertf3454862021-03-10 16:09:31 +01002
Jakub Latusek2eea1492020-10-21 13:36:29 +02003{{/*
Sylvain Desbureaux1d061a42019-11-08 15:27:20 +01004# Copyright © 2019 Orange
Krzysztof Opasiakf54d6682020-01-08 18:35:44 +01005# Copyright © 2020 Samsung Electronics
Sylvain Desbureaux1d061a42019-11-08 15:27:20 +01006#
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 Latusek2eea1492020-10-21 13:36:29 +020018*/}}
Sylvain Desbureaux1d061a42019-11-08 15:27:20 +010019
Krzysztof Opasiakc5dac872020-07-02 20:10:04 +020020# make sure the script fails if any of commands failed
21set -e
22
Krzysztof Opasiakf54d6682020-01-08 18:35:44 +010023while read DB ; do
Guillaume Lambert05c791d2021-03-12 10:59:34 +010024 USER_VAR="MYSQL_USER_$(echo $DB | tr '[:lower:]' '[:upper:]')"
25 PASS_VAR="MYSQL_PASSWORD_$(echo $DB | tr '[:lower:]' '[:upper:]')"
Guillaume Lambertf3454862021-03-10 16:09:31 +010026{{/*
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")
Krzysztof Opasiakf54d6682020-01-08 18:35:44 +010035 MYSQL_OPTS=( -h ${DB_HOST} -P ${DB_PORT} -uroot -p${MYSQL_ROOT_PASSWORD} )
Sylvain Desbureaux1d061a42019-11-08 15:27:20 +010036
Krzysztof Opasiakf54d6682020-01-08 18:35:44 +010037 echo "Creating database ${DB} and user ${USER}..."
Sylvain Desbureaux1d061a42019-11-08 15:27:20 +010038
Krzysztof Opasiakf54d6682020-01-08 18:35:44 +010039 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 Desbureaux1d061a42019-11-08 15:27:20 +010042
Krzysztof Opasiakf54d6682020-01-08 18:35:44 +010043 echo "Created database ${DB} and user ${USER}."
44done <<EOF
45{{ .Values.config.mysqlDatabase }}
46{{- range $db, $_value := .Values.config.mysqlAdditionalDatabases }}
47{{ $db }}
48{{- end }}
49EOF