Skip to content

Instantly share code, notes, and snippets.

@spion
Last active December 19, 2015 00:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spion/8b1f723b191b15c127f1 to your computer and use it in GitHub Desktop.
Save spion/8b1f723b191b15c127f1 to your computer and use it in GitHub Desktop.

Migrating from forever to nac

Creating a nacfile

Add a nacfile to the project, then (optionally) add it to git. For example:

command: node
args:
  - app.js
env:
  NODE_ENV: production
  PORT: 8130

Whatever you put in env will be available from the process.env global in app.js. This means you can also edit *app.js` to change the listening command:

app.listen(process.env.PORT || 8130);

which means you can now control the port from the nacfile.

For other languages, search their documentation for APIs to access environment variables.

Create a nac app on the server

If the file is not in git, use scp (or other means) to copy it on the server.

Otherwise, if you deploy via git push you can just push the project with the added nacfile.

Alternatively, login to the server via ssh and do a git pull from the project directory to get the nacfile.

Now you can create a new app based on this nacfile. Pick a name for the app and then use

nac <appname> create path/to/nacfile.yaml

This will add your app to nac. You can check the status with

nac <appname> status

You will see that it's not running. nac doesn't start the app without explicitly being told to start it. Once its told to start it, it will try to keep it started at all times (remembering it across daemon/system restarts)

Update post-receive script (git push deploy only)

If you deploy via git push, you will need to find and edit the post-receive script. It can usually be found one directory up from the project dir. e.g. from the project dir type:

vim ../appname.post-receive.sh

There, find forever restart jsfile.js and change it with nac <appname> restart

Do the switcharoo

forever list

Find out which one is the app you're replacing then use the ID number it gives you to stop it

forever stop <ID>; nac <appname> start

e.g.

forever stop 2; nac doxbee start;

At this point the old app will be stopped and the new will be started.

Switching away from post-receive

You can also do deploys via nac instead of using a post-receive hook.

Copy your post-receive script, but also add the git fetching and pulling at the top.

You can use $NACNAME instead of hard-coding the app name, $NACDIR for the working dir of the app and $NACFILE for the location of the yaml config file.

Then, add it to the scripts section in nac. E.g.

scripts:
  deploy: ./scripts/deploy.sh

Here is an example deploy script:

#!/bin/bash

cd $NACDIR
refspec=$1
if [ -z "$refspec" ]
then
    refspec="master"
fi
git fetch upstream && git stash && git checkout upstream/$refspec
npm install
grunt
if [[ "$NACNAME" =~ .+-test ]]
then
    # simple restart works for all apps
    nac $NACNAME restart
else
    # use cluster reload signal for production
    # (works if your app is using learnboost up or recluster)
    nac $NACNAME kill SIGUSR2
fi

You can now use this command to deploy the master branch of the app:

nac myapp run deploy master

It will fetch and checkout master, install all npm modules, run the grunt build script then gracefully reload the app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment