This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
Migrate Helm chart to MariaDB #1230
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
FROM alpine | ||
FROM golang:1.8-alpine | ||
|
||
RUN apk update && \ | ||
apk add postgresql-client | ||
|
||
apk add postgresql-client mariadb-client git gcc musl-dev | ||
RUN go get bitbucket.org/liamstask/goose/lib/goose | ||
RUN go get github.com/go-sql-driver/mysql | ||
COPY goose /usr/local/bin/ | ||
COPY deploy/db/dbconf.yml db/dbconf.yml | ||
COPY deploy/db/migrations db/migrations | ||
COPY deploy/db/scripts/run-postflight-job.k8s.sh /run-postflight-job.sh | ||
|
||
CMD ["/run-postflight-job.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,81 @@ | ||
#!/bin/sh | ||
set -e | ||
|
||
execStatement() { | ||
function execStatement { | ||
stmt=$1 | ||
PGPASSFILE=/tmp/pgpass psql -U $POSTGRES_USER -h $PGSQL_HOST -p $PGSQL_PORT -d postgres -w -tc "$stmt" | ||
} | ||
|
||
execBackupRestore() { | ||
orig_server="hsc-stproxy-int" | ||
dest_server=$PGSQL_HOST | ||
bkup="pg_dump -U $PGSQL_USER -h $orig_server -p $PGSQL_PORT -w $PGSQL_DATABASE" | ||
stor="psql -U $PGSQL_USER -h $dest_server -p $PGSQL_PORT -w $PGSQL_DATABASE" | ||
if [ "$DATABASE_PROVIDER" = "mysql" ]; then | ||
echo "Executing: mysql -u $DB_ADMIN_USER -h $DB_HOST -P $DB_PORT -p$DB_ADMIN_PASSWORD -e $stmt" | ||
mysql -u $DB_ADMIN_USER -h $DB_HOST -P $DB_PORT -p$DB_ADMIN_PASSWORD -e $stmt | ||
fi | ||
|
||
PGPASSFILE=/tmp/pgpass $bkup | PGPASSFILE=/tmp/pgpass $stor | ||
if [ "$DATABASE_PROVIDER" = "pgsql" ]; then | ||
echo "Executing: PGPASSFILE=/tmp/pgpass psql -U $DB_USER -h $DB_HOST -p $DB_PORT -d postgres -w -tc \"$stmt\"" | ||
PGPASSFILE=/tmp/pgpass psql -U $DB_USER -h $DB_HOST -p $DB_PORT -d postgres -w -tc "$stmt" | ||
fi | ||
} | ||
|
||
# Save the superuser info to file to ensure secure access | ||
echo "*:$PGSQL_PORT:postgres:$POSTGRES_USER:$(cat $POSTGRES_PASSWORD_FILE)" > /tmp/pgpass | ||
echo "*:$PGSQL_PORT:$PGSQL_DATABASE:$PGSQL_USER:$(cat $PGSQL_PASSWORDFILE)" >> /tmp/pgpass | ||
chmod 0600 /tmp/pgpass | ||
if [ "$DATABASE_PROVIDER" = "pgsql" ]; then | ||
# Save the superuser info to file to ensure secure access | ||
echo "*:$DB_PORT:postgres:$DB_USER:$(cat $DB_PASSWORD_FILE)" > /tmp/pgpass | ||
echo "*:$DB_PORT:$DB_DATABASE_NAME:$DB_USER:$(cat $DB_PASSWORDFILE)" >> /tmp/pgpass | ||
chmod 0600 /tmp/pgpass | ||
stratosDbExists=$(execStatement "SELECT 1 FROM pg_database WHERE datname = '$DB_DATABASE_NAME';") | ||
# Get db user password from secrets file | ||
PWD=$(cat $DB_PASSWORDFILE) | ||
DBCONF_KEY=k8s | ||
DB_PASSWORD=$PWD | ||
fi | ||
|
||
# Get db user password from secrets file | ||
PWD=$(cat $PGSQL_PASSWORDFILE) | ||
if [ "$DATABASE_PROVIDER" = "mysql" ]; then | ||
echo "DB Provider is MYSQL" | ||
stratosDbExists=$(execStatement "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$DB_DATABASE_NAME';") | ||
DBCONF_KEY=mariadb-k8s | ||
fi | ||
|
||
# Create the database if necessary | ||
stratosDbExists=$(execStatement "SELECT 1 FROM pg_database WHERE datname = '$PGSQL_DATABASE';") | ||
# Create DB if neccessary | ||
if [ -z "$stratosDbExists" ] ; then | ||
echo "Creating database $PGSQL_DATABASE" | ||
execStatement "CREATE DATABASE \"$PGSQL_DATABASE\";" | ||
echo "Creating user $PGSQL_USER" | ||
execStatement "CREATE USER $PGSQL_USER WITH ENCRYPTED PASSWORD '$PWD';" | ||
echo "Granting privs for $PGSQL_DATABASE to $PGSQL_USER" | ||
execStatement "GRANT ALL PRIVILEGES ON DATABASE \"$PGSQL_DATABASE\" TO $PGSQL_USER;" | ||
echo "Creating database $DB_DATABASE_NAME" | ||
execStatement "CREATE DATABASE \"$DB_DATABASE_NAME\";" | ||
echo "Creating user $DB_USER" | ||
if [ "$DATABASE_PROVIDER" = "pgsql" ]; then | ||
execStatement "CREATE USER $DB_USER WITH ENCRYPTED PASSWORD '$PWD';" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DB_PASSWD There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated! |
||
fi | ||
if [ "$DATABASE_PROVIDER" = "mysql" ]; then | ||
execStatement "CREATE USER $DB_USER IDENTIFIED BY '$DB_PASSWORD';" | ||
fi | ||
|
||
echo "Granting privs for $DB_DATABASE_NAME to $DB_USER" | ||
execStatement "GRANT ALL PRIVILEGES ON DATABASE \"$DB_DATABASE_NAME\" TO $DB_USER;" | ||
DBCONF_KEY=mariadb-k8s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
else | ||
echo "$PGSQL_DATABASE already exists" | ||
echo "$DB_DATABASE_NAME already exists" | ||
fi | ||
|
||
# Backup existing database from stolon cluster and restore it to the single instance | ||
#execBackupRestore | ||
|
||
# Migrate the database if necessary | ||
echo "Checking database to see if migration is necessary." | ||
|
||
echo "DBCONFIG: $DBCONF_KEY" | ||
echo "Connection string: $DB_USER:$DB_PASSWORD@tcp($DB_HOST:$DB_PORT)/$DB_DATABASE_NAME?parseTime=true" | ||
# Check the version | ||
echo "Checking database version." | ||
PGSQL_PASSWORD=$PWD goose --env=k8s dbversion | ||
goose --env=$DBCONF_KEY dbversion | ||
|
||
# Check the status | ||
echo "Checking database status." | ||
PGSQL_PASSWORD=$PWD goose --env=k8s status | ||
goose --env=$DBCONF_KEY status | ||
|
||
# Run migrations | ||
echo "Attempting database migrations." | ||
PGSQL_PASSWORD=$PWD goose --env=k8s up | ||
goose --env=$DBCONF_KEY up | ||
|
||
# CHeck the status | ||
echo "Checking database status." | ||
PGSQL_PASSWORD=$PWD goose --env=k8s status | ||
goose --env=$DBCONF_KEY status | ||
|
||
# Check the version | ||
echo "Checking database version." | ||
PGSQL_PASSWORD=$PWD goose --env=k8s dbversion | ||
goose --env=$DBCONF_KEY dbversion | ||
|
||
echo "Database operation(s) complete." | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bash
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function
keyword works in sh, and bash isn't available in the postflight container.