run migrate for django 1.7 or evolve for django 1.5
[plstackapi.git] / planetstack / scripts / opencloud
1 #!/bin/sh
2
3 if [ -z "$1" ]; then
4     echo usage: $0 "[initdb | createdb | dropdb | syncdb | runserver | resetdb | dumpdata]"
5     exit
6 fi
7
8 BACKUP_DIR=/opt/planetstack_backups
9
10 DJANGO_17=`python -c "import django; from distutils.version import StrictVersion; print int(StrictVersion(django.get_version()) >= StrictVersion('1.7'))"`
11
12 cd /opt/planetstack
13
14 function 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"
33         sudo -u postgres initdb -D /var/lib/pgsql/data/
34         sudo -u postgres pg_ctl -D /var/lib/pgsql/data -l logfile start
35     fi
36
37 }
38 function createdb {
39     echo "Creating OpenCloud database..."
40     sudo -u postgres createdb planetstack
41 }
42 function dropdb {
43     echo "Dropping OpenCloud database..."
44     sudo -u postgres dropdb planetstack
45 }
46 function syncdb {
47     echo "Syncing OpenCloud services..."
48     python /opt/planetstack/manage.py syncdb --noinput
49     if [[ $DJANGO_17 ]]; then
50         echo "Loading initial data from fixture..."
51         python /opt/planetstack/manage.py loaddata /opt/planetstack/core/fixtures/initial_data.json
52     fi
53 }
54 function evolvedb {
55     echo "Evolving OpenCloud services..."
56     python /opt/planetstack/manage.py evolve --hint --execute --noinput
57 }
58 function migratedb {
59     echo "Migrating OpenCloud services..."
60     python /opt/planetstack/manage.py migrate
61 }
62 function stopserver {
63     echo "Stopping any running OpenCloud Service(s)"
64     pkill -f "python.*runserver"
65 }
66 function runserver {
67     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&
70 }
71
72 function dumpdata {
73     mkdir -p $BACKUP_DIR
74     FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json"
75     echo "Saving data to $FN"
76     python manage.py dumpdata core hpc syndicate_storage requestrouter -a --indent 4 > $FN
77     if [[ ! -f $FN ]]; then
78         echo "FAILED to create $FN"
79         exit
80     fi
81     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
87     rm -f $BACKUP_DIR/dumpdata-latest.json
88     ln -s $FN $BACKUP_DIR/dumpdata-latest.json
89 }
90
91 function 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
104 COMMAND=$1
105
106 if [ "$COMMAND" = "initdb" ]; then
107     stopserver
108     ensure_postgres_running
109     createdb
110     syncdb
111 fi
112 if [ "$COMMAND" = "repairdb" ]; then
113     stopserver
114     ensure_postgres_running
115     dumpdata
116     # TODO: This is where we could run migration scripts to upgrade the
117     #   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
121     createdb
122     syncdb
123 fi
124 if [ "$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
136 fi
137 if [ "$COMMAND" = "evolvedb" -o "$COMMAND" = "migratedb" ]; then
138     stopserver
139     ensure_postgres_running
140     if [[ $DJANGO_17 ]]; then
141         migratedb
142     else
143         evolvedb
144     fi
145 fi
146 if [ "$COMMAND" = "resetdb" ]; then
147     stopserver
148     dropdb
149     createdb
150     syncdb
151 fi
152 if [ "$COMMAND" = "syncdb" ]; then
153     stopserver
154     syncdb
155 fi
156 if [ "$COMMAND" = "runserver" ]; then
157     stopserver
158     runserver
159 fi
160 if [ "$COMMAND" = "stopserver" ]; then
161     stopserver
162 fi
163 if [ "$COMMAND" = "dumpdata" ]; then
164     dumpdata
165 fi
166 if [ "$COMMAND" = "genkeys" ]; then
167     genkeys
168 fi