Rails Force SSL Warning

The Rails production.rb config file contains this nice little option if you ever need to run your entire web app over SSL.

Rails.application.configure do
    config.force_ssl = true
end

Warning

It is important, however, to understand exactly what this does. It does not just redirect all your requests to HTTPS. It also makes use of the HTTP Strict Transport Secruity policy (HSTS). This means your server will respond to every request with an included Strict-Transport-Security max-age=31536000 header. Browsers read this as:

Only use HTTPS for the next 31536000 seconds (1 year).

You can browse and clear Chrome's HSTS cache by navigating to:

chrome://net-internals/#hsts

Alternative

So, if you want to ever offer your service over vanilla HTTP again, it is important that you do not use config.force_ssl. Instead, use this ActionController::ForceSSL method in your application_controller.rb file to redirect all your traffic to SSL without using HSTS.

class ApplicationController < ActionController::Base
  force_ssl if: :ssl_configured?

  def ssl_configured?
    !Rails.env.development?
  end
end

Or, only redirect the specific actions that need to be processed over HTTPS using the same method.

Wordless, HAML, SASS, and Coffeescript in your Wordpress

Wordpress can be a giant pain to develop for, especially if you come from a Ruby background. The framework feels outdated and clunky compared to Rails. A lot of the reason it feels this way is the lack of integration with our favorite build tools, such as HAML, SASS, Coffeescript. If you love these tools, like I do, you should check out the Wordless plugin for Wordpress.

Wordless creates a Rails-style theme that organizes all of your assets and let's you use HAML, SASS, and Coffeescript. Then, when you're ready to deploy you can compile and compress your assets, and it converts everything to regular PHP/HTML files.

If you read my previous post about using Docker for Wordpress development, I created a fork of eugeneware's image that you can use here.

git clone https://github.com/johnthepink/docker-wordpress-nginx-wordless
  1. Open this image in Kitematic, create an app, and it will install all the depencies for you, (rvm, ruby, wordless, wordpress, nginx, mysql, etc)
  2. In your browser, navigate to your new wordpress install (Kitematic has a 'View App' icon)
  3. Fill out the info about your site
  4. Login to the admin
  5. Click on 'Appearance' and activate the wordless theme
  6. Click 'Plugins' and actived the wordless plugin
  7. Go to the root of your site and see wordless is working
  8. Develop your custom theme

Check out the Wordless documentation for more info.

Using Kitematic/Docker for Wordpress Development

Kitematic

Kitematic is an awesome new tool for interacting with Docker, an awesome virtualization tool. I recently needed to do some Wordpress development, and rather than install a web server, mySQL, PHP, etc., I was able to generate and run a virtual server using Kitematic that set all this up for me.

Go ahead and download Kitematic, and go through the installation process.

Kitematic uses Dockerfiles to generate Docker containers, which are small virtual environments. Dockerfiles specify an operating system, as well as a series of commands to install the dependencies, languages, and other things necessary for the environment.

If you haven't used Kitematic before, go ahead and make a docker-images directory somewhere on your computer.

mkdir ~/docker-images/ && cd ~/docker-images/

Searching Github will bring up a lot of wordpress-related Dockerfiles. I used this one, as it handles everything for you. We can clone this in to the directory we just made.

git clone https://github.com/eugeneware/docker-wordpress-nginx

Back in Kitematic, click on the 'Images' button and then 'Create Image'. Click 'Select Folder' and then select the folder you just created. It will take a few minutes to install everything.

When the image is ready, click on the 'Apps' icon, and click 'Create App'. Name the app, and select the 'docker-wordpress-nginx' image.

Once the app is ready, click the 'View App' icon and your new Wordpress install will open in your browser.

Happy Dockering!

Deploy From Github To Heroku Using Hipchat/Hubot

So you've got Hipchat running at your company, and you've heard of teams using it for deployment, but you don't want to spend money on some kind of continuous integration service. Here's a cheap way you can start using Hipchat to deploy your repositories to Heroku, or any other server.

Set Up Hubot

First, if you don't have Hubot up and running, follow these install instructions and then deploy it to Heroku. Then come back. Don't worry, it's free.

Set Up Auth Admin

