Commit 81cdb5ba authored by Julian Kornberger's avatar Julian Kornberger

Remove whitespace

parent 432d0fdf
......@@ -5,7 +5,7 @@ basedir="`dirname $0`/../.."
# Extract the new commit id
ref=`cat /dev/stdin | awk '{ print $2 }'`
# Export working copy
# Export working copy
export GIT_WORK_TREE=$basedir/tmp/cache
mkdir -p $GIT_WORK_TREE
GIT_WORK_TREE=$basedir/tmp/cache git checkout -f --quiet $ref
......
......@@ -5,7 +5,7 @@ class Hash
end
self
end
def deep_symbolize_keys!
symbolize_keys!
# symbolize each hash in .values
......@@ -14,4 +14,4 @@ class Hash
values.select{|v| v.is_a?(Array) }.flatten.each{|h| h.deep_symbolize_keys! if h.is_a?(Hash) }
self
end
end
\ No newline at end of file
end
class Zone
attr_reader :zonefile
def initialize(domain, template_dir)
@domain = domain
@zonefile = Zonefile.new("","output/#{domain}", domain)
@template_dir = template_dir
end
def template(name)
eval_file "#{@template_dir}/#{name}.rb"
end
# 1.2.3.4 - host
# 1.2.3.4, 600 - host with TTL
# www, 1.2.3.4, 600 - name, host and TTL
# www, 1.2.3.4, 600 - name, host and TTL
def a(*args)
if [String,String,String] == args[0..2].map(&:class)
# name, ipv4 and ipv6
......@@ -28,26 +28,26 @@ class Zone
a_record :a, *args
end
end
def aaaa(*args)
a_record :a4, *args
end
def a_record(type, *args)
ttl = extract_ttl! args
host = args.pop
name = args.pop || '@'
push type, name, ttl, host: host
end
# mx - host with default priority (10)
# mx, 15 - host and priority
# mx, 15, 600 - host, priority and TTL
# name, mx, 15 - name, host, priority
# name, mx, 15, 600 - name, host, priority and TTL
# name, mx, 15, 600 - name, host, priority and TTL
def mx(*args)
if args[1].is_a?(String)
# name and host given
name = args.shift
......@@ -57,33 +57,33 @@ class Zone
host = args.shift || '@'
name = '@'
end
pri = args.shift || 10
ttl = args.shift
push :mx, name, ttl, host: host, pri: pri
end
# ns1.example.com. - host
# ns1.example.com., 600 - host with TTL
# ns1.example.com., 600 - host with TTL
def ns(*args)
ttl = extract_ttl! args
host = args.pop
name = args.pop || '@'
push :ns, name, ttl, host: host
end
def cname(name, *args)
ttl = extract_ttl! args
push :cname, name, ttl, host: (args.pop || "@")
push :cname, name, ttl, host: (args.pop || "@")
end
def srv(*args)
options = extract_options! args
name = "." << args.shift if args[0].is_a?(String)
raise ArgumentError, "wrong number of arguments" unless (4..5).include?(args.count)
service = args.shift
......@@ -107,13 +107,13 @@ class Zone
push :srv, "_#{service}._#{protocol}#{name}", ttl, options.merge(host: host, port: port)
end
def txt(*args)
ttl = extract_ttl! args
text = args.pop.to_s.strip
name = args.pop || '@'
text = "\"#{text}\"" if text =~ /\s/
push :txt, name, ttl, text: text
end
......@@ -138,31 +138,31 @@ class Zone
push :tlsa, "_#{port}._#{protocol}#{name}", ttl,
certificate_usage: usage, selector: selector, matching_type: matching, data: data
end
# name in not-reversed order
def ptr(name, host, ttl=nil)
host = "#{host}." if host[-1] != '.'
push :ptr, name, ttl, host: host
end
def ptr6(name, *args)
raise ArgumentError, "no double colon allowed" if name.include?("::")
# left fill blocks with zeros, reverse order all characters and join them with points
ptr name.split(":").map{|b| b.rjust(4,"0") }.join.reverse.split("").join("."), *args
end
protected
# evaluates a file
def eval_file(file)
instance_eval File.read(file), file
end
def push(type, name, ttl, options={})
@zonefile.send(type) << {class: 'IN', name: name, ttl: ttl, }.merge(options)
end
# extracts the last argument if it is a Hash
def extract_options!(args)
args.last.is_a?(Hash) ? args.pop : {}
......@@ -172,5 +172,5 @@ class Zone
def extract_ttl!(args)
args.pop if args.last.is_a?(Fixnum)
end
end
class ZoneGenerator
def initialize(basedir)
@generated = "#{basedir}/tmp/generated"
@workspace = "#{basedir}/tmp/cache"
@zones_dir = "#{@workspace}/zones"
@template_dir = "#{@workspace}/templates"
@tmp_named = "#{@generated}/named.conf"
@tmp_zones = "#{@generated}/zones"
@config = YAML.load_file("#{@workspace}/config.yaml")
@config.deep_symbolize_keys!
@soa = {
......@@ -31,39 +31,39 @@ class ZoneGenerator
FileUtils.rm_rf @generated # Tote Zonen-Definitionen brauchen wir nicht.
FileUtils.mkdir_p @generated
end
# Generates all zones
def generate
File.open(@tmp_named,"w") do |f|
Dir.glob("#{@zones_dir}/**/*.rb").sort.each do |file|
domain = File.basename(file).sub(/\.rb$/,"")
generate_zone(file, domain)
f.puts "zone \"#{domain}\" IN { type master; file \"#{@config[:zones_dir]}/#{domain}\"; };"
end
end
end
# Generates a single zone file
def generate_zone(file, domain)
zone = Zone.new(domain, @template_dir)
zone.send :eval_file, file
new_zonefile = zone.zonefile
new_zonefile.soa.merge! @soa
# path to the deployed version
old_file = "#{@config[:zones_dir]}/#{domain}"
# is there already a deployed version?
if File.exists?(old_file)
# parse the deployed version
old_output = File.read(old_file)
old_zonefile = Zonefile.new(old_output)
new_zonefile.soa[:serial] = old_zonefile.soa[:serial]
# content of the new version
new_output = new_zonefile.output
# has anything changed?
if new_output != old_output
puts "#{domain} has been updated"
......@@ -77,28 +77,28 @@ class ZoneGenerator
new_zonefile.new_serial
new_output = new_zonefile.output
end
# Write new zonefile
output_file_path = "#{@tmp_zones}/#{domain}"
FileUtils.mkdir_p File.dirname(output_file_path)
File.open(output_file_path, "w"){|f| f.write new_output }
end
def deploy
# Remove zones directory
FileUtils.rm_rf @config[:zones_dir]
FileUtils.copy @tmp_named, @config[:named_conf]
FileUtils.copy_entry @tmp_zones, @config[:zones_dir]
cmd = @config[:execute]
print "Executing '#{cmd}' ... "
out = `#{cmd}`
puts "done"
if $?.to_i != 0
raise out
end
end
end
\ No newline at end of file
end
......@@ -12,80 +12,80 @@ describe Zone do
subject.a "127.0.0.1"
subject.zonefile.a.must_equal [{:class=>"IN", :name=>"@", :ttl=>nil, :host=>"127.0.0.1"}]
end
it "should create host, ttl" do
subject.a "127.0.0.1", 600
subject.zonefile.a.must_equal [{:class=>"IN", :name=>"@", :ttl=>600, :host=>"127.0.0.1"}]
end
it "should create name, host, ttl" do
subject.a "www", "127.0.0.1", 600
subject.zonefile.a.must_equal [{:class=>"IN", :name=>"www", :ttl=>600, :host=>"127.0.0.1"}]
end
it "should create name with ipv4 and ipv6" do
subject.a "www", "127.0.0.1", "::ffff:7f00:1"
subject.zonefile.a.must_equal [{:class=>"IN", :name=>"www", :ttl=>nil, :host=>"127.0.0.1"}]
subject.zonefile.a4.must_equal [{:class=>"IN", :name=>"www", :ttl=>nil, :host=>"::ffff:7f00:1"}]
end
it "should create name with ipv4, ipv6 and TTL" do
subject.a "www", "127.0.0.1", "::ffff:7f00:1", 600
subject.zonefile.a.must_equal [{:class=>"IN", :name=>"www", :ttl=>600, :host=>"127.0.0.1"}]
subject.zonefile.a4.must_equal [{:class=>"IN", :name=>"www", :ttl=>600, :host=>"::ffff:7f00:1"}]
end
end
describe "cname record" do
it "without args" do
assert_raises ArgumentError do
subject.cname
end
end
it "with name" do
subject.cname 'www'
subject.zonefile.cname.must_equal [{:class=>"IN", :name=>"www", :ttl=>nil, :host=>"@"}]
end
it "with name, host" do
subject.cname 'www', "other-server."
subject.zonefile.cname.must_equal [{:class=>"IN", :name=>"www", :ttl=>nil, :host=>"other-server."}]
end
it "with name, ttl" do
subject.cname 'www', 600
subject.zonefile.cname.must_equal [{:class=>"IN", :name=>"www", :ttl=>600, :host=>"@"}]
end
it "with name, ttl, host" do
subject.cname 'www', "other-server.", 600
subject.zonefile.cname.must_equal [{:class=>"IN", :name=>"www", :ttl=>600, :host=>"other-server."}]
end
end
describe "mx record" do
it "should create without args" do
subject.mx
subject.zonefile.mx.must_equal [{:class=>"IN", :name=>"@", :ttl=>nil, :host=>"@", :pri=>10}]
end
it "should create with host" do
subject.mx "mail"
subject.zonefile.mx.must_equal [{:class=>"IN", :name=>"@", :ttl=>nil, :host=>"mail", :pri=>10}]
end
it "should create with host, priority" do
subject.mx "mail", 20
subject.zonefile.mx.must_equal [{:class=>"IN", :name=>"@", :ttl=>nil, :host=>"mail", :pri=>20}]
end
it "should create with host, priority, ttl " do
subject.mx "mail", 20, 600
subject.zonefile.mx.must_equal [{:class=>"IN", :name=>"@", :ttl=>600, :host=>"mail", :pri=>20}]
end
end
describe "srv record" do
it "without port" do
assert_raises ArgumentError do
......@@ -120,7 +120,7 @@ describe Zone do
subject.zonefile.ptr.must_equal [{:class=>"IN", :name=>127, :ttl=>nil, :host=>"foobar.org."}]
end
end
describe "ptr6 record" do
it "with a double colon" do
assert_raises ArgumentError do
......@@ -138,7 +138,7 @@ describe Zone do
subject.zonefile.ptr.must_equal [{:class=>"IN", :name=>"1.0.0.0.0.7.0.0.e.2.a.8.9.1.3.1", :ttl=>nil, :host=>"example.com."}]
end
end
describe "tlsa record" do
it "should create tlsa record" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment