|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# configuration |
| 4 | +SCHEMA_VERSION='327df67' |
| 5 | +DUMP_VERSION='v2' |
| 6 | + |
| 7 | +# setup |
| 8 | +build_dependencies='curl git build-essential' |
| 9 | +sudo apt-get update > /dev/null |
| 10 | +sudo apt-get install -y ${build_dependencies} > /dev/null |
| 11 | + |
| 12 | +# install postgres |
| 13 | +ops_password="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32)" |
| 14 | +frontend_password="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32)" |
| 15 | +sudo apt-get install -y postgresql postgis > /dev/null |
| 16 | +sudo systemctl enable postgresql |
| 17 | +sudo service postgresql start |
| 18 | +echo "CREATE DATABASE features;" | sudo -u postgres psql |
| 19 | +echo "CREATE USER ops WITH login password 'changeme';" | sudo -u postgres psql |
| 20 | +echo "CREATE USER frontend WITH login password 'changeme';" | sudo -u postgres psql |
| 21 | +echo "ALTER USER ops WITH password '${ops_password}';" | sudo -u postgres psql |
| 22 | +echo "ALTER USER frontend WITH password '${frontend_password}';" | sudo -u postgres psql |
| 23 | +echo "GRANT ops TO postgres;" | sudo -u postgres psql |
| 24 | +echo "GRANT frontend TO postgres;" | sudo -u postgres psql |
| 25 | +curl -sL "https://raw.githubusercontent.com/CatalystCode/featureService/${SCHEMA_VERSION}/schema.sql" | sudo -u postgres psql -qd features |
| 26 | + |
| 27 | +# populate postgres |
| 28 | +dbdump="$(mktemp)" |
| 29 | +time_download_start="$(date +%s)"; echo "Starting to populate features database, this may take a while" |
| 30 | +curl -Lo "${dbdump}.gz" "https://fortiscentral.blob.core.windows.net/locations/feature-service.${DUMP_VERSION}.sql.gz"; gunzip -f "${dbdump}.gz" |
| 31 | +time_download_end="$(date +%s)"; echo "Seconds to download dump: $((time_download_end-time_download_start))" |
| 32 | +sudo -u postgres psql -qd features < "${dbdump}" |
| 33 | +time_insert_end="$(date +%s)"; echo "Seconds to populate database: $((time_insert_end-time_download_end))" |
| 34 | +rm "${dbdump}" |
| 35 | + |
| 36 | +# install node |
| 37 | +curl -sL 'https://deb.nodesource.com/setup_6.x' | sudo -E bash - |
| 38 | +sudo apt-get install -y nodejs > /dev/null |
| 39 | + |
| 40 | +# enable binding to port 80 |
| 41 | +sudo apt-get install -y authbind > /dev/null |
| 42 | +sudo touch '/etc/authbind/byport/80' |
| 43 | +sudo chown "${USER}:${USER}" '/etc/authbind/byport/80' |
| 44 | +sudo chmod 755 '/etc/authbind/byport/80' |
| 45 | + |
| 46 | +# install app |
| 47 | +if [ ! -d featureService ]; then |
| 48 | + git clone --depth 1 'https://github.com/CatalystCode/featureService.git' |
| 49 | + (cd featureService; npm install) |
| 50 | +fi |
| 51 | + |
| 52 | +# autostart app |
| 53 | +sudo apt-get install -y supervisor > /dev/null |
| 54 | +sudo systemctl enable supervisor |
| 55 | +sudo service supervisor start |
| 56 | +sudo tee '/etc/supervisor/conf.d/featureService.conf' > /dev/null << EOF |
| 57 | +[program:featureService] |
| 58 | +command=/usr/bin/authbind "$(which node)" "$(readlink -f featureService/server.js)" |
| 59 | +autostart=true |
| 60 | +autorestart=true |
| 61 | +startretries=3 |
| 62 | +stderr_logfile=/tmp/featureService.err.log |
| 63 | +stdout_logfile=/tmp/featureService.out.log |
| 64 | +user=${USER} |
| 65 | +environment=PORT=80,FEATURES_CONNECTION_STRING="postgres://frontend:${frontend_password}@127.0.0.1/features" |
| 66 | +EOF |
| 67 | +sudo supervisorctl reread |
| 68 | +sudo supervisorctl update |
| 69 | + |
| 70 | +# cleanup |
| 71 | +sudo apt-get remove -y ${build_dependencies} > /dev/null |
| 72 | +sudo apt-get autoremove -y > /dev/null |
| 73 | +sudo apt-get upgrade -y |
0 commit comments