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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 561ef395898f15a897a2974c48cd79c0002d2a88
4
- data.tar.gz: 14093a1e73d508d0bf1312d793525e6738aa3ec5
3
+ metadata.gz: f887012730bfc1dfc79c363084aeab22d6eaeba2
4
+ data.tar.gz: 561ef4b9e484d2904c7018fec31218f089c645d1
5
5
  SHA512:
6
- metadata.gz: 5513b50e495cd7e59ea272730ac2badd1cdbc80a236b2d1c432e9cf9af1920b2e7842bd1c4fd219bd4f4c5d858ffa1ea998eaeea761e880aac724d741edf1f0b
7
- data.tar.gz: 037db703b3c5e8fa570a95899a9a6c77303c399feaaa93664a0912a178cfaeb432aade2bf0cd09b6e3042abe0c252a08d0111ec6c8bcb75615ba07ceed2b4351
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
- # if event.action == 'start' || event.action == 'die'
34
- client.sync_hosts Docker::Container.all({:all => true})
35
- # end
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.warn 'Docker socket connection timed out whilst listening for events. Reconnecting...'
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!(/^\//, '') + host_suffix
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
- class Server < Base
70
- def run(ip, port)
71
- HostManager.log.info "Running on host manager service on #{ip}:#{port}"
72
- @tcp_server = TCPServer.new ip, port
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
- loop {
79
+ def get_hosts
80
+ unless @host_manager
81
+ @host_manager = Hosts::File.read(HostManager.host_file_path)
82
+ end
75
83
 
76
- client = @tcp_server.accept
77
- content = client.gets
84
+ @host_manager
85
+ end
78
86
 
79
- log.debug("Recieved data: #{content.to_s.strip}")
87
+ def build_hosts_data(container_data)
88
+ data = []
80
89
 
81
- begin
82
- unless content.to_s.chomp.strip.empty?
83
- data = JSON.parse(content)
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
- if data.is_a?(Hash)
86
- required_keys = %w(action hostname ip containerId)
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
- elsif data.is_a?(Array)
95
- hosts = get_hosts
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
- # initially remove all entries
98
- hosts.elements.delete_if do |element|
99
- if valid_entry?(element)
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
- # add recieved entries
106
- data.each do |entry|
107
- comment = HostManager.comment_prefix + (entry['containerId'] ? " Container ID: #{entry['containerId']}" : '')
114
+ end
115
+ end
108
116
 
109
- hosts.elements << Hosts::Entry.new(entry['ip'], entry['hostname'], :comment => comment)
110
- hosts.write
111
- end
117
+ data
118
+ end
112
119
 
113
- hosts.write
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
- end
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
- rescue => e
120
- log.error("Caught exception attempting to execute with data #{content}. #{e}")
141
+ # add recieved entries
142
+ data.each do |entry|
143
+ add_entry(entry, hosts);
121
144
  end
122
145
 
123
- client.close
124
- }
146
+ hosts.write
125
147
 
148
+ end
126
149
  end
127
150
 
128
- def get_hosts
129
- Hosts::File.read(HostManager.host_file_path)
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
- if hostname == element.name && ip == element.address
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
- def self.create(ip, port)
169
- server = self.new
170
- server.run(ip, port)
171
- end
195
+ end
172
196
 
173
- private
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
- # Determine if entry is managed by this utility or not
176
- def valid_entry?(element)
177
- # check it's a managed host first
178
- if element.respond_to?(:comment) && element.comment.to_s.start_with?(HostManager.comment_prefix)
179
- # check we have hostname and ip
180
- if element.respond_to?(:name) && element.respond_to?(:address)
181
- return true
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
- end
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
- data.push(item)
233
- end
234
- end
235
- end
258
+ begin
259
+ update_hosts_file(data)
236
260
 
237
- end
261
+ `service dnsmasq restart`
238
262
 
239
- send(data)
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)
@@ -79,6 +79,7 @@ module Vagrant
79
79
  pid_file << pid
80
80
  pid_file.close
81
81
  log "Wrote server PID (#{pid}) to #{pid_path}"
82
+ Process.detach(pid)
82
83
  @started = true
83
84
  end
84
85
  end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module DnsdockHostUpdater
3
- VERSION = '0.0.14'.freeze
3
+ VERSION = '0.0.15'.freeze
4
4
  end
5
5
  end
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.14
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: 2016-12-27 00:00:00.000000000 Z
11
+ date: 2017-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docker-api