blob: 586e4077fa45dc6cd8443325b796686ff7d4e21a [file] [log] [blame]
Andy Bavier37044342015-02-06 16:39:06 -05001#!/bin/bash
Siobhan Tully44fd4cc2014-02-23 00:07:12 -05002
3if [ -z "$1" ]; then
Scott Bakerf4db3812014-05-09 16:42:13 -07004 echo usage: $0 "[initdb | createdb | dropdb | syncdb | runserver | resetdb | dumpdata]"
Siobhan Tully44fd4cc2014-02-23 00:07:12 -05005 exit
6fi
7
Scott Baker0c550f72015-02-04 21:50:08 -08008XOS_DIR=/opt/xos
9BACKUP_DIR=/opt/xos_backups
Scott Bakerd232c2b2015-02-04 15:04:26 -080010DBNAME=planetstack
Scott Bakerb4785022014-05-15 13:22:28 -070011
Scott Bakera40c9352014-09-16 09:46:35 -070012DJANGO_17=`python -c "import django; from distutils.version import StrictVersion; print int(StrictVersion(django.get_version()) >= StrictVersion('1.7'))"`
13
Scott Bakerd232c2b2015-02-04 15:04:26 -080014cd $XOS_DIR
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050015
Scott Bakerb4785022014-05-15 13:22:28 -070016function 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 Bavier37044342015-02-06 16:39:06 -050027 service postgresql initdb
28 service postgresql start
Scott Bakerb4785022014-05-15 13:22:28 -070029
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 Tully44fd4cc2014-02-23 00:07:12 -050034 sudo -u postgres initdb -D /var/lib/pgsql/data/
35 sudo -u postgres pg_ctl -D /var/lib/pgsql/data -l logfile start
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050036 fi
Scott Bakerb4785022014-05-15 13:22:28 -070037
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050038}
39function createdb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080040 echo "Creating XOS database..."
41 sudo -u postgres createdb $DBNAME
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050042}
43function dropdb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080044 echo "Dropping XOS database..."
45 sudo -u postgres dropdb $DBNAME
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050046}
47function syncdb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080048 echo "Syncing XOS services..."
Scott Baker596397c2015-02-04 22:47:45 -080049 python $XOS_DIR/manage.py syncdb --noinput
Scott Bakera40c9352014-09-16 09:46:35 -070050 if [[ $DJANGO_17 ]]; then
51 echo "Loading initial data from fixture..."
Scott Baker596397c2015-02-04 22:47:45 -080052 python $XOS_DIR/manage.py --noobserver --nomodelpolicy loaddata $XOS_DIR/core/fixtures/initial_data.json
Scott Bakera40c9352014-09-16 09:46:35 -070053 fi
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050054}
Scott Baker25b70fd2014-05-15 14:11:58 -070055function evolvedb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080056 echo "Evolving XOS services..."
Scott Baker596397c2015-02-04 22:47:45 -080057 python $XOS_DIR/manage.py evolve --hint --execute --noinput
Scott Baker25b70fd2014-05-15 14:11:58 -070058}
Scott Bakere363ac02014-09-12 15:10:01 -070059function migratedb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080060 echo "Migrating XOS services..."
Scott Baker596397c2015-02-04 22:47:45 -080061 python $XOS_DIR/manage.py migrate
Scott Bakere363ac02014-09-12 15:10:01 -070062}
Scott Bakerb4785022014-05-15 13:22:28 -070063function stopserver {
Scott Bakerd232c2b2015-02-04 15:04:26 -080064 echo "Stopping any running XOS Service(s)"
Scott Bakerb4785022014-05-15 13:22:28 -070065 pkill -f "python.*runserver"
66}
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050067function runserver {
Andy Bavier37044342015-02-06 16:39:06 -050068 ensure_postgres_running
Scott Baker596397c2015-02-04 22:47:45 -080069 PUBLIC_HOSTNAME=`$XOS_DIR/xos-config.py get server_hostname $HOSTNAME`
Scott Bakerd232c2b2015-02-04 15:04:26 -080070 echo "Starting XOS Service on $PUBLIC_HOSTNAME:8000"
Scott Baker3a96c542015-02-06 16:26:04 -080071 python manage.py runserver $PUBLIC_HOSTNAME:8000 --insecure&
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050072}
73
Scott Bakerf4db3812014-05-09 16:42:13 -070074function dumpdata {
Scott Bakerb4785022014-05-15 13:22:28 -070075 mkdir -p $BACKUP_DIR
76 FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json"
77 echo "Saving data to $FN"
Scott Bakera2e881c2014-08-15 16:52:55 -070078 python manage.py dumpdata core hpc syndicate_storage requestrouter -a --indent 4 > $FN
Scott Bakerb4785022014-05-15 13:22:28 -070079 if [[ ! -f $FN ]]; then
80 echo "FAILED to create $FN"
81 exit
82 fi
Scott Baker25b70fd2014-05-15 14:11:58 -070083 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 Bakerb4785022014-05-15 13:22:28 -070089 rm -f $BACKUP_DIR/dumpdata-latest.json
90 ln -s $FN $BACKUP_DIR/dumpdata-latest.json
Scott Bakerf4db3812014-05-09 16:42:13 -070091}
92
Sapan Bhatia51a92b12014-09-08 10:53:53 -040093function 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 Bakerba4e0102015-02-18 16:42:14 -0800106function 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 Tully44fd4cc2014-02-23 00:07:12 -0500116COMMAND=$1
117
118if [ "$COMMAND" = "initdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700119 stopserver
120 ensure_postgres_running
121 createdb
122 syncdb
Scott Bakerb4785022014-05-15 13:22:28 -0700123fi
Scott Baker25b70fd2014-05-15 14:11:58 -0700124if [ "$COMMAND" = "repairdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700125 stopserver
126 ensure_postgres_running
127 dumpdata
Scott Baker25b70fd2014-05-15 14:11:58 -0700128 # TODO: This is where we could run migration scripts to upgrade the
Scott Bakerb4785022014-05-15 13:22:28 -0700129 # dumped data to the new models.
Scott Baker596397c2015-02-04 22:47:45 -0800130 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 Bakerb4785022014-05-15 13:22:28 -0700132 dropdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500133 createdb
134 syncdb
Scott Baker25b70fd2014-05-15 14:11:58 -0700135fi
136if [ "$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 Baker596397c2015-02-04 22:47:45 -0800143 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 Baker25b70fd2014-05-15 14:11:58 -0700145 dropdb
146 createdb
147 syncdb
148fi
Scott Bakerbaf62562014-09-17 22:19:54 -0700149if [ "$COMMAND" = "evolvedb" -o "$COMMAND" = "migratedb" ]; then
Scott Baker25b70fd2014-05-15 14:11:58 -0700150 stopserver
151 ensure_postgres_running
Scott Bakerbaf62562014-09-17 22:19:54 -0700152 if [[ $DJANGO_17 ]]; then
153 migratedb
154 else
155 evolvedb
156 fi
Scott Bakere363ac02014-09-12 15:10:01 -0700157fi
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500158if [ "$COMMAND" = "resetdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700159 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500160 dropdb
161 createdb
162 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500163fi
164if [ "$COMMAND" = "syncdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700165 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500166 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500167fi
168if [ "$COMMAND" = "runserver" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700169 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500170 runserver
171fi
Scott Bakerb4785022014-05-15 13:22:28 -0700172if [ "$COMMAND" = "stopserver" ]; then
173 stopserver
174fi
Scott Bakerf4db3812014-05-09 16:42:13 -0700175if [ "$COMMAND" = "dumpdata" ]; then
176 dumpdata
177fi
Sapan Bhatia51a92b12014-09-08 10:53:53 -0400178if [ "$COMMAND" = "genkeys" ]; then
179 genkeys
180fi
Scott Baker069ca8b2015-02-16 23:34:48 -0800181if [ "$COMMAND" = "generateapi" ]; then
Scott Bakerd82a9dd2015-02-16 23:47:11 -0800182 python apigen/modelgen apigen/api.template.py > xos/xosapi.py
Scott Bakerba4e0102015-02-18 16:42:14 -0800183fi
184if [ "$COMMAND" = "remigrate" ]; then
185 ensure_postgres_running
186 remigrate
187 createdb
188 syncdb
Scott Baker069ca8b2015-02-16 23:34:48 -0800189fi