Andy Bavier | e4b346e | 2015-02-06 16:39:06 -0500 | [diff] [blame] | 1 | #!/bin/bash |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 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 | 06116da | 2015-02-04 21:50:08 -0800 | [diff] [blame] | 8 | XOS_DIR=/opt/xos |
| 9 | BACKUP_DIR=/opt/xos_backups |
Scott Baker | d2ebcd4 | 2015-02-19 21:56:58 -0800 | [diff] [blame] | 10 | DBNAME=xos |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 11 | |
Scott Baker | 0245137 | 2014-09-16 09:46:35 -0700 | [diff] [blame] | 12 | DJANGO_17=`python -c "import django; from distutils.version import StrictVersion; print int(StrictVersion(django.get_version()) >= StrictVersion('1.7'))"` |
| 13 | |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 14 | cd $XOS_DIR |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 15 | |
Scott Baker | 5bec759 | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 16 | function is_ubuntu { |
| 17 | if ! which lsb_release &> /dev/null; then |
| 18 | # lsb_release is not installed |
| 19 | return 1 |
| 20 | fi |
| 21 | if lsb_release -i | grep -i ubuntu &> /dev/null; then |
| 22 | return 0 |
| 23 | fi |
| 24 | return 2 |
| 25 | } |
| 26 | |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 27 | function ensure_postgres_running { |
| 28 | # "sudo -u postgres pg_ctl -D /var/lib/postgres/data status" doesn't work |
| 29 | # right on Vicci, so let's try to detect it by seeing if the port is |
| 30 | # being listened on |
| 31 | |
Scott Baker | 5bec759 | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 32 | netstat -nl | grep -i ":5432 " > /dev/null |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 33 | if [[ $? == 0 ]]; then |
| 34 | echo "Postgres is already running" |
| 35 | return |
| 36 | fi |
| 37 | |
Scott Baker | 5bec759 | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 38 | # note that initdb isn't needed in Ubuntu distributions, and calling it |
| 39 | # will throw spurious error messages |
| 40 | if ! is_ubuntu; then |
| 41 | service postgresql initdb |
| 42 | fi |
Andy Bavier | e4b346e | 2015-02-06 16:39:06 -0500 | [diff] [blame] | 43 | service postgresql start |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 44 | |
Scott Baker | 5bec759 | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 45 | netstat -nl | grep -i ":5432 " > /dev/null |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 46 | if [[ $? != 0 ]]; then |
Scott Baker | 5bec759 | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 47 | # it's still not running... |
| 48 | # this is intended for Vicci where some D-Bus issue is |
| 49 | # preventing systemctl from working properly. |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 50 | echo "Trying fallback mechanism to start Postgres" |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 51 | sudo -u postgres initdb -D /var/lib/pgsql/data/ |
| 52 | 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] | 53 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 54 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 55 | } |
Scott Baker | 5bec759 | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 56 | |
Scott Baker | f8435ca | 2015-07-20 10:57:08 -0700 | [diff] [blame] | 57 | function wait_postgres { |
| 58 | sudo -u postgres psql -c '\q' |
| 59 | while [[ "$?" != "0" ]]; do |
| 60 | echo Waiting for postgres to start |
| 61 | sleep 1 |
| 62 | sudo -u postgres psql -c '\q' |
| 63 | done |
| 64 | } |
| 65 | |
Scott Baker | 5bec759 | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 66 | function db_exists { |
| 67 | sudo -u postgres psql $DBNAME -c '\q' 2>/dev/null |
| 68 | return $? |
| 69 | } |
| 70 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 71 | function createdb { |
Scott Baker | f8435ca | 2015-07-20 10:57:08 -0700 | [diff] [blame] | 72 | wait_postgres |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 73 | echo "Creating XOS database..." |
| 74 | sudo -u postgres createdb $DBNAME |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 75 | } |
| 76 | function dropdb { |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 77 | echo "Dropping XOS database..." |
| 78 | sudo -u postgres dropdb $DBNAME |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 79 | } |
| 80 | function syncdb { |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 81 | echo "Syncing XOS services..." |
Scott Baker | a8b263b | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 82 | python $XOS_DIR/manage.py syncdb --noinput |
Scott Baker | 0245137 | 2014-09-16 09:46:35 -0700 | [diff] [blame] | 83 | if [[ $DJANGO_17 ]]; then |
| 84 | echo "Loading initial data from fixture..." |
Scott Baker | a8b263b | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 85 | python $XOS_DIR/manage.py --noobserver --nomodelpolicy loaddata $XOS_DIR/core/fixtures/initial_data.json |
Scott Baker | 0245137 | 2014-09-16 09:46:35 -0700 | [diff] [blame] | 86 | fi |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 87 | } |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 88 | function evolvedb { |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 89 | echo "Evolving XOS services..." |
Scott Baker | a8b263b | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 90 | python $XOS_DIR/manage.py evolve --hint --execute --noinput |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 91 | } |
Scott Baker | 6ecdc42 | 2014-09-12 15:10:01 -0700 | [diff] [blame] | 92 | function migratedb { |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 93 | echo "Migrating XOS services..." |
Scott Baker | a8b263b | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 94 | python $XOS_DIR/manage.py migrate |
Scott Baker | 6ecdc42 | 2014-09-12 15:10:01 -0700 | [diff] [blame] | 95 | } |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 96 | function stopserver { |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 97 | echo "Stopping any running XOS Service(s)" |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 98 | pkill -f "python.*runserver" |
| 99 | } |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 100 | function runserver { |
Andy Bavier | e4b346e | 2015-02-06 16:39:06 -0500 | [diff] [blame] | 101 | ensure_postgres_running |
Scott Baker | a8b263b | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 102 | PUBLIC_HOSTNAME=`$XOS_DIR/xos-config.py get server_hostname $HOSTNAME` |
Scott Baker | dc74f7d | 2015-02-04 15:04:26 -0800 | [diff] [blame] | 103 | echo "Starting XOS Service on $PUBLIC_HOSTNAME:8000" |
Scott Baker | e712c79 | 2015-02-06 16:26:04 -0800 | [diff] [blame] | 104 | python manage.py runserver $PUBLIC_HOSTNAME:8000 --insecure& |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 107 | function dumpdata { |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 108 | mkdir -p $BACKUP_DIR |
| 109 | FN="$BACKUP_DIR/dumpdata-`date +%Y-%m-%d_%H:%M:%S`.json" |
| 110 | echo "Saving data to $FN" |
Scott Baker | a2e881c | 2014-08-15 16:52:55 -0700 | [diff] [blame] | 111 | 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] | 112 | if [[ ! -f $FN ]]; then |
| 113 | echo "FAILED to create $FN" |
| 114 | exit |
| 115 | fi |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 116 | SIZE=$(du -k "$FN" | cut -f 1) |
| 117 | if [[ $SIZE -lt 9 ]]; then |
| 118 | echo "Dumpdata was empty. Deleting and aborting" |
| 119 | rm $FN |
| 120 | exit |
| 121 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 122 | rm -f $BACKUP_DIR/dumpdata-latest.json |
| 123 | ln -s $FN $BACKUP_DIR/dumpdata-latest.json |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Sapan Bhatia | 0e8769f | 2014-09-08 10:53:53 -0400 | [diff] [blame] | 126 | function genkeys { |
| 127 | mkdir -p public_keys |
| 128 | mkdir -p private_keys |
| 129 | echo "Generating keys" |
| 130 | keyczart create --location=private_keys --name="OpenCloud" --purpose=crypt --asymmetric=rsa |
| 131 | keyczart addkey --location=private_keys --status=primary --size=1024 |
| 132 | keyczart pubkey --location=private_keys --destination=public_keys |
| 133 | if [[ ! -f public_keys/1 ]]; then |
| 134 | echo "FAILED to create keys" |
| 135 | exit |
| 136 | fi |
| 137 | } |
| 138 | |
Scott Baker | 4d8a638 | 2015-02-18 16:42:14 -0800 | [diff] [blame] | 139 | function remigrate { |
Scott Baker | 5bec759 | 2015-02-19 11:34:41 -0800 | [diff] [blame] | 140 | if db_exists; then |
| 141 | dropdb |
| 142 | fi |
Scott Baker | 4d8a638 | 2015-02-18 16:42:14 -0800 | [diff] [blame] | 143 | rm -rf /opt/xos/*/migrations |
| 144 | python ./manage.py makemigrations core |
| 145 | python ./manage.py makemigrations hpc |
| 146 | python ./manage.py makemigrations requestrouter |
| 147 | python ./manage.py makemigrations syndicate_storage |
Scott Baker | 9ff6960 | 2015-05-06 12:04:09 -0700 | [diff] [blame] | 148 | python ./manage.py makemigrations cord |
Scott Baker | 92a05d7 | 2015-09-10 09:02:01 -0700 | [diff] [blame] | 149 | python ./manage.py makemigrations ceilometer |
Scott Baker | 4d8a638 | 2015-02-18 16:42:14 -0800 | [diff] [blame] | 150 | #python ./manage.py makemigrations servcomp |
| 151 | } |
| 152 | |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 153 | COMMAND=$1 |
| 154 | |
| 155 | if [ "$COMMAND" = "initdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 156 | stopserver |
| 157 | ensure_postgres_running |
| 158 | createdb |
| 159 | syncdb |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 160 | fi |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 161 | if [ "$COMMAND" = "repairdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 162 | stopserver |
| 163 | ensure_postgres_running |
| 164 | dumpdata |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 165 | # TODO: This is where we could run migration scripts to upgrade the |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 166 | # dumped data to the new models. |
Scott Baker | a8b263b | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 167 | mv $XOS_DIR/core/fixtures/initial_data.json $XOS_DIR/core/fixtures/initial_data.json-old |
| 168 | cp $BACKUP_DIR/dumpdata-latest.json $XOS_DIR/core/fixtures/initial_data.json |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 169 | dropdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 170 | createdb |
| 171 | syncdb |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 172 | fi |
| 173 | if [ "$COMMAND" = "restoredb" ]; then |
| 174 | if [[ ! -f $BACKUP_DIR/dumpdata-latest.json ]]; then |
| 175 | echo There is no dumpdata to restore |
| 176 | exit |
| 177 | fi |
| 178 | stopserver |
| 179 | ensure_postgres_running |
Scott Baker | a8b263b | 2015-02-04 22:47:45 -0800 | [diff] [blame] | 180 | mv $XOS_DIR/core/fixtures/initial_data.json $XOS_DIR/core/fixtures/initial_data.json-old |
| 181 | cp $BACKUP_DIR/dumpdata-latest.json $XOS_DIR/core/fixtures/initial_data.json |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 182 | dropdb |
| 183 | createdb |
| 184 | syncdb |
| 185 | fi |
Scott Baker | 4bb7cbc | 2014-09-17 22:19:54 -0700 | [diff] [blame] | 186 | if [ "$COMMAND" = "evolvedb" -o "$COMMAND" = "migratedb" ]; then |
Scott Baker | 25b70fd | 2014-05-15 14:11:58 -0700 | [diff] [blame] | 187 | stopserver |
| 188 | ensure_postgres_running |
Scott Baker | 4bb7cbc | 2014-09-17 22:19:54 -0700 | [diff] [blame] | 189 | if [[ $DJANGO_17 ]]; then |
| 190 | migratedb |
| 191 | else |
| 192 | evolvedb |
| 193 | fi |
Scott Baker | 6ecdc42 | 2014-09-12 15:10:01 -0700 | [diff] [blame] | 194 | fi |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 195 | if [ "$COMMAND" = "resetdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 196 | stopserver |
S.Çağlar Onur | 995e07d | 2015-02-24 15:34:39 -0500 | [diff] [blame] | 197 | ensure_postgres_running |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 198 | dropdb |
| 199 | createdb |
| 200 | syncdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 201 | fi |
| 202 | if [ "$COMMAND" = "syncdb" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 203 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 204 | syncdb |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 205 | fi |
| 206 | if [ "$COMMAND" = "runserver" ]; then |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 207 | stopserver |
Siobhan Tully | 44fd4cc | 2014-02-23 00:07:12 -0500 | [diff] [blame] | 208 | runserver |
| 209 | fi |
Scott Baker | b478502 | 2014-05-15 13:22:28 -0700 | [diff] [blame] | 210 | if [ "$COMMAND" = "stopserver" ]; then |
| 211 | stopserver |
| 212 | fi |
Scott Baker | f4db381 | 2014-05-09 16:42:13 -0700 | [diff] [blame] | 213 | if [ "$COMMAND" = "dumpdata" ]; then |
| 214 | dumpdata |
| 215 | fi |
Sapan Bhatia | 0e8769f | 2014-09-08 10:53:53 -0400 | [diff] [blame] | 216 | if [ "$COMMAND" = "genkeys" ]; then |
| 217 | genkeys |
| 218 | fi |
Scott Baker | c57ddcc | 2015-02-16 23:34:48 -0800 | [diff] [blame] | 219 | if [ "$COMMAND" = "generateapi" ]; then |
Scott Baker | cd4a0c2 | 2015-02-16 23:47:11 -0800 | [diff] [blame] | 220 | python apigen/modelgen apigen/api.template.py > xos/xosapi.py |
Scott Baker | 4d8a638 | 2015-02-18 16:42:14 -0800 | [diff] [blame] | 221 | fi |
| 222 | if [ "$COMMAND" = "remigrate" ]; then |
| 223 | ensure_postgres_running |
| 224 | remigrate |
| 225 | createdb |
| 226 | syncdb |
S.Çağlar Onur | 995e07d | 2015-02-24 15:34:39 -0500 | [diff] [blame] | 227 | fi |