Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | if [ -z "$1" ]; then |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 4 | echo usage: $0 "[initdb | createdb | dropdb | syncdb | runserver | resetdb | dumpdata]" |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 5 | exit |
| 6 | fi |
| 7 | |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 8 | BACKUP_DIR=/opt/planetstack_backups |
| 9 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 10 | cd /opt/planetstack |
| 11 | |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 12 | function 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 Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 31 | sudo -u postgres initdb -D /var/lib/pgsql/data/ |
| 32 | sudo -u postgres pg_ctl -D /var/lib/pgsql/data -l logfile start |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 33 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 34 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 35 | } |
| 36 | function createdb { |
| 37 | echo "Creating OpenCloud database..." |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 38 | sudo -u postgres createdb planetstack |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 39 | } |
| 40 | function dropdb { |
| 41 | echo "Dropping OpenCloud database..." |
| 42 | sudo -u postgres dropdb planetstack |
| 43 | } |
| 44 | function syncdb { |
| 45 | echo "Syncing OpenCloud services..." |
| 46 | python /opt/planetstack/manage.py syncdb --noinput |
| 47 | } |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 48 | function evolvedb { |
| 49 | echo "Syncing OpenCloud services..." |
| 50 | python /opt/planetstack/manage.py evolve --hint --execute --noinput |
| 51 | } |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 52 | function stopserver { |
| 53 | echo "Stopping any running OpenCloud Service(s)" |
| 54 | pkill -f "python.*runserver" |
| 55 | } |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 56 | function runserver { |
Scott Baker | a381a41 | 2014-05-15 15:35:27 -0700 | [diff] [blame] | 57 | 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 Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 62 | function dumpdata { |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 63 | mkdir -p $BACKUP_DIR |
| 64 | FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json" |
| 65 | echo "Saving data to $FN" |
Scott Baker | a2e881c | 2014-08-15 16:52:55 -0700 | [diff] [blame] | 66 | python manage.py dumpdata core hpc syndicate_storage requestrouter -a --indent 4 > $FN |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 67 | if [[ ! -f $FN ]]; then |
| 68 | echo "FAILED to create $FN" |
| 69 | exit |
| 70 | fi |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 71 | 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 Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 77 | rm -f $BACKUP_DIR/dumpdata-latest.json |
| 78 | ln -s $FN $BACKUP_DIR/dumpdata-latest.json |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Sapan Bhatia | 51a92b1 | 2014-09-08 10:53:53 -0400 | [diff] [blame^] | 81 | function genkeys { |
| 82 | mkdir -p public_keys |
| 83 | mkdir -p private_keys |
| 84 | echo "Generating keys" |
| 85 | keyczart create --location=private_keys --name="OpenCloud" --purpose=crypt --asymmetric=rsa |
| 86 | keyczart addkey --location=private_keys --status=primary --size=1024 |
| 87 | keyczart pubkey --location=private_keys --destination=public_keys |
| 88 | if [[ ! -f public_keys/1 ]]; then |
| 89 | echo "FAILED to create keys" |
| 90 | exit |
| 91 | fi |
| 92 | } |
| 93 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 94 | COMMAND=$1 |
| 95 | |
| 96 | if [ "$COMMAND" = "initdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 97 | stopserver |
| 98 | ensure_postgres_running |
| 99 | createdb |
| 100 | syncdb |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 101 | fi |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 102 | if [ "$COMMAND" = "repairdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 103 | stopserver |
| 104 | ensure_postgres_running |
| 105 | dumpdata |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 106 | # TODO: This is where we could run migration scripts to upgrade the |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 107 | # dumped data to the new models. |
| 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 |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 111 | createdb |
| 112 | syncdb |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 113 | fi |
| 114 | if [ "$COMMAND" = "restoredb" ]; then |
| 115 | if [[ ! -f $BACKUP_DIR/dumpdata-latest.json ]]; then |
| 116 | echo There is no dumpdata to restore |
| 117 | exit |
| 118 | fi |
| 119 | stopserver |
| 120 | ensure_postgres_running |
| 121 | mv /opt/planetstack/core/fixtures/initial_data.json /opt/planetstack/core/fixtures/initial_data.json-old |
| 122 | cp $BACKUP_DIR/dumpdata-latest.json /opt/planetstack/core/fixtures/initial_data.json |
| 123 | dropdb |
| 124 | createdb |
| 125 | syncdb |
| 126 | fi |
| 127 | if [ "$COMMAND" = "evolvedb" ]; then |
| 128 | stopserver |
| 129 | ensure_postgres_running |
| 130 | evolvedb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 131 | fi |
| 132 | if [ "$COMMAND" = "resetdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 133 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 134 | dropdb |
| 135 | createdb |
| 136 | syncdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 137 | fi |
| 138 | if [ "$COMMAND" = "syncdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 139 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 140 | syncdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 141 | fi |
| 142 | if [ "$COMMAND" = "runserver" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 143 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 144 | runserver |
| 145 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 146 | if [ "$COMMAND" = "stopserver" ]; then |
| 147 | stopserver |
| 148 | fi |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 149 | if [ "$COMMAND" = "dumpdata" ]; then |
| 150 | dumpdata |
| 151 | fi |
Sapan Bhatia | 51a92b1 | 2014-09-08 10:53:53 -0400 | [diff] [blame^] | 152 | if [ "$COMMAND" = "genkeys" ]; then |
| 153 | genkeys |
| 154 | fi |