Class: BindDocker

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

Constant Summary collapse

IMAGE =
'clcloud/dnsuats'
CONTAINER_NAME =
'dns-funtime'
@@zonefiles =
[]
@@named_conf =
nil

Class Method Summary collapse

Class Method Details

.add_zonefile(zonefile_path) ⇒ Object



38
39
40
# File 'lib/bind_docker.rb', line 38

def self.add_zonefile zonefile_path
  @@zonefiles.push File.new(zonefile_path)
end

.build_docker_file(tmp_dir) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/bind_docker.rb', line 50

def self.build_docker_file tmp_dir
  File.open(tmp_dir + "/Dockerfile", 'w') do |file|
    file.write(docker_preamble)
    @@zonefiles.each do |zonefile|
      file.write("COPY #{filename(zonefile)} /var/cache/bind/#{filename(zonefile)}")
    end
    file.write("COPY #{filename(@@named_conf)} /etc/bind/#{filename(@@named_conf)}")
    file.close
  end
end

.clear_zonfilesObject



42
43
44
# File 'lib/bind_docker.rb', line 42

def self.clear_zonfiles
  @@zonefiles.clear
end

.docker_ipObject



84
85
86
87
88
89
90
91
# File 'lib/bind_docker.rb', line 84

def self.docker_ip
  if (/darwin/ =~ RUBY_PLATFORM)
    docker_machine_info = JSON.parse `docker-machine inspect default`
    docker_machine_info['Driver']['Driver']['IPAddress']
  else
    "127.0.0.1"
  end
end

.docker_preambleObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bind_docker.rb', line 61

def self.docker_preamble
  <<-HEREDOC
FROM phusion/baseimage:0.9.18
MAINTAINER [email protected]

ENV DATA_DIR=/data \
  BIND_USER=bind


RUN rm -rf /etc/apt/apt.conf.d/docker-gzip-indexes \
 && apt-get update \
 && DEBIAN_FRONTEND=noninteractive apt-get install -y bind9 dnsutils \
 && rm -rf /var/lib/apt/lists/*

EXPOSE 53/udp
EXPOSE 53/tcp
VOLUME ["${DATA_DIR}"]
CMD ["/usr/sbin/named", "-g", "-c", "/etc/bind/named.conf"]

  HEREDOC
end

.filename(file) ⇒ Object



14
15
16
# File 'lib/bind_docker.rb', line 14

def self.filename file
  File.split(file)[1]
end

.named_conf(named_conf_path) ⇒ Object



46
47
48
# File 'lib/bind_docker.rb', line 46

def self.named_conf named_conf_path
  @@named_conf = File.new(named_conf_path)
end

.resolve(address) ⇒ Object



34
35
36
# File 'lib/bind_docker.rb', line 34

def self.resolve address
  Resolv.getaddress(address).to_s
end

.resolve_bucket_by_region(bucket, tld) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/bind_docker.rb', line 93

def self.resolve_bucket_by_region bucket, tld
  begin
    Resolv.getaddress("#{bucket}.#{tld}").to_s
  rescue Resolv::ResolvError
    :nxdomain
  end
end

.run!Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bind_docker.rb', line 18

def self.run!
  Dir.mktmpdir do |dir|
    FileUtils.cp @@named_conf, dir, {}
    FileUtils.cp @@zonefiles, dir, {}
    build_docker_file dir
    Dir.chdir(dir) do
      `docker build -t #{IMAGE} .`
      `docker run -d -p 1053:53/udp -p 1053:53/tcp --name=#{CONTAINER_NAME} #{IMAGE}`
    end
  end
end

.stop!Object



30
31
32
# File 'lib/bind_docker.rb', line 30

def self.stop!
  `docker rm -f #{CONTAINER_NAME} > /dev/null 2>&1`
end