Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

lots of refactoring. features next

  • Loading branch information...
commit 7e866c79ca129e5b180adbbe0e08b77a5972e937 1 parent c2569df
@jnewland authored
Showing with 151 additions and 31 deletions.
  1. +13 −31 bin/god_web
  2. +23 −0 lib/environment.rb
  3. +103 −0 lib/god_web.rb
  4. +12 −0 views/status.erb
View
44 bin/god_web
@@ -1,42 +1,24 @@
#!/usr/bin/env ruby
-require 'rubygems'
-require 'god'
-require 'sinatra'
-require 'stringio'
-require 'yaml'
-#included until http://sinatra.lighthouseapp.com/projects/9779/tickets/16-patch-http-authentication is in a released version
-require File.dirname(__FILE__) + '/../lib/sinatra_http_auth'
-
-config = {
- 'god_port' => 17165,
- 'username' => nil,
- 'password' => nil
-}
-begin
- config.merge!(YAML.load(File.read(ARGV[0])))
-rescue
-end
+require File.dirname(__FILE__) + '/../lib/environment'
before do
- unless config['username'].nil? && config['password'].nil?
+ unless GODWEB_CONFIG['username'].nil? && GODWEB_CONFIG['password'].nil?
authenticate_or_request_with_http_basic "GodWeb" do
- |user, pass| user == config['username'] && pass == config['password']
+ |user, pass| user == GODWEB_CONFIG['username'] && pass == GODWEB_CONFIG['password']
end
end
-end
-
-def with_captured_stdout(&block)
- io = StringIO.new
- $stdout = io
- yield
- $stdout = STDOUT
- io.string
+ GODWEB.ping
end
get '/' do
- with_captured_stdout do
- response.header['Content-Type'] = 'text/plain'
- God::CLI::Command.new('status', {:port => config['god_port']}, [])
- end
+ @status = GODWEB.status
+ show(:status)
end
+
+private
+
+ def show(template, title = 'GodWeb')
+ @title = title
+ erb(template)
+ end
View
23 lib/environment.rb
@@ -0,0 +1,23 @@
+require 'rubygems'
+require 'god'
+require 'sinatra'
+require 'stringio'
+require 'yaml'
+require 'erb'
+#included until http://sinatra.lighthouseapp.com/projects/9779/tickets/16-patch-http-authentication is in a released version
+require File.dirname(__FILE__) + '/sinatra_http_auth'
+require File.dirname(__FILE__) + '/god_web'
+
+config = {
+ 'god_port' => 17165,
+ 'username' => nil,
+ 'password' => nil
+}
+begin
+ config.merge!(YAML.load(File.read(ARGV[0])))
+ GODWEB_CONFIG = config
+rescue
+ GODWEB_CONFIG = config
+end
+
+GODWEB = GodWeb.new(config)
View
103 lib/god_web.rb
@@ -0,0 +1,103 @@
+class GodWeb
+ def initialize(config)
+ @config = config
+ setup
+ ping
+ end
+
+ def setup
+ DRb.start_service
+ @server = DRbObject.new(nil, God::Socket.socket(@config['god_port']))
+ end
+
+ # ping server to ensure that it is responsive
+ def ping
+ tries = 3
+ begin
+ @server.ping
+ rescue Exception => e
+ if (tries -= 1) > 0
+ retry
+ end
+ raise e, "The server is not available (or you do not have permissions to access it)"
+ end
+ end
+
+private
+
+ def method_missing(meth,*args)
+ if %w{load status log quit terminate}.include?(meth.to_s)
+ ping
+ send("#{meth}_command")
+ elsif %w{start stop restart monitor unmonitor remove}.include?(meth.to_s)
+ ping
+ lifecycle_command(args.first, meth.to_s)
+ else
+ raise NoMethodError
+ end
+ end
+
+ def status_command
+ watches = @server.status
+ end
+
+ #TODO
+ # def log_command
+ # begin
+ # Signal.trap('INT') { exit }
+ # name = @args[1]
+ #
+ # unless name
+ # puts "You must specify a Task or Group name"
+ # exit!
+ # end
+ #
+ # t = Time.at(0)
+ # loop do
+ # print @server.running_log(name, t)
+ # t = Time.now
+ # sleep 1
+ # end
+ # rescue God::NoSuchWatchError
+ # puts "No such watch"
+ # rescue DRb::DRbConnError
+ # puts "The server went away"
+ # end
+ # end
+
+ def quit_command
+ begin
+ @server.terminate
+ return false
+ rescue DRb::DRbConnError
+ return true
+ end
+ end
+
+ def terminate_command
+ stopped_all = false
+ if @server.stop_all
+ stopped_all = true
+ end
+
+ begin
+ @server.terminate
+ return false
+ rescue DRb::DRbConnError
+ return stopped_all
+ end
+ end
+
+
+ def lifecycle_command(*args)
+ # get the name of the watch/group
+ name = args.first
+ command = args.last
+
+ # send @command
+ watches = @server.control(name, command)
+
+ watches.empty? ? [] : watches
+ end
+
+end
View
12 views/status.erb
@@ -0,0 +1,12 @@
+<html>
+ <head>
+ <title><%= @title %></title>
+ </head>
+ <body>
+ <ul>
+ <% @status.each do |watch, status| %>
+ <li id="<%= watch %>" class="<%= status[:state] %>"><%= watch %> - <%= status[:state] %></li>
+ <% end %>
+ </ul>
+ </body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.