Installing Homebrew & Fabric on OSX 10.9 Mavericks

I’ve just upgraded my Mac at work to 10.9.1 Mavericks. No messing about – completely clean install on a formatted disc. Last year I wrote a quick post about installing Fabric on my Mac, this is an update of that post to reflect a few changes.

If you don’t know…

Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

I think Fabric is a good solution to manage the deployment of websites and applications – it is an integral part of my workflow these days.

Homebrew is a package manager for OSX and is an alternative to the commonly used MacPorts. You can find out a bit more about the differences between them in this thread on arstechnica.

Install Command Line Developer Tools

These were previously part of Xcode but now need to be installed separately, open a terminal and type:

xcode-select --install

…and follow the prompts.

Install Homebrew

The address of the download appears to have chnaged slightly since last year…

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

…and follow the prompts.

When you see the successful installation message, type:

brew doctor

This will alert you to any problems that you need to resolve.

If everything is good you should see the following message:

Your system is ready to brew.

Install Git, Python and Fabric

brew install git
brew install python

You now need to edit your system paths so that Python can be found:

I edited my .bashrc file: nano ~/.bashrc and added the following line:

export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH

And then /etc/paths sudo nano /etc/paths so that usr/local/bin (the location of packages installed by Homebrew) is first:

/usr/local/bin

/usr/local/sbin

/usr/bin

/bin

/usr/sbin

/sbin

And finally install Fabric. There isn’t a Homebrew package available by default, so I just installed Fabric using pip – the pip package manager for Python is installed at as part of Python.

pip install fabric

Check if it works…

Let’s check and see if everything has worked…

$ fab

Fatal error: Couldn\'t find any fabfiles!

Remember that -f can be used to specify fabfile path, and use -h for help.

Aborting.

In this case the Fatal error is good. It means that Fabric has installed correctly and you now need to write some fab files.

Installing Homebrew & Fabric on OSX

I recently started using Fabric to simplify my deployment process.

Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

I quite like Fabric – I’m not 100% sold, but it’s easy to get started with and it won’t be long until you can write complex scripts.

Being able to deploy a complex app with a single command is great. I do not miss long checklists and having to run through step after step in a hurry.

fab deploy

Pretty easy isn’t it?

Why Fabric? To be honest – why not. I know a little bit of Python, so why not learn a bit more? I like the glueyness, the fact that it (mostly) lets you get on with things and doesn’t force you to work in a particular way.

I had a devil of a time getting it up and running though on my Mac running 10.6.8 (Snow Leopard).

Getting started…

Homebrew http://mxcl.github.io/homebrew/
is a package manager for OSX and alternative to MacPorts. Install Fabric and Python via Homebrew to (hopefully) make your life a bit easier…

Install Homebrew

Open a terminal. Run the commands as your user, not sudo

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Install Homebrew and then check the install.

brew doctor

Running brew doctor will identify any problems with your install. Any problems are displayed. Work through any issues it flags up one by one. In my case I ended up having to remove MacPorts and had to reinstall Xcode.

Remove MacPorts

MacPorts is another package manager, but apparently it often doesn’t play with Homebrew.

Follow the instructions http://guide.macports.org/chunked/installing.macports.uninstalling.html

sudo port -fp uninstall installed
sudo rm -rf \
    /opt/local \
    /Applications/DarwinPorts \
    /Applications/MacPorts \
    /Library/LaunchDaemons/org.macports.* \
    /Library/Receipts/DarwinPorts*.pkg \
    /Library/Receipts/MacPorts*.pkg \
    /Library/StartupItems/DarwinPortsStartup \
    /Library/Tcl/darwinports1.0 \
    /Library/Tcl/macports1.0 \
    ~/.macports

Remove Xcode

Xcode is Apple’s Integrated Development Environment (IDE) containing tools for developing software for OS X and iOS.

Based on issues listed by brew doctor I removed Xcode and the re-installed it.

sudo /Developer/Library/uninstall-devtools --mode=all

Make sure you have your Apple ID to hand if you don’t have the .dmg and need to download it from Apple.

Install Git

Next I installed Git using Homebrew if you do not already have it (I use Mercurial day to day, so I didn’t)

