BACKUP_DIR=/opt/planetstack_backups
+DJANGO_17=`python -c "import django; from distutils.version import StrictVersion; print int(StrictVersion(django.get_version()) >= StrictVersion('1.7'))"`
+
cd /opt/planetstack
function ensure_postgres_running {
function syncdb {
echo "Syncing OpenCloud services..."
python /opt/planetstack/manage.py syncdb --noinput
+ if [[ $DJANGO_17 ]]; then
+ echo "Loading initial data from fixture..."
+ python /opt/planetstack/manage.py --noobserver --nomodelpolicy loaddata /opt/planetstack/core/fixtures/initial_data.json
+ fi
+}
+function evolvedb {
+ echo "Evolving OpenCloud services..."
+ python /opt/planetstack/manage.py evolve --hint --execute --noinput
+}
+function migratedb {
+ echo "Migrating OpenCloud services..."
+ python /opt/planetstack/manage.py migrate
}
function stopserver {
echo "Stopping any running OpenCloud Service(s)"
pkill -f "python.*runserver"
}
function runserver {
- echo "Starting OpenCloud Service on $HOSTNAME:8000"
- python manage.py runserver $HOSTNAME:8000&
+ PUBLIC_HOSTNAME=`/opt/planetstack/planetstack-config.py get server_hostname $HOSTNAME`
+ echo "Starting OpenCloud Service on $PUBLIC_HOSTNAME:8000"
+ python manage.py runserver $PUBLIC_HOSTNAME:8000&
}
function dumpdata {
mkdir -p $BACKUP_DIR
FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json"
echo "Saving data to $FN"
- python manage.py dumpdata core hpc syndicate requestrouter --indent 4 > $FN
+ python manage.py dumpdata core hpc syndicate_storage requestrouter -a --indent 4 > $FN
if [[ ! -f $FN ]]; then
echo "FAILED to create $FN"
exit
fi
+ SIZE=$(du -k "$FN" | cut -f 1)
+ if [[ $SIZE -lt 9 ]]; then
+ echo "Dumpdata was empty. Deleting and aborting"
+ rm $FN
+ exit
+ fi
rm -f $BACKUP_DIR/dumpdata-latest.json
ln -s $FN $BACKUP_DIR/dumpdata-latest.json
}
+function genkeys {
+ mkdir -p public_keys
+ mkdir -p private_keys
+ echo "Generating keys"
+ keyczart create --location=private_keys --name="OpenCloud" --purpose=crypt --asymmetric=rsa
+ keyczart addkey --location=private_keys --status=primary --size=1024
+ keyczart pubkey --location=private_keys --destination=public_keys
+ if [[ ! -f public_keys/1 ]]; then
+ echo "FAILED to create keys"
+ exit
+ fi
+}
+
COMMAND=$1
if [ "$COMMAND" = "initdb" ]; then
ensure_postgres_running
createdb
syncdb
- runserver
fi
-if [ "$COMMAND" = "upgradedb" ]; then
+if [ "$COMMAND" = "repairdb" ]; then
stopserver
ensure_postgres_running
dumpdata
- # TODO: This is where we would run migration scripts to upgrade the
+ # TODO: This is where we could run migration scripts to upgrade the
# dumped data to the new models.
mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old
cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json
dropdb
createdb
syncdb
- runserver
+fi
+if [ "$COMMAND" = "restoredb" ]; then
+ if [[ ! -f $BACKUP_DIR/dumpdata-latest.json ]]; then
+ echo There is no dumpdata to restore
+ exit
+ fi
+ stopserver
+ ensure_postgres_running
+ mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old
+ cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json
+ dropdb
+ createdb
+ syncdb
+fi
+if [ "$COMMAND" = "evolvedb" -o "$COMMAND" = "migratedb" ]; then
+ stopserver
+ ensure_postgres_running
+ if [[ $DJANGO_17 ]]; then
+ migratedb
+ else
+ evolvedb
+ fi
fi
if [ "$COMMAND" = "resetdb" ]; then
stopserver
dropdb
createdb
syncdb
- runserver
fi
if [ "$COMMAND" = "syncdb" ]; then
stopserver
syncdb
- runserver
fi
if [ "$COMMAND" = "runserver" ]; then
stopserver
if [ "$COMMAND" = "dumpdata" ]; then
dumpdata
fi
+if [ "$COMMAND" = "genkeys" ]; then
+ genkeys
+fi