1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
122
123
124
125
126
127
128
129
130
131
132
133
134
|
module LeapCli
module Commands
desc "Compile generated files."
command :compile do |c|
c.desc 'Compiles node configuration files into hiera files used for deployment.'
c.command :all do |all|
all.action do |global_options,options,args|
compile_hiera_files
end
end
c.desc "Compile a DNS zone file for your provider."
c.command :zone do |zone|
zone.action do |global_options, options, args|
compile_zone_file
end
end
c.default_command :all
end
protected
def compile_hiera_files(nodes=nil)
# these must come first
update_compiled_ssh_configs
# export generated files
manager.export_nodes(nodes)
manager.export_secrets(nodes.nil?) # only do a "clean" export if we are examining all the nodes
end
def update_compiled_ssh_configs
update_authorized_keys
update_known_hosts
end
##
## ZONE FILE
##
def relative_hostname(fqdn)
@domain_regexp ||= /\.?#{Regexp.escape(provider.domain)}$/
fqdn.sub(@domain_regexp, '')
end
#
# serial is any number less than 2^32 (4294967296)
#
def compile_zone_file
hosts_seen = {}
f = $stdout
f.puts ZONE_HEADER % {:domain => provider.domain, :ns => provider.domain, :contact => provider.contacts.default.sub('@','.')}
max_width = manager.nodes.values.inject(0) {|max, node| [max, relative_hostname(node.domain.full).length].max }
put_line = lambda do |host, line|
host = '@' if host == ''
f.puts("%-#{max_width}s %s" % [host, line])
end
f.puts ORIGIN_HEADER
# 'A' records for primary domain
manager.nodes[:environment => '!local'].each_node do |node|
if node.dns['aliases'] && node.dns.aliases.include?(provider.domain)
put_line.call "", "IN A #{node.ip_address}"
end
end
# NS records
if provider['dns'] && provider.dns['nameservers']
provider.dns.nameservers.each do |ns|
put_line.call "", "IN NS #{ns}."
end
end
# all other records
manager.environments.each do |env|
next if env == 'local'
nodes = manager.nodes[:environment => env]
next unless nodes.any?
f.puts ENV_HEADER % (env.nil? ? 'default' : env)
nodes.each_node do |node|
if node.dns.public
hostname = relative_hostname(node.domain.full)
put_line.call relative_hostname(node.domain.full), "IN A #{node.ip_address}"
end
if node.dns['aliases']
node.dns.aliases.each do |host_alias|
if host_alias != node.domain.full && host_alias != provider.domain
put_line.call relative_hostname(host_alias), "IN CNAME #{relative_hostname(node.domain.full)}"
end
end
end
if node.services.include? 'mx'
put_line.call relative_hostname(node.domain.full_suffix), "IN MX 10 #{relative_hostname(node.domain.full)}"
end
end
end
end
ENV_HEADER = %[
;;
;; ENVIRONMENT %s
;;
]
ZONE_HEADER = %[
;;
;; BIND data file for %{domain}
;;
$TTL 600
$ORIGIN %{domain}.
@ IN SOA %{ns}. %{contact}. (
0000 ; serial
7200 ; refresh ( 24 hours)
3600 ; retry ( 2 hours)
1209600 ; expire (1000 hours)
600 ) ; minimum ( 2 days)
;
]
ORIGIN_HEADER = %[
;;
;; ZONE ORIGIN
;;
]
end
end
|