#!/bin/bash
set -eux

RUN_DB_SYNC=${1:-""}
function run_db_sync() {
    [ -n "$RUN_DB_SYNC" ]
}

db_pass=$(os-apply-config --key db-password)

STATEDIR=/mnt/state/var/lib/mysql
DONE=${STATEDIR}/db.initialized
if [ -e ${DONE} ] ; then
  echo "DB files already initialized."
else
  for FILE in ibdata1 ib_logfile0 ib_logfile1 ; do
    if [ -e $STATEDIR/$FILE ] ; then
      mv $STATEDIR/$FILE $STATEDIR/$FILE.bak
    fi
  done
  # Sometimes packages make the files ownership wrong
  chown -R mysql.mysql ${STATEDIR}
  touch ${DONE}

  os-svc-restart -n mysql
fi

PATH=/usr/local/bin:$PATH

os-db-create keystone keystone $db_pass
run_db_sync && keystone-manage db_sync

if which cinder-manage 1>/dev/null 2>&1; then
    os-db-create cinder cinder $db_pass
    run_db_sync && cinder-manage db sync
fi

if which ironic-dbsync 1>/dev/null 2>&1; then
    os-db-create ironic ironic $db_pass
    run_db_sync && ironic-dbsync --config-file /etc/ironic/ironic.conf
fi

if which tuskar-dbsync 1>/dev/null 2>&1; then
    os-db-create tuskar tuskar $db_pass
    run_db_sync && tuskar-dbsync --config-file /etc/tuskar/tuskar.conf
fi

if which ceilometer-dbsync 1>/dev/null 2>&1; then
    os-db-create ceilometer ceilometer $db_pass
    run_db_sync && ceilometer-dbsync --config-file /etc/ceilometer/ceilometer.conf
fi

os-db-create nova nova $db_pass
run_db_sync && nova-manage db sync

os-db-create nova_bm nova $db_pass
run_db_sync && nova-baremetal-manage db sync

os-db-create glance glance $db_pass
run_db_sync && glance-manage db_sync

os-db-create heat heat $db_pass
run_db_sync && heat-manage db_sync

os-db-create ovs_neutron neutron $db_pass
neutron-db-manage --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugins/ml2/ml2_conf.ini upgrade head
