Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added herd script

  • Loading branch information...
commit 983f141aa6e976dcd379f8bb3d172514c8a7eb44 1 parent 82fc612
Wes Oldenbeuving Narnach authored

Showing 1 changed file with 106 additions and 0 deletions. Show diff stats Hide diff stats

  1. +106 0 bin/herd
106 bin/herd
... ... @@ -0,0 +1,106 @@
  1 +#!/usr/bin/env ruby
  2 +require 'yaml'
  3 +
  4 +class Herd
  5 + attr_reader :project
  6 +
  7 + def initialize(project)
  8 + @project = project
  9 + end
  10 +
  11 + def init
  12 + config[project] = {
  13 + 'rails_dirs' => []
  14 + }
  15 + File.open(config_file, 'wb') {|f| f.write(config.to_yaml)}
  16 + end
  17 +
  18 + def start
  19 + check_rails_dirs
  20 + all_mongrels 'start'
  21 + end
  22 +
  23 + def stop
  24 + all_mongrels 'stop'
  25 + end
  26 +
  27 + def restart
  28 + all_mongrels 'restart'
  29 + end
  30 +
  31 + private
  32 +
  33 + def all_mongrels(cmd)
  34 + rails_dirs.each do |dir|
  35 + mongrel(dir, cmd)
  36 + end
  37 + end
  38 +
  39 + def check_rails_dirs
  40 + raise "No rails directories defined for this project" if rails_dirs.size == 0
  41 + end
  42 +
  43 + def config
  44 + @config ||= YAML.load_file(config_file)
  45 + rescue SystemCallError => e
  46 + return @config ||= {}
  47 + end
  48 +
  49 + def config_file
  50 + File.expand_path("~/.herd.yml")
  51 + end
  52 +
  53 + def mongrel(dir, cmd)
  54 + sh ('cd %s' % dir), ('mongrel_rails cluster::%s %s' % [cmd, mongrel_options.join(' ')])
  55 + end
  56 +
  57 + def mongrel_options
  58 + @mongrel_options ||= (project_config['mongrel_options'] || [])
  59 + end
  60 +
  61 + def project_config
  62 + @project_config ||= (config[project] || {})
  63 + end
  64 +
  65 + def rails_dirs
  66 + @rails_dirs ||= (project_config['rails_dirs'] || [])
  67 + end
  68 +
  69 + def sh(*cmds)
  70 + printf "# %s\n" % cmds.join(" && \n# ")
  71 + system cmds.join(" && ")
  72 + end
  73 +end
  74 +
  75 +cmd = ARGV.shift
  76 +if %w[-h h help --help].include? cmd or cmd.nil?
  77 + puts <<-EOS
  78 +Herd - Herd a pack of Rails mongrels
  79 +
  80 +Syntax
  81 + herd <action> <projectname>
  82 +
  83 +Examples
  84 + herd init foobar
  85 + Create a new entry in ~/.herd.yml for the foobar project:
  86 + ---
  87 + foobar:
  88 + rails_dirs: []
  89 + The next step is to edit this entry in a text editor,
  90 + to actually point to your rails directories:
  91 + ---
  92 + foobar:
  93 + rails_dirs:
  94 + - ~/projects/foobar/backend
  95 + - ~/projects/foobar/frontend
  96 +
  97 + herd <start | stop | restart> <projectname>
  98 + Send a command to the mongrel cluster of each rails directory in the project.
  99 + EOS
  100 + exit
  101 +end
  102 +
  103 +project = ARGV.shift
  104 +raise(ArgumentError, "You must provide a project name") if project.nil?
  105 +h = Herd.new project
  106 +h.send(cmd)

0 comments on commit 983f141

Please sign in to comment.
Something went wrong with that request. Please try again.