blob: 54852b96076dfb67a9763a50ad0ed9f610a410c0 [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
Scott Baker02451372014-09-16 09:46:35 -070010DJANGO_17=`python -c "import django; from distutils.version import StrictVersion; print int(StrictVersion(django.get_version()) >= StrictVersion('1.7'))"`
11
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050012cd /opt/planetstack
13
Scott Bakerb4785022014-05-15 13:22:28 -070014function ensure_postgres_running {
15 # "sudo -u postgres pg_ctl -D /var/lib/postgres/data status" doesn't work
16 # right on Vicci, so let's try to detect it by seeing if the port is
17 # being listened on
18
19 netstat -nl | grep -i ":5432 "
20 if [[ $? == 0 ]]; then
21 echo "Postgres is already running"
22 return
23 fi
24
25 /sbin/service postgresql initdb
26 /sbin/service postgresql start
27 /sbin/chkconfig postgresql on
28
29 netstat -nl | grep -i ":5432 "
30 if [[ $? != 0 ]]; then
31 # it's still not running
32 echo "Trying fallback mechanism to start Postgres"
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050033 sudo -u postgres initdb -D /var/lib/pgsql/data/
34 sudo -u postgres pg_ctl -D /var/lib/pgsql/data -l logfile start
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050035 fi
Scott Bakerb4785022014-05-15 13:22:28 -070036
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050037}
38function createdb {
39 echo "Creating OpenCloud database..."
Scott Bakerb4785022014-05-15 13:22:28 -070040 sudo -u postgres createdb planetstack
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050041}
42function dropdb {
43 echo "Dropping OpenCloud database..."
44 sudo -u postgres dropdb planetstack
45}
46function syncdb {
47 echo "Syncing OpenCloud services..."
48 python /opt/planetstack/manage.py syncdb --noinput
Scott Baker02451372014-09-16 09:46:35 -070049 if [[ $DJANGO_17 ]]; then
50 echo "Loading initial data from fixture..."
Scott Bakerd3a5a2e2014-11-19 18:01:13 -080051 python /opt/planetstack/manage.py --noobserver --nomodelpolicy loaddata /opt/planetstack/core/fixtures/initial_data.json
Scott Baker02451372014-09-16 09:46:35 -070052 fi
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050053}
Scott Baker25b70fd2014-05-15 14:11:58 -070054function evolvedb {
Scott Baker6ecdc422014-09-12 15:10:01 -070055 echo "Evolving OpenCloud services..."
Scott Baker25b70fd2014-05-15 14:11:58 -070056 python /opt/planetstack/manage.py evolve --hint --execute --noinput
57}
Scott Baker6ecdc422014-09-12 15:10:01 -070058function migratedb {
59 echo "Migrating OpenCloud services..."
60 python /opt/planetstack/manage.py migrate
61}
Scott Bakerb4785022014-05-15 13:22:28 -070062function stopserver {
63 echo "Stopping any running OpenCloud Service(s)"
64 pkill -f "python.*runserver"
65}
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050066function runserver {
Scott Bakera381a412014-05-15 15:35:27 -070067 PUBLIC_HOSTNAME=`/opt/planetstack/planetstack-config.py get server_hostname $HOSTNAME`
68 echo "Starting OpenCloud Service on $PUBLIC_HOSTNAME:8000"
69 python manage.py runserver $PUBLIC_HOSTNAME:8000&
Siobhan Tully44fd4cc2014-02-23 00:07:12 -050070}
71
Scott Bakerf4db3812014-05-09 16:42:13 -070072function dumpdata {
Scott Bakerb4785022014-05-15 13:22:28 -070073 mkdir -p $BACKUP_DIR
74 FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json"
75 echo "Saving data to $FN"
Scott Bakera2e881c2014-08-15 16:52:55 -070076 python manage.py dumpdata core hpc syndicate_storage requestrouter -a --indent 4 > $FN
Scott Bakerb4785022014-05-15 13:22:28 -070077 if [[ ! -f $FN ]]; then
78 echo "FAILED to create $FN"
79 exit
80 fi
Scott Baker25b70fd2014-05-15 14:11:58 -070081 SIZE=$(du -k "$FN" | cut -f 1)
82 if [[ $SIZE -lt 9 ]]; then
83 echo "Dumpdata was empty. Deleting and aborting"
84 rm $FN
85 exit
86 fi
Scott Bakerb4785022014-05-15 13:22:28 -070087 rm -f $BACKUP_DIR/dumpdata-latest.json
88 ln -s $FN $BACKUP_DIR/dumpdata-latest.json
Scott Bakerf4db3812014-05-09 16:42:13 -070089}
90
Sapan Bhatia0e8769f2014-09-08 10:53:53 -040091function genkeys {
92 mkdir -p public_keys
93 mkdir -p private_keys
94 echo "Generating keys"
95 keyczart create --location=private_keys --name="OpenCloud" --purpose=crypt --asymmetric=rsa
96 keyczart addkey --location=private_keys --status=primary --size=1024
97 keyczart pubkey --location=private_keys --destination=public_keys
98 if [[ ! -f public_keys/1 ]]; then
99 echo "FAILED to create keys"
100 exit
101 fi
102}
103
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500104COMMAND=$1
105
106if [ "$COMMAND" = "initdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700107 stopserver
108 ensure_postgres_running
109 createdb
110 syncdb
Scott Bakerb4785022014-05-15 13:22:28 -0700111fi
Scott Baker25b70fd2014-05-15 14:11:58 -0700112if [ "$COMMAND" = "repairdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700113 stopserver
114 ensure_postgres_running
115 dumpdata
Scott Baker25b70fd2014-05-15 14:11:58 -0700116 # TODO: This is where we could run migration scripts to upgrade the
Scott Bakerb4785022014-05-15 13:22:28 -0700117 # dumped data to the new models.
118 mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old
119 cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json
120 dropdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500121 createdb
122 syncdb
Scott Baker25b70fd2014-05-15 14:11:58 -0700123fi
124if [ "$COMMAND" = "restoredb" ]; then
125 if [[ ! -f $BACKUP_DIR/dumpdata-latest.json ]]; then
126 echo There is no dumpdata to restore
127 exit
128 fi
129 stopserver
130 ensure_postgres_running
131 mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old
132 cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json
133 dropdb
134 createdb
135 syncdb
136fi
Scott Baker4bb7cbc2014-09-17 22:19:54 -0700137if [ "$COMMAND" = "evolvedb" -o "$COMMAND" = "migratedb" ]; then
Scott Baker25b70fd2014-05-15 14:11:58 -0700138 stopserver
139 ensure_postgres_running
Scott Baker4bb7cbc2014-09-17 22:19:54 -0700140 if [[ $DJANGO_17 ]]; then
141 migratedb
142 else
143 evolvedb
144 fi
Scott Baker6ecdc422014-09-12 15:10:01 -0700145fi
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500146if [ "$COMMAND" = "resetdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700147 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500148 dropdb
149 createdb
150 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500151fi
152if [ "$COMMAND" = "syncdb" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700153 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500154 syncdb
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500155fi
156if [ "$COMMAND" = "runserver" ]; then
Scott Bakerb4785022014-05-15 13:22:28 -0700157 stopserver
Siobhan Tully44fd4cc2014-02-23 00:07:12 -0500158 runserver
159fi
Scott Bakerb4785022014-05-15 13:22:28 -0700160if [ "$COMMAND" = "stopserver" ]; then
161 stopserver
162fi
Scott Bakerf4db3812014-05-09 16:42:13 -0700163if [ "$COMMAND" = "dumpdata" ]; then
164 dumpdata
165fi
Sapan Bhatia0e8769f2014-09-08 10:53:53 -0400166if [ "$COMMAND" = "genkeys" ]; then
167 genkeys
168fi