Tuesday, September 13, 2016

Server Deployment

So my highlight of this week has clearly been decided: Deploying a front-end server I know absolutely nothing about.

To give it some context:
Doing Wishpool for Assignment 3, I took up the back-end role. Building a back-end in Rails (yay Rails again), after doing deployment for Assignment 1, deploying the back-end was a piece of cake. Well, almost. I spent like some 10 hours on it back in Assignment 1 and I was mostly very confused throughout. I believe I've now found the optimal way to deploy it.

http://www.learnwithdaniel.com/2015/01/apache-puma-via-reverse-proxy/

This site saved my life. Forget Passenger and Capistrano and what not. This is the fastest way to go from nothing to deployed.

(Substitute some of the init new project stuff with git clone). Also, since in 3216 we use AWS, don't bother with the Amazon images... Just grab Ubuntu, since it's more familiar to most of us anyway.

But that's where it gets interesting. Seemingly this can be applied to any webserver - that is, our front end! Which is somehow magically served with gulp.js (don't ask me how, try asking my teammates!) Anyway we had something like an hour to deploy it before UI Reviews, so guess what - I replicated the Reverse Proxy over and tadaa! It works!

Anyway, the other cool thing I've been picking up is how to automate work with scripts. Here's some useful scripts to share:

First one: SSH script to connect to our server. Seriously why bother remembering the ip address of the server when you can just type ./ssh.sh ?

#!/bin/bash
ssh -i <path_to_file> ubuntu@<server_ip_address>

Next up - redeploy. We want to keep downtime to a minimum, so... How about a one liner like  ./redeploy.sh? Sidenote: This one is specifically for Rails, using a daemonized server and the reverse proxy pass. I've currently got one for the front end as well, but... Not 100% sure it works.

#!/bin/bash
cd <path_to_repo>
kill $(lsof -ti :<port_number_of_server>)
export GIT_MERGE_AUTOEDIT=no
source ~/.bashrc
git fetch
git merge <origin/deployment_branch>
bundle install
bundle exec rake db:migrate
bundle exec rails s -d

3 comments:

  1. If you find yourself writing bash scripts for deployment, you should seriously consider Fabric (http://www.fabfile.org/). If you want, I can share the fabfile I've written for my assignment one. It was used to deploy a Django project, but the steps are almost exactly the same, down to the 'source' command.

    ReplyDelete
  2. The alternatives would be some kind of automated pipeline from SnapCI or Heroku with server hooks which does the deployment automatically when you push changes or merge.

    For our group we settled on using hot reload feature of node forever so we just need to `git pull` every time.

    ReplyDelete
  3. A more common way of convenient ssh-ing is to create a config file in `~/.ssh` and add the server credentials to it. Here's what I use for nusmods:
    ```
    Host nusmods
    Hostname nusmods.com
    User ubuntu
    IdentityFile "~/.ssh/nusmods.com"
    UseRoaming no
    ```

    The private key will be in `~/.ssh/nusmods.com`. To ssh in, simply type `$ ssh nusmods` in your console.

    ReplyDelete