I seem to work in tangent directions (going all over the place!). I’ll get bored and start on a new ‘I think I can do that’. I’ll do some stuff, then either get bored with it or refine the initial concept. Since I live in a low-tech area (Gadsden Alabama), I rely on my former co-workers the give me feedback on my latest tangent. I can pretty much tell if the tangent if worthless by that feedback. Nothing is totally worthless - sometimes a technique (kludge! Idea!) has promises, maybe in another area.

To get that feedback, I’d usually publish the demo on a Rails development application binded to some port on 0.0.0.0 (rails s -p 8765 - b 0.0.0.0). I’d open the port on my router and forward it to my laptop. While that works, it opened up my laptop to port scanners and then, if I was getting some non-techie to look at it, they’d get confused: Why I was asking them to go to http://mydomain.us:8765? I thought you could just do www.xyz.com! If I got serious about the tangent I’d deploy the application to a staging server (Linux or my MacMini). Of course I’d have to remember how to deploy the application.

A recent tangent was a Bingo Controller Rails application. As proof of concept, I loaded up a RaspberryPI with rails and ran it on the PI. It actually ran quite well, but I was still opening up ports. I then remembered that I used Nginx on the MacMini to act as a front-end to a staging server. If I got more serious on the tangent, I’d put it on a VPS. So all this post is going to do is remind me how I did this and maybe someone else can use the technique. Over the years I’ve set up all kinds of tools to support me, but I seen to forget why and how I did it. Even in the RaspberryPI tangent I set up, it just used file copies to move the application to the PI. That was pretty stupid in that I had set up a Git repository server to handle my Real applications. How I set up my git server is forgotten, if you google ‘set up git server’ there are numerous ways and I may have to change mine. Think I had set it up using git protocol, but I really just use ssh.

The basic set up is:

  • A Rails development environment (my laptop). Set up with all the usuals (rbenv). Tangents usually start with a basic scaffold application with my standards (slim, maybe SimpleForms, jQuery and Foundation)

  • A Git Repository. I used the Gist Setting up a Remote Repository and Doing Initial Push to remember what I did. I assume that your router port forwards 22 and 443 to a server. I could have used GitHub, but I don’t have a private account and don’t want to put what may be crap on my public account.

  • A domain that points to your router

Setup remote repository:

  ssh git@example.com
  mkdir my_project.git
  cd my_project.git
  git init --bare

On local machine:

  cd my_project
  git init
  git add *
  git commit -m "Initial commit"
  git remote add origin example.com:my_project.git
  git push -u origin master
  • Nginx running someplace on your network. Again, assuming your router is port forwarding port 80 to this machine. But then you can use another port if whoever you’re asking to look at the demo can understand :port on a URL.

That’s it

I’ll step through the Bingo Controller application life cycle to give a little more detail. Bingo started as a single table application.

The Games
create_table "games", force: :cascade do |t|
  t.string "status"
  t.string "calls"
  t.string "pattern"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "game_status"
  t.index ["status"], name: "index_games_on_status"
end
The Model
class Game < ApplicationRecord
  serialize :calls, Array
  GameStatus = %w(current bingo won)
end

I later added a User model and Pattern model but the content of the application is not needed, it just has a controller that Adds (or controls) calls and a display that shows the current status of the game. The initial application will be developed and tested on my laptop.

When I decide I want to publish the Demo

  • Create the git repository and push the application to the repository (see above)

  • ssh into where I’m going to run the demo and get it running (assuming ssh keys set up)

  ssh user@demo.local # or user@10.0.1.x
  cd apps
  git clone developer@example.com:/Users/developer/repo/bingo.git bingo
  cd bingo
  bundle exec bundle 
  # could have version problems. Might have to also do a migration of copy over a development.db
  rails s -p 8765 - b 0.0.0.0  #get rails running
  # use browser to see if it is up (http://10.0.1.x:8765)
  • ssh into your web server (where port 80 goes to)
  ssh developer@example.com # or user@10.0.1.x
  # cd to your nginx config directory (example homebrew on a mac)
  cd /usr/local/etc/nginx/sites-enabled
  • create a nginx file in sites-enabled to handle the demo. I setup nginx to respond to subdomain with proxy forward to machine on local network
server {
    server_name bingo.example.com;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://10.0.1.x:8765;
    }
}

Use browser to see if you can get to http://bingo.example.com

You be done!

Now don’t do like I first did, make changes on the demo instead of the laptop and try to figure out how to merge them. Just use git.

  • Something needs fixed or added.
  • make changes and git commit
  • ssh to the demo machine and the demo app
  • git pull

Your’ve made the changes

OK that semi worked but I’m changing….

I ran into problems. I moved most demo to a MacMini. Then I found out that HFS filesystem had problems running a rails development mode as a daemon with large (not really too big) assets. The file watcher/spring was spawning to many processes and startup was not complete.

After a few days of band-aiding the problem, I figured out the best thing I could do was to run it in production and just do a hand deploy.

No changes required on basic Rais 5 code. Make sure the config/environments/production.rb has the following default lines:

  # Disable serving static files from the `/public` folder by default since
  # Apache or NGINX already handles this.
  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

I put in astartdemos.sh script in my apps directory for now to start up all demos at one time, but you only set two ENV variables and run a few commands. This is just a short version of what Capistrono does an a deploy.

#!/bin/bash
#startdemos.sh script to start any rails demo apps on restart

export SECRET_KEY_BASE=353f180d1222c8f187e0eae474ad88c8048821a23109a2b1bef76cf3b7a4af84a446cf38ecddb7d27b15452855f728a56a6c7ef51ab926faa3d0542da442a8be
# recompile assets for any new pull. clobber assets before pull
# same secret_key_base used for all demos

cd /Users/developer/apps/bingo
bundle exec bundle
rake assets:clobber
export RAILS_SERVE_STATIC_FILES=true
rake assets:precompile
rails s -p 8602 -e production -b 0.0.0.0 -d

cd /Users/developer/apps/icash
bundle exec bundle
rake assets:clobber
export RAILS_SERVE_STATIC_FILES=true
rake assets:precompile
rails s -p 8603 -e production -b 0.0.0.0 -d

cd /Users/developer/apps/myvfw
bundle exec bundle
rake assets:clobber
export RAILS_SERVE_STATIC_FILES=true
rake assets:precompile
rails s -p 8600 -e production -b 0.0.0.0 -d


# if you are going to just stop/start on demo, do the above in steps

# ps aux | grep demo

# from the list find the process of what your going to update from the ps list
# kill 26322

# cd to that app and clobber the assests so git pull won't complain
# rake assets:clobber
# git pull

# now just cut and paste (or type) any of the above commands