Class: Elasticdns::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticdns.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Generator

Returns a new instance of Generator.



11
12
13
# File 'lib/elasticdns.rb', line 11

def initialize(options={})
  @config = Elasticdns::Config.new(options).to_hash
end

Instance Method Details

#bind_checkconfObject



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_checkzoneObject



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_cmdObject



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

#ec2initObject



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

#runObject



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_fileObject



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|
      meta_name = i.tags[@config[:ec2_tag_attribute]]
      if i.status == :running
        @config[:bind9_masters].each do |master|
          if meta_name == 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_fileObject



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.tags[@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_fileObject



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