blob: 901ad80a3aacf4952ec8c0e911abedc0bb4a1e95 [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 {
Scott Bakere363ac02014-09-12 15:10:01 -070049 echo "Evolving OpenCloud services..."
Scott Baker25b70fd2014-05-15 14:11:58 -070050 python /opt/planetstack/manage.py evolve --hint --execute --noinput
51}
Scott Bakere363ac02014-09-12 15:10:01 -070052function migratedb {
53 echo "Migrating OpenCloud services..."
54 python /opt/planetstack/manage.py migrate
55}
Scott Bakerb4785022014-05-15 13:22:28 -070056function stopserver {
57 echo "Stopping any running OpenCloud Service(s)"
58 pkill -f "python.*runserver"
59}
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050060function runserver {
Scott Bakera381a412014-05-15 15:35:27 -070061 PUBLIC_HOSTNAME=`/opt/planetstack/planetstack-config.py get server_hostname $HOSTNAME`
62 echo "Starting OpenCloud Service on $PUBLIC_HOSTNAME:8000"
63 python manage.py runserver $PUBLIC_HOSTNAME:8000&
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050064}
65
Scott Bakerf4db3812014-05-09 16:42:13 -070066function dumpdata {
Scott Bakerb4785022014-05-15 13:22:28 -070067 mkdir -p $BACKUP_DIR
68 FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json"
69 echo "Saving data to $FN"
Scott Bakera2e881c2014-08-15 16:52:55 -070070 python manage.py dumpdata core hpc syndicate_storage requestrouter -a --indent 4 > $FN
Scott Bakerb4785022014-05-15 13:22:28 -070071 if [[ ! -f $FN ]]; then
72 echo "FAILED to create $FN"
73 exit
74 fi
Scott Baker25b70fd2014-05-15 14:11:58 -070075 SIZE=$(du -k "$FN" | cut -f 1)
76 if [[ $SIZE -lt 9 ]]; then
77 echo "Dumpdata was empty. Deleting and aborting"
78 rm $FN
79 exit
80 fi
Scott Bakerb4785022014-05-15 13:22:28 -070081 rm -f $BACKUP_DIR/dumpdata-latest.json
82 ln -s $FN $BACKUP_DIR/dumpdata-latest.json
Scott Bakerf4db3812014-05-09 16:42:13 -070083}
84
Sapan Bhatia51a92b12014-09-08 10:53:53 -040085function genkeys {
86 mkdir -p public_keys
87 mkdir -p private_keys
88 echo "Generating keys"
89 keyczart create --location=private_keys --name="OpenCloud" --purpose=crypt --asymmetric=rsa
90 keyczart addkey --location=private_keys --status=primary --size=1024
91 keyczart pubkey --location=private_keys --destination=public_keys
92 if [[ ! -f public_keys/1 ]]; then
93 echo "FAILED to create keys"
94 exit
95 fi
96}
97
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050098COMMAND=$1
99
100if [ "$COMMAND" = "initdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700101 stopserver
102 ensure_postgres_running
103 createdb
104 syncdb
Scott Bakerb4785022014-05-15 13:22:28 -0700105fi
Scott Baker25b70fd2014-05-15 14:11:58 -0700106if [ "$COMMAND" = "repairdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700107 stopserver
108 ensure_postgres_running
109 dumpdata
Scott Baker25b70fd2014-05-15 14:11:58 -0700110 # TODO: This is where we could run migration scripts to upgrade the
Scott Bakerb4785022014-05-15 13:22:28 -0700111 # dumped data to the new models.
112 mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old
113 cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json
114 dropdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500115 createdb
116 syncdb
Scott Baker25b70fd2014-05-15 14:11:58 -0700117fi
118if [ "$COMMAND" = "restoredb" ]; then
119 if [[ ! -f $BACKUP_DIR/dumpdata-latest.json ]]; then
120 echo There is no dumpdata to restore
121 exit
122 fi
123 stopserver
124 ensure_postgres_running
125 mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old
126 cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json
127 dropdb
128 createdb
129 syncdb
130fi
131if [ "$COMMAND" = "evolvedb" ]; then
132 stopserver
133 ensure_postgres_running
134 evolvedb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500135fi
Scott Bakere363ac02014-09-12 15:10:01 -0700136if [ "$COMMAND" = "migratedb" ]; then
137 stopserver
138 ensure_postgres_running
139 migratedb
140fi
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500141if [ "$COMMAND" = "resetdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700142 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500143 dropdb
144 createdb
145 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500146fi
147if [ "$COMMAND" = "syncdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700148 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500149 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500150fi
151if [ "$COMMAND" = "runserver" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700152 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500153 runserver
154fi
Scott Bakerb4785022014-05-15 13:22:28 -0700155if [ "$COMMAND" = "stopserver" ]; then
156 stopserver
157fi
Scott Bakerf4db3812014-05-09 16:42:13 -0700158if [ "$COMMAND" = "dumpdata" ]; then
159 dumpdata
160fi
Sapan Bhatia51a92b12014-09-08 10:53:53 -0400161if [ "$COMMAND" = "genkeys" ]; then
162 genkeys
163fi