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 | 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 | |
| 27 | /sbin/service postgresql initdb |
| 28 | /sbin/service postgresql start |
| 29 | /sbin/chkconfig postgresql on |
| 30 | |
| 31 | netstat -nl | grep -i ":5432 " |
| 32 | if [[ $? != 0 ]]; then |
| 33 | # it's still not running |
| 34 | echo "Trying fallback mechanism to start Postgres" |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 35 | sudo -u postgres initdb -D /var/lib/pgsql/data/ |
| 36 | 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] | 37 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 38 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 39 | } |
| 40 | function createdb { |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 41 | echo "Creating XOS database..." |
| 42 | sudo -u postgres createdb $DBNAME |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 43 | } |
| 44 | function dropdb { |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 45 | echo "Dropping XOS database..." |
| 46 | sudo -u postgres dropdb $DBNAME |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 47 | } |
| 48 | function syncdb { |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 49 | echo "Syncing XOS services..." |
| 50 | python $XOSDIR/manage.py syncdb --noinput |
Scott Baker | 0245137 | 2014-09-16 09:46:35 -0700 | [diff] [blame] | 51 | if [[ $DJANGO_17 ]]; then |
| 52 | echo "Loading initial data from fixture..." |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 53 | python $XOSDIR/manage.py --noobserver --nomodelpolicy loaddata $XOSDIR/core/fixtures/initial_data.json |
Scott Baker | 0245137 | 2014-09-16 09:46:35 -0700 | [diff] [blame] | 54 | fi |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 55 | } |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 56 | function evolvedb { |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 57 | echo "Evolving XOS services..." |
| 58 | python $XOSDIR/manage.py evolve --hint --execute --noinput |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 59 | } |
Scott Baker | 6ecdc42 | 2014-09-12 15:10:01 -0700 | [diff] [blame] | 60 | function migratedb { |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 61 | echo "Migrating XOS services..." |
| 62 | python $XOSDIR/manage.py migrate |
Scott Baker | 6ecdc42 | 2014-09-12 15:10:01 -0700 | [diff] [blame] | 63 | } |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 64 | function stopserver { |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 65 | echo "Stopping any running XOS Service(s)" |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 66 | pkill -f "python.*runserver" |
| 67 | } |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 68 | function runserver { |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 69 | PUBLIC_HOSTNAME=`$XOSDIR/xos-config.py get server_hostname $HOSTNAME` |
| 70 | echo "Starting XOS Service on $PUBLIC_HOSTNAME:8000" |
Scott Baker | a381a41 | 2014-05-15 15:35:27 -0700 | [diff] [blame] | 71 | python manage.py runserver $PUBLIC_HOSTNAME:8000& |
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 | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 120 | mv $XOSDIR/core/fixtures/initial_data.json $XOSDIR/core/fixtures/initial_data.json-old |
| 121 | cp $BACKUP_DIR/dumpdata-latest.json $XOSDIR/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 | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 133 | mv $XOSDIR/core/fixtures/initial_data.json $XOSDIR/core/fixtures/initial_data.json-old |
| 134 | cp $BACKUP_DIR/dumpdata-latest.json $XOSDIR/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 |