Permalink
Browse files

Update issue 4

First class that loads some YAML files and expose their data
as well as config params and tests
  • Loading branch information...
rip@devco.net
rip@devco.net committed Oct 6, 2009
1 parent 13b147a commit a6645300fbdcf0174e31536133555151c11c283e
View
@@ -23,6 +23,7 @@ class RecordCallError < RuntimeError; end
autoload :Config, "pdns/config.rb"
autoload :Log, "pdns/log.rb"
autoload :Stats, "pdns/stats.rb"
+ autoload :Extdata, "pdns/extdata.rb"
# should have a copy of Pdns::Config
@@config = nil
@@ -13,6 +13,7 @@ module Pdns
# max_log_size = 1024000
# maint_interval = 60
# statsdir = /var/log/pdns/stats
+ # datadir = /var/lib/pdns
#
# Additionally freeform config can be set for modules, these need to be handled by the modules but
# config lines like:
@@ -23,7 +24,7 @@ module Pdns
class Config
attr_accessor :logfile, :loglevel, :records_dir, :soa_contact, :soa_nameserver,
:reload_interval, :keep_logs, :max_log_size, :geoipdb, :maint_interval,
- :modules, :statsdir, :recordstats
+ :modules, :statsdir, :recordstats, :datadir
def initialize(configfile)
@logfile = "/var/log/pdns/pipe-backend.log"
@@ -38,6 +39,7 @@ def initialize(configfile)
@modules = {}
@statsdir = "/var/log/pdns/stats"
@recordstats = true
+ @datadir = "/var/lib/pdns"
if File.exists?(configfile)
@@ -74,6 +76,8 @@ def initialize(configfile)
@maint_interval = val.to_i
when "statsdir"
@statsdir = val
+ when "datadir"
+ @datadir = val
when "loglevel"
@loglevel = val
when "recordstats"
@@ -0,0 +1,54 @@
+require 'yaml'
+require 'find'
+
+module Pdns
+ class Extdata
+ def initialize
+ loaddata
+ end
+
+ # Loads all data files in data dir, first it zeros the Hash containing all data
+ # then goes through all files and load them, this ensures we have no unintended
+ # stale data at any time
+ def loaddata
+ datadir = Pdns.config.datadir
+ @data = Hash.new
+
+ raise "Cannot process external data, #{datadir} does not exist" unless File.directory?(datadir)
+ Pdns.debug("Looking for external data in #{datadir}")
+
+ dir = Dir.new(datadir)
+
+ dir.entries.grep(/\.pdb$/).each do |r|
+ Pdns.debug("Loading data from #{datadir}/#{r}")
+
+ pdbname = File.basename(r, ".pdb").to_sym
+
+ begin
+ @data[pdbname] = Hash.new
+ @data[pdbname][:loadtime] = Time.now.to_i
+ @data[pdbname][:data] = YAML.load_file("#{datadir}/#{r}")
+
+ raise("Coult not find any data in file") unless @data[pdbname][:data]
+ rescue Exception => e
+ Pdns.error("Could not load #{datadir}/#{r}: #{e}")
+ @data.delete pdbname if @data.include?(pdbname)
+ end
+ end
+ end
+
+ # Returns the entire data record for a certain record
+ def all_data(record)
+ @data.include?(record) ? @data[record] : {}
+ end
+
+ # Returns a specific key from a record else default
+ def data(record, key, default=nil)
+ return default unless @data.include?(record)
+
+ @data[record][:data].include?(key) ? @data[record][:data][key] : default
+ end
+ end
+end
+
+# vi:tabstop=4:expandtab:ai
@@ -31,7 +31,7 @@ def self.add_resolver(name, options = {}, &block)
# Clears out all the resolvers that are supported, this should be called before loading new ones from disk
# for example to be sure you don't have any weird leftovers
#
- # It only clears the @@resolvers hash not the @@resolverstats hash to keep stats across reloads
+ # It only clears the @@resolvers hash and not any stats to keep stats across reloads
def self.empty!
@@resolvers = {}
end
@@ -0,0 +1,4 @@
+---
+:created: 1254850881
+:webserver_a_load: 10
+:webserver_b_load: 20
@@ -8,6 +8,7 @@ keep_logs = 10
max_log_size = 1024000
maint_interval = 60
statsdir = /tmp
+datadir = data
# GeoIP Configuration
geoip.dblocation = /var/lib/GeoIP/GeoIP.dat
@@ -0,0 +1,32 @@
+require 'test/unit'
+require 'pdns'
+
+class TC_ExtdataTests < Test::Unit::TestCase
+ def test_if_data_loads
+ Pdns.config = Pdns::Config.new("etc/pdns-ruby-backend.cfg")
+
+ d = Pdns::Extdata.new
+
+ assert_equal 1254850881, d.data(:foo, :created)
+ end
+
+ def test_if_default_works
+ Pdns.config = Pdns::Config.new("etc/pdns-ruby-backend.cfg")
+
+ d = Pdns::Extdata.new
+
+ assert_equal 1, d.data(:foo, :foo, 1)
+ assert_equal 1, d.data(:bar, :foo, 1)
+ end
+
+ def test_all_data
+ Pdns.config = Pdns::Config.new("etc/pdns-ruby-backend.cfg")
+
+ d = Pdns::Extdata.new
+
+ assert_equal 20, d.all_data(:foo)[:data][:webserver_b_load]
+ assert_equal Hash.new, d.all_data(:bar)
+ end
+end
+
+# vi:tabstop=4:expandtab:ai:filetype=ruby
@@ -8,3 +8,4 @@
require 'tc_resolvers.rb'
require 'tc_response.rb'
require 'tc_stats.rb'
+require 'tc_extdata.rb'

0 comments on commit a664530

Please sign in to comment.