vagrant-dnsdock-hostupdater 0.0.21 → 0.0.22
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +28 -0
- data/vagrant-dnsdock-hostupdater.gemspec +27 -0
- metadata +3 -22
- data/lib/client.rb +0 -4
- data/lib/event-watcher.rb +0 -51
- data/lib/host-manager.rb +0 -298
- data/lib/hosts/aef/hosts.rb +0 -51
- data/lib/hosts/aef/hosts/comment.rb +0 -73
- data/lib/hosts/aef/hosts/element.rb +0 -108
- data/lib/hosts/aef/hosts/empty_element.rb +0 -50
- data/lib/hosts/aef/hosts/entry.rb +0 -123
- data/lib/hosts/aef/hosts/file.rb +0 -252
- data/lib/hosts/aef/hosts/helpers.rb +0 -121
- data/lib/hosts/aef/hosts/section.rb +0 -141
- data/lib/hosts/aef/hosts/version.rb +0 -29
- data/lib/hosts/aef/linebreak/linebreak.rb +0 -148
- data/lib/hosts/hosts.rb +0 -25
- data/lib/hosts/hosts/bare.rb +0 -23
- data/lib/launch-control +0 -46
- data/lib/server.rb +0 -3
- data/lib/vagrant-dnsdock-hostupdater.rb +0 -10
- data/lib/vagrant-dnsdock-hostupdater/command.rb +0 -12
- data/lib/vagrant-dnsdock-hostupdater/plugin.rb +0 -90
- data/lib/version.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4443b4ad71d5f0411f5fe45885f00076e6e198bb
|
4
|
+
data.tar.gz: 176b2520288f6ce58893bcac0c9009414f4497a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7fd4a704daf039d44eab40326217dd69b78cf32dc990d1a6aa38ef6b7775fe3adac922114f1d9662cc731f5c23509b87f352c688637d951b70c4a9f6e0338413
|
7
|
+
data.tar.gz: 07c71b202a4351546927880e9ad45ba32d9aa7c37811e3b87e3a17cc59f3a387be23b5726cfa1941af1efac652c025bc35c0a106ac8b0e14d7b2e2fd9188da98
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
vagrant-dnsdock-hostupdater (0.0.22)
|
5
|
+
docker-api (~> 1.33)
|
6
|
+
linebreak (~> 2.1.0)
|
7
|
+
log4r (~> 1.1.10)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: http://rubygems.org/
|
11
|
+
specs:
|
12
|
+
docker-api (1.33.2)
|
13
|
+
excon (>= 0.38.0)
|
14
|
+
json
|
15
|
+
excon (0.54.0)
|
16
|
+
json (2.0.3)
|
17
|
+
linebreak (2.1.0)
|
18
|
+
log4r (1.1.10)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
x64-mingw32
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
vagrant-dnsdock-hostupdater!
|
26
|
+
|
27
|
+
BUNDLED WITH
|
28
|
+
1.14.6
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'lib/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'vagrant-dnsdock-hostupdater'
|
5
|
+
s.version = Vagrant::DnsdockHostUpdater::VERSION
|
6
|
+
s.summary = 'Update hosts'
|
7
|
+
s.description = <<-DESCRIPTION
|
8
|
+
vagrant-dnsdock-hostupdater is a Vagrant plugin which provides the ability to keep the host machines hosts file
|
9
|
+
configured to point at bridged docker containers based on container names: e.g. <container_name>.local.signal.sh.
|
10
|
+
DNSDock should be configured on the guest machine to enable containers to resolve these hosts.
|
11
|
+
DESCRIPTION
|
12
|
+
s.authors = ['Brian Coit']
|
13
|
+
s.email = 'brian.coit@cellosignal.com'
|
14
|
+
s.files = Dir['{lib}'] + ['Rakefile', 'Gemfile', 'Gemfile.lock', 'vagrant-dnsdock-hostupdater.gemspec']
|
15
|
+
s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ['lib']
|
17
|
+
s.homepage = 'https://bitbucket.org/briancoit'
|
18
|
+
s.license = 'ISC'
|
19
|
+
|
20
|
+
|
21
|
+
s.required_ruby_version = '>= 2.0.0'
|
22
|
+
|
23
|
+
s.add_runtime_dependency 'docker-api', '~> 1.33'
|
24
|
+
# gem 'hosts', '~> 0.1.1'
|
25
|
+
s.add_runtime_dependency 'log4r', '~> 1.1.10'
|
26
|
+
s.add_runtime_dependency 'linebreak', '~> 2.1.0'
|
27
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.22
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Coit
|
@@ -62,28 +62,9 @@ extensions: []
|
|
62
62
|
extra_rdoc_files: []
|
63
63
|
files:
|
64
64
|
- Gemfile
|
65
|
+
- Gemfile.lock
|
65
66
|
- Rakefile
|
66
|
-
-
|
67
|
-
- lib/event-watcher.rb
|
68
|
-
- lib/host-manager.rb
|
69
|
-
- lib/hosts/aef/hosts.rb
|
70
|
-
- lib/hosts/aef/hosts/comment.rb
|
71
|
-
- lib/hosts/aef/hosts/element.rb
|
72
|
-
- lib/hosts/aef/hosts/empty_element.rb
|
73
|
-
- lib/hosts/aef/hosts/entry.rb
|
74
|
-
- lib/hosts/aef/hosts/file.rb
|
75
|
-
- lib/hosts/aef/hosts/helpers.rb
|
76
|
-
- lib/hosts/aef/hosts/section.rb
|
77
|
-
- lib/hosts/aef/hosts/version.rb
|
78
|
-
- lib/hosts/aef/linebreak/linebreak.rb
|
79
|
-
- lib/hosts/hosts.rb
|
80
|
-
- lib/hosts/hosts/bare.rb
|
81
|
-
- lib/launch-control
|
82
|
-
- lib/server.rb
|
83
|
-
- lib/vagrant-dnsdock-hostupdater.rb
|
84
|
-
- lib/vagrant-dnsdock-hostupdater/command.rb
|
85
|
-
- lib/vagrant-dnsdock-hostupdater/plugin.rb
|
86
|
-
- lib/version.rb
|
67
|
+
- vagrant-dnsdock-hostupdater.gemspec
|
87
68
|
homepage: https://bitbucket.org/briancoit
|
88
69
|
licenses:
|
89
70
|
- ISC
|
data/lib/client.rb
DELETED
data/lib/event-watcher.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
require_relative 'host-manager'
|
2
|
-
require 'yaml'
|
3
|
-
require 'pp'
|
4
|
-
|
5
|
-
module HostManager
|
6
|
-
class DockerEvent
|
7
|
-
|
8
|
-
def self.update(ip, port, host_suffix)
|
9
|
-
client = HostManager::Client.create(ip, port)
|
10
|
-
client.host_suffix host_suffix
|
11
|
-
client.sync_hosts Docker::Container.all({:all => true})
|
12
|
-
client.close
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.run(ip, port, host_suffix)
|
16
|
-
# sync existing host entries on start (normally this will remove old left-over entries)
|
17
|
-
self.update(ip, port, host_suffix)
|
18
|
-
|
19
|
-
sleep(2)
|
20
|
-
loop {
|
21
|
-
run_once(ip, port, host_suffix)
|
22
|
-
}
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
def self.run_once(ip, port, host_suffix)
|
27
|
-
begin
|
28
|
-
Docker::Event.stream { |event|
|
29
|
-
HostManager.log.info 'Connected to docker socket. Listening for events.'
|
30
|
-
# respond to all container events
|
31
|
-
|
32
|
-
begin
|
33
|
-
if event.type == 'container' && %w(start die).include?(event.action)
|
34
|
-
self.update(ip, port, host_suffix)
|
35
|
-
end
|
36
|
-
|
37
|
-
rescue => e
|
38
|
-
puts e.inspect
|
39
|
-
end
|
40
|
-
|
41
|
-
break
|
42
|
-
}
|
43
|
-
|
44
|
-
rescue Docker::Error::TimeoutError
|
45
|
-
HostManager.log.info 'Docker socket connection timed out whilst listening for events. Reconnecting...'
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
HostManager::DockerEvent.run('192.168.200.1', 2991, '.local.signal.sh')
|
data/lib/host-manager.rb
DELETED
@@ -1,298 +0,0 @@
|
|
1
|
-
require 'socket' # Get sockets from stdlib
|
2
|
-
require 'json'
|
3
|
-
require_relative 'hosts/hosts'
|
4
|
-
require 'docker'
|
5
|
-
require 'logger'
|
6
|
-
require 'log4r'
|
7
|
-
|
8
|
-
module OS
|
9
|
-
def OS.windows?
|
10
|
-
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
|
11
|
-
end
|
12
|
-
|
13
|
-
def OS.mac?
|
14
|
-
(/darwin/ =~ RUBY_PLATFORM) != nil
|
15
|
-
end
|
16
|
-
|
17
|
-
def OS.unix?
|
18
|
-
!OS.windows?
|
19
|
-
end
|
20
|
-
|
21
|
-
def OS.linux?
|
22
|
-
OS.unix? and not OS.mac?
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
module HostManager
|
27
|
-
|
28
|
-
def self.log
|
29
|
-
unless @log
|
30
|
-
@log = Log4r::Logger.new('main')
|
31
|
-
@log.outputters << Log4r::Outputter.stdout
|
32
|
-
@log.outputters << Log4r::FileOutputter.new('logmain', :filename => 'host-manager.log')
|
33
|
-
end
|
34
|
-
|
35
|
-
@log
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.comment_prefix
|
39
|
-
'Managed by Vagrant plugin - do not remove.'
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.host_file_path
|
43
|
-
OS.windows? ? `echo %SystemRoot%\\System32\\drivers\\etc\\hosts`.chomp : '/etc/hosts'
|
44
|
-
end
|
45
|
-
|
46
|
-
class Base
|
47
|
-
|
48
|
-
def log
|
49
|
-
HostManager.log
|
50
|
-
end
|
51
|
-
|
52
|
-
def host_name(container_name)
|
53
|
-
if container_name.include?('.')
|
54
|
-
container_name
|
55
|
-
else
|
56
|
-
sanitize_host_value(container_name) + host_suffix
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def compose_host_names(container)
|
61
|
-
names = []
|
62
|
-
labels = container.info['Config']['Labels']
|
63
|
-
|
64
|
-
name = labels['com.docker.compose.project']
|
65
|
-
service = labels['com.docker.compose.service']
|
66
|
-
|
67
|
-
|
68
|
-
if name != nil && service != nil
|
69
|
-
service = sanitize_host_value(service)
|
70
|
-
name = sanitize_host_value(name)
|
71
|
-
|
72
|
-
container_num = labels['com.docker.compose.container-number']
|
73
|
-
if container_num == '1'
|
74
|
-
names.push("#{service}.#{name}#{host_suffix}")
|
75
|
-
else
|
76
|
-
names.push("#{service}-#{container_num}.#{name}#{host_suffix}")
|
77
|
-
end
|
78
|
-
else
|
79
|
-
names.push(host_name(container.info['Name']))
|
80
|
-
end
|
81
|
-
|
82
|
-
names
|
83
|
-
end
|
84
|
-
|
85
|
-
def host_suffix(suffix = nil)
|
86
|
-
if suffix != nil
|
87
|
-
@domain_suffix = suffix
|
88
|
-
else
|
89
|
-
@domain_suffix
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
# Determine if entry is managed by this utility or not
|
94
|
-
def valid_entry?(element)
|
95
|
-
# check it's a managed host first
|
96
|
-
if element.respond_to?(:comment) && element.comment.to_s.start_with?(HostManager.comment_prefix)
|
97
|
-
# check we have hostname and ip
|
98
|
-
if element.respond_to?(:name) && element.respond_to?(:address)
|
99
|
-
return true
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
def get_hosts
|
105
|
-
unless @host_manager
|
106
|
-
@host_manager = Hosts::File.read(HostManager.host_file_path)
|
107
|
-
end
|
108
|
-
|
109
|
-
@host_manager
|
110
|
-
end
|
111
|
-
|
112
|
-
def build_hosts_data(container_data)
|
113
|
-
data = []
|
114
|
-
|
115
|
-
if container_data && container_data.is_a?(Array)
|
116
|
-
container_data.each do |container_info|
|
117
|
-
container = Docker::Container.get(container_info.info['id'])
|
118
|
-
|
119
|
-
if container.info['State']['Running']
|
120
|
-
name = container.info['Name']
|
121
|
-
|
122
|
-
if name
|
123
|
-
ip = container.info['NetworkSettings']['IPAddress']
|
124
|
-
|
125
|
-
if ip
|
126
|
-
compose_host_names(container).each do |hostname|
|
127
|
-
item = {
|
128
|
-
:hostname => hostname,
|
129
|
-
:ip => ip,
|
130
|
-
:containerId => container.info['id']
|
131
|
-
}
|
132
|
-
|
133
|
-
data.push(item)
|
134
|
-
end
|
135
|
-
else
|
136
|
-
log.warn("Ignored request to create hosts entry due to no IP being returned from container: #{name}")
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
data
|
145
|
-
end
|
146
|
-
|
147
|
-
def update_hosts_file(data)
|
148
|
-
if data.is_a?(Hash)
|
149
|
-
required_keys = %w(action hostname ip containerId)
|
150
|
-
required_keys.each do |key|
|
151
|
-
unless data.key?(key)
|
152
|
-
raise "Data for key #{key} not provided."
|
153
|
-
end
|
154
|
-
end
|
155
|
-
execute(data['action'], data['hostname'], data['ip'], data['containerId'])
|
156
|
-
|
157
|
-
elsif data.is_a?(Array)
|
158
|
-
hosts = get_hosts
|
159
|
-
|
160
|
-
# initially remove all entries
|
161
|
-
hosts.elements.delete_if do |element|
|
162
|
-
if valid_entry?(element)
|
163
|
-
log.info 'Removing entry for ' + element.name
|
164
|
-
true
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
# add recieved entries
|
169
|
-
data.each do |entry|
|
170
|
-
add_entry(entry, hosts)
|
171
|
-
hosts.write
|
172
|
-
end
|
173
|
-
|
174
|
-
hosts.write
|
175
|
-
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
def add_entry(entry, hosts)
|
180
|
-
if entry[:ip] && entry[:containerId] && entry[:hostname]
|
181
|
-
comment = HostManager.comment_prefix + (entry['containerId'] ? " Container ID: #{entry[:containerId]}" : '')
|
182
|
-
log.info "Adding entry: #{entry[:hostname]} => #{entry[:ip]}"
|
183
|
-
hosts.elements << Hosts::Entry.new(entry[:ip], entry[:hostname], :comment => comment)
|
184
|
-
|
185
|
-
else
|
186
|
-
log.warn 'Unable to write entry due to missing values [ip,containerId,hostname]: ' + entry.to_json
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
def execute(action, hostname, ip, container_id = nil)
|
191
|
-
hosts = get_hosts
|
192
|
-
|
193
|
-
if action == 'create'
|
194
|
-
hosts.elements.each do |element|
|
195
|
-
if valid_entry?(element) && hostname == element.name
|
196
|
-
raise "Entry already exists! (#{ip} #{hostname})"
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
comment = HostManager.comment_prefix + (container_id ? " Container ID: #{container_id}" : '')
|
201
|
-
|
202
|
-
hosts.elements << Hosts::Entry.new(ip, hostname, :comment => comment)
|
203
|
-
hosts.write
|
204
|
-
|
205
|
-
elsif action == 'delete'
|
206
|
-
hosts.elements.delete_if do |element|
|
207
|
-
if valid_entry?(element)
|
208
|
-
if hostname == element.name
|
209
|
-
log.info 'Removing entry for ' + element.name
|
210
|
-
true
|
211
|
-
end
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
hosts.write
|
216
|
-
else
|
217
|
-
raise 'No valid action specified.'
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
private
|
222
|
-
|
223
|
-
def sanitize_host_value(value)
|
224
|
-
value.sub(/^\//, '').gsub(/_/, '-')
|
225
|
-
end
|
226
|
-
|
227
|
-
end
|
228
|
-
|
229
|
-
class Server < Base
|
230
|
-
def run(ip, port)
|
231
|
-
HostManager.log.info "Running on host manager service on #{ip}:#{port}"
|
232
|
-
@tcp_server = TCPServer.new ip, port
|
233
|
-
|
234
|
-
loop {
|
235
|
-
|
236
|
-
client = @tcp_server.accept
|
237
|
-
content = client.gets
|
238
|
-
|
239
|
-
log.debug("Recieved data: #{content.to_s.strip}")
|
240
|
-
|
241
|
-
begin
|
242
|
-
unless content.to_s.chomp.strip.empty?
|
243
|
-
data = JSON.parse(content)
|
244
|
-
update_hosts_file(data)
|
245
|
-
end
|
246
|
-
|
247
|
-
rescue => e
|
248
|
-
log.error("Caught exception attempting to execute with data #{content}. #{e}")
|
249
|
-
end
|
250
|
-
|
251
|
-
client.close
|
252
|
-
}
|
253
|
-
|
254
|
-
end
|
255
|
-
|
256
|
-
def self.create(ip, port)
|
257
|
-
server = self.new
|
258
|
-
server.run(ip, port)
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
|
-
class Client < Base
|
263
|
-
|
264
|
-
def initialize(ip, port)
|
265
|
-
@socket = TCPSocket.new(ip, port)
|
266
|
-
end
|
267
|
-
|
268
|
-
def close
|
269
|
-
log.info('Closing client connection.')
|
270
|
-
@socket.close
|
271
|
-
end
|
272
|
-
|
273
|
-
def sync_hosts(container_data)
|
274
|
-
data = build_hosts_data(container_data)
|
275
|
-
|
276
|
-
begin
|
277
|
-
update_hosts_file(data)
|
278
|
-
|
279
|
-
`service dnsmasq restart`
|
280
|
-
|
281
|
-
rescue => e
|
282
|
-
log.error("Caught exception attempting to write to hosts file with data #{data.to_json}. #{e}")
|
283
|
-
end
|
284
|
-
|
285
|
-
send(data)
|
286
|
-
end
|
287
|
-
|
288
|
-
def send(data)
|
289
|
-
log.info("Sending data via client: #{data.to_json}")
|
290
|
-
@socket.write data.to_json + "\n"
|
291
|
-
end
|
292
|
-
|
293
|
-
def self.create(ip, port)
|
294
|
-
self.new(ip, port)
|
295
|
-
end
|
296
|
-
end
|
297
|
-
|
298
|
-
end
|