Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 2b25b46c1d5137908b8757b9c1da4881b58d348a @grantr committed May 31, 2009
Showing with 210 additions and 0 deletions.
  1. +12 −0 bin/game_warden
  2. +21 −0 example/config.rb
  3. +20 −0 example/empty_config.rb
  4. +54 −0 lib/config_builder.rb
  5. +9 −0 lib/configuration.rb
  6. +64 −0 lib/delegating_watcher.rb
  7. +16 −0 lib/event_context.rb
  8. +13 −0 lib/game_warden.rb
  9. +1 −0 vendor/zookeeper
12 bin/game_warden
@@ -0,0 +1,12 @@
+#!/usr/bin/env jruby
+
+config_file = ARGV[0]
+
+unless config_file
+ puts "USAGE: game_warden <config_file>"
+ exit 1
+end
+
+require File.join(File.dirname(__FILE__), '../lib/game_warden')
+
+GameWarden.run(config_file)
21 example/config.rb
@@ -0,0 +1,21 @@
+configure do |config|
+ config.servers = ["127.0.0.1:2181"]
+ #config.prefix = "/gamewarden"
+end
+
+register "/memcached", "memcached servers"
+
+watch "/memcached" do
+ on :children_changed do |event|
+ puts "children changed to #{event.nodes.inspect}"
+ # update dna with new child list
+ # run chef
+ end
+end
+
+
+maintain "/memcached/server", :sequence => true do
+ "127.0.0.1:1024"
+end
+
+#maintain "/memcached/127.0.0.1:1024"
20 example/empty_config.rb
@@ -0,0 +1,20 @@
+configure do |config|
+ config.servers = ["127.0.0.1:2181"]
+ #config.prefix = "/gamewarden"
+end
+
+#watch "/memcached" do
+ #on :children_changed do |event|
+ #puts "children changed to #{event.nodes.inspect}"
+ ## update dna with new child list
+ ## run chef
+ #end
+#end
+
+#register "/memcached", "memcached servers"
+
+#maintain "/memcached/server", :sequence => true do
+ #"127.0.0.1:1024"
+#end
+
+#maintain "/memcached/127.0.0.1:1024"
54 lib/config_builder.rb
@@ -0,0 +1,54 @@
+class ConfigBuilder
+ attr_accessor :config
+
+ def initialize(config="", &block)
+ if block_given?
+ instance_eval(&block)
+ else
+ instance_eval(config)
+ end
+ end
+
+ def configure(&block)
+ @config = Configuration.new
+ yield @config
+ raise "server list required" if @config.servers.empty?
+ @config.zookeeper = ZooKeeper.new(:host => @config.servers.join(","), :watcher => @config.watcher)
+ end
+
+ def watch(path, &block)
+ raise_unless_config
+ WatchContext.new(config, path, &block)
+ end
+
+ def register(*args)
+ raise_unless_config
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ path, data = *args
+ data = yield if block_given?
+ unless config.zookeeper.exists(path)
+ config.zookeeper.create options.merge(:path => path, :data => data)
+ end
+ end
+
+ def maintain(path, data=nil, options={}, &block)
+ raise_unless_config
+ register(path, data, {:ephemeral => true}, &block)
+ end
+
+ def raise_unless_config
+ raise "configure block must be first" unless @config
+ end
+
+ class WatchContext
+ def initialize(config, path, &block)
+ @config = config
+ @path = path
+ instance_eval(&block)
+ end
+
+ def on(events, options={}, &block)
+ @config.watcher.add_watch(@path, events, block)
+ end
+ end
+end
9 lib/configuration.rb
@@ -0,0 +1,9 @@
+class Configuration
+ attr_accessor :prefix, :zookeeper, :servers, :watcher
+
+ def initialize
+ @prefix = ""
+ @watcher = DelegatingWatcher.new(self)
+ @servers = []
+ end
+end
64 lib/delegating_watcher.rb
@@ -0,0 +1,64 @@
+class DelegatingWatcher
+ attr_accessor :watchers, :config
+
+ def initialize(config)
+ @watchers = {}
+ @config = config
+ end
+
+ def add_watch(path, events, proc)
+ Array(events).each do |event|
+ event_type = event_symbol_to_zk(event)
+ @watchers[[path, event_type]] = proc
+ watch_event(path, event_type)
+ end
+ end
+
+ def process(event)
+ puts "processing: #{event.inspect}"
+ if proc = @watchers[[event.path, event.type]]
+ begin
+ nodes = get_nodes_for_event(event)
+ proc.call EventContext.new(event, nodes)
+ ensure
+ watch_event(event.path, event.type)
+ end
+ end
+ end
+
+ private
+ def event_symbol_to_zk(event)
+ case event
+ when :none
+ ZooKeeper::WatcherEvent::EventNone
+ when :node_created
+ ZooKeeper::WatcherEvent::EventNodeCreated
+ when :node_deleted
+ ZooKeeper::WatcherEvent::EventNodeDeleted
+ when :data_changed
+ ZooKeeper::WatcherEvent::EventNodeDataChanged
+ when :children_changed
+ ZooKeeper::WatcherEvent::EventNodeChildrenChanged
+ end
+ end
+
+ def get_nodes_for_event(event)
+ case event.type
+ when ZooKeeper::WatcherEvent::EventNodeChildrenChanged
+ config.zookeeper.children(event.path)
+ else
+ [event.path]
+ end
+ end
+
+ def watch_event(path, type)
+ case type
+ when ZooKeeper::WatcherEvent::EventNodeCreated, ZooKeeper::WatcherEvent::EventNodeDeleted
+ config.zookeeper.exists(:path => path, :watch => true)
+ when ZooKeeper::WatcherEvent::EventNodeDataChanged
+ config.zookeeper.get(:path => path, :watch => true)
+ when ZooKeeper::WatcherEvent::EventNodeChildrenChanged
+ config.zookeeper.children(:path => path, :watch => true)
+ end
+ end
+end
16 lib/event_context.rb
@@ -0,0 +1,16 @@
+class EventContext
+ attr_accessor :nodes, :event, :type
+
+ def initialize(event, nodes)
+ @event = event
+ @nodes = nodes
+ end
+
+ def node
+ nodes.first
+ end
+
+ def type
+ @event.type
+ end
+end
13 lib/game_warden.rb
@@ -0,0 +1,13 @@
+require 'config_builder'
+require 'configuration'
+require 'delegating_watcher'
+require 'event_context'
+require 'vendor/zookeeper/lib/zookeeper'
+
+module GameWarden
+ def self.run(config_filename)
+ config_file = File.open(config_filename)
+ builder = ConfigBuilder.new(config_file.read)
+ sleep
+ end
+end
1 vendor/zookeeper
@@ -0,0 +1 @@
+Subproject commit 2dbad5798db21ff77e4f540a86a48434624caf28

0 comments on commit 2b25b46

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