Merge "Add capability to create test database in Postgresql"

This commit is contained in:
Zuul 2020-06-30 17:25:43 +00:00 committed by Gerrit Code Review
commit 55a09b539a
7 changed files with 101 additions and 127 deletions

View File

@ -14,4 +14,9 @@ limitations under the License.
*/}} */}}
set -ex set -ex
sudo ./tmp/start.sh sudo ./tmp/start.sh
if [ -f /tmp/create_test_database.sh ]; then
./tmp/create_test_database.sh
fi
tail -f /var/log/syslog tail -f /var/log/syslog

View File

@ -0,0 +1,46 @@
#!/bin/bash
set -e +x
IFS=', ' read -re -a BACKUP_RESTORE_NAMESPACE_ARRAY <<< "$BACKUP_RESTORE_NAMESPACE_LIST"
function database_cmd() {
NAMESPACE=$1
POSTGRES_PWD=$(kubectl get secret -n "$NAMESPACE" postgresql-admin -o yaml | grep POSTGRES_PASSWORD | awk '{print $2}' | base64 -d)
POSTGRES_CREDS="postgresql://postgres:${POSTGRES_PWD}@postgresql.${NAMESPACE}.svc.cluster.local?sslmode=disable"
SQL_CMD="psql $POSTGRES_CREDS"
echo $SQL_CMD
}
for NAMESPACE in "${BACKUP_RESTORE_NAMESPACE_ARRAY[@]}";
do
PSQL=$(database_cmd $NAMESPACE)
# Verify if test database exists already
DB_CMD="\connect ${TEST_DB_NAME}"
if $PSQL -tc "$DB_CMD" > /dev/null 2>&1; then
echo "Test database already exists in namespace $NAMESPACE."
echo "Dropping the database, then will re-create it."
$PSQL -tc "DROP DATABASE ${TEST_DB_NAME};"
fi
# Create test database
DB_CMD="CREATE DATABASE ${TEST_DB_NAME};"
$PSQL -tc "$DB_CMD"
# Add a table to the test database
$PSQL << EOF
\connect ${TEST_DB_NAME};
CREATE TABLE test_table1
( name character varying (255), age integer NOT NULL );
EOF
# Add a couple rows to the table of the test database
$PSQL << EOF
\connect ${TEST_DB_NAME};
INSERT INTO test_table1 VALUES ( 'name0', '0' );
INSERT INTO test_table1 VALUES ( 'name1', '1' );
EOF
done

View File