Next, you will want to use Hubot's Auth feature to give yourself admin status. To do this, you will need to know your Hipchat user id. Run this in Hipchat:

@hubot show users

Your id should be to the left of your name. Next, add your id as the Auth admin by running this from the command line:

heroku config:set HUBOT_AUTH_ADMIN=<your user id>

Now, when you ask Hubot who the admin is, it should say you.

@hubot who has admin role

Set Up Deploy Role

The next thing you should do is give whoever needs the ability to deploy from Hipchat the role 'deploy':

@hubot <your name> has deploy role

Now when you ask Hubot what your role is, it should respond with admin and deploy roles:

@hubot what role does <your name> have

Set Environment Variables

The last thing we need to do before setting up our scripts is add some environment variables to Hubot's Heroku server so it can securely talk to Github and Heroku.

First, you need to generate a new RSA key pair by running this in your terminal. Name it hubot_rsa, or something like that.

ssh-keygen -t rsa
cat ~/.ssh/hubot_rsa.pub

Copy the output, and then add it to your Github settings and Heroku settings.

Next, copy the private key you just created out of the terminal output:

cat ~/.ssh/hubot_rsa

Finally, we are going to add all this to Hubot's environment variables. Make sure to user double quotes on the private key to preserve line breaks.

heroku config:set APPNAME_GITHUB_URL=<insert git-based github remote (not https)>
heroku config:set APPNAME_PRODUCTION_URL=<insert git-based heroku remote>
heroku config:set SSH_PRIVATE_KEY="<insert private key>"

Install Hubot Script

Finally, ready for scripts. Take this bad boy, create a file name deploy.coffee in Hubot's scripts directory, and paste it in.

# Description:
#   Deploy is used to deploy from git repositories to servers.
#
# Commands:
#   hubot deploy <repo>
#
# Author:
#   johnthepink

util = require "util"
exec = require("child_process").exec

module.exports = (robot) ->
  robot.respond /deploy (.*)/i, (msg) ->

    # return unless deploy role
    unless robot.auth.hasRole(msg.envelope.user,'deploy')
      msg.send 'You do not have the deploy permissions. Contact the admin.'
      return

    repo = msg.match[1]

    # you can extend this if statement to deploy more than one app
    if repo == 'app'
      msg.send "Deploying #{repo}"

      child = exec "./deploy-app.sh", (err, stdout, stderr) ->
        msg.send err if err
        msg.send stderr if stderr
        msg.send stdout
    else
      msg.send 'I do not know him'

Install Shell Script

Do the same for this script, but this time in Hubot's root directory, and name it deploy-app.sh.

#!/bin/sh

# Mostly stolen from https://github.com/vidpresso/hubot-syncer/blob/master/pullpushprod.sh

cd /app

echo "Setting up SSH."
mkdir /app/.ssh
echo "$SSH_PRIVATE_KEY" > .ssh/id_rsa
echo "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
echo "Host heroku.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config

echo "Cloning repo."
git clone $APPNAME_GITHUB_URL
cd /app/appname

echo "Adding heroku url."
git remote add prod $APPNAME_PRODUCTION_URL

echo "Pushing to heroku."
git checkout master # makes sure there's a master branch to push.
git push prod master

echo "Cleaning up..."
rm -Rf /app/appname
rm -Rf /app/.ssh

exit 0

Deploying!

Hubot is now ready to push the master branch of your Github repo to Heroku. Just call him out:

@hubot deploy app

The great part about this is you can extend the deploy.coffee file to handle multiple apps, and then create a shell script for each of them. If you don't use Heroku, you could easily modify deploy-app.sh to use rsync. You would just need to add your public key to the server and your good to go.

Command Line PHP Server

I spend a lot of time in Rails and Middleman and other Ruby frameworks. But, every once in a while I need to do some Wordpress development, and switching between those two environments can be a pain if you're not careful.

My old way of dealing with this was to:

  1. Shut down Pow
  2. Start up Xamp
  3. Play around in my /private/etc/hosts file
  4. Flush my DNS
  5. etc.

That's just not ideal. Lucky enough, PHP 5.4.0 and higher include a built in command line server. This is perfect for me. All you have to do is run this from the directory you want to serve:

php -S localhost:8000

More info is available in the PHP documenation.