Module: Unbind

Defined in:
lib/unbind.rb,
lib/unbind/view.rb,
lib/unbind/zone.rb,
lib/unbind/version.rb

Defined Under Namespace

Classes: View, Zone

Constant Summary collapse

Commands =
%w(master slave zone).freeze
VERSION =
"0.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/unbind.rb', line 16

def config
  @config
end

.geoipObject (readonly)

Returns the value of attribute geoip.



16
17
18
# File 'lib/unbind.rb', line 16

def geoip
  @geoip
end

Class Method Details

.clear_configObject



54
55
56
# File 'lib/unbind.rb', line 54

def clear_config
  @config = {}
end

.init_geoipObject



63
64
65
66
67
68
69
70
71
# File 'lib/unbind.rb', line 63

def init_geoip
  if @config[:geoip_dat]
    begin
      require 'geoip'
      @geoip = GeoIP.new(@config[:geoip_dat])
    rescue LoadError
    end
  end
end

.load_config(config) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/unbind.rb', line 18

def load_config config
  clear_config

  files = File.directory?(config) ? Dir["#{config}/**/*.conf"] : Dir[config]
  raise "No files could be found (search path: #{config})" if files.empty?
  files.each { |f| raise "File '#{f}' could not be loaded" unless load_file(f) }

  prepare_config

  true
end

.load_file(f) ⇒ Object

private



44
45
46
# File 'lib/unbind.rb', line 44

def load_file f
  load_string(File.read(File.expand_path(f)))
end

.load_string(s) ⇒ Object



48
49
50
51
52
# File 'lib/unbind.rb', line 48

def load_string s
  data = YAML.load(s)
  raise "config data should be a hash" unless data.is_a? Hash
  @config.merge!(data) and true
end

.masterObject



30
31
32
# File 'lib/unbind.rb', line 30

def master(*)
  views.map { |v| v.master }.join("\n")
end

.policiesObject



85
86
87
88
89
# File 'lib/unbind.rb', line 85

def policies
  @policies ||= @config[:zones].reduce([]) { |a, (name, config)| a + (config[:policies] || []) }.map { |policy_name|
    Unbind::Policy.const_get(policy_name.camelize)
  }
end

.prepare_configObject



58
59
60
61
# File 'lib/unbind.rb', line 58

def prepare_config
  symbolize_config
  init_geoip
end

.slaveObject



34
35
36
# File 'lib/unbind.rb', line 34

def slave(*)
  views.map { |v| v.slave }.join("\n")
end

.symbolize_configObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/unbind.rb', line 73

def symbolize_config
  @config.symbolize_keys!

  @config[:views] ||= {}
  @config[:zones] ||= {}

  @config[:zones].each do |k, v|
    v.symbolize_keys!
    v[:data].symbolize_keys!
  end
end

.viewsObject



95
96
97
# File 'lib/unbind.rb', line 95

def views
  @views ||= @config[:views].map { |name, clients| Unbind::View.new(name, {clients: clients, zones: zones}) }
end

.zone(zone_names) ⇒ Object



38
39
40
# File 'lib/unbind.rb', line 38

def zone zone_names
  zone_names.map { |z| Unbind::Zone.new(z, @config[:zones][z]).generate }.join("\n")
end

.zonesObject



91
92
93
# File 'lib/unbind.rb', line 91

def zones
  @zones ||= @config[:zones].map { |name, config| Unbind::Zone.new(name, config) }
end