@ -422,36 +422,14 @@ function do_show_schema() {
fi fi
} }
# Params: [namespace] <database> # Params: [namespace] <tablename>
# 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() {
CREATE_ARGS=("$@")
check_args CREATE_ARGS 1 2
if [[ $? -ne 0 ]]; then
return 1
fi
CREATE_ARGS=("${CREATE_ARGS[@]:0:1}" "$NAMESPACE" "${CREATE_ARGS[@]:1}")
create_database "${CREATE_ARGS[@]}"
if [[ $? -ne 0 ]]; then
return 1
fi
}
# Params: [namespace] <database> <tablename>
# Column names and types will be hardcoded for now # Column names and types will be hardcoded for now
# NOTE: "test_" is automatically prepended before the provided database # NOTE: Database is always a pre-provisioned database.
# name, in order to prevent accidental modification of
# an application database.
function do_create_table() { function do_create_table() {
CREATE_ARGS=("$@") CREATE_ARGS=("$@")
check_args CREATE_ARGS 2 3 check_args CREATE_ARGS 1 2
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
return 1 return 1
fi fi
@ -463,16 +441,17 @@ function do_create_table() {
fi fi
} }
# Params: [namespace] <database> <table> # Params: [namespace] <table>
# The row values are hardcoded for now. # The row values are hardcoded for now.
# NOTE: "test_" is automatically prepended before the provided database # NOTE: Database is always a pre-provisioned database.
# name, in order to prevent accidental modification of
# an application database.
function do_create_row() { function do_create_row() {
CREATE_ARGS=("$@") CREATE_ARGS=("$@")
# There could be any number of arguments, so no check_args call here check_args CREATE_ARGS 1 2
if [[ $? -ne 0 ]]; then
return 1
fi
setup_namespace "${CREATE_ARGS[1]}" setup_namespace "${CREATE_ARGS[1]}"
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
@ -486,16 +465,14 @@ function do_create_row() {
fi fi
} }
# Params: [namespace] <database> <table> <colname> <value> # Params: [namespace] <table> <colname> <value>
# Where: <colname> = <value> is the condition used to find the row to be deleted. # Where: <colname> = <value> is the condition used to find the row to be deleted.
# NOTE: "test_" is automatically prepended before the provided database # NOTE: Database is always a pre-provisioned database.
# name, in order to prevent accidental modification/deletion of
# an application database.
function do_delete_row() { function do_delete_row() {
DELETE_ARGS=("$@") DELETE_ARGS=("$@")
check_args DELETE_ARGS 5 6 check_args DELETE_ARGS 3 4
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
return 1 return 1
fi fi
@ -508,35 +485,12 @@ function do_delete_row() {
fi fi
} }
# Params: [namespace] <database> <tablename> # Params: [namespace] <tablename>
# NOTE: "test_" is automatically prepended before the provided database # NOTE: Database is always a pre-provisioned database.
# name, in order to prevent accidental modification/deletion of
# an application database.
function do_delete_table() { function do_delete_table() {
DELETE_ARGS=("$@") DELETE_ARGS=("$@")
check_args DELETE_ARGS 2 3
if [[ $? -ne 0 ]]; then
return 1
fi
DELETE_ARGS=("${DELETE_ARGS[@]:0:1}" "$NAMESPACE" "${DELETE_ARGS[@]:1}")
delete_table "${DELETE_ARGS[@]}"
if [[ $? -ne 0 ]]; then
return 1
fi
}
# Params: [namespace] <database>
# 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 1 2 check_args DELETE_ARGS 1 2
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
return 1 return 1
@ -544,7 +498,7 @@ function do_delete_database() {
DELETE_ARGS=("${DELETE_ARGS[@]:0:1}" "$NAMESPACE" "${DELETE_ARGS[@]:1}") DELETE_ARGS=("${DELETE_ARGS[@]:0:1}" "$NAMESPACE" "${DELETE_ARGS[@]:1}")
delete_database "${DELETE_ARGS[@]}" delete_table "${DELETE_ARGS[@]}"
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
return 1 return 1
fi fi
@ -757,12 +711,10 @@ function execute_selection() {
"show_tables"|"st") do_show_tables "${ARGS[@]}";; "show_tables"|"st") do_show_tables "${ARGS[@]}";;
"show_rows"|"sr") do_show_rows "${ARGS[@]}";; "show_rows"|"sr") do_show_rows "${ARGS[@]}";;
"show_schema"|"ss") do_show_schema "${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_table"|"ctt") do_create_table "${ARGS[@]}";;
"create_test_row"|"ctr") do_create_row "${ARGS[@]}";; "create_test_row"|"ctr") do_create_row "${ARGS[@]}";;
"delete_test_row"|"dtr") do_delete_row "${ARGS[@]}";; "delete_test_row"|"dtr") do_delete_row "${ARGS[@]}";;
"delete_test_table"|"dtt") do_delete_table "${ARGS[@]}";; "delete_test_table"|"dtt") do_delete_table "${ARGS[@]}";;
"delete_test_database"|"dtd") do_delete_database "${ARGS[@]}";;
"restore"|"r") do_restore "${ARGS[@]}";; "restore"|"r") do_restore "${ARGS[@]}";;
"sql_prompt"|"sql") do_sql_prompt "${ARGS[@]}";; "sql_prompt"|"sql") do_sql_prompt "${ARGS[@]}";;
"command_history"|"ch") do_command_history;; "command_history"|"ch") do_command_history;;

