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