Class: TestLab::Provisioner::Bind

Inherits:
Object
  • Object
show all
Defined in:
lib/testlab/provisioners/bind.rb

Overview

Bind Provisioner Class

Author:

  • Zachary Patten <zachary AT jovelabs DOT com>

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, ui = nil) ⇒ Bind

Returns a new instance of Bind.



13
14
15
16
17
18
19
20
21
22
# File 'lib/testlab/provisioners/bind.rb', line 13

def initialize(config={}, ui=nil)
  @config = (config || Hash.new)
  @ui     = (ui     || TestLab.ui)

  @config[:bind] ||= Hash.new
  @config[:bind][:domain]     ||= "tld.invalid"
  @config[:bind][:forwarders] ||= %w(8.8.8.8 8.8.4.4)

  @ui.logger.debug { "config(#{@config.inspect})" }
end

Instance Method Details

#bind_install(node) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/testlab/provisioners/bind.rb', line 140

def bind_install(node)
  node.bootstrap(<<-EOSHELL)
    export DEBIAN_FRONTEND="noninteractive"

    (dpkg --status bind9 &> /dev/null || apt-get -qy install bind9)
    rm -fv /etc/bind/{*.arpa,*.zone,*.conf*}
  EOSHELL
end

#bind_provision(node) ⇒ Object



156
157
158
159
# File 'lib/testlab/provisioners/bind.rb', line 156

def bind_provision(node)
  build_bind_conf(node)
  bind_reload(node)
end

#bind_reload(node) ⇒ Object



149
150
151
152
153
154
# File 'lib/testlab/provisioners/bind.rb', line 149

def bind_reload(node)
  node.bootstrap(<<-EOSHELL)
    chown -Rv bind:bind /etc/bind
    rndc reload
  EOSHELL
end

#build_bind_conf(node) ⇒ Object

Builds the BIND configuration



133
134
135
136
137
138
# File 'lib/testlab/provisioners/bind.rb', line 133

def build_bind_conf(node)
  node.file(:target => %(/etc/bind/named.conf), :chown => "bind:bind") do |file|
    build_bind_main_partial(file)
    build_bind_zone_partial(node, file)
  end
end

#build_bind_db(node, zone, records) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/testlab/provisioners/bind.rb', line 123

def build_bind_db(node, zone, records)
  bind_db_template = File.join(TestLab::Provisioner.template_dir, "bind", 'bind-db.erb')

  node.file(:target => %(/etc/bind/db.#{zone}), :chown => "bind:bind") do |file|
    file.puts(ZTK::Template.do_not_edit_notice(:message => "TestLab v#{TestLab::VERSION} BIND DB: #{zone}", :char => ';'))
    file.puts(ZTK::Template.render(bind_db_template, { :zone => zone, :records => records }))
  end
end

#build_bind_main_partial(file) ⇒ Object

Builds the main bind configuration sections



66
67
68
69
70
71
# File 'lib/testlab/provisioners/bind.rb', line 66

def build_bind_main_partial(file)
  bind_conf_template = File.join(TestLab::Provisioner.template_dir, "bind", "bind.erb")

  file.puts(ZTK::Template.do_not_edit_notice(:message => "TestLab v#{TestLab::VERSION} BIND Configuration", :char => '//'))
  file.puts(ZTK::Template.render(bind_conf_template, @config))
end

#build_bind_recordsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/testlab/provisioners/bind.rb', line 73

def build_bind_records
  forward_records = Hash.new
  reverse_records = Hash.new

  TestLab::Container.all.each do |container|
    container.domain ||= @config[:bind][:domain]

    container.interfaces.each do |interface|
      forward_records[container.domain] ||= Array.new
      forward_records[container.domain] << %(#{container.id} IN A #{interface.ip})

      reverse_records[interface.network_id] ||= Array.new
      reverse_records[interface.network_id] << %(#{interface.ptr} IN PTR #{container.fqdn}.)
    end

  end
  { :forward => forward_records, :reverse => reverse_records }
end

#build_bind_zone_partial(node, file) ⇒ Object

Builds the bind configuration sections for our zones



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/testlab/provisioners/bind.rb', line 93

def build_bind_zone_partial(node, file)
  bind_zone_template = File.join(TestLab::Provisioner.template_dir, "bind", 'bind-zone.erb')

  bind_records = build_bind_records
  forward_records = bind_records[:forward]
  reverse_records = bind_records[:reverse]

  TestLab::Network.all.each do |network|
    context = {
      :zone => network.arpa
    }

    file.puts
    file.puts(ZTK::Template.render(bind_zone_template, context))

    build_bind_db(node, network.arpa, reverse_records[network.id])
  end

  TestLab::Container.domains.each do |domain|
    context = {
      :zone => domain
    }

    file.puts
    file.puts(ZTK::Template.render(bind_zone_template, context))

    build_bind_db(node, domain, forward_records[domain])
  end
end

#on_container_provision(container) ⇒ Boolean Also known as: on_container_up

Bind: Container Provision

Parameters:

Returns:

  • (Boolean)

    True if successful.



41
42
43
44
45
46
47
48
49
# File 'lib/testlab/provisioners/bind.rb', line 41

def on_container_provision(container)
  @ui.logger.debug { "BIND Provisioner: Container #{container.id}" }

  # ensure we override the config hash with the nodes configuration
  @config = container.node.config
  bind_provision(container.node)

  true
end

#on_network_provision(network) ⇒ Boolean Also known as: on_network_up

Bind: Network Provision

Parameters:

Returns:

  • (Boolean)

    True if successful.



56
57
58
59
60
61
62
# File 'lib/testlab/provisioners/bind.rb', line 56

def on_network_provision(network)
  @ui.logger.debug { "BIND Provisioner: Network #{network.id}" }

  bind_reload(network.node)

  true
end

#on_node_provision(node) ⇒ Boolean

Bind: Node Provision

Parameters:

Returns:

  • (Boolean)

    True if successful.



28
29
30
31
32
33
34
35
# File 'lib/testlab/provisioners/bind.rb', line 28

def on_node_provision(node)
  @ui.logger.debug { "BIND Provisioner: Node #{node.id}" }

  bind_install(node)
  bind_provision(node)

  true
end