View File

@ -88,36 +88,16 @@ function sql_prompt() {
${DB_CMD} ${DB_CMD}
} }
# Params: <namespace> <tablename>
# Params: <namespace> <database>
# 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=("$@")
NAMESPACE=${CREATE_ARGS[1]}
DATABASE="test_"
DATABASE+=${CREATE_ARGS[2]}
DB_CMD=$(database_cmd $NAMESPACE)
${DB_CMD} -c "CREATE DATABASE ${DATABASE};"
}
# Params: <namespace> <database> <tablename>
# Column names and types will be hardcoded for now # Column names and types will be hardcoded for now
# NOTE: "test_" is automatically prepended before the provided database # NOTE: Database is always a pre-provisioned database
# name, in order to prevent accidental modification of
# an application database.
function create_table() { function create_table() {
CREATE_ARGS=("$@") CREATE_ARGS=("$@")
NAMESPACE=${CREATE_ARGS[1]} NAMESPACE=${CREATE_ARGS[1]}
DATABASE="test_" DATABASE=${TEST_DB_NAME}
DATABASE+=${CREATE_ARGS[2]} TABLENAME=${CREATE_ARGS[2]}
TABLENAME=${CREATE_ARGS[3]}
CREATE_CMD="CREATE TABLE ${TABLENAME} ( name character varying (255), age integer NOT NULL )" CREATE_CMD="CREATE TABLE ${TABLENAME} ( name character varying (255), age integer NOT NULL )"
@ -129,19 +109,16 @@ function create_table() {
EOF EOF
} }
# Params: <namespace> <database> <table> # Params: <namespace> <table>
# The row values are hardcoded for now. # The row values are hardcoded for now.
# NOTE: "test_" is automatically prepended before the provided database # NOTE: Database is always a pre-provisioned database
# name, in order to prevent accidental modification of
# an application database.
function create_row() { function create_row() {
CREATE_ARGS=("$@") CREATE_ARGS=("$@")
NAMESPACE=${CREATE_ARGS[1]} NAMESPACE=${CREATE_ARGS[1]}
DATABASE="test_" DATABASE=${TEST_DB_NAME}
DATABASE+=${CREATE_ARGS[2]} TABLENAME=${CREATE_ARGS[2]}
TABLENAME=${CREATE_ARGS[3]}
DB_CMD=$(database_cmd $NAMESPACE) DB_CMD=$(database_cmd $NAMESPACE)
@ -156,21 +133,18 @@ function create_row() {
EOF EOF
} }
# Params: <namespace> <database> <table> <colname> <value> # Params: <namespace> <table> <colname> <value>
# Where: <colname> = <value> is the condition used to find the row to be deleted. # Where: <colname> = <value> is the condition used to find the row to be deleted.
# NOTE: "test_" is automatically prepended before the provided database # NOTE: Database is always a pre-provisioned database
# name, in order to prevent accidental modification/deletion of
# an application database.
function delete_row() { function delete_row() {
DELETE_ARGS=("$@") DELETE_ARGS=("$@")
NAMESPACE=${DELETE_ARGS[1]} NAMESPACE=${DELETE_ARGS[1]}
DATABASE="test_" DATABASE=${TEST_DB_NAME}
DATABASE+=${DELETE_ARGS[2]} TABLENAME=${DELETE_ARGS[2]}
TABLENAME=${DELETE_ARGS[3]} COLNAME=${DELETE_ARGS[3]}
COLNAME=${DELETE_ARGS[4]} VALUE=${DELETE_ARGS[4]}
VALUE=${DELETE_ARGS[5]}
DELETE_CMD="DELETE FROM ${TABLENAME} WHERE ${COLNAME} = '${VALUE}'" DELETE_CMD="DELETE FROM ${TABLENAME} WHERE ${COLNAME} = '${VALUE}'"
@ -182,18 +156,15 @@ function delete_row() {
EOF EOF
} }
# Params: <namespace> <database> <tablename> # Params: <namespace> <tablename>
# NOTE: "test_" is automatically prepended before the provided database # NOTE: Database is always a pre-provisioned database
# name, in order to prevent accidental modification/deletion of
# an application database.
function delete_table() { function delete_table() {
DELETE_ARGS=("$@") DELETE_ARGS=("$@")
NAMESPACE=${DELETE_ARGS[1]} NAMESPACE=${DELETE_ARGS[1]}
DATABASE="test_" DATABASE=${TEST_DB_NAME}
DATABASE+=${DELETE_ARGS[2]} TABLENAME=${DELETE_ARGS[2]}
TABLENAME=${DELETE_ARGS[3]}
DB_CMD=$(database_cmd $NAMESPACE) DB_CMD=$(database_cmd $NAMESPACE)
@ -202,20 +173,3 @@ function delete_table() {
DROP TABLE IF EXISTS ${TABLENAME}; DROP TABLE IF EXISTS ${TABLENAME};
EOF EOF
} }
# Params: <namespace> <database>
# 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=("$@")
NAMESPACE=${DELETE_ARGS[1]}
DATABASE="test_"
DATABASE+=${DELETE_ARGS[2]}
DB_CMD=$(database_cmd $NAMESPACE)
${DB_CMD} -c "DROP DATABASE IF EXISTS ${DATABASE};"
}

View File

@ -31,6 +31,11 @@ data:
pgutils.sh: | pgutils.sh: |
{{ tuple "bin/utility/_pgutils.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} {{ tuple "bin/utility/_pgutils.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 }}
pg-ondemand-job.sh: | pg-ondemand-job.sh: |
{{ tuple "bin/utility/_pg_ondemand_job.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} {{ tuple "bin/utility/_pg_ondemand_job.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}

View File

@ -124,6 +124,10 @@ spec:
value: "postgresql" value: "postgresql"
- name: BACKUP_RESTORE_NAMESPACE_LIST - name: BACKUP_RESTORE_NAMESPACE_LIST
value: {{ .Values.conf.postgresql_backup_restore.enabled_namespaces | quote }} value: {{ .Values.conf.postgresql_backup_restore.enabled_namespaces | quote }}
{{- if .Values.manifests.create_test_database }}
- name: TEST_DB_NAME
value: {{ .Values.conf.postgresql_backup_restore.test_database_name | quote }}
{{- end }}
volumeMounts: volumeMounts:
- name: postgresql-utility-bin - name: postgresql-utility-bin
mountPath: /tmp/bootstrap.sh mountPath: /tmp/bootstrap.sh
@ -153,6 +157,12 @@ spec:
mountPath: /tmp/pg-ondemand-job.sh mountPath: /tmp/pg-ondemand-job.sh
subPath: pg-ondemand-job.sh subPath: pg-ondemand-job.sh
readOnly: true readOnly: true
{{- if .Values.manifests.create_test_database }}
- name: postgresql-utility-bin
mountPath: /tmp/create_test_database.sh
subPath: create_test_database.sh
readOnly: true
{{- end }}
- name: postgresql-utility-sudoers - name: postgresql-utility-sudoers
mountPath: /etc/sudoers.d/utilscli-sudo mountPath: /etc/sudoers.d/utilscli-sudo
subPath: utilscli-sudo subPath: utilscli-sudo

View File

@ -105,6 +105,7 @@ secrets:
conf: conf:
postgresql_backup_restore: postgresql_backup_restore:
enabled_namespaces: "openstack" enabled_namespaces: "openstack"
test_database_name: "test_database"
secrets: secrets:
rgw_secret: postgresql-backup-user rgw_secret: postgresql-backup-user
conf_secret: postgresql-backup-restore conf_secret: postgresql-backup-restore
@ -213,3 +214,4 @@ manifests:
secret_etc: true secret_etc: true
secret_admin: true secret_admin: true
deployment_utility: true deployment_utility: true
create_test_database: false