cloudfoundry-incubator/stratos

View on GitHub
deploy/stratos-ui-release/jobs/backend/templates/post-deploy.erb

Summary

Maintainability
Test Coverage
#!/bin/bash
<% if p('stratos_ui.backend.use_mysql') == true %>
set -e

# TODO check if installation is required
# Check Git is installed
echo "Installing Git and MariaDB Client"
apt-get update
apt-get install -y git mariadb-client

echo "PWD: $PWD"
export GOROOT="/var/vcap/packages/golang/"
export GOPATH="/var/vcap/packages/backend/go"
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH

function execStatement {
    stmt=$1
    echo "Executing: mysql -u <%= p('stratos_ui.backend.mysql_admin_user') %> -h<%= p('stratos_ui.backend.mysql_host') %> -P <%= p('stratos_ui.backend.mysql_port') %> -pxxxxxxxxxxxx -e \"$stmt\""
    mysql -u <%= p('stratos_ui.backend.mysql_admin_user') %> -h<%= p('stratos_ui.backend.mysql_host') %> -P <%= p('stratos_ui.backend.mysql_port') %> -p<%= p('stratos_ui.backend.mysql_admin_password') %> -e "$stmt"

}

echo "Checking if DB exists"

stratosDbExists=$(execStatement  "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '<%= p('stratos_ui.backend.mysql_db') %>';")

# Create DB if neccessary
if [ -z "$stratosDbExists" ] ; then
    echo "Creating database <%= p('stratos_ui.backend.mysql_db') %>"
    execStatement "CREATE DATABASE \"<%= p('stratos_ui.backend.mysql_db') %>\";"
    echo "Creating user <%= p('stratos_ui.backend.mysql_user') %>"
    execStatement "CREATE USER <%= p('stratos_ui.backend.mysql_user') %> IDENTIFIED BY '<%= p('stratos_ui.backend.mysql_user_password') %>';"
    
    echo "Granting privs for <%= p('stratos_ui.backend.mysql_db') %> to <%= p('stratos_ui.backend.mysql_user') %>"
    execStatement "GRANT ALL PRIVILEGES ON DATABASE \"<%= p('stratos_ui.backend.mysql_db') %>\" TO <%= p('stratos_ui.backend.mysql_user') %>;"
else
    echo "<%= p('stratos_ui.backend.mysql_db') %> already exists"
fi

# Migrate the database if necessary
echo "Checking database to see if migration is necessary."

echo "Connection string: <%= p('stratos_ui.backend.mysql_user') %>:<%= p('stratos_ui.backend.mysql_user_password') %>@tcp(<%= p('stratos_ui.backend.mysql_host') %>:<%= p('stratos_ui.backend.mysql_port') %>)/<%= p('stratos_ui.backend.mysql_db') %>?parseTime=true"
# Check the version
echo "Checking database version."
goose -path=/var/vcap/packages/backend/deploy/db -env=bosh-mysql dbversion

# Check the status
echo "Checking database status."
goose -path=/var/vcap/packages/backend/deploy/db -env=bosh-mysql status

# Run migrations
echo "Attempting database migrations."
goose -path=/var/vcap/packages/backend/deploy/db -env=bosh-mysql up

# CHeck the status
echo "Checking database status."
goose -path=/var/vcap/packages/backend/deploy/db -env=bosh-mysql status

# Check the version
echo "Checking database version."
goose -path /var/vcap/packages/backend/deploy/db -env=bosh-mysql dbversion

echo "Database operation(s) complete."


# Check if Upgrade Lock file exists
if [ ! -f "<%= p('stratos_ui.backend.upgrade_volume_path') %>/<%= p('stratos_ui.backend.upgrade_volume_file') %>" ]; then
  exit 1
fi
# Remove the lock file on the shared volume
echo "Removing the <%= p('stratos_ui.backend.upgrade_volume_file') %> file from the shared upgrade volume <%= p('stratos_ui.backend.upgrade_volume_path') %>."
rm <%= p('stratos_ui.backend.upgrade_volume_path') %>/<%= p('stratos_ui.backend.upgrade_volume_file') %> || true

echo "Removed the upgrade lock file."

exit 0
<% end %>