Skip to content

Commit 15d7a86

Browse files
authored
Merge pull request #10 from CatalystCode/install-script
Add one-click install script for project
2 parents b348198 + 59fb7dc commit 15d7a86

File tree

2 files changed

+104
-38
lines changed

2 files changed

+104
-38
lines changed

README.md

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,50 @@
1-
featureService
2-
==============
1+
# featureService #
32

4-
Development Setup
5-
=====
6-
In order to install on your local machine, you will need to install:
3+
## Development setup ##
74

8-
1. Postgres + postgis ([instructions for Ubuntu 16.04](http://www.gis-blog.com/how-to-install-postgis-2-3-on-ubuntu-16-04-lts/))
9-
2. Node ([instructions for Ubuntu 16.04](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04))
5+
### System dependencies ###
106

11-
Once those are installed, go open a bash shell on the project directory and type:
7+
In order to install on your local machine, you will need to install:
128

13-
```
14-
psql postgres
15-
```
9+
- Postgres + postgis ([instructions for Ubuntu 16.04](http://www.gis-blog.com/how-to-install-postgis-2-3-on-ubuntu-16-04-lts/))
10+
- Node ([instructions for Ubuntu 16.04](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04))
1611

17-
You will be welcomed with the postgres prompt. Now, create the geofeatures database and switch to it:
12+
### Database setup ###
1813

19-
```
20-
CREATE DATABASE geofeatures;
21-
\c geofeatures;
14+
Once the system dependencies are installed, go open a bash shell on the project directory and set up the database:
2215

23-
CREATE USER frontend PASSWORD your_password_here;
16+
```sh
17+
cat << EOF | sudo -u postgres psql
18+
CREATE DATABASE features;
19+
CREATE USER frontend WITH login password 'your_password_here';
20+
CREATE USER ops WITH login password 'your_other_password_here';
21+
GRANT ops TO postgres;
22+
GRANT frontend TO postgres;
23+
EOF
24+
sudo -u postgres psql -d features < schema.sql
2425
```
2526

26-
Next, load the database schema:
27+
### Application setup ###
2728

28-
```
29-
psql -d geofeatures < schema.sql
30-
```
31-
32-
Make sure the frontend user has access to the geofeatures database:
29+
First, make sure the environment variables for the service are set:
3330

34-
```
35-
GRANT ALL PRIVILEGES ON DATABASE geofeatures TO frontend;
36-
```
37-
38-
Then, make sure the environment variable for the newly-created user is set:
39-
40-
```
41-
export FEATURES_CONNECTION_STRING='postgres://frontend:[email protected]/geofeatures'
31+
```sh
32+
export FEATURES_CONNECTION_STRING='postgres://frontend:[email protected]/features'
4233
export PORT=3035
4334
```
4435

45-
Now, make sure the nodejs dependencies are installed:
36+
Now you're ready to install and start the service:
4637

47-
```
38+
```sh
4839
npm install
40+
node server.js
4941
```
5042

51-
Running
52-
=======
53-
All you need to do is run:
43+
## Production setup ##
5444

55-
```
56-
node server.js
57-
```
45+
You can run the script `scripts/install.sh` to set up a production machine with the featureService and all its dependencies. The script will:
46+
47+
- Install postgres and postgis
48+
- Populate the postgres features database
49+
- Install the featureService
50+
- Autostart the featureService on port 80

scripts/install.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)