Class: Elasticdns::Generator
- Inherits:
-
Object
- Object
- Elasticdns::Generator
- Defined in:
- lib/elasticdns.rb
Instance Method Summary collapse
- #bind_checkconf ⇒ Object
- #bind_checkzone ⇒ Object
- #bind_init_cmd ⇒ Object
- #closefile(file) ⇒ Object
- #ec2init ⇒ Object
-
#initialize(options = {}) ⇒ Generator
constructor
A new instance of Generator.
- #md5(string) ⇒ Object
- #md5chk(orig_file, new_md5) ⇒ Object
- #run ⇒ Object
- #update_acl_masters_file ⇒ Object
- #update_hosts_file ⇒ Object
- #update_notify_file ⇒ Object
- #updatefile(ips, new_md5, temp_file, orig_file, prefix) ⇒ Object
- #write_to_file(ips, orig_file, temp_file, prefix) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Generator
Returns a new instance of Generator.
11 12 13 |
# File 'lib/elasticdns.rb', line 11 def initialize(={}) @config = Elasticdns::Config.new().to_hash end |
Instance Method Details
#bind_checkconf ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/elasticdns.rb', line 64 def bind_checkconf unless system("#{@config[:bind9_checkconf_path]} #{@config[:bind9_named_conf_file]}") puts "Bind checkconf failed" return false else return true end end |
#bind_checkzone ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/elasticdns.rb', line 73 def bind_checkzone @config[:bind9_zone_files].each do |zone| unless system("#{@config[:bind9_checkzone_path]} #{zone}") puts "Bind checkzone failed" return false else return true end end end |
#bind_init_cmd ⇒ Object
84 85 86 87 88 |
# File 'lib/elasticdns.rb', line 84 def bind_init_cmd if bind_checkconf && bind_checkzone system("#{@config[:bind9_init_cmd]}") end end |
#closefile(file) ⇒ Object
30 31 32 33 |
# File 'lib/elasticdns.rb', line 30 def closefile(file) file.close file.unlink end |
#ec2init ⇒ Object
24 25 26 27 28 |
# File 'lib/elasticdns.rb', line 24 def ec2init AWS.memoize do AWS::EC2.new(:access_key_id => @config[:ec2_access_key_id], :secret_access_key => @config[:ec2_secret_access_key], :region => @config[:ec2_region]) end end |
#md5(string) ⇒ Object
35 36 37 |
# File 'lib/elasticdns.rb', line 35 def md5(string) md5 = Digest::MD5.hexdigest(string) end |
#md5chk(orig_file, new_md5) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/elasticdns.rb', line 39 def md5chk(orig_file, new_md5) File.open(orig_file, 'r') do |file| file.each_line do |line| if line =~ /### MD5:/ old_md5 = line.split(':')[1] if old_md5 =~ /#{new_md5}/ return new_md5 end end end end if File.exists?(orig_file) return false end |
#run ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/elasticdns.rb', line 15 def run pp @config if @config[:debug] if @config[:slave] == 'true' update_acl_masters_file else update_notify_file end end |
#update_acl_masters_file ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/elasticdns.rb', line 138 def update_acl_masters_file # File dedicated to slave servers orig_file = @config[:bind9_acl_masters_file] temp_file = Tempfile.new('acl_masters_file') prefix = "acl 'masters'" begin ec2 = ec2init ips = [] ec2.instances.each do |i| = i.[@config[:ec2_tag_attribute]] if i.status == :running @config[:bind9_masters].each do |master| if == master ips << i.send(@config[:ec2_ip_attribute]) end end end end write_to_file(ips, orig_file, temp_file, prefix) ensure closefile(temp_file) end end |
#update_hosts_file ⇒ Object
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 |
# File 'lib/elasticdns.rb', line 90 def update_hosts_file orig_file = '/etc/hosts' temp_file = Tempfile.new('hosts') begin ec2 = ec2init File.open(orig_file, 'r') do |file| file.each_line do |line| unless line =~ /### EC2 HOSTS/ temp_file.write("#{line}") end end end ec2.instances.each do |i| if i.status == :running if i.[@config[:ec2_tag_attribute]] temp_file.write("#{i.private_ip_address}\t#{i.tags[@config[:ec2_tag_attribute]]}\t### EC2 HOSTS\n") end end end temp_file.rewind FileUtils.mv(temp_file.path, orig_file) ensure temp_file.close temp_file.unlink end end |
#update_notify_file ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/elasticdns.rb', line 117 def update_notify_file # File dedicated to master servers orig_file = @config[:bind9_notify_file] temp_file = Tempfile.new('also-notify') prefix = "also-notify" begin ec2 = ec2init ips = [] ec2.instances.each do |i| if i.status == :running ips << i.send(@config[:ec2_ip_attribute]) #unless @config[:bind9_masters].include?(i.tags[@config[:ec2_tag_attribute]]) end end write_to_file(ips, orig_file, temp_file, prefix) ensure temp_file.close temp_file.unlink end end |
#updatefile(ips, new_md5, temp_file, orig_file, prefix) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/elasticdns.rb', line 53 def updatefile(ips, new_md5, temp_file, orig_file, prefix) puts "Updating #{orig_file}" md5 = "### MD5:#{new_md5}\n" line = "#{prefix} { #{ips.to_s.gsub(/\[|\]|"/, '').gsub(/,/, ';')}; };\n" temp_file.write(md5) temp_file.write(line) temp_file.rewind FileUtils.mv(temp_file.path, orig_file) return line end |
#write_to_file(ips, orig_file, temp_file, prefix) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/elasticdns.rb', line 162 def write_to_file(ips, orig_file, temp_file, prefix) if ips[0] unless md5chk(orig_file, md5(ips.to_s)) update = updatefile(ips, md5(ips.to_s), temp_file, orig_file, prefix) bind_init_cmd puts "#{orig_file} updated with: " + update else puts "nothing changed in #{orig_file}: " + md5(ips.to_s) end else puts 'No master found' end end |