blob: 5093c4dfe3e8ae8f0e4ef421a7236d0be10180d5 [file] [log] [blame]
Siobhan Tully44fd4cc2014-02-23 00:07:12 -05001#!/bin/sh
2
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 Bakerb4785022014-05-15 13:22:28 -07008BACKUP_DIR=/opt/planetstack_backups
9
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050010cd /opt/planetstack
11
Scott Bakerb4785022014-05-15 13:22:28 -070012function ensure_postgres_running {
13 # "sudo -u postgres pg_ctl -D /var/lib/postgres/data status" doesn't work
14 # right on Vicci, so let's try to detect it by seeing if the port is
15 # being listened on
16
17 netstat -nl | grep -i ":5432 "
18 if [[ $? == 0 ]]; then
19 echo "Postgres is already running"
20 return
21 fi
22
23 /sbin/service postgresql initdb
24 /sbin/service postgresql start
25 /sbin/chkconfig postgresql on
26
27 netstat -nl | grep -i ":5432 "
28 if [[ $? != 0 ]]; then
29 # it's still not running
30 echo "Trying fallback mechanism to start Postgres"
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050031 sudo -u postgres initdb -D /var/lib/pgsql/data/
32 sudo -u postgres pg_ctl -D /var/lib/pgsql/data -l logfile start
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050033 fi
Scott Bakerb4785022014-05-15 13:22:28 -070034
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050035}
36function createdb {
37 echo "Creating OpenCloud database..."
Scott Bakerb4785022014-05-15 13:22:28 -070038 sudo -u postgres createdb planetstack
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050039}
40function dropdb {
41 echo "Dropping OpenCloud database..."
42 sudo -u postgres dropdb planetstack
43}
44function syncdb {
45 echo "Syncing OpenCloud services..."
46 python /opt/planetstack/manage.py syncdb --noinput
47}
Scott Baker25b70fd2014-05-15 14:11:58 -070048function evolvedb {
49 echo "Syncing OpenCloud services..."
50 python /opt/planetstack/manage.py evolve --hint --execute --noinput
51}
Scott Bakerb4785022014-05-15 13:22:28 -070052function stopserver {
53 echo "Stopping any running OpenCloud Service(s)"
54 pkill -f "python.*runserver"
55}
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050056function runserver {
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050057 echo "Starting OpenCloud Service on $HOSTNAME:8000"
58 python manage.py runserver $HOSTNAME:8000&
59}
60
Scott Bakerf4db3812014-05-09 16:42:13 -070061function dumpdata {
Scott Bakerb4785022014-05-15 13:22:28 -070062 mkdir -p $BACKUP_DIR
63 FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json"
64 echo "Saving data to $FN"
65 python manage.py dumpdata core hpc syndicate requestrouter --indent 4 > $FN
66 if [[ ! -f $FN ]]; then
67 echo "FAILED to create $FN"
68 exit
69 fi
Scott Baker25b70fd2014-05-15 14:11:58 -070070 SIZE=$(du -k "$FN" | cut -f 1)
71 if [[ $SIZE -lt 9 ]]; then
72 echo "Dumpdata was empty. Deleting and aborting"
73 rm $FN
74 exit
75 fi
Scott Bakerb4785022014-05-15 13:22:28 -070076 rm -f $BACKUP_DIR/dumpdata-latest.json
77 ln -s $FN $BACKUP_DIR/dumpdata-latest.json
Scott Bakerf4db3812014-05-09 16:42:13 -070078}
79
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050080COMMAND=$1
81
82if [ "$COMMAND" = "initdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -070083 stopserver
84 ensure_postgres_running
85 createdb
86 syncdb
Scott Bakerb4785022014-05-15 13:22:28 -070087fi
Scott Baker25b70fd2014-05-15 14:11:58 -070088if [ "$COMMAND" = "repairdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -070089 stopserver
90 ensure_postgres_running
91 dumpdata
Scott Baker25b70fd2014-05-15 14:11:58 -070092 # TODO: This is where we could run migration scripts to upgrade the
Scott Bakerb4785022014-05-15 13:22:28 -070093 # dumped data to the new models.
94 mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old
95 cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json
96 dropdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050097 createdb
98 syncdb
Scott Baker25b70fd2014-05-15 14:11:58 -070099fi
100if [ "$COMMAND" = "restoredb" ]; then
101 if [[ ! -f $BACKUP_DIR/dumpdata-latest.json ]]; then
102 echo There is no dumpdata to restore
103 exit
104 fi
105 stopserver
106 ensure_postgres_running
107 mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old
108 cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json
109 dropdb
110 createdb
111 syncdb
112fi
113if [ "$COMMAND" = "evolvedb" ]; then
114 stopserver
115 ensure_postgres_running
116 evolvedb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500117fi
118if [ "$COMMAND" = "resetdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700119 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500120 dropdb
121 createdb
122 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500123fi
124if [ "$COMMAND" = "syncdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700125 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500126 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500127fi
128if [ "$COMMAND" = "runserver" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700129 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500130 runserver
131fi
Scott Bakerb4785022014-05-15 13:22:28 -0700132if [ "$COMMAND" = "stopserver" ]; then
133 stopserver
134fi
Scott Bakerf4db3812014-05-09 16:42:13 -0700135if [ "$COMMAND" = "dumpdata" ]; then
136 dumpdata
137fi