X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=init.d%2Fsfa;h=7cb510c3f6549b01098ae97246ea7ddb9f199b25;hb=b9ae62cc0463f168771bf3d4ebf348bb920acb4f;hp=3e733eedd471e8c5c8c1e38c41797ff2ce2c24ca;hpb=1cc5655cb474079e27e5e787b25c7baeea3fce6c;p=sfa.git diff --git a/init.d/sfa b/init.d/sfa index 3e733eed..7cb510c3 100755 --- a/init.d/sfa +++ b/init.d/sfa @@ -59,6 +59,48 @@ function postgresql_check () { return 1 } +# use a single date of this script invocation for the dump_*_db functions. +DATE=$(date +"%Y-%m-%d-%H-%M-%S") + +# Dumps the database - optional argument to specify filename suffix +function dump_sfa_db() { + if [ -n "$1" ] ; then suffix="-$1" ; else suffix="" ; fi + dumpfile=/var/lib/pgsql/backups/$(date +"${SFA_DB_NAME}.${DATE}${suffix}.sql") + pg_dump -U $SFA_DB_USER $SFA_DB_NAME > $dumpfile + check +} + +# Updates the database by applying migration scripts +# using sqlalchemy-migrate for this +# migrate repo is /usr/share/sfa/migrations/ +# and version scripts are thus in /usr/share/sfa/migrations/versions/ +function db_migrate() { + + # only if enabled + [ "$SFA_DB_ENABLED" == 1 ] || return + + alchemy_url="postgresql+psycopg2://${SFA_DB_USER}:${SFA_DB_PASSWORD}@:${SFA_DB_PORT}/${SFA_DB_NAME}" + migrate_repo="/usr/share/sfa/migrations" + # check if under version control, and initialize it otherwise + if ! sqlalchemy-migrate db_version --url=$alchemy_url $migrate_repo >& /dev/null ; then + # at this point it would make sense to + # check that the 'users' table is in the db + # if not, backup/dump, and run a nuke/import + # and then only init the db version to 0 + echo -n $"version init in db ${SFA_DB_NAME}" + sqlalchemy-migrate version_control --url=$alchemy_url $migrate_repo + fi + version_before=$(sqlalchemy-migrate db_version --url=$alchemy_url $migrate_repo) + check + sqlalchemy-migrate upgrade --url=$alchemy_url $migrate_repo 2> /dev/null + check + version_after=$(sqlalchemy-migrate db_version --url=$alchemy_url $migrate_repo) + if [ "$version_before" != "$version_after" -o "$ERRORS" != 0 ] ; then + MESSAGE=$"DB version: $version_before -> $version_after" + echo -n "$MESSAGE" + [ "$ERRORS" == 0 ] && success "$MESSAGE" || failure "$MESSAGE" ; echo + fi +} # Regenerate configuration files - almost verbatim from plc.init function reload () { @@ -126,7 +168,7 @@ function db_start () { ( egrep -v '^(PGDATA=|PGLOG=|PGPORT=)' $postgresql_sysconfig echo "PGDATA=$PGDATA" echo "PGLOG=/var/log/pgsql" - echo "PGPORT=$PLC_DB_PORT" + echo "PGPORT=$SFA_DB_PORT" ) >> $tmp ; mv -f $tmp $postgresql_sysconfig ######## /var/lib/pgsql/data @@ -167,6 +209,10 @@ function db_start () { echo "host $SFA_DB_NAME $SFA_DB_USER 127.0.0.1/32 password" [ -n "$registry_ip" ] && echo "host $SFA_DB_NAME $SFA_DB_USER ${registry_ip}/32 password" ) >>$pghba_conf + + if [ "$SFA_GENERIC_FLAVOUR" == "openstack" ] ; then + [ -n "$registry_ip" ] && echo "host nova nova ${registry_ip}/32 password" >> $pghba_conf + fi # Fix ownership (sed -i changes it) chown postgres:postgres $postgresql_conf $pghba_conf @@ -213,9 +259,6 @@ function db_start () { if ! psql -U $SFA_DB_USER -c "" $SFA_DB_NAME >/dev/null 2>&1 ; then createdb -U postgres --template=template0 --encoding=UNICODE --owner=$SFA_DB_USER $SFA_DB_NAME check - # install db schema - psql -U $SFA_DB_USER -f /usr/share/sfa/sfa.sql $SFA_DB_NAME - check fi check @@ -245,6 +288,7 @@ function start() { reload db_start + db_migrate # install peer certs action $"SFA installing peer certs" daemon /usr/bin/sfa-start.py -t -d $OPTIONS