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
sudo ./tmp/start.sh
if [ -f /tmp/create_test_database.sh ]; then
./tmp/create_test_database.sh
fi
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
}
# 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_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>
# Params: [namespace] <tablename>
# 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.
# NOTE: Database is always a pre-provisioned database.
function do_create_table() {
CREATE_ARGS=("$@")
check_args CREATE_ARGS 2 3
check_args CREATE_ARGS 1 2
if [[ $? -ne 0 ]]; then
return 1
fi
@ -463,16 +441,17 @@ function do_create_table() {
fi
}
# Params: [namespace] <database> <table>
# Params: [namespace] <table>
# 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: Database is always a pre-provisioned database.
function do_create_row() {
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]}"
if [[ $? -ne 0 ]]; then
@ -486,16 +465,14 @@ function do_create_row() {
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.
# NOTE: "test_" is automatically prepended before the provided database
# name, in order to prevent accidental modification/deletion of
# an application database.
# NOTE: Database is always a pre-provisioned database.
function do_delete_row() {
DELETE_ARGS=("$@")
check_args DELETE_ARGS 5 6
check_args DELETE_ARGS 3 4
if [[ $? -ne 0 ]]; then
return 1
fi
@ -508,35 +485,12 @@ function do_delete_row() {
fi
}
# Params: [namespace] <database> <tablename>
# NOTE: "test_" is automatically prepended before the provided database
# name, in order to prevent accidental modification/deletion of
# an application database.
# Params: [namespace] <tablename>
# NOTE: Database is always a pre-provisioned database.
function do_delete_table() {
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
if [[ $? -ne 0 ]]; then
return 1
@ -544,7 +498,7 @@ function do_delete_database() {
DELETE_ARGS=("${DELETE_ARGS[@]:0:1}" "$NAMESPACE" "${DELETE_ARGS[@]:1}")
delete_database "${DELETE_ARGS[@]}"
delete_table "${DELETE_ARGS[@]}"
if [[ $? -ne 0 ]]; then
return 1
fi
@ -757,12 +711,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;;

View File

@ -88,36 +88,16 @@ function sql_prompt() {
${DB_CMD}
}
# 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>
# Params: <namespace> <tablename>
# 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.
# NOTE: Database is always a pre-provisioned database
function create_table() {
CREATE_ARGS=("$@")
NAMESPACE=${CREATE_ARGS[1]}
DATABASE="test_"
DATABASE+=${CREATE_ARGS[2]}
TABLENAME=${CREATE_ARGS[3]}
DATABASE=${TEST_DB_NAME}
TABLENAME=${CREATE_ARGS[2]}
CREATE_CMD="CREATE TABLE ${TABLENAME} ( name character varying (255), age integer NOT NULL )"
@ -129,19 +109,16 @@ function create_table() {
EOF
}
# Params: <namespace> <database> <table>
# Params: <namespace> <table>
# 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: Database is always a pre-provisioned database
function create_row() {
CREATE_ARGS=("$@")
NAMESPACE=${CREATE_ARGS[1]}
DATABASE="test_"
DATABASE+=${CREATE_ARGS[2]}
TABLENAME=${CREATE_ARGS[3]}
DATABASE=${TEST_DB_NAME}
TABLENAME=${CREATE_ARGS[2]}
DB_CMD=$(database_cmd $NAMESPACE)
@ -156,21 +133,18 @@ function create_row() {
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.
# NOTE: "test_" is automatically prepended before the provided database
# name, in order to prevent accidental modification/deletion of
# an application database.
# NOTE: Database is always a pre-provisioned database
function delete_row() {
DELETE_ARGS=("$@")
NAMESPACE=${DELETE_ARGS[1]}
DATABASE="test_"
DATABASE+=${DELETE_ARGS[2]}
TABLENAME=${DELETE_ARGS[3]}
COLNAME=${DELETE_ARGS[4]}
VALUE=${DELETE_ARGS[5]}
DATABASE=${TEST_DB_NAME}
TABLENAME=${DELETE_ARGS[2]}
COLNAME=${DELETE_ARGS[3]}
VALUE=${DELETE_ARGS[4]}
DELETE_CMD="DELETE FROM ${TABLENAME} WHERE ${COLNAME} = '${VALUE}'"
@ -182,18 +156,15 @@ function delete_row() {
EOF
}
# Params: <namespace> <database> <tablename>
# NOTE: "test_" is automatically prepended before the provided database
# name, in order to prevent accidental modification/deletion of
# an application database.
# Params: <namespace> <tablename>
# NOTE: Database is always a pre-provisioned database
function delete_table() {
DELETE_ARGS=("$@")
NAMESPACE=${DELETE_ARGS[1]}
DATABASE="test_"
DATABASE+=${DELETE_ARGS[2]}
TABLENAME=${DELETE_ARGS[3]}
DATABASE=${TEST_DB_NAME}
TABLENAME=${DELETE_ARGS[2]}
DB_CMD=$(database_cmd $NAMESPACE)
@ -202,20 +173,3 @@ function delete_table() {
DROP TABLE IF EXISTS ${TABLENAME};
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: |
{{ 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: |
{{ tuple "bin/utility/_pg_ondemand_job.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}

View File

@ -124,6 +124,10 @@ spec:
value: "postgresql"
- name: BACKUP_RESTORE_NAMESPACE_LIST
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:
- name: postgresql-utility-bin
mountPath: /tmp/bootstrap.sh
@ -153,6 +157,12 @@ spec:
mountPath: /tmp/pg-ondemand-job.sh
subPath: pg-ondemand-job.sh
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
mountPath: /etc/sudoers.d/utilscli-sudo
subPath: utilscli-sudo

View File

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