blob: 88a8d8853dffa89a7602f33cecf67b6aa2fbb592 [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 Bakerb4785022014-05-15 13:22:28 -070048function stopserver {
49 echo "Stopping any running OpenCloud Service(s)"
50 pkill -f "python.*runserver"
51}
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050052function runserver {
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050053 echo "Starting OpenCloud Service on $HOSTNAME:8000"
54 python manage.py runserver $HOSTNAME:8000&
55}
56
Scott Bakerf4db3812014-05-09 16:42:13 -070057function dumpdata {
Scott Bakerb4785022014-05-15 13:22:28 -070058 mkdir -p $BACKUP_DIR
59 FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json"
60 echo "Saving data to $FN"
61 python manage.py dumpdata core hpc syndicate requestrouter --indent 4 > $FN
62 if [[ ! -f $FN ]]; then
63 echo "FAILED to create $FN"
64 exit
65 fi
66 rm -f $BACKUP_DIR/dumpdata-latest.json
67 ln -s $FN $BACKUP_DIR/dumpdata-latest.json
Scott Bakerf4db3812014-05-09 16:42:13 -070068}
69
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050070COMMAND=$1
71
72if [ "$COMMAND" = "initdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -070073 stopserver
74 ensure_postgres_running
75 createdb
76 syncdb
77 runserver
78fi
79if [ "$COMMAND" = "upgradedb" ]; then
80 stopserver
81 ensure_postgres_running
82 dumpdata
83 # TODO: This is where we would run migration scripts to upgrade the
84 # dumped data to the new models.
85 mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old
86 cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json
87 dropdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050088 createdb
89 syncdb
90 runserver
91fi
92if [ "$COMMAND" = "resetdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -070093 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050094 dropdb
95 createdb
96 syncdb
97 runserver
98fi
99if [ "$COMMAND" = "syncdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700100 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500101 syncdb
102 runserver
103fi
104if [ "$COMMAND" = "runserver" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700105 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500106 runserver
107fi
Scott Bakerb4785022014-05-15 13:22:28 -0700108if [ "$COMMAND" = "stopserver" ]; then
109 stopserver
110fi
Scott Bakerf4db3812014-05-09 16:42:13 -0700111if [ "$COMMAND" = "dumpdata" ]; then
112 dumpdata
113fi