Mar 20
2008

Find out what ports your mongrels are using

I like mongrel. I deploy all of our Rails apps using it.

But…I got sick of opening and reading each of my mongrel_cluster.yml config files when adding a new mongrel to the server.

I cooked up this quick script to list all Rails apps on a production box – and their mongrel ports. It assumes all of your apps live in /var/www/(app_name)/current. You might have to do some modifications if you store your Rails apps in a different place.

Maybe you’ll find it useful as well.

list_mongrel_ports.rb

#!/usr/bin/env ruby
# Loops through all rails apps and compiles port information
# for mongrels.
#
# Great for figuring out what ports are in use when deploying a new app.
require 'fileutils'

APP_DIR = '/var/www/'

def get_mongrel_config_info(app)
  config_f = File.join(APP_DIR, app, 'current', 'config', 'mongrel_cluster.yml')
  if File.exists?(config_f)
    starting_port, servers = ''
    File.open(config_f, 'r') do |f|
      while line = f.gets
        if line.include?('port:')
          starting_port = line.gsub(/port:|"| /, '')
        elsif line.include?('servers:')
          servers = line.gsub(/servers:| /, '')
        end
      end
    end
    servers ||= 1
    ports = []
    servers.to_i.times do |i|
      ports << starting_port.to_i+i
    end
    return ports.join(', ')
  else
    return nil
  end
end

Dir.open(APP_DIR).each do |app|
  info = get_mongrel_config_info(app)
  puts "#{app}\n  ports: #{info}" unless info.nil?
end

Sample output

-> ./list_mongrel_ports.rb 
cashboard
  ports: 8500, 8501, 8502, 8503, 8504
getcashboard
  ports: 8010, 8011
cbinfo
  ports: 8700
cbforum
  ports: 8705

Hrm…looks like I could do a little reorganization of my ports :)

Written by Seth Banks

Seth spends most of his days leading the design team at Green Bits and improving Cashboard. Occasionally he finds time to write about music, design, startups, and technology.

Tagged: rails