Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally dump the openstack databases #37

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ This is the list of available environmental variables:
- `SOS_EDPM_PROFILES`: List of sos report profiles to use. Empty string to run
them all. Defaults to: `system,storage,virt`
- `SOS_EDPM_PLUGINS`: List of sos report plugins to use. This is optional.
- `OPENSTACK_DATABASES`: comma separated list of OpenStack databases that should
be dumped. It is possible to set it to `ALL` and dump all databases. By default
this env var is unset, hence the database dump is skipped.
- `ADDITIONAL_NAMESPACES`: comma separated list of additional namespaces where
we want to gather the associated resources.

## Development

Expand Down
53 changes: 53 additions & 0 deletions collection-scripts/gather_db
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

# When called from the shell directly
if [[ -z "$DIR_NAME" ]]; then
CALLED=1
DIR_NAME=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
source "${DIR_NAME}/common.sh"
fi

# Default DB DUMP Option
DB_OPT="--single-transaction --complete-insert --skip-lock-tables --lock-tables=0"
DB_DUMP=${BASE_COLLECTION_PATH}/dbs

function dump_db {
local dbpod="$1"
local ns="$2"
local dbpass="$3"
local dbname="$4"
local dump="${4:-openstack_databases}"
run_bg /usr/bin/oc -n $ns rsh -c galera $dbpod mysqldump -uroot -p$dbpass $DB_OPT $dbname > "$DB_DUMP"/$dump.sql
}

# If unset or an empty string return/exit
if [ "${OPENSTACK_DATABASES-unset}" = "unset" ] || [[ -z "${OPENSTACK_DATABASES}" ]]; then
# If no databases options are passed, skip the database dump
echo "Skip Database dump: an empty list is provided"
[[ $CALLED -eq 1 ]] && exit 0
exit 0
fi

# Create the db_dump directory in the BASE_COLLECTION_PATH
mkdir -p "$DB_DUMP"
data=$(/usr/bin/oc get openstackcontrolplane --all-namespaces -o go-template='{{range $ins,$service := .items}}{{printf "%s %s\n" $service.metadata.namespace $service.spec.secret}}{{end}}')
while read -r namespace secret; do
[[ -z "$namespace" || -z "$secret" ]] && break
# get the pwd used to run mysqldump from the galera pod
dbpass=$(/usr/bin/oc get -n $namespace secret/"$secret" -o go-template='{{ index .data "AdminPassword" | base64decode }}')
# select the (first) galera pod (and exclude the galera-cellX database services)
dbpod="$(/usr/bin/oc -n $namespace get pod -l app=galera --no-headers -o=custom-columns=NAME:.metadata.name | grep -v cell | tr '\n' ' ' | awk '{print $1}')"
if [[ "$OPENSTACK_DATABASES" == "ALL" ]]; then
DB_OPT="$DB_OPT --all-databases"
# dump all databases
dump_db "$dbpod" "$namespace" "$dbpass"
else
# Convert the database list to an array
IFS=',' read -r -a OPENSTACK_DATABASES <<< "$OPENSTACK_DATABASES"
# for each service dump the associated database (if exists)
for service in "${OPENSTACK_DATABASES[@]}"; do
dump_db "$dbpod" "$namespace" "$dbpass" "$service"
done
fi
done <<< "$data"
[[ $CALLED -eq 1 ]] && wait_bg
3 changes: 3 additions & 0 deletions collection-scripts/gather_run
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ source "${DIR_NAME}/gather_network"
# get SVC status (inspect ctlplane)
source "${DIR_NAME}/gather_services_status"

# dump the openstack database
source "${DIR_NAME}/gather_db"
#
# Wait for background tasks to complete
wait_bg