doc: add deployment with monit
This commit is contained in:
parent
8a047bf180
commit
72952f1a9f
18
README.md
18
README.md
|
@ -10,7 +10,9 @@ REST API to request GTFS data from postgres database
|
|||
|
||||
# General
|
||||
|
||||
TODO
|
||||
Requirements:
|
||||
|
||||
* Node.js >= 10
|
||||
|
||||
# Quick Start Guide
|
||||
|
||||
|
@ -42,14 +44,6 @@ npm run start
|
|||
|
||||
# SQL Statemants
|
||||
|
||||
## To-do List
|
||||
|
||||
* How many routes is an agency serving in the schedule?
|
||||
|
||||
* Show a calendar for a trip
|
||||
|
||||
## VBN GTFS Data Set
|
||||
|
||||
* get a certain service by service_id
|
||||
```
|
||||
select * from calendar where service_id='675';
|
||||
|
@ -61,37 +55,31 @@ select agency.agency_name, routes.route_short_name, routes.agency_id, trips.rout
|
|||
```
|
||||
|
||||
* get all routes from agency
|
||||
|
||||
```
|
||||
select route_short_name from routes,agency where routes.agency_id=agency.agency_id and agency.agency_id='381';
|
||||
```
|
||||
|
||||
* get trip direction from trip number
|
||||
|
||||
```
|
||||
select trip_headsign from trips where trip_short_name='1226016';
|
||||
```
|
||||
|
||||
* get route number from trip number
|
||||
|
||||
```
|
||||
select routes.route_short_name from routes, trips where trips.trip_short_name='1226015' AND routes.route_id=trips.route_id;
|
||||
```
|
||||
|
||||
* get agency name from route number
|
||||
|
||||
```
|
||||
select agency.agency_name from agency,routes where routes.route_id='47189' AND routes.agency_id=agency.agency_id;
|
||||
```
|
||||
|
||||
* get agency URL from route number
|
||||
|
||||
```
|
||||
select agency.agency_url from agency,routes where routes.route_id='47189' AND routes.agency_id=agency.agency_id;
|
||||
```
|
||||
|
||||
* other
|
||||
|
||||
```
|
||||
select route_id,trip_headsign from trips where trip_short_name='1226016';
|
||||
select * from routes where route_id='47189';
|
||||
|
|
|
@ -0,0 +1,104 @@
|
|||
# Deployment with Monit
|
||||
|
||||
* configure Node.js for deployment user\
|
||||
```
|
||||
node -v
|
||||
which node
|
||||
touch ~/.profile
|
||||
chmod 700 ~/.profile
|
||||
curl https://raw.githubusercontent.com/creationix/nvm/v0.39.1/install.sh | bash
|
||||
cat .profile
|
||||
```
|
||||
* log out and in again to varify node.js is available in the correct version\
|
||||
```
|
||||
echo $NVM_DIR
|
||||
nvm -h
|
||||
nvm ls-remote
|
||||
nvm i 16.15.1
|
||||
/usr/bin/node -v
|
||||
node -v
|
||||
```
|
||||
* checkout git repository of API\
|
||||
```
|
||||
git clone https://github.com/Software-Ingenieur-Begerad/postgres-gtfs-rest-api.git
|
||||
cd postgres-gtfs-rest-api/
|
||||
```
|
||||
* configure API\
|
||||
```
|
||||
scp ~/.env <usr>@<deployment host>:~
|
||||
mv ~/.env postgres-gtfs-rest-api/
|
||||
vi .env
|
||||
```
|
||||
* install dependencies and test API\
|
||||
```
|
||||
cd postgres-gtfs-rest-api/
|
||||
npm i
|
||||
less README.md
|
||||
DEBUG=trip-count,route-count,service-overview,mapping,gtfs,date,servicedays,service,trips,agency-url,agency-name,route-short-name,trip-headsign,routes,frequencies,stops,config,index,app,root,agency,db node index.js
|
||||
curl <host>:<port>/route-count?agencyid=231
|
||||
```
|
||||
|
||||
* configure Monit\
|
||||
```
|
||||
cd
|
||||
touch .monitrc
|
||||
chmod 600 .monitrc
|
||||
vi .monitrc
|
||||
```
|
||||
* enter the following configuration filling in the <> gaps\
|
||||
```
|
||||
set daemon 60
|
||||
with start delay 12
|
||||
set logfile <~>/monit/var/monit.log
|
||||
set idfile <~>/monit/var/monit.id
|
||||
set statefile <~>/monit/var/monit.state
|
||||
set mailserver localhost
|
||||
set mail-format { from: webmaster@<host> }
|
||||
set alert <usr email>@<host>
|
||||
set httpd port 32123 address 127.0.0.1
|
||||
allow monit:ao5Ge1wohGaije
|
||||
check process <service name> with pidfile <~>/var/<service name>.pid
|
||||
start program "<~>/bin/start-<service name>"
|
||||
stop program "/bin/bash -c '/bin/kill $( cat <~>/var/<service name>.pid )'"
|
||||
```
|
||||
* enter the following start script filling in the <> gaps\
|
||||
```
|
||||
#!/bin/bash
|
||||
|
||||
export PATH=<~>/.nvm/versions/node/v16.15.1/bin:/usr/local/bin:/usr/bin:/bin
|
||||
export HOME=<~>
|
||||
export NVM_DIR="$HOME/.nvm"
|
||||
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
|
||||
|
||||
cd $HOME/postgres-gtfs-rest-api
|
||||
exec node index.js >$HOME/var/<service name>.log 2>&1 &
|
||||
echo $! >$HOME/var/<service name>.pid
|
||||
```
|
||||
* configure cron\
|
||||
```
|
||||
TODO
|
||||
```
|
||||
* configure Apache2 web server
|
||||
```
|
||||
cd doms/<domain>/
|
||||
rm -rf subs/www/ subs-ssl/www/
|
||||
vi htdocs-ssl/.htaccess
|
||||
```
|
||||
* enter the following Apache2 config filling in the <> gaps\
|
||||
```
|
||||
DirectoryIndex disabled
|
||||
RewriteEngine On
|
||||
RewriteBase /
|
||||
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
|
||||
RewriteRule .* http://127.0.0.1:<port>%{REQUEST_URI} [proxy]
|
||||
RequestHeader set X-Forwarded-Proto "https"
|
||||
```
|
||||
* test API in a browser\
|
||||
```
|
||||
https://www.v1gtfs.vbn.api.swingbe.de/
|
||||
```
|
||||
* test API with CLI instruction\
|
||||
```
|
||||
curl https://www.v1gtfs.vbn.api.swingbe.de/
|
||||
curl https://www.v1gtfs.vbn.api.swingbe.de/route-count?agencyid=231
|
||||
```
|
|
@ -0,0 +1,71 @@
|
|||
# Deployment with pm2
|
||||
|
||||
* archive project and copy onto host system
|
||||
```
|
||||
cd ..
|
||||
tar -czvf <archive name>.tar.gz --exclude={"postgres-gtfs-rest-api/.git","postgres-gtfs-rest-api/node_modules"} postgres-gtfs-rest-api/
|
||||
scp -P <host ssh port> <archive name>.tar.gz <user>@<host>.<domain>:~
|
||||
```
|
||||
|
||||
* [Setup Node.js and NPM](https://github.com/Software-Ingenieur-Begerad/setup/blob/main/doc/node.md) on target system
|
||||
|
||||
* copy service source into the working folder
|
||||
```
|
||||
sudo tar -xzf ~/<archive name>.tar.gz -C /opt/
|
||||
```
|
||||
|
||||
* install dependencies
|
||||
```
|
||||
cd /opt/<archive name>
|
||||
npm i
|
||||
```
|
||||
|
||||
* set up service environment on host system
|
||||
```
|
||||
sudo vi /opt/<archive name>/.env
|
||||
```
|
||||
|
||||
* define the following environment variables
|
||||
```
|
||||
DB_HOST=<host running database>
|
||||
DB_PORT=<port of host running database>
|
||||
DB_USER=<user granted permissions for database>
|
||||
DB_PASSWORD=<user key>
|
||||
DB_NAME=<database name>
|
||||
NODE_ENV=<node environment mode>
|
||||
PORT=<port offering this service>
|
||||
```
|
||||
|
||||
* create group and user <service name>
|
||||
like this [setup](https://github.com/Software-Ingenieur-Begerad/setup/blob/main/doc/grp-usr.md)
|
||||
|
||||
* adjust group and user privileges
|
||||
```
|
||||
sudo chown -R <service name>:<service name> /opt/<archive name>
|
||||
```
|
||||
|
||||
* prepare pm2 like this [setup](https://github.com/Software-Ingenieur-Begerad/setup/blob/main/doc/pm2.md)
|
||||
|
||||
* start the service as npm start script with PM2
|
||||
```
|
||||
cd /opt/<archive name>
|
||||
pm2 start --name <archive name> npm -- start --watch
|
||||
```
|
||||
|
||||
* register/save the current list of processes you want to manage using PM2 so that they will re-spawn at system boot (every time it is expected or an unexpected server restart)
|
||||
```
|
||||
pm2 save
|
||||
```
|
||||
|
||||
* restart your system, and check if all the serviceis running under PM2
|
||||
```
|
||||
pm2 ls
|
||||
```
|
||||
or
|
||||
```
|
||||
pm2 status
|
||||
```
|
||||
|
||||
* configure web server as proxy for instance with this [example](etc/apache2/sites-available/example.conf) config file for Apache2
|
||||
|
||||
* if service shall be provided to other consumers than localhost, adjust firewall accordingsly
|
100
doc/setup.md
100
doc/setup.md
|
@ -1,89 +1,27 @@
|
|||
# Postgres-gtfs-rest-api Setup
|
||||
|
||||
## Table of Contents
|
||||
0. [Prepqration](#Preparation)
|
||||
1. [Deployment with pm2](#deployment-with-pm2)
|
||||
2. [Deployment with Monit](#deployment-with-monit)
|
||||
0. [Prepqration](#preparation)
|
||||
1. [Deployment with Monit](#deployment-with-monit)
|
||||
2. [Deployment with pm2](#deployment-with-pm2)
|
||||
|
||||
# Preparation
|
||||
|
||||
* check out git repositories onto a development system as descirpted in the
|
||||
* check out git repository as descirbted in the
|
||||
[Quick Start Guide](../README.md#Quick-Start-Guide)
|
||||
* download GTFS static file in question
|
||||
* make use of [this](https://github.com/Software-Ingenieur-Begerad/gtfs2psqlschema#readme) repository to load GTFS data into Postgres database
|
||||
* set up ssh tunnel\
|
||||
```
|
||||
ssh -L <local port>:localhost:5432 <user>@<postgrs host>
|
||||
```
|
||||
* create schema\
|
||||
```
|
||||
~/git/gtfs2psqlschema/gtfs_schema.sh . > <schema name>.sql
|
||||
```
|
||||
* load feed into database\
|
||||
```
|
||||
psql --host=localhost --port=5442 --username=<usr> --dbname=<db> --file=<schema name> --password
|
||||
|
||||
# Deployment with Pm2
|
||||
|
||||
* archive project and copy onto host system
|
||||
```
|
||||
cd ..
|
||||
tar -czvf <archive name>.tar.gz --exclude={"postgres-gtfs-rest-api/.git","postgres-gtfs-rest-api/node_modules"} postgres-gtfs-rest-api/
|
||||
scp -P <host ssh port> <archive name>.tar.gz <user>@<host>.<domain>:~
|
||||
```
|
||||
|
||||
* [Setup Node.js and NPM](https://github.com/Software-Ingenieur-Begerad/setup/blob/main/doc/node.md) on target system
|
||||
|
||||
* copy service source into the working folder
|
||||
```
|
||||
sudo tar -xzf ~/<archive name>.tar.gz -C /opt/
|
||||
```
|
||||
|
||||
* install dependencies
|
||||
```
|
||||
cd /opt/<archive name>
|
||||
npm i
|
||||
```
|
||||
|
||||
* set up service environment on host system
|
||||
```
|
||||
sudo vi /opt/<archive name>/.env
|
||||
```
|
||||
|
||||
* define the following environment variables
|
||||
```
|
||||
DB_HOST=<host running database>
|
||||
DB_PORT=<port of host running database>
|
||||
DB_USER=<user granted permissions for database>
|
||||
DB_PASSWORD=<user key>
|
||||
DB_NAME=<database name>
|
||||
NODE_ENV=<node environment mode>
|
||||
PORT=<port offering this service>
|
||||
```
|
||||
|
||||
* create group and user <service name>
|
||||
like this [setup](https://github.com/Software-Ingenieur-Begerad/setup/blob/main/doc/grp-usr.md)
|
||||
|
||||
* adjust group and user privileges
|
||||
```
|
||||
sudo chown -R <service name>:<service name> /opt/<archive name>
|
||||
```
|
||||
|
||||
* prepare pm2 like this [setup](https://github.com/Software-Ingenieur-Begerad/setup/blob/main/doc/pm2.md)
|
||||
|
||||
* start the service as npm start script with PM2
|
||||
```
|
||||
cd /opt/<archive name>
|
||||
pm2 start --name <archive name> npm -- start --watch
|
||||
```
|
||||
|
||||
* register/save the current list of processes you want to manage using PM2 so that they will re-spawn at system boot (every time it is expected or an unexpected server restart)
|
||||
```
|
||||
pm2 save
|
||||
```
|
||||
|
||||
* restart your system, and check if all the serviceis running under PM2
|
||||
```
|
||||
pm2 ls
|
||||
```
|
||||
or
|
||||
```
|
||||
pm2 status
|
||||
```
|
||||
|
||||
* configure web server as proxy for instance with this [example](etc/apache2/sites-available/example.conf) config file for Apache2
|
||||
|
||||
* if service shall be provided to other consumers than localhost, adjust firewall accordingsly
|
||||
|
||||
# Deployment With Monit
|
||||
|
||||
```
|
||||
TODO
|
||||
```
|
||||
# [Deployment with Monit](./deployment-with-monit.md)
|
||||
# [Deployment with pm2](./deployment-with-pm2.md)
|
||||
|
|
Loading…
Reference in New Issue