From b597e0addcafb418f80a4ca230086b803bba6db7 Mon Sep 17 00:00:00 2001 From: "Huang, Sophie (sh879n)" Date: Wed, 24 Jun 2020 20:27:43 +0000 Subject: [PATCH] Add the capability to create a test database In this patchset, if configured to do so, a test database with a table and a couple of rows in the table is created at bootstrap time of the mysqlclient utility container. The test database will be created for each of the Mariadb DBs in the mariadb backup restore enabled namespaces. Change-Id: I3afb85debfafde0585abd726fe8b2f999f590294 --- .../templates/bin/_bootstrap.sh.tpl | 5 + .../bin/utility/_create_test_database.sh.tpl | 54 +++++++++++ .../templates/bin/utility/_dbutils.tpl | 94 +++++-------------- .../templates/bin/utility/_mysqlutils.sh.tpl | 81 +++++----------- .../templates/configmap-bin.yaml | 5 + .../templates/deployment-utility.yaml | 10 ++ charts/mysqlclient-utility/values.yaml | 2 + 7 files changed, 125 insertions(+), 126 deletions(-) create mode 100644 charts/mysqlclient-utility/templates/bin/utility/_create_test_database.sh.tpl diff --git a/charts/mysqlclient-utility/templates/bin/_bootstrap.sh.tpl b/charts/mysqlclient-utility/templates/bin/_bootstrap.sh.tpl index 91e3d094..f20eb938 100644 --- a/charts/mysqlclient-utility/templates/bin/_bootstrap.sh.tpl +++ b/charts/mysqlclient-utility/templates/bin/_bootstrap.sh.tpl @@ -14,4 +14,9 @@ limitations under the License. */}} set -ex sudo ./tmp/start.sh + +if [ -f /tmp/create_test_database.sh ]; then + ./tmp/create_test_database.sh +fi + tail -f /var/log/syslog diff --git a/charts/mysqlclient-utility/templates/bin/utility/_create_test_database.sh.tpl b/charts/mysqlclient-utility/templates/bin/utility/_create_test_database.sh.tpl new file mode 100644 index 00000000..d111ce54 --- /dev/null +++ b/charts/mysqlclient-utility/templates/bin/utility/_create_test_database.sh.tpl @@ -0,0 +1,54 @@ +#!/bin/bash + +set -e +x + +trap cleanup EXIT SIGTERM SIGINT + +IFS=', ' read -re -a BACKUP_RESTORE_NAMESPACE_ARRAY <<< "$BACKUP_RESTORE_NAMESPACE_LIST" +ADMIN_USER_CNF=$(mktemp -p /tmp) + +function cleanup { + rm -f "${ADMIN_USER_CNF}" + echo 'Cleanup Finished.' +} + +for NAMESPACE in "${BACKUP_RESTORE_NAMESPACE_ARRAY[@]}"; +do + kubectl -n "$NAMESPACE" get secret mariadb-secrets -o yaml \ + | grep admin_user.cnf | awk '{print $2}' | base64 -d > "${ADMIN_USER_CNF}" + USER=$(grep user "$ADMIN_USER_CNF" | awk '{print $3}') + PASSWD=$(grep password "$ADMIN_USER_CNF" | awk '{print $3}') + PORT=$(grep port "$ADMIN_USER_CNF" | awk '{print $3}') + + MYSQL="mysql \ + -u $USER -p${PASSWD} \ + --host=mariadb.$NAMESPACE.svc.cluster.local \ + --port=$PORT \ + --connect-timeout 10" + + # Verify if test database exists already + DB_ARGS="use ${TEST_DB_NAME}" + if $MYSQL --execute="$DB_ARGS" > /dev/null 2>&1; then + echo "Test database already exists in namespace $NAMESPACE." + else + + # Create test database + DB_ARGS="CREATE DATABASE ${TEST_DB_NAME};" + $MYSQL --execute="$DB_ARGS" + + # Add a table to the test database + DB_ARGS="USE ${TEST_DB_NAME};CREATE TABLE test_table1 \ + ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, user_id int(11) DEFAULT 0, PRIMARY KEY (id) );" + $MYSQL --execute="$DB_ARGS" + + # Add a couple rows to the table of the test database + DB_ARGS="USE ${TEST_DB_NAME};LOCK TABLES test_table1 WRITE \ + ;INSERT INTO test_table1 (name) value ('name') \ + ;UPDATE test_table1 SET user_id=id,name=CONCAT(name,user_id) WHERE id = LAST_INSERT_ID() \ + ;UNLOCK TABLES;" + $MYSQL --execute="$DB_ARGS" + $MYSQL --execute="$DB_ARGS" + + echo "Test database created in namespace $NAMESPACE." + fi +done diff --git a/charts/mysqlclient-utility/templates/bin/utility/_dbutils.tpl b/charts/mysqlclient-utility/templates/bin/utility/_dbutils.tpl index f5c3f7e5..14d03b09 100755 --- a/charts/mysqlclient-utility/templates/bin/utility/_dbutils.tpl +++ b/charts/mysqlclient-utility/templates/bin/utility/_dbutils.tpl @@ -432,11 +432,12 @@ function do_show_schema() { fi } -# Params: -# NOTE: "test_" is automatically prepended before the provided database -# name, in order to prevent accidental modification/deletion of -# an application database. -function do_create_database() { +# Params: +# Column names and types will be hardcoded for now +# NOTE: In order for this function to work, create_test_database in +# values.yaml file needs to be set to true to create a test database +# at bootrap time. +function do_create_table() { CREATE_ARGS=("$@") @@ -449,53 +450,29 @@ function do_create_database() { ensure_ondemand_pod_exists CREATE_ARGS[3]=$ONDEMAND_POD - create_database "${CREATE_ARGS[@]}" - if [[ $? -ne 0 ]]; then - return 1 - fi -} - -# Params: -# Column names and types will be hardcoded for now -# NOTE: "test_" is automatically prepended before the provided database -# name, in order to prevent accidental modification of -# an application database. -function do_create_table() { - - CREATE_ARGS=("$@") - - check_args CREATE_ARGS 3 3 - if [[ $? -ne 0 ]]; then - return 1 - fi - - # Be sure that an ondemand pod is ready (start if not started) - ensure_ondemand_pod_exists - CREATE_ARGS[4]=$ONDEMAND_POD - create_table "${CREATE_ARGS[@]}" if [[ $? -ne 0 ]]; then return 1 fi } -# Params: +# Params:
# The row values are hardcoded for now. -# NOTE: "test_" is automatically prepended before the provided database -# name, in order to prevent accidental modification of -# an application database. +# NOTE: In order for this function to work, create_test_database in +# values.yaml file needs to be set to true to create a test database +# at bootrap time. function do_create_row() { CREATE_ARGS=("$@") - check_args CREATE_ARGS 3 3 + check_args CREATE_ARGS 2 2 if [[ $? -ne 0 ]]; then return 1 fi # Be sure that an ondemand pod is ready (start if not started) ensure_ondemand_pod_exists - CREATE_ARGS[4]=$ONDEMAND_POD + CREATE_ARGS[3]=$ONDEMAND_POD create_row "${CREATE_ARGS[@]}" if [[ $? -ne 0 ]]; then @@ -503,23 +480,23 @@ function do_create_row() { fi } -# Params:
+# Params:
# Where: = is the condition used to find the row to be deleted. -# NOTE: "test_" is automatically prepended before the provided database -# name, in order to prevent accidental modification/deletion of -# an application database. +# NOTE: In order for this function to work, create_test_database in +# values.yaml file needs to be set to true to create a test database +# at bootrap time. function do_delete_row() { DELETE_ARGS=("$@") - check_args DELETE_ARGS 5 5 + check_args DELETE_ARGS 4 4 if [[ $? -ne 0 ]]; then return 1 fi # Be sure that an ondemand pod is ready (start if not started) ensure_ondemand_pod_exists - DELETE_ARGS[6]=$ONDEMAND_POD + DELETE_ARGS[5]=$ONDEMAND_POD delete_row "${DELETE_ARGS[@]}" if [[ $? -ne 0 ]]; then @@ -527,37 +504,14 @@ function do_delete_row() { fi } -# Params: -# NOTE: "test_" is automatically prepended before the provided database -# name, in order to prevent accidental modification/deletion of -# an application database. +# Params: +# NOTE: In order for this function to work, create_test_database in +# values.yaml file needs to be set to true to create a test database +# at bootrap time. function do_delete_table() { DELETE_ARGS=("$@") - check_args DELETE_ARGS 3 3 - if [[ $? -ne 0 ]]; then - return 1 - fi - - # Be sure that an ondemand pod is ready (start if not started) - ensure_ondemand_pod_exists - DELETE_ARGS[4]=$ONDEMAND_POD - - delete_table "${DELETE_ARGS[@]}" - if [[ $? -ne 0 ]]; then - return 1 - fi -} - -# Params: -# NOTE: "test_" is automatically prepended before the provided database -# name, in order to prevent accidental modification/deletion of -# an application database. -function do_delete_database() { - - DELETE_ARGS=("$@") - check_args DELETE_ARGS 2 2 if [[ $? -ne 0 ]]; then return 1 @@ -567,7 +521,7 @@ function do_delete_database() { ensure_ondemand_pod_exists DELETE_ARGS[3]=$ONDEMAND_POD - delete_database "${DELETE_ARGS[@]}" + delete_table "${DELETE_ARGS[@]}" if [[ $? -ne 0 ]]; then return 1 fi @@ -778,12 +732,10 @@ function execute_selection() { "show_tables"|"st") do_show_tables "${ARGS[@]}";; "show_rows"|"sr") do_show_rows "${ARGS[@]}";; "show_schema"|"ss") do_show_schema "${ARGS[@]}";; - "create_test_database"|"ctd") do_create_database "${ARGS[@]}";; "create_test_table"|"ctt") do_create_table "${ARGS[@]}";; "create_test_row"|"ctr") do_create_row "${ARGS[@]}";; "delete_test_row"|"dtr") do_delete_row "${ARGS[@]}";; "delete_test_table"|"dtt") do_delete_table "${ARGS[@]}";; - "delete_test_database"|"dtd") do_delete_database "${ARGS[@]}";; "restore"|"r") do_restore "${ARGS[@]}";; "sql_prompt"|"sql") do_sql_prompt "${ARGS[@]}";; "command_history"|"ch") do_command_history;; diff --git a/charts/mysqlclient-utility/templates/bin/utility/_mysqlutils.sh.tpl b/charts/mysqlclient-utility/templates/bin/utility/_mysqlutils.sh.tpl index 3d346bbb..7441e7d2 100644 --- a/charts/mysqlclient-utility/templates/bin/utility/_mysqlutils.sh.tpl +++ b/charts/mysqlclient-utility/templates/bin/utility/_mysqlutils.sh.tpl @@ -63,96 +63,67 @@ function sql_prompt() { kubectl exec -it -n "${SHOW_ARGS[1]}" "${SHOW_ARGS[2]}" -- ${MYSQL_CMD} } -# Params: -# NOTE: "test_" is automatically prepended before the provided database -# name, in order to prevent accidental modification/deletion of -# an application database. -function create_database() { - - CREATE_ARGS=("$@") - - MYSQL_CMD=$(database_cmd) - DB_ARGS="CREATE DATABASE test_${CREATE_ARGS[2]};" - - # Execute the command in the on-demand pod - kubectl exec -it -n "${CREATE_ARGS[1]}" "${CREATE_ARGS[3]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" -} - -# Params:
-# NOTE: "test_" is automatically prepended before the provided database -# name, in order to prevent accidental modification/deletion of -# an application database. +# Params:
+# Create a table in an existing test database. +# NOTE: In order for this function to work, create_test_database in +# values.yaml file needs to be set to true to create a test database +# at bootstrap time. function create_table() { CREATE_ARGS=("$@") MYSQL_CMD=$(database_cmd) - DB_ARGS="USE test_${CREATE_ARGS[2]};CREATE TABLE ${CREATE_ARGS[3]} \ + DB_ARGS="USE $TEST_DB_NAME;CREATE TABLE ${CREATE_ARGS[2]} \ ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, user_id int(11) DEFAULT 0, PRIMARY KEY (id) );" # Execute the command in the on-demand pod - kubectl exec -it -n "${CREATE_ARGS[1]}" "${CREATE_ARGS[4]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" + kubectl exec -it -n "${CREATE_ARGS[1]}" "${CREATE_ARGS[3]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" } -# Params:
-# NOTE: "test_" is automatically prepended before the provided database -# name, in order to prevent accidental modification/deletion of -# an application database. +# Params:
+# NOTE: In order for this function to work, create_test_database in +# values.yaml file needs to be set to true to create a test database +# at bootstrap time. function create_row() { CREATE_ARGS=("$@") MYSQL_CMD=$(database_cmd) - DB_ARGS="USE test_${CREATE_ARGS[2]};LOCK TABLES ${CREATE_ARGS[3]} WRITE \ - ;INSERT INTO ${CREATE_ARGS[3]} (name) value ('name') \ - ;UPDATE ${CREATE_ARGS[3]} SET user_id=id,name=CONCAT(name,user_id) WHERE id = LAST_INSERT_ID() \ + DB_ARGS="USE $TEST_DB_NAME;LOCK TABLES ${CREATE_ARGS[2]} WRITE \ + ;INSERT INTO ${CREATE_ARGS[2]} (name) value ('name') \ + ;UPDATE ${CREATE_ARGS[2]} SET user_id=id,name=CONCAT(name,user_id) WHERE id = LAST_INSERT_ID() \ ;UNLOCK TABLES;" # Execute the command in the on-demand pod - kubectl exec -it -n "${CREATE_ARGS[1]}" "${CREATE_ARGS[4]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" + kubectl exec -it -n "${CREATE_ARGS[1]}" "${CREATE_ARGS[3]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" } -# Params:
+# Params:
# Where: = is the condition used to find the row to be deleted. -# NOTE: "test_" is automatically prepended before the provided database -# name, in order to prevent accidental modification/deletion of -# an application database. +# NOTE: In order for this function to work, create_test_database in +# values.yaml file needs to be set to true to create a test database +# at bootstrap time. function delete_row() { DELETE_ARGS=("$@") MYSQL_CMD=$(database_cmd) - DB_ARGS="USE test_${DELETE_ARGS[2]};DELETE FROM ${DELETE_ARGS[3]} WHERE ${DELETE_ARGS[4]} = '${DELETE_ARGS[5]}';" + DB_ARGS="USE $TEST_DB_NAME;DELETE FROM ${DELETE_ARGS[2]} WHERE ${DELETE_ARGS[3]} = '${DELETE_ARGS[4]}';" # Execute the command in the on-demand pod - kubectl exec -it -n "${DELETE_ARGS[1]}" "${DELETE_ARGS[6]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" + kubectl exec -it -n "${DELETE_ARGS[1]}" "${DELETE_ARGS[5]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" } -# Params: -# NOTE: "test_" is automatically prepended before the provided database -# name, in order to prevent accidental modification/deletion of -# an application database. +# Params: +# NOTE: In order for this function to work, create_test_database in +# values.yaml file needs to be set to true to create a test database +# at bootstrap time. function delete_table() { DELETE_ARGS=("$@") MYSQL_CMD=$(database_cmd) - DB_ARGS="USE test_${DELETE_ARGS[2]};DROP TABLE IF EXISTS ${DELETE_ARGS[3]};" - - # Execute the command in the on-demand pod - kubectl exec -it -n "${DELETE_ARGS[1]}" "${DELETE_ARGS[4]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" -} - -# Params: -# NOTE: "test_" is automatically prepended before the provided database -# name, in order to prevent accidental modification/deletion of -# an application database. -function delete_database() { - - DELETE_ARGS=("$@") - - MYSQL_CMD=$(database_cmd) - DB_ARGS="DROP DATABASE IF EXISTS test_${DELETE_ARGS[2]};" + DB_ARGS="USE $TEST_DB_NAME;DROP TABLE IF EXISTS ${DELETE_ARGS[2]};" # Execute the command in the on-demand pod kubectl exec -it -n "${DELETE_ARGS[1]}" "${DELETE_ARGS[3]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" diff --git a/charts/mysqlclient-utility/templates/configmap-bin.yaml b/charts/mysqlclient-utility/templates/configmap-bin.yaml index 2468c111..a164e809 100644 --- a/charts/mysqlclient-utility/templates/configmap-bin.yaml +++ b/charts/mysqlclient-utility/templates/configmap-bin.yaml @@ -49,6 +49,11 @@ data: mysqlutils.sh: | {{ tuple "bin/utility/_mysqlutils.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} +{{- if .Values.manifests.create_test_database }} + create_test_database.sh: | +{{ tuple "bin/utility/_create_test_database.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} +{{- end }} + bootstrap.sh: | {{ tuple "bin/_bootstrap.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} diff --git a/charts/mysqlclient-utility/templates/deployment-utility.yaml b/charts/mysqlclient-utility/templates/deployment-utility.yaml index aa280a53..6d1dc163 100644 --- a/charts/mysqlclient-utility/templates/deployment-utility.yaml +++ b/charts/mysqlclient-utility/templates/deployment-utility.yaml @@ -123,6 +123,10 @@ spec: env: - name: BACKUP_RESTORE_NAMESPACE_LIST value: {{ .Values.conf.mariadb_backup_restore.enabled_namespaces | quote }} + {{- if .Values.manifests.create_test_database }} + - name: TEST_DB_NAME + value: {{ .Values.conf.mariadb_backup_restore.test_database_name | quote }} + {{- end }} - name: BACKUP_RESTORE_SCOPE value: "mariadb" volumeMounts: @@ -154,6 +158,12 @@ spec: mountPath: /tmp/mariadb-ondemand-job.sh subPath: mariadb-ondemand-job.sh readOnly: true + {{- if .Values.manifests.create_test_database }} + - name: mysqlclient-utility-bin-utilscli + mountPath: /tmp/create_test_database.sh + subPath: create_test_database.sh + readOnly: true + {{- end }} - name: mysqlclient-utility-sudoers mountPath: /etc/sudoers.d/utilscli-sudo subPath: utilscli-sudo diff --git a/charts/mysqlclient-utility/values.yaml b/charts/mysqlclient-utility/values.yaml index 48d0d1b7..6438b85f 100644 --- a/charts/mysqlclient-utility/values.yaml +++ b/charts/mysqlclient-utility/values.yaml @@ -101,6 +101,7 @@ pod: conf: mariadb_backup_restore: enabled_namespaces: "" + test_database_name: "test_database" secrets: rgw_secret: mariadb-backup-user conf_secret: mariadb-backup-restore @@ -188,3 +189,4 @@ manifests: configmap_etc_client: true configmap_etc_sudoers: true deployment_utility: true + create_test_database: false