Skip to content
This repository has been archived by the owner on Oct 5, 2020. It is now read-only.

Fixed #269: fixed init script, and added docs/scripts for server deployment #294

Merged
merged 1 commit into from
Feb 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 5 additions & 34 deletions app/templates/README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,9 @@ Edit `./local.json` to set your desired ports

gulp serve-local # this will watch the .less file for changes, compile them to .css, and run the node server

# Installing as service
# Installation and deployment on server

The code includes a service script, and a service config to make installing express server service as easy as possible. The following files are involved:

- etc/init.d/node-express-service (generic express server service script)
- etc/{app}/conf.sh (application specific service configuration, any application name allowed)
- boot.js (entry point for express service, calls out to server.js)
- node-app.js (required by boot.js)

The conf.sh is 'sourced' by the service script, and allows overriding the built-in defaults. Usually you only need to override SOURCE\_DIR, APP\_PORT, and ML\_PORT. Make sure they match the appropriate environment.

Next install [forever](https://www.npmjs.com/package/forever) globally if it is not already installed.

- `$ [sudo] npm install forever -g`

Next, push all source files to the appropriate server. The following assumes it was dropped under /space/projects/ in a folder called slush-app.live. Take these steps to install the services:

- cd /space/projects/slush-app.live
- gulp build # this will create the ./dist/ folder with all the required assests and code
- cd /etc
- sudo ln -s /space/projects/slush-app.live/etc/{env} slush-app
- cd /etc/init.d
- sudo ln -s /space/projects/slush-app.live/etc/init.d/node-express-service slush-app
- sudo chkconfig --add slush-app
- sudo chkconfig --levels 2345 slush-app on

Next to start it, use the following commands (from any directory):

- sudo service slush-app start

These services will also print usage without param, but they support info, restart, start, status, and stop. The info param is very useful to check the settings.
See etc/INSTALL.md

# Data

Expand All @@ -94,7 +66,6 @@ Or on Windows:

## General Data Information

The application assumes that you're storing JSON data. This shows up in the
default format request for the MLRest service's `searchContext`, the
`detailController`'s (`detail-ctrl.js`) request to get a document, and in the
out-of-the-box detail view.
The application comes with a sample set of JSON data. The detail controller,
the part that handles showing your data, can not only handle JSON, but also
XML, Binary, and Text data out of the box.
96 changes: 96 additions & 0 deletions app/templates/etc/INSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Deployment on server

Copying sources onto the server can be cumbersome, but it is possible to leverage git to help you with this. You do so by initializing a bare git repository on the server itself:

* sudo mkdir -p /space/projects/@sample-app-name.git
* sudo mkdir -p /space/projects/@sample-app-name.live
* sudo chown $USER:sshuser /space/projects/@sample-app-name.git
* sudo chown $USER:sshuser /space/projects/@sample-app-name.live
* cd /space/projects/@sample-app-name.git
* git init --bare

Note: you can omit :sshuser in the chown, or replace that with a different group shared by users. It is meant to make deployment by colleagues easier.

Last but not least, enable a post-receive hook:

* vi hooks/post-receive
* Insert:

#!/bin/sh
in=$(cat)
branch=${in##*/}
GIT_WORK_TREE=/space/projects/@sample-app-name.live git checkout -f $branch

* Save/quit vi
* chmod +x hooks/post-receive

The .git directory will be a true git repository, with the complete changes. That enables sending over only differences between your local machine, and the server. A clean/forced checkout will be put in the .live directory each time changes are pushed into the git repository on the server. Be careful with manual changes, they get overwritten easily by the post-receive script.

Once a local git repo has been setup on the server, add it as a remote to the git clone on your local machine:

* git remote add prod USERNAME@your.demoserver.com:/space/projects/@sample-app-name.git

Then run the following each time you want to update code on the demo server:

* git push prod

Once done that, you can run all above deployment steps on the server, just go to /space/projects/@sample-app-name.live/. A few ml commands might return an error, but only after having done what needed to be done. You can run the ml tasks remotely as well, might be a bit slower.

Note: .git and .live files on the server are 'owned' by $USER:sshuser. You may need to chown them to yourself to be able to push things. Also make sure to run `npm install` and `bower install` if dependencies changes, and `gulp build` on the server each time UI changes are applied.

# Installing services

The code includes a service script, and a service config to make installing an express server service as easy as possible. The following files are involved:

- etc/init.d/node-express-service (generic express server service script)
- etc/{env}/conf.sh (application specific service configuration, any application name allowed)
- boot.js (entry point for express service, calls out to server.js)
- node-app.js (required by boot.js)

The conf.sh is 'sourced' by the service script, and allows overriding the built-in defaults. Usually you only need to override SOURCE\_DIR, APP\_PORT, and ML\_PORT. Make sure they match the appropriate environment.

Next install [forever](https://www.npmjs.com/package/forever) globally if it is not already installed.

- `$ [sudo] npm install forever -g`

Next, push all source files to the appropriate server. The following assumes it was dropped under /space/projects/ in a folder called @sample-app-name.live. Take these steps to install the services:

- cd /space/projects/@sample-app-name.live
- gulp build # this will create the ./dist/ folder with all the required assests and code
- cd /etc
- sudo ln -s /space/projects/@sample-app-name.live/etc/{env} @sample-app-name
- cd /etc/init.d
- sudo ln -s /space/projects/@sample-app-name.live/etc/init.d/node-express-service @sample-app-name
- sudo chkconfig --add @sample-app-name
- sudo chkconfig --levels 2345 @sample-app-name on

Or, slightly shorter:

- cd /space/projects/@sample-app-name.live/etc
- ./install.sh (not as sudo!)

Next to start it, use the following commands (from any directory):

- sudo service @sample-app-name start

These services will also print usage without param, but they support info, restart, start, status, and stop. The info param is very useful to check the settings.

# Initializing httpd

Next to this, you likely want to enable the httpd daemon. Often only very limited ports are exposed on servers, and we usually deliberately configure the application outside that scope. Add a forwarding rule for the appropriate dns:

- sudo chkconfig --levels 2345 httpd on
- sudo service httpd stop
- sudo vi /etc/httpd/conf/httpd.conf, uncomment the line with:

NameVirtualHost *:80

- and append:

<VirtualHost *:80>
ServerName @sample-app-name.demoserver.com
RewriteEngine On
RewriteRule ^(.*)$ http://localhost:@node-port$1 [P]
</VirtualHost>

- sudo service httpd start
4 changes: 2 additions & 2 deletions app/templates/etc/init.d/node-express-service
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ start() {
echo ""
daemon --user=root \
forever start -p $FOREVER_DIR --pidFile $PIDFILE -l $LOGFILE -o $LOGFILE \
-e $LOGFILE -a -d -v --sourceDir=$SOURCE_DIR $SOURCE_FILE \
-e $LOGFILE -a -d -v --sourceDir=$SOURCE_DIR --uid=$NAME $SOURCE_FILE \
--app-port=$APP_PORT --ml-host=$ML_HOST --ml-port=$ML_PORT \
--server-script=$SERVER_SCRIPT --node-env=$NODE_ENV
RETVAL=$?
Expand All @@ -85,7 +85,7 @@ stop() {
# Send a stop signal to forever
echo ""
forever stop -p $FOREVER_DIR --pidFile $PIDFILE -l $LOGFILE -o $LOGFILE \
-e $LOGFILE -a -d -v --sourceDir=$SOURCE_DIR $SOURCE_FILE
-e $LOGFILE -a -d -v --sourceDir=$SOURCE_DIR $NAME
RETVAL=$?
# Get rid of pid file if process was dead
if [ -f $PIDFILE ]; then
Expand Down
17 changes: 17 additions & 0 deletions app/templates/etc/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

sudo npm -g install bower
sudo npm -g install gulp
sudo npm -g install forever

cd ..
npm install
bower install
gulp build

cd /etc
sudo ln -s /space/projects/@sample-app-name.live/etc/prod @sample-app-name
cd /etc/init.d
sudo ln -s /space/projects/@sample-app-name.live/etc/init.d/node-express-service @sample-app-name
sudo chkconfig --add @sample-app-name
sudo chkconfig --levels 2345 @sample-app-name on
5 changes: 2 additions & 3 deletions app/templates/etc/prod/conf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
#SERVER_SCRIPT=./node-server/node-app.js

# port at which node server should be listening
#APP_PORT=9040
APP_PORT=80
APP_PORT=@node-port

# Node environment. This should be 'build' for distribution or 'dev' for iterative development
NODE_ENV=build
Expand All @@ -25,4 +24,4 @@ NODE_ENV=build

# MarkLogic port to which node server should proxy
#ML_PORT=8040
ML_PORT=8040
ML_PORT=@ml-http-port
2 changes: 2 additions & 0 deletions slushfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ gulp.task('init', ['checkForUpdates'], function (done) {
}))
.pipe(replace('@sample-app-name', answers.nameDashed, {skipBinary:true}))
.pipe(replace('@sample-app-role', answers.nameDashed + '-role', {skipBinary:true}))
.pipe(replace('@node-port', answers.nodePort, {skipBinary:true}))
.pipe(replace('@ml-http-port', answers.appPort, {skipBinary:true}))
.pipe(gulp.dest('./')) // Relative to cwd
.on('end', function () {
done(); // Finished!
Expand Down