-
Notifications
You must be signed in to change notification settings - Fork 138
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
Migrate Helm chart to MariaDB #1230
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"] |
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." | ||
|
||
|
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.