blob: daee3d631f31ec7d240b6b0bc7fda60479502ded [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 {
Scott Bakera381a412014-05-15 15:35:27 -070057 PUBLIC_HOSTNAME=`/opt/planetstack/planetstack-config.py get server_hostname $HOSTNAME`
58 echo "Starting OpenCloud Service on $PUBLIC_HOSTNAME:8000"
59 python manage.py runserver $PUBLIC_HOSTNAME:8000&
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050060}
61
Scott Bakerf4db3812014-05-09 16:42:13 -070062function dumpdata {
Scott Bakerb4785022014-05-15 13:22:28 -070063 mkdir -p $BACKUP_DIR
64 FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json"
65 echo "Saving data to $FN"
Scott Bakera2e881c2014-08-15 16:52:55 -070066 python manage.py dumpdata core hpc syndicate_storage requestrouter -a --indent 4 > $FN
Scott Bakerb4785022014-05-15 13:22:28 -070067 if [[ ! -f $FN ]]; then
68 echo "FAILED to create $FN"
69 exit
70 fi
Scott Baker25b70fd2014-05-15 14:11:58 -070071 SIZE=$(du -k "$FN" | cut -f 1)
72 if [[ $SIZE -lt 9 ]]; then
73 echo "Dumpdata was empty. Deleting and aborting"
74 rm $FN
75 exit
76 fi
Scott Bakerb4785022014-05-15 13:22:28 -070077 rm -f $BACKUP_DIR/dumpdata-latest.json
78 ln -s $FN $BACKUP_DIR/dumpdata-latest.json
Scott Bakerf4db3812014-05-09 16:42:13 -070079}
80
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050081COMMAND=$1
82
83if [ "$COMMAND" = "initdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -070084 stopserver
85 ensure_postgres_running
86 createdb
87 syncdb
Scott Bakerb4785022014-05-15 13:22:28 -070088fi
Scott Baker25b70fd2014-05-15 14:11:58 -070089if [ "$COMMAND" = "repairdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -070090 stopserver
91 ensure_postgres_running
92 dumpdata
Scott Baker25b70fd2014-05-15 14:11:58 -070093 # TODO: This is where we could run migration scripts to upgrade the
Scott Bakerb4785022014-05-15 13:22:28 -070094 # dumped data to the new models.
95 mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old
96 cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json
97 dropdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050098 createdb
99 syncdb
Scott Baker25b70fd2014-05-15 14:11:58 -0700100fi
101if [ "$COMMAND" = "restoredb" ]; then
102 if [[ ! -f $BACKUP_DIR/dumpdata-latest.json ]]; then
103 echo There is no dumpdata to restore
104 exit
105 fi
106 stopserver
107 ensure_postgres_running
108 mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old
109 cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json
110 dropdb
111 createdb
112 syncdb
113fi
114if [ "$COMMAND" = "evolvedb" ]; then
115 stopserver
116 ensure_postgres_running
117 evolvedb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500118fi
119if [ "$COMMAND" = "resetdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700120 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500121 dropdb
122 createdb
123 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500124fi
125if [ "$COMMAND" = "syncdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700126 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500127 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500128fi
129if [ "$COMMAND" = "runserver" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700130 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500131 runserver
132fi
Scott Bakerb4785022014-05-15 13:22:28 -0700133if [ "$COMMAND" = "stopserver" ]; then
134 stopserver
135fi
Scott Bakerf4db3812014-05-09 16:42:13 -0700136if [ "$COMMAND" = "dumpdata" ]; then
137 dumpdata
138fi