-
Notifications
You must be signed in to change notification settings - Fork 2
/
docker_event_handler.rb
89 lines (73 loc) · 2.37 KB
/
docker_event_handler.rb
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
#!/usr/bin/env ruby
# Docker Event Listener / DDNS
# Author: Kelly Becker <[email protected]>
# She is a nice girl i call her my Cherry Apple because she works at Apple but is more elegant like a Cherry :D
# Website: http://kellybecker.me
# Original Code: https://gist.github.com/KellyLSB/4315a0323ed0fe1d79b6
# License: MIT
# Set up a proper logger
require 'logger'
log_file = ARGV.first || '-'
log = Logger.new(log_file == '-' ? $stdout : log_file)
# Create a PID file for this service
File.open('/var/run/docker_ddns.pid', 'w+') { |f| f.write($$) }
# Capture the terminate signal
trap("INT") do
log.info "Caught INT Signal... Exiting."
File.unlink('/var/run/docker_ddns.pid')
sleep 1
exit
end
# Welcome message
log.info "Starting Docker Dynamic DNS - Event Handler"
log.info "Maintainer: Kelly Becker <[email protected]>"
log.info "Website: http://kellybecker.me"
# Default Configuration
ENV['DDNS_KEY'] ||= "/etc/bind/ddns.key"
ENV['NET_NS'] ||= "10.1.0.1"
ENV['NET_DOMAIN'] ||= "kellybecker.me"
ENV['DOCKER_PID'] ||= "/var/run/docker.pid"
# Ensure docker is running
time_waited = Time.now.to_i
until File.exist?(ENV['DOCKER_PID'])
if (Time.now.to_i - time_waited) > 600
log.fatal "Docker daemon still not started after 10 minutes... Please Contact Your SysAdmin!"
exit 1
end
log.warn "Docker daemon is not running yet..."
sleep 5
end
log.info "Docker Daemon UP! - Listening for Events..."
# Find CGroup Mount
File.open('/proc/mounts', 'r').each do |line|
dev, mnt, fstype, options, dump, fsck = line.split
next if "#{fstype}" != "cgroup"
next if "#{options}".include?('devices')
ENV['CGROUPMNT'] = mnt
end.close
# Exit if missing CGroup Mount
unless ENV['CGROUPMNT']
log.fatal "Could not locate cgroup mount point."
exit 1
end
# Listen To Docker.io Events
events = IO.popen('docker events')
# Keep Listening for incoming data
while line = events.gets
# Container Configuration
ENV['CONTAINER_EVENT'] = line.split.last
ENV['CONTAINER_CID_LONG'] = line.gsub(/^.*([0-9a-f]{64}).*$/i, '\1')
ENV['CONTAINER_CID'] = ENV['CONTAINER_CID_LONG'][0...12]
# Event Fired info
log.info "Event Fired (#{ENV['CONTAINER_CID']}): #{ENV['CONTAINER_EVENT']}."
case ENV['CONTAINER_EVENT']
when 'start'
#Run Add to Bind9
# add-zone.sh $CONTAINER_CID_LONG
docker restart proxy
end
when 'stop'
docker restart proxy
end
end
exit