blob: 966ad9bdaf5deb301ba81007d459050f84524ebe [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
Scott Bakercecee002015-07-20 10:57:08 -070057function wait_postgres {
58 sudo -u postgres psql -c '\q'
59 while [[ "$?" != "0" ]]; do
60 echo Waiting for postgres to start
61 sleep 1
62 sudo -u postgres psql -c '\q'
Jeremy Mowery2dd1a582015-12-04 14:07:11 -070063 done
Scott Bakercecee002015-07-20 10:57:08 -070064}
65
Scott Baker4b71a2d2015-02-19 11:34:41 -080066function db_exists {
Jeremy Mowery2dd1a582015-12-04 14:07:11 -070067 sudo -u postgres psql $DBNAME -c '\q' 2>/dev/null
Scott Baker4b71a2d2015-02-19 11:34:41 -080068 return $?
Jeremy Mowery2dd1a582015-12-04 14:07:11 -070069}
Scott Baker4b71a2d2015-02-19 11:34:41 -080070
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050071function createdb {
Scott Bakercecee002015-07-20 10:57:08 -070072 wait_postgres
Scott Bakerd232c2b2015-02-04 15:04:26 -080073 echo "Creating XOS database..."
74 sudo -u postgres createdb $DBNAME
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050075}
76function dropdb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080077 echo "Dropping XOS database..."
78 sudo -u postgres dropdb $DBNAME
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050079}
80function syncdb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080081 echo "Syncing XOS services..."
Scott Baker596397c2015-02-04 22:47:45 -080082 python $XOS_DIR/manage.py syncdb --noinput
Scott Bakera40c9352014-09-16 09:46:35 -070083 if [[ $DJANGO_17 ]]; then
84 echo "Loading initial data from fixture..."
Scott Baker596397c2015-02-04 22:47:45 -080085 python $XOS_DIR/manage.py --noobserver --nomodelpolicy loaddata $XOS_DIR/core/fixtures/initial_data.json
Scott Bakera40c9352014-09-16 09:46:35 -070086 fi
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050087}
Scott Baker25b70fd2014-05-15 14:11:58 -070088function evolvedb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080089 echo "Evolving XOS services..."
Scott Baker596397c2015-02-04 22:47:45 -080090 python $XOS_DIR/manage.py evolve --hint --execute --noinput
Scott Baker25b70fd2014-05-15 14:11:58 -070091}
Scott Bakere363ac02014-09-12 15:10:01 -070092function migratedb {
Scott Bakerd232c2b2015-02-04 15:04:26 -080093 echo "Migrating XOS services..."
Scott Baker596397c2015-02-04 22:47:45 -080094 python $XOS_DIR/manage.py migrate
Scott Bakere363ac02014-09-12 15:10:01 -070095}
Scott Bakerb4785022014-05-15 13:22:28 -070096function stopserver {
Scott Bakerd232c2b2015-02-04 15:04:26 -080097 echo "Stopping any running XOS Service(s)"
Scott Bakerb4785022014-05-15 13:22:28 -070098 pkill -f "python.*runserver"
99}
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500100function runserver {
Andy Bavier37044342015-02-06 16:39:06 -0500101 ensure_postgres_running
Scott Baker596397c2015-02-04 22:47:45 -0800102 PUBLIC_HOSTNAME=`$XOS_DIR/xos-config.py get server_hostname $HOSTNAME`
Scott Bakerd232c2b2015-02-04 15:04:26 -0800103 echo "Starting XOS Service on $PUBLIC_HOSTNAME:8000"
Scott Baker3a96c542015-02-06 16:26:04 -0800104 python manage.py runserver $PUBLIC_HOSTNAME:8000 --insecure&
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500105}
106
Scott Bakerf4db3812014-05-09 16:42:13 -0700107function dumpdata {
Scott Bakerb4785022014-05-15 13:22:28 -0700108 mkdir -p $BACKUP_DIR
109 FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json"
110 echo "Saving data to $FN"
Scott Bakera2e881c2014-08-15 16:52:55 -0700111 python manage.py dumpdata core hpc syndicate_storage requestrouter -a --indent 4 > $FN
Scott Bakerb4785022014-05-15 13:22:28 -0700112 if [[ ! -f $FN ]]; then
113 echo "FAILED to create $FN"
114 exit
115 fi
Scott Baker25b70fd2014-05-15 14:11:58 -0700116 SIZE=$(du -k "$FN" | cut -f 1)
117 if [[ $SIZE -lt 9 ]]; then
118 echo "Dumpdata was empty. Deleting and aborting"
119 rm $FN
120 exit
121 fi
Scott Bakerb4785022014-05-15 13:22:28 -0700122 rm -f $BACKUP_DIR/dumpdata-latest.json
123 ln -s $FN $BACKUP_DIR/dumpdata-latest.json
Scott Bakerf4db3812014-05-09 16:42:13 -0700124}
125
Sapan Bhatia51a92b12014-09-08 10:53:53 -0400126function genkeys {
127 mkdir -p public_keys
128 mkdir -p private_keys
129 echo "Generating keys"
130 keyczart create --location=private_keys --name="OpenCloud" --purpose=crypt --asymmetric=rsa
131 keyczart addkey --location=private_keys --status=primary --size=1024
132 keyczart pubkey --location=private_keys --destination=public_keys
133 if [[ ! -f public_keys/1 ]]; then
134 echo "FAILED to create keys"
135 exit
136 fi
137}
138
Tony Mackf4f57932015-10-31 14:18:11 +0000139function makemigrations {
Tony Mackdb841862015-11-01 13:50:12 +0000140 rm -rf /opt/xos/*/migrations /opt/xos/services/*/migrations
Scott Bakerba4e0102015-02-18 16:42:14 -0800141 python ./manage.py makemigrations core
142 python ./manage.py makemigrations hpc
143 python ./manage.py makemigrations requestrouter
144 python ./manage.py makemigrations syndicate_storage
Scott Bakerfd15c3d2015-05-06 12:04:09 -0700145 python ./manage.py makemigrations cord
Scott Baker48cf8742015-09-10 09:02:01 -0700146 python ./manage.py makemigrations ceilometer
Jeremy Mowery54332cb2015-11-29 21:02:22 -0700147 python ./manage.py makemigrations helloworldservice_complete
Jeremy Mowery2dd1a582015-12-04 14:07:11 -0700148 python ./manage.py makemigrations vpn
Scott Bakerc44448c2015-10-16 16:54:07 -0700149 python ./manage.py makemigrations onos
Scott Bakerba4e0102015-02-18 16:42:14 -0800150 #python ./manage.py makemigrations servcomp
151}
152
Tony Mackf4f57932015-10-31 14:18:11 +0000153function remigrate {
154 if db_exists; then
155 dropdb
156 fi
157 makemigrations
158}
159
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500160COMMAND=$1
161
162if [ "$COMMAND" = "initdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700163 stopserver
164 ensure_postgres_running
165 createdb
166 syncdb
Scott Bakerb4785022014-05-15 13:22:28 -0700167fi
Scott Baker25b70fd2014-05-15 14:11:58 -0700168if [ "$COMMAND" = "repairdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700169 stopserver
170 ensure_postgres_running
171 dumpdata
Scott Baker25b70fd2014-05-15 14:11:58 -0700172 # TODO: This is where we could run migration scripts to upgrade the
Scott Bakerb4785022014-05-15 13:22:28 -0700173 # dumped data to the new models.
Scott Baker596397c2015-02-04 22:47:45 -0800174 mv $XOS_DIR/core/fixtures/initial_data.json $XOS_DIR/core/fixtures/initial_data.json-old
175 cp $BACKUP_DIR/dumpdata-latest.json $XOS_DIR/core/fixtures/initial_data.json
Scott Bakerb4785022014-05-15 13:22:28 -0700176 dropdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500177 createdb
178 syncdb
Scott Baker25b70fd2014-05-15 14:11:58 -0700179fi
180if [ "$COMMAND" = "restoredb" ]; then
181 if [[ ! -f $BACKUP_DIR/dumpdata-latest.json ]]; then
182 echo There is no dumpdata to restore
183 exit
184 fi
185 stopserver
186 ensure_postgres_running
Scott Baker596397c2015-02-04 22:47:45 -0800187 mv $XOS_DIR/core/fixtures/initial_data.json $XOS_DIR/core/fixtures/initial_data.json-old
188 cp $BACKUP_DIR/dumpdata-latest.json $XOS_DIR/core/fixtures/initial_data.json
Scott Baker25b70fd2014-05-15 14:11:58 -0700189 dropdb
190 createdb
191 syncdb
192fi
Scott Bakerbaf62562014-09-17 22:19:54 -0700193if [ "$COMMAND" = "evolvedb" -o "$COMMAND" = "migratedb" ]; then
Scott Baker25b70fd2014-05-15 14:11:58 -0700194 stopserver
195 ensure_postgres_running
Scott Bakerbaf62562014-09-17 22:19:54 -0700196 if [[ $DJANGO_17 ]]; then
197 migratedb
198 else
199 evolvedb
200 fi
Scott Bakere363ac02014-09-12 15:10:01 -0700201fi
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500202if [ "$COMMAND" = "resetdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700203 stopserver
S.Çağlar Onur3c297b42015-02-24 15:34:39 -0500204 ensure_postgres_running
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500205 dropdb
206 createdb
207 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500208fi
209if [ "$COMMAND" = "syncdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700210 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500211 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500212fi
213if [ "$COMMAND" = "runserver" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700214 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500215 runserver
216fi
Scott Bakerb4785022014-05-15 13:22:28 -0700217if [ "$COMMAND" = "stopserver" ]; then
218 stopserver
219fi
Scott Bakerf4db3812014-05-09 16:42:13 -0700220if [ "$COMMAND" = "dumpdata" ]; then
221 dumpdata
222fi
Sapan Bhatia51a92b12014-09-08 10:53:53 -0400223if [ "$COMMAND" = "genkeys" ]; then
224 genkeys
225fi
Scott Baker069ca8b2015-02-16 23:34:48 -0800226if [ "$COMMAND" = "generateapi" ]; then
Scott Bakerd82a9dd2015-02-16 23:47:11 -0800227 python apigen/modelgen apigen/api.template.py > xos/xosapi.py
Scott Bakerba4e0102015-02-18 16:42:14 -0800228fi
229if [ "$COMMAND" = "remigrate" ]; then
230 ensure_postgres_running
231 remigrate
232 createdb
233 syncdb
S.Çağlar Onur3c297b42015-02-24 15:34:39 -0500234fi
Tony Mackf4f57932015-10-31 14:18:11 +0000235if [ "$COMMAND" = "makemigrations" ]; then
236 makemigrations
237 syncdb
238fi