brew install git
brew upgrade git

Install python

brew install python

Update your paths to use the version of Python that you’ve just installed.

sudo nano /etc/paths
nano ~/.bashrc

Install Fabric

pip install fabric

Edit: To install Fabric you need to use the pip package manager which was installed as part of Python.

Phew! Everything should be working now… time to write a fabfile.py

Default form values in Rails

One of the problems I initially ran into using Rails was the problem of how the hell you set initial values for your forms? (For example a select menu with a default value chosen but which a user can change). It turns out that as long as you don’t try and set the values in your views, it’s easy and there are several solutions.

As an example I started off with a classic Post model, then moved on to Ghost and Roast to try out alternatives (well you’ve got to call your models something…) I generated an identical scaffold for each and then ran a migration e.g.


rails generate scaffold Post name:string title:string type_id:integer

rake db:migrate


1. Use model after_initialize

/app/models/post.rb


class Post < ActiveRecord::Base
  attr_accessible :name, :title, :type_id
  validates :name,  :presence => true
  
  after_initialize :init
  
  private
    def init
      if self.new_record? && self.type_id.nil?
        self.type_id = 5
      end
    end

end

Edit the model and add an after_initialize callback. This callback is called after a new object is instantiated. The init method sets the default value, but only if the value has not been set yet.

http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

2. Set the default in the controller

/app/controllers/ghosts_controller.rb

  # GET /ghosts/new
  # GET /ghosts/new.json
  def new
    @ghost = Ghost.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @ghost }
    end
  end

Using this alternative solution, you simply set the default value in the new method using @ghost.type_id = 5. This value will only ever be set before the new view for this controller is created.

  # GET /ghosts/new
  # GET /ghosts/new.json
  def new
    @ghost = Ghost.new
    @ghost.type_id = 5

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @ghost }
    end
  end

This solution is easy implement and has the advantage that what you are doing is obvious – the default isn’t tucked away where you might forget about it.

3. Set the default in the database

When the rails generate command was run, a migration was automatically created:

/db/migrate/20130209234708_create_roasts.rb

class CreateRoasts < ActiveRecord::Migration
  def change
    create_table :roasts do |t|
      t.string :name
      t.string :title
      t.integer :type_id
      t.timestamps
    end
  end
end

We can edit the migration to set a default value here e.g.

class CreateRoasts < ActiveRecord::Migration
  def change
    create_table :roasts do |t|
      t.string :name
      t.string :title
      t.integer :type_id, :default => 5
      t.timestamps
    end
  end
end

Conclusion

Here are three ways of setting the default values. What you choose to do is up to you, I think it is important that a solution is consistent with the rest of the code that you are working with (as long as it isn't bad code). Maintainability and clarity shouldn't be neglected and I subscribe to the school of thought that you should always keep things simple - when complicated problems crop up, write complicated solutions, but don't complicate things for the sake of it.

Personally I would be disinclined to use the final method because I think it would be easy to forget about.

Creating a method in the model may be overkill most of the time, but if you are setting a lot of default values or your default values change with context, then setting the default values in the method makes sense to me.

These code examples were built with Ruby on Rails 3.2.9

Sinatra templates from params

Having been playing about with Sinatra I’m finding it cropping up in all kinds of useful ways. I shouldn’t be too surprised though, as that is the whole raison d’etre of micro-frameworks.

When I get around to it I am writing a little Sinatra app, but for me it has a found a real niche helping to prototype or mess around with CSS and Javascript – it only takes a few seconds to create a little app to play around with.

Here is a minimal app.rb file. It will load templates from the (default) /views directory based on the :name parameter.


require 'rubygems'
require 'sinatra'

get '/' do
    erb :index
end

get '/map/:name' do
    erb params[:name].to_sym
end

The key here is to_sym – this is not Sinatra specific it is part of the Ruby language. to_sym converts a string into a symbol.

If the concept of a symbol is a little woolly read this for an excellent explanation. An easy (although probably incorrect or simplistic) is to think of a ruby symbol simply as an immutable name for something that can either be a string or an integer. Once created it cannot change.