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 | |
Scott Baker | 4d8a638 | 2015-02-18 16:42:14 -0800 | [diff] [blame^] | 106 | function remigrate { |
| 107 | dropdb |
| 108 | rm -rf /opt/xos/*/migrations |
| 109 | python ./manage.py makemigrations core |
| 110 | python ./manage.py makemigrations hpc |
| 111 | python ./manage.py makemigrations requestrouter |
| 112 | python ./manage.py makemigrations syndicate_storage |
| 113 | #python ./manage.py makemigrations servcomp |
| 114 | } |
| 115 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 116 | COMMAND=$1 |
| 117 | |
| 118 | if [ "$COMMAND" = "initdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 119 | stopserver |
| 120 | ensure_postgres_running |
| 121 | createdb |
| 122 | syncdb |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 123 | fi |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 124 | if [ "$COMMAND" = "repairdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 125 | stopserver |
| 126 | ensure_postgres_running |
| 127 | dumpdata |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 128 | # TODO: This is where we could run migration scripts to upgrade the |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 129 | # dumped data to the new models. |
Scott Baker | a8b263b | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 130 | mv $XOS_DIR/core/fixtures/initial_data.json $XOS_DIR/core/fixtures/initial_data.json-old |
| 131 | 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] | 132 | dropdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 133 | createdb |
| 134 | syncdb |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 135 | fi |
| 136 | if [ "$COMMAND" = "restoredb" ]; then |
| 137 | if [[ ! -f $BACKUP_DIR/dumpdata-latest.json ]]; then |
| 138 | echo There is no dumpdata to restore |
| 139 | exit |
| 140 | fi |
| 141 | stopserver |
| 142 | ensure_postgres_running |
Scott Baker | a8b263b | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 143 | mv $XOS_DIR/core/fixtures/initial_data.json $XOS_DIR/core/fixtures/initial_data.json-old |
| 144 | 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] | 145 | dropdb |
| 146 | createdb |
| 147 | syncdb |
| 148 | fi |
Scott Baker | 4bb7cbc | 2014-09-17 22:19:54 -0700 | [diff] [blame] | 149 | if [ "$COMMAND" = "evolvedb" -o "$COMMAND" = "migratedb" ]; then |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 150 | stopserver |
| 151 | ensure_postgres_running |
Scott Baker | 4bb7cbc | 2014-09-17 22:19:54 -0700 | [diff] [blame] | 152 | if [[ $DJANGO_17 ]]; then |
| 153 | migratedb |
| 154 | else |
| 155 | evolvedb |
| 156 | fi |
Scott Baker | 6ecdc42 | 2014-09-12 15:10:01 -0700 | [diff] [blame] | 157 | fi |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 158 | if [ "$COMMAND" = "resetdb" ]; 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 | dropdb |
| 161 | createdb |
| 162 | syncdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 163 | fi |
| 164 | if [ "$COMMAND" = "syncdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 165 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 166 | syncdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 167 | fi |
| 168 | if [ "$COMMAND" = "runserver" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 169 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 170 | runserver |
| 171 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 172 | if [ "$COMMAND" = "stopserver" ]; then |
| 173 | stopserver |
| 174 | fi |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 175 | if [ "$COMMAND" = "dumpdata" ]; then |
| 176 | dumpdata |
| 177 | fi |
Sapan Bhatia | 0e8769f | 2014-09-08 10:53:53 -0400 | [diff] [blame] | 178 | if [ "$COMMAND" = "genkeys" ]; then |
| 179 | genkeys |
| 180 | fi |
Scott Baker | c57ddcc | 2015-02-16 23:34:48 -0800 | [diff] [blame] | 181 | if [ "$COMMAND" = "generateapi" ]; then |
Scott Baker | cd4a0c2 | 2015-02-16 23:47:11 -0800 | [diff] [blame] | 182 | python apigen/modelgen apigen/api.template.py > xos/xosapi.py |
Scott Baker | 4d8a638 | 2015-02-18 16:42:14 -0800 | [diff] [blame^] | 183 | fi |
| 184 | if [ "$COMMAND" = "remigrate" ]; then |
| 185 | ensure_postgres_running |
| 186 | remigrate |
| 187 | createdb |
| 188 | syncdb |
Scott Baker | c57ddcc | 2015-02-16 23:34:48 -0800 | [diff] [blame] | 189 | fi |