Permalink
Browse files

Added herd script

  • Loading branch information...
1 parent 82fc612 commit 983f141aa6e976dcd379f8bb3d172514c8a7eb44 @Narnach Narnach committed Jul 29, 2008
Showing with 106 additions and 0 deletions.
  1. +106 −0 bin/herd
View
@@ -0,0 +1,106 @@
+#!/usr/bin/env ruby
+require 'yaml'
+
+class Herd
+ attr_reader :project
+
+ def initialize(project)
+ @project = project
+ end
+
+ def init
+ config[project] = {
+ 'rails_dirs' => []
+ }
+ File.open(config_file, 'wb') {|f| f.write(config.to_yaml)}
+ end
+
+ def start
+ check_rails_dirs
+ all_mongrels 'start'
+ end
+
+ def stop
+ all_mongrels 'stop'
+ end
+
+ def restart
+ all_mongrels 'restart'
+ end
+
+ private
+
+ def all_mongrels(cmd)
+ rails_dirs.each do |dir|
+ mongrel(dir, cmd)
+ end
+ end
+
+ def check_rails_dirs
+ raise "No rails directories defined for this project" if rails_dirs.size == 0
+ end
+
+ def config
+ @config ||= YAML.load_file(config_file)
+ rescue SystemCallError => e
+ return @config ||= {}
+ end
+
+ def config_file
+ File.expand_path("~/.herd.yml")
+ end
+
+ def mongrel(dir, cmd)
+ sh ('cd %s' % dir), ('mongrel_rails cluster::%s %s' % [cmd, mongrel_options.join(' ')])
+ end
+
+ def mongrel_options
+ @mongrel_options ||= (project_config['mongrel_options'] || [])
+ end
+
+ def project_config
+ @project_config ||= (config[project] || {})
+ end
+
+ def rails_dirs
+ @rails_dirs ||= (project_config['rails_dirs'] || [])
+ end
+
+ def sh(*cmds)
+ printf "# %s\n" % cmds.join(" && \n# ")
+ system cmds.join(" && ")
+ end
+end
+
+cmd = ARGV.shift
+if %w[-h h help --help].include? cmd or cmd.nil?
+ puts <<-EOS
+Herd - Herd a pack of Rails mongrels
+
+Syntax
+ herd <action> <projectname>
+
+Examples
+ herd init foobar
+ Create a new entry in ~/.herd.yml for the foobar project:
+ ---
+ foobar:
+ rails_dirs: []
+ The next step is to edit this entry in a text editor,
+ to actually point to your rails directories:
+ ---
+ foobar:
+ rails_dirs:
+ - ~/projects/foobar/backend
+ - ~/projects/foobar/frontend
+
+ herd <start | stop | restart> <projectname>
+ Send a command to the mongrel cluster of each rails directory in the project.
+ EOS
+ exit
+end
+
+project = ARGV.shift
+raise(ArgumentError, "You must provide a project name") if project.nil?
+h = Herd.new project
+h.send(cmd)

0 comments on commit 983f141

Please sign in to comment.