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 | |
Scott Baker | 0245137 | 2014-09-16 09:46:35 -0700 | [diff] [blame] | 10 | DJANGO_17=`python -c "import django; from distutils.version import StrictVersion; print int(StrictVersion(django.get_version()) >= StrictVersion('1.7'))"` |
| 11 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 12 | cd /opt/planetstack |
| 13 | |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 14 | function ensure_postgres_running { |
| 15 | # "sudo -u postgres pg_ctl -D /var/lib/postgres/data status" doesn't work |
| 16 | # right on Vicci, so let's try to detect it by seeing if the port is |
| 17 | # being listened on |
| 18 | |
| 19 | netstat -nl | grep -i ":5432 " |
| 20 | if [[ $? == 0 ]]; then |
| 21 | echo "Postgres is already running" |
| 22 | return |
| 23 | fi |
| 24 | |
| 25 | /sbin/service postgresql initdb |
| 26 | /sbin/service postgresql start |
| 27 | /sbin/chkconfig postgresql on |
| 28 | |
| 29 | netstat -nl | grep -i ":5432 " |
| 30 | if [[ $? != 0 ]]; then |
| 31 | # it's still not running |
| 32 | echo "Trying fallback mechanism to start Postgres" |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 33 | sudo -u postgres initdb -D /var/lib/pgsql/data/ |
| 34 | 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] | 35 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 36 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 37 | } |
| 38 | function createdb { |
| 39 | echo "Creating OpenCloud database..." |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 40 | sudo -u postgres createdb planetstack |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 41 | } |
| 42 | function dropdb { |
| 43 | echo "Dropping OpenCloud database..." |
| 44 | sudo -u postgres dropdb planetstack |
| 45 | } |
| 46 | function syncdb { |
| 47 | echo "Syncing OpenCloud services..." |
| 48 | python /opt/planetstack/manage.py syncdb --noinput |
Scott Baker | 0245137 | 2014-09-16 09:46:35 -0700 | [diff] [blame] | 49 | if [[ $DJANGO_17 ]]; then |
| 50 | echo "Loading initial data from fixture..." |
Scott Baker | d3a5a2e | 2014-11-19 18:01:13 -0800 | [diff] [blame] | 51 | python /opt/planetstack/manage.py --noobserver --nomodelpolicy loaddata /opt/planetstack/core/fixtures/initial_data.json |
Scott Baker | 0245137 | 2014-09-16 09:46:35 -0700 | [diff] [blame] | 52 | fi |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 53 | } |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 54 | function evolvedb { |
Scott Baker | 6ecdc42 | 2014-09-12 15:10:01 -0700 | [diff] [blame] | 55 | echo "Evolving OpenCloud services..." |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 56 | python /opt/planetstack/manage.py evolve --hint --execute --noinput |
| 57 | } |
Scott Baker | 6ecdc42 | 2014-09-12 15:10:01 -0700 | [diff] [blame] | 58 | function migratedb { |
| 59 | echo "Migrating OpenCloud services..." |
| 60 | python /opt/planetstack/manage.py migrate |
| 61 | } |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 62 | function stopserver { |
| 63 | echo "Stopping any running OpenCloud Service(s)" |
| 64 | pkill -f "python.*runserver" |
| 65 | } |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 66 | function runserver { |
Scott Baker | a381a41 | 2014-05-15 15:35:27 -0700 | [diff] [blame] | 67 | PUBLIC_HOSTNAME=`/opt/planetstack/planetstack-config.py get server_hostname $HOSTNAME` |
| 68 | echo "Starting OpenCloud Service on $PUBLIC_HOSTNAME:8000" |
| 69 | python manage.py runserver $PUBLIC_HOSTNAME:8000& |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 72 | function dumpdata { |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 73 | mkdir -p $BACKUP_DIR |
| 74 | FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json" |
| 75 | echo "Saving data to $FN" |
Scott Baker | a2e881c | 2014-08-15 16:52:55 -0700 | [diff] [blame] | 76 | python manage.py dumpdata core hpc syndicate_storage requestrouter -a --indent 4 > $FN |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 77 | if [[ ! -f $FN ]]; then |
| 78 | echo "FAILED to create $FN" |
| 79 | exit |
| 80 | fi |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 81 | SIZE=$(du -k "$FN" | cut -f 1) |
| 82 | if [[ $SIZE -lt 9 ]]; then |
| 83 | echo "Dumpdata was empty. Deleting and aborting" |
| 84 | rm $FN |
| 85 | exit |
| 86 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 87 | rm -f $BACKUP_DIR/dumpdata-latest.json |
| 88 | ln -s $FN $BACKUP_DIR/dumpdata-latest.json |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Sapan Bhatia | 0e8769f | 2014-09-08 10:53:53 -0400 | [diff] [blame] | 91 | function genkeys { |
| 92 | mkdir -p public_keys |
| 93 | mkdir -p private_keys |
| 94 | echo "Generating keys" |
| 95 | keyczart create --location=private_keys --name="OpenCloud" --purpose=crypt --asymmetric=rsa |
| 96 | keyczart addkey --location=private_keys --status=primary --size=1024 |
| 97 | keyczart pubkey --location=private_keys --destination=public_keys |
| 98 | if [[ ! -f public_keys/1 ]]; then |
| 99 | echo "FAILED to create keys" |
| 100 | exit |
| 101 | fi |
| 102 | } |
| 103 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 104 | COMMAND=$1 |
| 105 | |
| 106 | if [ "$COMMAND" = "initdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 107 | stopserver |
| 108 | ensure_postgres_running |
| 109 | createdb |
| 110 | syncdb |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 111 | fi |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 112 | if [ "$COMMAND" = "repairdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 113 | stopserver |
| 114 | ensure_postgres_running |
| 115 | dumpdata |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 116 | # TODO: This is where we could run migration scripts to upgrade the |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 117 | # dumped data to the new models. |
| 118 | mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old |
| 119 | cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json |
| 120 | dropdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 121 | createdb |
| 122 | syncdb |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 123 | fi |
| 124 | if [ "$COMMAND" = "restoredb" ]; then |
| 125 | if [[ ! -f $BACKUP_DIR/dumpdata-latest.json ]]; then |
| 126 | echo There is no dumpdata to restore |
| 127 | exit |
| 128 | fi |
| 129 | stopserver |
| 130 | ensure_postgres_running |
| 131 | mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old |
| 132 | cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json |
| 133 | dropdb |
| 134 | createdb |
| 135 | syncdb |
| 136 | fi |
Scott Baker | 4bb7cbc | 2014-09-17 22:19:54 -0700 | [diff] [blame] | 137 | if [ "$COMMAND" = "evolvedb" -o "$COMMAND" = "migratedb" ]; then |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 138 | stopserver |
| 139 | ensure_postgres_running |
Scott Baker | 4bb7cbc | 2014-09-17 22:19:54 -0700 | [diff] [blame] | 140 | if [[ $DJANGO_17 ]]; then |
| 141 | migratedb |
| 142 | else |
| 143 | evolvedb |
| 144 | fi |
Scott Baker | 6ecdc42 | 2014-09-12 15:10:01 -0700 | [diff] [blame] | 145 | fi |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 146 | if [ "$COMMAND" = "resetdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 147 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 148 | dropdb |
| 149 | createdb |
| 150 | syncdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 151 | fi |
| 152 | if [ "$COMMAND" = "syncdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 153 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 154 | syncdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 155 | fi |
| 156 | if [ "$COMMAND" = "runserver" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 157 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 158 | runserver |
| 159 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 160 | if [ "$COMMAND" = "stopserver" ]; then |
| 161 | stopserver |
| 162 | fi |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 163 | if [ "$COMMAND" = "dumpdata" ]; then |
| 164 | dumpdata |
| 165 | fi |
Sapan Bhatia | 0e8769f | 2014-09-08 10:53:53 -0400 | [diff] [blame] | 166 | if [ "$COMMAND" = "genkeys" ]; then |
| 167 | genkeys |
| 168 | fi |