vagrant-dnsdock-hostupdater 0.0.14 → 0.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/event-watcher.rb +15 -7
- data/lib/host-manager.rb +110 -83
- data/lib/vagrant-dnsdock-hostupdater/plugin.rb +1 -0
- data/lib/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f887012730bfc1dfc79c363084aeab22d6eaeba2
|
4
|
+
data.tar.gz: 561ef4b9e484d2904c7018fec31218f089c645d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43e3e245116edbf2e96310f45692f7c256cd8b20113b3ef51690ef87f3b62111c89f556311cdfe45a1b2aed005a337f6a41a301007d331c9dae7a6fdf1a7c9e8
|
7
|
+
data.tar.gz: 23269abc8eeeb028ad774e27aa2631ed9d0ba526b49848a47a4ad2fda6c44a46847bb9a86213958f43b2a2c0a0241fc7ce05917bef87f36094050f6a4061b599
|
data/lib/event-watcher.rb
CHANGED
@@ -1,6 +1,17 @@
|
|
1
1
|
require_relative 'host-manager'
|
2
2
|
require 'yaml'
|
3
3
|
|
4
|
+
def update
|
5
|
+
client = HostManager::Client.create('10.10.10.1', 2991)
|
6
|
+
client.host_suffix '.local.signal.sh'
|
7
|
+
client.sync_hosts Docker::Container.all({:all => true})
|
8
|
+
client.close
|
9
|
+
end
|
10
|
+
|
11
|
+
sleep(2)
|
12
|
+
|
13
|
+
update
|
14
|
+
|
4
15
|
loop {
|
5
16
|
begin
|
6
17
|
Docker::Event.stream { |event|
|
@@ -20,8 +31,6 @@ loop {
|
|
20
31
|
|
21
32
|
# container_name = container.info['Name'].sub!(/^\//, '')
|
22
33
|
|
23
|
-
client = HostManager::Client.create('10.10.10.1', 2991)
|
24
|
-
client.host_suffix '.local.signal.sh'
|
25
34
|
|
26
35
|
# To handle updating single entries
|
27
36
|
# if event.action == 'start'
|
@@ -30,11 +39,10 @@ loop {
|
|
30
39
|
# client.send_action('delete', container_name, ip, container_id)
|
31
40
|
# end
|
32
41
|
|
33
|
-
|
34
|
-
|
35
|
-
|
42
|
+
if event.action == 'start' || event.action == 'die'
|
43
|
+
update
|
44
|
+
end
|
36
45
|
|
37
|
-
client.close
|
38
46
|
end
|
39
47
|
rescue => e
|
40
48
|
puts e.inspect
|
@@ -44,7 +52,7 @@ loop {
|
|
44
52
|
}
|
45
53
|
|
46
54
|
rescue Docker::Error::TimeoutError
|
47
|
-
HostManager.log.
|
55
|
+
HostManager.log.info 'Docker socket connection timed out whilst listening for events. Reconnecting...'
|
48
56
|
end
|
49
57
|
}
|
50
58
|
|
data/lib/host-manager.rb
CHANGED
@@ -53,7 +53,7 @@ module HostManager
|
|
53
53
|
if container_name.include?('.')
|
54
54
|
container_name
|
55
55
|
else
|
56
|
-
container_name.sub
|
56
|
+
container_name.sub(/^\//, '').gsub(/_/, '-') + host_suffix
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
@@ -64,69 +64,98 @@ module HostManager
|
|
64
64
|
@domain_suffix
|
65
65
|
end
|
66
66
|
end
|
67
|
-
end
|
68
67
|
|
69
|
-
|
70
|
-
def
|
71
|
-
|
72
|
-
|
68
|
+
# Determine if entry is managed by this utility or not
|
69
|
+
def valid_entry?(element)
|
70
|
+
# check it's a managed host first
|
71
|
+
if element.respond_to?(:comment) && element.comment.to_s.start_with?(HostManager.comment_prefix)
|
72
|
+
# check we have hostname and ip
|
73
|
+
if element.respond_to?(:name) && element.respond_to?(:address)
|
74
|
+
return true
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
73
78
|
|
74
|
-
|
79
|
+
def get_hosts
|
80
|
+
unless @host_manager
|
81
|
+
@host_manager = Hosts::File.read(HostManager.host_file_path)
|
82
|
+
end
|
75
83
|
|
76
|
-
|
77
|
-
|
84
|
+
@host_manager
|
85
|
+
end
|
78
86
|
|
79
|
-
|
87
|
+
def build_hosts_data(container_data)
|
88
|
+
data = []
|
80
89
|
|
81
|
-
|
82
|
-
|
83
|
-
|
90
|
+
if container_data && container_data.is_a?(Array)
|
91
|
+
container_data.each do |container_info|
|
92
|
+
container = Docker::Container.get(container_info.info['id'])
|
84
93
|
|
85
|
-
|
86
|
-
|
87
|
-
required_keys.each do |key|
|
88
|
-
unless data.key?(key)
|
89
|
-
raise "Data for key #{key} not provided."
|
90
|
-
end
|
91
|
-
end
|
92
|
-
execute(data['action'], data['hostname'], data['ip'], data['containerId'])
|
94
|
+
if container.info['State']['Running']
|
95
|
+
name = container.info['Name']
|
93
96
|
|
94
|
-
|
95
|
-
|
97
|
+
if name
|
98
|
+
ip = container.info['NetworkSettings']['IPAddress']
|
99
|
+
|
100
|
+
if ip
|
101
|
+
item = {
|
102
|
+
:hostname => host_name(name),
|
103
|
+
:ip => ip,
|
104
|
+
:containerId => container.info['id']
|
105
|
+
}
|
96
106
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
log.info 'Removing entry for ' + element.name
|
101
|
-
true
|
102
|
-
end
|
107
|
+
data.push(item)
|
108
|
+
else
|
109
|
+
log.warn("Ignored request to create hosts entry due to no IP being returned from container: #{name}")
|
103
110
|
end
|
111
|
+
end
|
112
|
+
end
|
104
113
|
|
105
|
-
|
106
|
-
|
107
|
-
comment = HostManager.comment_prefix + (entry['containerId'] ? " Container ID: #{entry['containerId']}" : '')
|
114
|
+
end
|
115
|
+
end
|
108
116
|
|
109
|
-
|
110
|
-
|
111
|
-
end
|
117
|
+
data
|
118
|
+
end
|
112
119
|
|
113
|
-
|
120
|
+
def update_hosts_file(data)
|
121
|
+
if data.is_a?(Hash)
|
122
|
+
required_keys = %w(action hostname ip containerId)
|
123
|
+
required_keys.each do |key|
|
124
|
+
unless data.key?(key)
|
125
|
+
raise "Data for key #{key} not provided."
|
126
|
+
end
|
127
|
+
end
|
128
|
+
execute(data['action'], data['hostname'], data['ip'], data['containerId'])
|
114
129
|
|
115
|
-
|
130
|
+
elsif data.is_a?(Array)
|
131
|
+
hosts = get_hosts
|
116
132
|
|
133
|
+
# initially remove all entries
|
134
|
+
hosts.elements.delete_if do |element|
|
135
|
+
if valid_entry?(element)
|
136
|
+
log.info 'Removing entry for ' + element.name
|
137
|
+
true
|
117
138
|
end
|
139
|
+
end
|
118
140
|
|
119
|
-
|
120
|
-
|
141
|
+
# add recieved entries
|
142
|
+
data.each do |entry|
|
143
|
+
add_entry(entry, hosts);
|
121
144
|
end
|
122
145
|
|
123
|
-
|
124
|
-
}
|
146
|
+
hosts.write
|
125
147
|
|
148
|
+
end
|
126
149
|
end
|
127
150
|
|
128
|
-
def
|
129
|
-
|
151
|
+
def add_entry(entry, hosts)
|
152
|
+
if entry[:ip] && entry[:containerId] && entry[:hostname]
|
153
|
+
comment = HostManager.comment_prefix + (entry[:containerId] ? " Container ID: #{entry[:containerId]}" : '')
|
154
|
+
hosts.elements << Hosts::Entry.new(entry[:ip], entry[:hostname], :comment => comment)
|
155
|
+
|
156
|
+
else
|
157
|
+
log.warn 'Unable to write entry due to missing values [ip,containerId,hostname]: ' + entry.to_json
|
158
|
+
end
|
130
159
|
end
|
131
160
|
|
132
161
|
def execute(action, hostname, ip, container_id = nil)
|
@@ -137,10 +166,8 @@ module HostManager
|
|
137
166
|
|
138
167
|
if action == 'create'
|
139
168
|
hosts.elements.each do |element|
|
140
|
-
if valid_entry?(element)
|
141
|
-
|
142
|
-
raise "Entry already exists! (#{ip} #{hostname})"
|
143
|
-
end
|
169
|
+
if valid_entry?(element) && hostname == element.name
|
170
|
+
raise "Entry already exists! (#{ip} #{hostname})"
|
144
171
|
end
|
145
172
|
end
|
146
173
|
|
@@ -165,24 +192,39 @@ module HostManager
|
|
165
192
|
end
|
166
193
|
end
|
167
194
|
|
168
|
-
|
169
|
-
server = self.new
|
170
|
-
server.run(ip, port)
|
171
|
-
end
|
195
|
+
end
|
172
196
|
|
173
|
-
|
197
|
+
class Server < Base
|
198
|
+
def run(ip, port)
|
199
|
+
HostManager.log.info "Running on host manager service on #{ip}:#{port}"
|
200
|
+
@tcp_server = TCPServer.new ip, port
|
174
201
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
202
|
+
loop {
|
203
|
+
|
204
|
+
client = @tcp_server.accept
|
205
|
+
content = client.gets
|
206
|
+
|
207
|
+
log.debug("Recieved data: #{content.to_s.strip}")
|
208
|
+
|
209
|
+
begin
|
210
|
+
unless content.to_s.chomp.strip.empty?
|
211
|
+
data = JSON.parse(content)
|
212
|
+
update_hosts_file(data)
|
213
|
+
end
|
214
|
+
|
215
|
+
rescue => e
|
216
|
+
log.error("Caught exception attempting to execute with data #{content}. #{e}")
|
182
217
|
end
|
183
|
-
|
218
|
+
|
219
|
+
client.close
|
220
|
+
}
|
221
|
+
|
184
222
|
end
|
185
223
|
|
224
|
+
def self.create(ip, port)
|
225
|
+
server = self.new
|
226
|
+
server.run(ip, port)
|
227
|
+
end
|
186
228
|
end
|
187
229
|
|
188
230
|
class Client < Base
|
@@ -211,33 +253,18 @@ module HostManager
|
|
211
253
|
end
|
212
254
|
|
213
255
|
def sync_hosts(container_data)
|
214
|
-
data =
|
215
|
-
|
216
|
-
if container_data && container_data.is_a?(Array)
|
217
|
-
container_data.each do |container_info|
|
218
|
-
container = Docker::Container.get(container_info.info['id'])
|
219
|
-
|
220
|
-
if container.info['State']['Running']
|
221
|
-
name = container.info['Name']
|
222
|
-
if name
|
223
|
-
ip = container.info['NetworkSettings']['IPAddress']
|
224
|
-
|
225
|
-
if ip
|
226
|
-
item = {
|
227
|
-
:hostname => host_name(name),
|
228
|
-
:ip => ip,
|
229
|
-
:containerId => container.info['id']
|
230
|
-
}
|
256
|
+
data = build_hosts_data(container_data)
|
231
257
|
|
232
|
-
|
233
|
-
|
234
|
-
end
|
235
|
-
end
|
258
|
+
begin
|
259
|
+
update_hosts_file(data)
|
236
260
|
|
237
|
-
|
261
|
+
`service dnsmasq restart`
|
238
262
|
|
239
|
-
|
263
|
+
rescue => e
|
264
|
+
log.error("Caught exception attempting to write to hosts file with data #{data.to_json}. #{e}")
|
240
265
|
end
|
266
|
+
|
267
|
+
send(data)
|
241
268
|
end
|
242
269
|
|
243
270
|
def send(data)
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vagrant-dnsdock-hostupdater
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Coit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: docker-api
|