Class: KnifeDnsUpdate::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/knife-dns-update/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Config

Returns a new instance of Config.



29
30
31
32
# File 'lib/knife-dns-update/config.rb', line 29

def initialize(path=nil)
  @entries = {}
  instance_eval(File.read(path), path, 1) if path
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



27
28
29
# File 'lib/knife-dns-update/config.rb', line 27

def entries
  @entries
end

Class Method Details

.option(name, default = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/knife-dns-update/config.rb', line 4

def option(name, default=nil)
  ivar = "@#{name}".to_sym
  define_method name do |*args|
    if args.length.zero?
      if instance_variable_defined?(ivar)
        instance_variable_get(ivar)
      else
        default
      end
    elsif block_given?
      instance_variable_set(ivar, yield(*args))
    elsif args.length == 1
      instance_variable_set(ivar, args[0])
    else
      instance_variable_set(ivar, args)
    end
  end
end

Instance Method Details

#entry(name, opt = {}, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/knife-dns-update/config.rb', line 34

def entry(name, opt={}, &block)
  raise "Entry #{name} without definition or block" unless opt||block_given?

  case opt
  when /^\d+\.\d+\.\d+\.\d+$/ then opt = { :a => opt }
  when /^[a-z0-9.-]+\.$/      then opt = { :cname => opt }
  when /^[a-z0-9.-]+$/        then opt = { :cname => [ opt, subdomain, zone ].compact.join('.') << '.' }
  when /^name:[a-z0-9.-]+$/   then opt = { :node => opt[5..-1] }
  when /:/                    then opt = { :query => opt }
  end

  opt[:block] = block if block_given?

  if opt[:q]
    opt[:query] = opt[:q]
    opt.delete(:q)
  end

  entries[name] ||= []
  entries[name] << opt
end

#personalization_token(node) ⇒ Object



71
72
73
# File 'lib/knife-dns-update/config.rb', line 71

def personalization_token(node)
  node.name.gsub(/[^a-z0-9-]+/, '-')
end

#record_for_node(node, is_root_record = false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/knife-dns-update/config.rb', line 56

def record_for_node(node, is_root_record=false)
  case
  when block_given?
    yield node
  when !is_root_record && node['cloud'] && node['cloud']['public_hostname']
    [ :cname, node['cloud']['public_hostname'] ]
  when node['cloud'] && node['cloud']['public_ipv4']
    [ :a, node['cloud']['public_ipv4'] ]
  when !is_root_record && node['fqdn'] && node['fqdn'] !~ /#{Regexp.escape(zone)}$/
    [ :cname, node['fqdn'] ]
  when node['ipaddress']
    [ :a, node['ipaddress'] ]
  end
end