blob: f26d8d0daf09a678e0b4a8a873ef83ae3631ef41 [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
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500106COMMAND=$1
107
108if [ "$COMMAND" = "initdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700109 stopserver
110 ensure_postgres_running
111 createdb
112 syncdb
Scott Bakerb4785022014-05-15 13:22:28 -0700113fi
Scott Baker25b70fd2014-05-15 14:11:58 -0700114if [ "$COMMAND" = "repairdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700115 stopserver
116 ensure_postgres_running
117 dumpdata
Scott Baker25b70fd2014-05-15 14:11:58 -0700118 # TODO: This is where we could run migration scripts to upgrade the
Scott Bakerb4785022014-05-15 13:22:28 -0700119 # dumped data to the new models.
Scott Baker596397c2015-02-04 22:47:45 -0800120 mv $XOS_DIR/core/fixtures/initial_data.json $XOS_DIR/core/fixtures/initial_data.json-old
121 cp $BACKUP_DIR/dumpdata-latest.json $XOS_DIR/core/fixtures/initial_data.json
Scott Bakerb4785022014-05-15 13:22:28 -0700122 dropdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500123 createdb
124 syncdb
Scott Baker25b70fd2014-05-15 14:11:58 -0700125fi
126if [ "$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 Baker596397c2015-02-04 22:47:45 -0800133 mv $XOS_DIR/core/fixtures/initial_data.json $XOS_DIR/core/fixtures/initial_data.json-old
134 cp $BACKUP_DIR/dumpdata-latest.json $XOS_DIR/core/fixtures/initial_data.json
Scott Baker25b70fd2014-05-15 14:11:58 -0700135 dropdb
136 createdb
137 syncdb
138fi
Scott Bakerbaf62562014-09-17 22:19:54 -0700139if [ "$COMMAND" = "evolvedb" -o "$COMMAND" = "migratedb" ]; then
Scott Baker25b70fd2014-05-15 14:11:58 -0700140 stopserver
141 ensure_postgres_running
Scott Bakerbaf62562014-09-17 22:19:54 -0700142 if [[ $DJANGO_17 ]]; then
143 migratedb
144 else
145 evolvedb
146 fi
Scott Bakere363ac02014-09-12 15:10:01 -0700147fi
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500148if [ "$COMMAND" = "resetdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700149 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500150 dropdb
151 createdb
152 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500153fi
154if [ "$COMMAND" = "syncdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700155 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500156 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500157fi
158if [ "$COMMAND" = "runserver" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700159 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500160 runserver
161fi
Scott Bakerb4785022014-05-15 13:22:28 -0700162if [ "$COMMAND" = "stopserver" ]; then
163 stopserver
164fi
Scott Bakerf4db3812014-05-09 16:42:13 -0700165if [ "$COMMAND" = "dumpdata" ]; then
166 dumpdata
167fi
Sapan Bhatia51a92b12014-09-08 10:53:53 -0400168if [ "$COMMAND" = "genkeys" ]; then
169 genkeys
170fi
Scott Baker069ca8b2015-02-16 23:34:48 -0800171if [ "$COMMAND" = "generateapi" ]; then
Scott Bakerd82a9dd2015-02-16 23:47:11 -0800172 python apigen/modelgen apigen/api.template.py > xos/xosapi.py
Scott Baker069ca8b2015-02-16 23:34:48 -0800173fi