blob: 4a82c03093967807d473653c0596c32e43aa2dc7 [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 Baker82d62282015-02-19 21:56:58 -080010DBNAME=xos
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 Baker4b71a2d2015-02-19 11:34:41 -080016function 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 Bakerb4785022014-05-15 13:22:28 -070027function 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 Baker4b71a2d2015-02-19 11:34:41 -080032 netstat -nl | grep -i ":5432 " > /dev/null
Scott Bakerb4785022014-05-15 13:22:28 -070033 if [[ $? == 0 ]]; then
34 echo "Postgres is already running"
35 return
36 fi
37
Scott Baker4b71a2d2015-02-19 11:34:41 -080038 # 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 Bavier37044342015-02-06 16:39:06 -050043 service postgresql start
Scott Bakerb4785022014-05-15 13:22:28 -070044
Scott Baker4b71a2d2015-02-19 11:34:41 -080045 netstat -nl | grep -i ":5432 " > /dev/null
Scott Bakerb4785022014-05-15 13:22:28 -070046 if [[ $? != 0 ]]; then
Scott Baker4b71a2d2015-02-19 11:34:41 -080047 # it's still not running...
48 # this is intended for Vicci where some D-Bus issue is
49 # preventing systemctl from working properly.
Scott Bakerb4785022014-05-15 13:22:28 -070050 echo "Trying fallback mechanism to start Postgres"
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050051 sudo -u postgres initdb -D /var/lib/pgsql/data/
52 sudo -u postgres pg_ctl -D /var/lib/pgsql/data -l logfile start
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050053 fi
Scott Bakerb4785022014-05-15 13:22:28 -070054
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050055}
Scott Baker4b71a2d2015-02-19 11:34:41 -080056
57function db_exists {
58 sudo -u postgres psql $DBNAME -c '\q' 2>/dev/null
59 return $?
60}
61
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050062function createdb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080063 echo "Creating XOS database..."
64 sudo -u postgres createdb $DBNAME
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050065}
66function dropdb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080067 echo "Dropping XOS database..."
68 sudo -u postgres dropdb $DBNAME
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050069}
70function syncdb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080071 echo "Syncing XOS services..."
Scott Baker596397c2015-02-04 22:47:45 -080072 python $XOS_DIR/manage.py syncdb --noinput
Scott Bakera40c9352014-09-16 09:46:35 -070073 if [[ $DJANGO_17 ]]; then
74 echo "Loading initial data from fixture..."
Scott Baker596397c2015-02-04 22:47:45 -080075 python $XOS_DIR/manage.py --noobserver --nomodelpolicy loaddata $XOS_DIR/core/fixtures/initial_data.json
Scott Bakera40c9352014-09-16 09:46:35 -070076 fi
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050077}
Scott Baker25b70fd2014-05-15 14:11:58 -070078function evolvedb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080079 echo "Evolving XOS services..."
Scott Baker596397c2015-02-04 22:47:45 -080080 python $XOS_DIR/manage.py evolve --hint --execute --noinput
Scott Baker25b70fd2014-05-15 14:11:58 -070081}
Scott Bakere363ac02014-09-12 15:10:01 -070082function migratedb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080083 echo "Migrating XOS services..."
Scott Baker596397c2015-02-04 22:47:45 -080084 python $XOS_DIR/manage.py migrate
Scott Bakere363ac02014-09-12 15:10:01 -070085}
Scott Bakerb4785022014-05-15 13:22:28 -070086function stopserver {
Scott Bakerd232c2b2015-02-04 15:04:26 -080087 echo "Stopping any running XOS Service(s)"
Scott Bakerb4785022014-05-15 13:22:28 -070088 pkill -f "python.*runserver"
89}
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050090function runserver {
Andy Bavier37044342015-02-06 16:39:06 -050091 ensure_postgres_running
Scott Baker596397c2015-02-04 22:47:45 -080092 PUBLIC_HOSTNAME=`$XOS_DIR/xos-config.py get server_hostname $HOSTNAME`
Scott Bakerd232c2b2015-02-04 15:04:26 -080093 echo "Starting XOS Service on $PUBLIC_HOSTNAME:8000"
Scott Baker3a96c542015-02-06 16:26:04 -080094 python manage.py runserver $PUBLIC_HOSTNAME:8000 --insecure&
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050095}
96
Scott Bakerf4db3812014-05-09 16:42:13 -070097function dumpdata {
Scott Bakerb4785022014-05-15 13:22:28 -070098 mkdir -p $BACKUP_DIR
99 FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json"
100 echo "Saving data to $FN"
Scott Bakera2e881c2014-08-15 16:52:55 -0700101 python manage.py dumpdata core hpc syndicate_storage requestrouter -a --indent 4 > $FN
Scott Bakerb4785022014-05-15 13:22:28 -0700102 if [[ ! -f $FN ]]; then
103 echo "FAILED to create $FN"
104 exit
105 fi
Scott Baker25b70fd2014-05-15 14:11:58 -0700106 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 Bakerb4785022014-05-15 13:22:28 -0700112 rm -f $BACKUP_DIR/dumpdata-latest.json
113 ln -s $FN $BACKUP_DIR/dumpdata-latest.json
Scott Bakerf4db3812014-05-09 16:42:13 -0700114}
115
Sapan Bhatia51a92b12014-09-08 10:53:53 -0400116function 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 Bakerba4e0102015-02-18 16:42:14 -0800129function remigrate {
Scott Baker4b71a2d2015-02-19 11:34:41 -0800130 if db_exists; then
131 dropdb
132 fi
Scott Bakerba4e0102015-02-18 16:42:14 -0800133 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
138 #python ./manage.py makemigrations servcomp
139}
140
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500141COMMAND=$1
142
143if [ "$COMMAND" = "initdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700144 stopserver
145 ensure_postgres_running
146 createdb
147 syncdb
Scott Bakerb4785022014-05-15 13:22:28 -0700148fi
Scott Baker25b70fd2014-05-15 14:11:58 -0700149if [ "$COMMAND" = "repairdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700150 stopserver
151 ensure_postgres_running
152 dumpdata
Scott Baker25b70fd2014-05-15 14:11:58 -0700153 # TODO: This is where we could run migration scripts to upgrade the
Scott Bakerb4785022014-05-15 13:22:28 -0700154 # dumped data to the new models.
Scott Baker596397c2015-02-04 22:47:45 -0800155 mv $XOS_DIR/core/fixtures/initial_data.json $XOS_DIR/core/fixtures/initial_data.json-old
156 cp $BACKUP_DIR/dumpdata-latest.json $XOS_DIR/core/fixtures/initial_data.json
Scott Bakerb4785022014-05-15 13:22:28 -0700157 dropdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500158 createdb
159 syncdb
Scott Baker25b70fd2014-05-15 14:11:58 -0700160fi
161if [ "$COMMAND" = "restoredb" ]; then
162 if [[ ! -f $BACKUP_DIR/dumpdata-latest.json ]]; then
163 echo There is no dumpdata to restore
164 exit
165 fi
166 stopserver
167 ensure_postgres_running
Scott Baker596397c2015-02-04 22:47:45 -0800168 mv $XOS_DIR/core/fixtures/initial_data.json $XOS_DIR/core/fixtures/initial_data.json-old
169 cp $BACKUP_DIR/dumpdata-latest.json $XOS_DIR/core/fixtures/initial_data.json
Scott Baker25b70fd2014-05-15 14:11:58 -0700170 dropdb
171 createdb
172 syncdb
173fi
Scott Bakerbaf62562014-09-17 22:19:54 -0700174if [ "$COMMAND" = "evolvedb" -o "$COMMAND" = "migratedb" ]; then
Scott Baker25b70fd2014-05-15 14:11:58 -0700175 stopserver
176 ensure_postgres_running
Scott Bakerbaf62562014-09-17 22:19:54 -0700177 if [[ $DJANGO_17 ]]; then
178 migratedb
179 else
180 evolvedb
181 fi
Scott Bakere363ac02014-09-12 15:10:01 -0700182fi
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500183if [ "$COMMAND" = "resetdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700184 stopserver
S.Çağlar Onur3c297b42015-02-24 15:34:39 -0500185 ensure_postgres_running
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500186 dropdb
187 createdb
188 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500189fi
190if [ "$COMMAND" = "syncdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700191 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500192 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500193fi
194if [ "$COMMAND" = "runserver" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700195 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500196 runserver
197fi
Scott Bakerb4785022014-05-15 13:22:28 -0700198if [ "$COMMAND" = "stopserver" ]; then
199 stopserver
200fi
Scott Bakerf4db3812014-05-09 16:42:13 -0700201if [ "$COMMAND" = "dumpdata" ]; then
202 dumpdata
203fi
Sapan Bhatia51a92b12014-09-08 10:53:53 -0400204if [ "$COMMAND" = "genkeys" ]; then
205 genkeys
206fi
Scott Baker069ca8b2015-02-16 23:34:48 -0800207if [ "$COMMAND" = "generateapi" ]; then
Scott Bakerd82a9dd2015-02-16 23:47:11 -0800208 python apigen/modelgen apigen/api.template.py > xos/xosapi.py
Scott Bakerba4e0102015-02-18 16:42:14 -0800209fi
210if [ "$COMMAND" = "remigrate" ]; then
211 ensure_postgres_running
212 remigrate
213 createdb
214 syncdb
S.Çağlar Onur3c297b42015-02-24 15:34:39 -0500215fi