Andy Bavier | 3704434 | 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 | 0c550f7 | 2015-02-04 21:50:08 -0800 | [diff] [blame] | 8 | XOS_DIR=/opt/xos |
| 9 | BACKUP_DIR=/opt/xos_backups |
Scott Baker | 82d6228 | 2015-02-19 21:56:58 -0800 | [diff] [blame] | 10 | DBNAME=xos |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 11 | |
Scott Baker | a40c935 | 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 | d232c2b | 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 | 4b71a2d | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 16 | function is_ubuntu { |
| 17 | if ! which lsb_release &> /dev/null; then |
| 18 | # lsb_release is not installed |
| 19 | return 1 |
| 20 | fi |
| 21 | if lsb_release -i | grep -i ubuntu &> /dev/null; then |
| 22 | return 0 |
| 23 | fi |
| 24 | return 2 |
| 25 | } |
| 26 | |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 27 | function ensure_postgres_running { |
| 28 | # "sudo -u postgres pg_ctl -D /var/lib/postgres/data status" doesn't work |
| 29 | # right on Vicci, so let's try to detect it by seeing if the port is |
| 30 | # being listened on |
| 31 | |
Scott Baker | 4b71a2d | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 32 | netstat -nl | grep -i ":5432 " > /dev/null |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 33 | if [[ $? == 0 ]]; then |
| 34 | echo "Postgres is already running" |
| 35 | return |
| 36 | fi |
| 37 | |
Scott Baker | 4b71a2d | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 38 | # note that initdb isn't needed in Ubuntu distributions, and calling it |
| 39 | # will throw spurious error messages |
| 40 | if ! is_ubuntu; then |
| 41 | service postgresql initdb |
| 42 | fi |
Andy Bavier | 3704434 | 2015-02-06 16:39:06 -0500 | [diff] [blame] | 43 | service postgresql start |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 44 | |
Scott Baker | 4b71a2d | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 45 | netstat -nl | grep -i ":5432 " > /dev/null |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 46 | if [[ $? != 0 ]]; then |
Scott Baker | 4b71a2d | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 47 | # it's still not running... |
| 48 | # this is intended for Vicci where some D-Bus issue is |
| 49 | # preventing systemctl from working properly. |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 50 | echo "Trying fallback mechanism to start Postgres" |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 51 | sudo -u postgres initdb -D /var/lib/pgsql/data/ |
| 52 | 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] | 53 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 54 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 55 | } |
Scott Baker | 4b71a2d | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 56 | |
| 57 | function db_exists { |
| 58 | sudo -u postgres psql $DBNAME -c '\q' 2>/dev/null |
| 59 | return $? |
| 60 | } |
| 61 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 62 | function createdb { |
Scott Baker | d232c2b | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 63 | echo "Creating XOS database..." |
| 64 | sudo -u postgres createdb $DBNAME |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 65 | } |
| 66 | function dropdb { |
Scott Baker | d232c2b | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 67 | echo "Dropping XOS database..." |
| 68 | sudo -u postgres dropdb $DBNAME |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 69 | } |
| 70 | function syncdb { |
Scott Baker | d232c2b | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 71 | echo "Syncing XOS services..." |
Scott Baker | 596397c | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 72 | python $XOS_DIR/manage.py syncdb --noinput |
Scott Baker | a40c935 | 2014-09-16 09:46:35 -0700 | [diff] [blame] | 73 | if [[ $DJANGO_17 ]]; then |
| 74 | echo "Loading initial data from fixture..." |
Scott Baker | 596397c | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 75 | python $XOS_DIR/manage.py --noobserver --nomodelpolicy loaddata $XOS_DIR/core/fixtures/initial_data.json |
Scott Baker | a40c935 | 2014-09-16 09:46:35 -0700 | [diff] [blame] | 76 | fi |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 77 | } |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 78 | function evolvedb { |
Scott Baker | d232c2b | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 79 | echo "Evolving XOS services..." |
Scott Baker | 596397c | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 80 | python $XOS_DIR/manage.py evolve --hint --execute --noinput |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 81 | } |
Scott Baker | e363ac0 | 2014-09-12 15:10:01 -0700 | [diff] [blame] | 82 | function migratedb { |
Scott Baker | d232c2b | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 83 | echo "Migrating XOS services..." |
Scott Baker | 596397c | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 84 | python $XOS_DIR/manage.py migrate |
Scott Baker | e363ac0 | 2014-09-12 15:10:01 -0700 | [diff] [blame] | 85 | } |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 86 | function stopserver { |
Scott Baker | d232c2b | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 87 | echo "Stopping any running XOS Service(s)" |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 88 | pkill -f "python.*runserver" |
| 89 | } |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 90 | function runserver { |
Andy Bavier | 3704434 | 2015-02-06 16:39:06 -0500 | [diff] [blame] | 91 | ensure_postgres_running |
Scott Baker | 596397c | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 92 | PUBLIC_HOSTNAME=`$XOS_DIR/xos-config.py get server_hostname $HOSTNAME` |
Scott Baker | d232c2b | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 93 | echo "Starting XOS Service on $PUBLIC_HOSTNAME:8000" |
Scott Baker | 3a96c54 | 2015-02-06 16:26:04 -0800 | [diff] [blame] | 94 | python manage.py runserver $PUBLIC_HOSTNAME:8000 --insecure& |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 97 | function dumpdata { |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 98 | mkdir -p $BACKUP_DIR |
| 99 | FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json" |
| 100 | echo "Saving data to $FN" |
Scott Baker | a2e881c | 2014-08-15 16:52:55 -0700 | [diff] [blame] | 101 | 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] | 102 | if [[ ! -f $FN ]]; then |
| 103 | echo "FAILED to create $FN" |
| 104 | exit |
| 105 | fi |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 106 | SIZE=$(du -k "$FN" | cut -f 1) |
| 107 | if [[ $SIZE -lt 9 ]]; then |
| 108 | echo "Dumpdata was empty. Deleting and aborting" |
| 109 | rm $FN |
| 110 | exit |
| 111 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 112 | rm -f $BACKUP_DIR/dumpdata-latest.json |
| 113 | ln -s $FN $BACKUP_DIR/dumpdata-latest.json |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Sapan Bhatia | 51a92b1 | 2014-09-08 10:53:53 -0400 | [diff] [blame] | 116 | function genkeys { |
| 117 | mkdir -p public_keys |
| 118 | mkdir -p private_keys |
| 119 | echo "Generating keys" |
| 120 | keyczart create --location=private_keys --name="OpenCloud" --purpose=crypt --asymmetric=rsa |
| 121 | keyczart addkey --location=private_keys --status=primary --size=1024 |
| 122 | keyczart pubkey --location=private_keys --destination=public_keys |
| 123 | if [[ ! -f public_keys/1 ]]; then |
| 124 | echo "FAILED to create keys" |
| 125 | exit |
| 126 | fi |
| 127 | } |
| 128 | |
Scott Baker | ba4e010 | 2015-02-18 16:42:14 -0800 | [diff] [blame] | 129 | function remigrate { |
Scott Baker | 4b71a2d | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 130 | if db_exists; then |
| 131 | dropdb |
| 132 | fi |
Scott Baker | ba4e010 | 2015-02-18 16:42:14 -0800 | [diff] [blame] | 133 | rm -rf /opt/xos/*/migrations |
| 134 | python ./manage.py makemigrations core |
| 135 | python ./manage.py makemigrations hpc |
| 136 | python ./manage.py makemigrations requestrouter |
| 137 | python ./manage.py makemigrations syndicate_storage |
Scott Baker | fd15c3d | 2015-05-06 12:04:09 -0700 | [diff] [blame] | 138 | python ./manage.py makemigrations cord |
Scott Baker | ba4e010 | 2015-02-18 16:42:14 -0800 | [diff] [blame] | 139 | #python ./manage.py makemigrations servcomp |
| 140 | } |
| 141 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 142 | COMMAND=$1 |
| 143 | |
| 144 | if [ "$COMMAND" = "initdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 145 | stopserver |
| 146 | ensure_postgres_running |
| 147 | createdb |
| 148 | syncdb |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 149 | fi |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 150 | if [ "$COMMAND" = "repairdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 151 | stopserver |
| 152 | ensure_postgres_running |
| 153 | dumpdata |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 154 | # TODO: This is where we could run migration scripts to upgrade the |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 155 | # dumped data to the new models. |
Scott Baker | 596397c | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 156 | mv $XOS_DIR/core/fixtures/initial_data.json $XOS_DIR/core/fixtures/initial_data.json-old |
| 157 | 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] | 158 | dropdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 159 | createdb |
| 160 | syncdb |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 161 | fi |
| 162 | if [ "$COMMAND" = "restoredb" ]; then |
| 163 | if [[ ! -f $BACKUP_DIR/dumpdata-latest.json ]]; then |
| 164 | echo There is no dumpdata to restore |
| 165 | exit |
| 166 | fi |
| 167 | stopserver |
| 168 | ensure_postgres_running |
Scott Baker | 596397c | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 169 | mv $XOS_DIR/core/fixtures/initial_data.json $XOS_DIR/core/fixtures/initial_data.json-old |
| 170 | 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] | 171 | dropdb |
| 172 | createdb |
| 173 | syncdb |
| 174 | fi |
Scott Baker | baf6256 | 2014-09-17 22:19:54 -0700 | [diff] [blame] | 175 | if [ "$COMMAND" = "evolvedb" -o "$COMMAND" = "migratedb" ]; then |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 176 | stopserver |
| 177 | ensure_postgres_running |
Scott Baker | baf6256 | 2014-09-17 22:19:54 -0700 | [diff] [blame] | 178 | if [[ $DJANGO_17 ]]; then |
| 179 | migratedb |
| 180 | else |
| 181 | evolvedb |
| 182 | fi |
Scott Baker | e363ac0 | 2014-09-12 15:10:01 -0700 | [diff] [blame] | 183 | fi |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 184 | if [ "$COMMAND" = "resetdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 185 | stopserver |
S.Çağlar Onur | 3c297b4 | 2015-02-24 15:34:39 -0500 | [diff] [blame] | 186 | ensure_postgres_running |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 187 | dropdb |
| 188 | createdb |
| 189 | syncdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 190 | fi |
| 191 | if [ "$COMMAND" = "syncdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 192 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 193 | syncdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 194 | fi |
| 195 | if [ "$COMMAND" = "runserver" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 196 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 197 | runserver |
| 198 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 199 | if [ "$COMMAND" = "stopserver" ]; then |
| 200 | stopserver |
| 201 | fi |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 202 | if [ "$COMMAND" = "dumpdata" ]; then |
| 203 | dumpdata |
| 204 | fi |
Sapan Bhatia | 51a92b1 | 2014-09-08 10:53:53 -0400 | [diff] [blame] | 205 | if [ "$COMMAND" = "genkeys" ]; then |
| 206 | genkeys |
| 207 | fi |
Scott Baker | 069ca8b | 2015-02-16 23:34:48 -0800 | [diff] [blame] | 208 | if [ "$COMMAND" = "generateapi" ]; then |
Scott Baker | d82a9dd | 2015-02-16 23:47:11 -0800 | [diff] [blame] | 209 | python apigen/modelgen apigen/api.template.py > xos/xosapi.py |
Scott Baker | ba4e010 | 2015-02-18 16:42:14 -0800 | [diff] [blame] | 210 | fi |
| 211 | if [ "$COMMAND" = "remigrate" ]; then |
| 212 | ensure_postgres_running |
| 213 | remigrate |
| 214 | createdb |
| 215 | syncdb |
S.Çağlar Onur | 3c297b4 | 2015-02-24 15:34:39 -0500 | [diff] [blame] | 216 | fi |