require 'rubygems' require 'ohai' require 'httpclient' $SPIDER_SERVANT_LIB = File.dirname(__FILE__) require "#{$SPIDER_SERVANT_LIB}/resource" require "#{$SPIDER_SERVANT_LIB}/resources/db" require "#{$SPIDER_SERVANT_LIB}/resources/db/mysql" module Spider module Servant class Servant DEFAULTS = { :config_file => '/etc/spider/servant.yml' } def initialize(config_file=nil) @config = { :server_url => 'http://localhost:8989/spider/master/ping' } config_file ||= Servant::DEFAULTS[:config_file] if File.exists?(Servant::DEFAULTS[:config_file]) if config_file y = YAML::load_file(config_file) @config.merge!(y) end ohai = Ohai::System.new ohai.all_plugins @ohai = ohai @config[:servant_id] ||= ohai["hostname"] @config[:servant_name] ||= @config[:servant_id] create_resources end def ping_server(url=nil) url ||= @config[:server_url] @system_status = @ohai.to_json clnt = HTTPClient.new clnt.post(url, Spider::HTTP.params_to_hash(self.description)) end def create_resources @resources = {} return unless @config['resources'].is_a?(Hash) @config['resources'].each do |res_type, hash| @resources[res_type] ||= {} case res_type when 'db' hash.each do |res_name, params| @resources[res_type][res_name] = Resources::Db.get_resource(params['url']) end end end end def description { :servant_id => @config[:servant_id], :servant_name => @config[:servant_name], :system_status => @system_status, :resources => resources_description } end def resources_description desc = {} @resources.each do |type, type_resources| desc[type] ||= {} type_resources.each do |name, resource| desc[type][name] = { :description => resource.description.to_json } end end end end end end