Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | if [ -z "$1" ]; then |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 4 | echo usage: $0 "[initdb | createdb | dropdb | syncdb | runserver | resetdb | dumpdata]" |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 5 | exit |
| 6 | fi |
| 7 | |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame^] | 8 | BACKUP_DIR=/opt/planetstack_backups |
| 9 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 10 | cd /opt/planetstack |
| 11 | |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame^] | 12 | function ensure_postgres_running { |
| 13 | # "sudo -u postgres pg_ctl -D /var/lib/postgres/data status" doesn't work |
| 14 | # right on Vicci, so let's try to detect it by seeing if the port is |
| 15 | # being listened on |
| 16 | |
| 17 | netstat -nl | grep -i ":5432 " |
| 18 | if [[ $? == 0 ]]; then |
| 19 | echo "Postgres is already running" |
| 20 | return |
| 21 | fi |
| 22 | |
| 23 | /sbin/service postgresql initdb |
| 24 | /sbin/service postgresql start |
| 25 | /sbin/chkconfig postgresql on |
| 26 | |
| 27 | netstat -nl | grep -i ":5432 " |
| 28 | if [[ $? != 0 ]]; then |
| 29 | # it's still not running |
| 30 | echo "Trying fallback mechanism to start Postgres" |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 31 | sudo -u postgres initdb -D /var/lib/pgsql/data/ |
| 32 | sudo -u postgres pg_ctl -D /var/lib/pgsql/data -l logfile start |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 33 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame^] | 34 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 35 | } |
| 36 | function createdb { |
| 37 | echo "Creating OpenCloud database..." |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame^] | 38 | sudo -u postgres createdb planetstack |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 39 | } |
| 40 | function dropdb { |
| 41 | echo "Dropping OpenCloud database..." |
| 42 | sudo -u postgres dropdb planetstack |
| 43 | } |
| 44 | function syncdb { |
| 45 | echo "Syncing OpenCloud services..." |
| 46 | python /opt/planetstack/manage.py syncdb --noinput |
| 47 | } |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame^] | 48 | function stopserver { |
| 49 | echo "Stopping any running OpenCloud Service(s)" |
| 50 | pkill -f "python.*runserver" |
| 51 | } |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 52 | function runserver { |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 53 | echo "Starting OpenCloud Service on $HOSTNAME:8000" |
| 54 | python manage.py runserver $HOSTNAME:8000& |
| 55 | } |
| 56 | |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 57 | function dumpdata { |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame^] | 58 | mkdir -p $BACKUP_DIR |
| 59 | FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json" |
| 60 | echo "Saving data to $FN" |
| 61 | python manage.py dumpdata core hpc syndicate requestrouter --indent 4 > $FN |
| 62 | if [[ ! -f $FN ]]; then |
| 63 | echo "FAILED to create $FN" |
| 64 | exit |
| 65 | fi |
| 66 | rm -f $BACKUP_DIR/dumpdata-latest.json |
| 67 | ln -s $FN $BACKUP_DIR/dumpdata-latest.json |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 70 | COMMAND=$1 |
| 71 | |
| 72 | if [ "$COMMAND" = "initdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame^] | 73 | stopserver |
| 74 | ensure_postgres_running |
| 75 | createdb |
| 76 | syncdb |
| 77 | runserver |
| 78 | fi |
| 79 | if [ "$COMMAND" = "upgradedb" ]; then |
| 80 | stopserver |
| 81 | ensure_postgres_running |
| 82 | dumpdata |
| 83 | # TODO: This is where we would run migration scripts to upgrade the |
| 84 | # dumped data to the new models. |
| 85 | mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old |
| 86 | cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json |
| 87 | dropdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 88 | createdb |
| 89 | syncdb |
| 90 | runserver |
| 91 | fi |
| 92 | if [ "$COMMAND" = "resetdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame^] | 93 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 94 | dropdb |
| 95 | createdb |
| 96 | syncdb |
| 97 | runserver |
| 98 | fi |
| 99 | if [ "$COMMAND" = "syncdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame^] | 100 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 101 | syncdb |
| 102 | runserver |
| 103 | fi |
| 104 | if [ "$COMMAND" = "runserver" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame^] | 105 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 106 | runserver |
| 107 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame^] | 108 | if [ "$COMMAND" = "stopserver" ]; then |
| 109 | stopserver |
| 110 | fi |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 111 | if [ "$COMMAND" = "dumpdata" ]; then |
| 112 | dumpdata |
| 113 | fi |