kontejner 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Dockerfile +8 -3
- data/README.md +9 -12
- data/lib/kontejner/resolver.rb +75 -6
- data/lib/kontejner/server.rb +20 -7
- data/lib/kontejner/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: e894cba528f5cbf2897cd28ff7dc0f2abfc33a4f
|
4
|
+
data.tar.gz: ca53c7661403f39873a5f449175392559f7a7706
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99c51bc973b50bd23d23863e96e92e9d9d9bb094bf546bc6b202a13aa597bee0d41eadd380f41432f13c92d60f44b0b7101bd379c2b57d9be00ec2d040ea6a85
|
7
|
+
data.tar.gz: 4e0e07ec2eec4458328bfa7cdc7d0d95964217050d6653cbfe3d98a6af71035044b76dde864c04e5e3c271919b04b8d8e1a05b941e50177c1998afc047ec2003
|
data/Dockerfile
CHANGED
@@ -3,6 +3,11 @@ FROM ubuntu:trusty
|
|
3
3
|
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 80F70E11F0F0D5F10CB20E62F5DA5F09C3173AA6 \
|
4
4
|
&& echo 'deb http://ppa.launchpad.net/brightbox/ruby-ng/ubuntu precise main' > /etc/apt/sources.list.d/ruby-ng.list \
|
5
5
|
&& apt-get -y -q update \
|
6
|
-
&& apt-get -y -q install git-core ruby2.1 rubygems ruby-switch \
|
7
|
-
&& ruby-switch --set ruby2.1
|
8
|
-
|
6
|
+
&& apt-get -y -q install git-core ruby2.1 ruby2.1-dev rubygems ruby-switch \
|
7
|
+
&& ruby-switch --set ruby2.1
|
8
|
+
|
9
|
+
RUN gem install kontejner --no-rdoc --no-ri
|
10
|
+
|
11
|
+
EXPOSE 53
|
12
|
+
ENTRYPOINT ["kontejner"]
|
13
|
+
CMD ["start"]
|
data/README.md
CHANGED
@@ -1,28 +1,25 @@
|
|
1
1
|
# Kontejner
|
2
2
|
|
3
|
-
|
3
|
+
Like [skydock](https://github.com/crosbymichael/skydock) + [skydns](https://github.com/skynetservices/skydns1), but in Ruby.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
gem 'kontejner'
|
10
|
-
|
11
|
-
And then execute:
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
7
|
+
Install it yourself as:
|
16
8
|
|
17
9
|
$ gem install kontejner
|
18
10
|
|
11
|
+
Then run it like:
|
12
|
+
```bash
|
13
|
+
kontejner start --domain=docker --port=7079 --docker=$DOCKER_HOST
|
14
|
+
```
|
15
|
+
|
19
16
|
## Usage
|
20
17
|
|
21
|
-
|
18
|
+
* Use EventMachine http client rather than blocking excon in Docker Api
|
22
19
|
|
23
20
|
## Contributing
|
24
21
|
|
25
|
-
1. Fork it ( https://github.com/
|
22
|
+
1. Fork it ( https://github.com/mikz/kontejner/fork )
|
26
23
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
24
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
25
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/kontejner/resolver.rb
CHANGED
@@ -1,21 +1,90 @@
|
|
1
1
|
require 'docker'
|
2
2
|
require 'active_support/cache'
|
3
|
+
require 'monitor'
|
4
|
+
require 'logger'
|
3
5
|
|
4
6
|
module Kontejner
|
5
7
|
class Resolver
|
8
|
+
include MonitorMixin
|
9
|
+
|
10
|
+
CACHE_TTL = 600
|
11
|
+
CACHE_OPTIONS = { ttl: CACHE_TTL, race_condition_ttl: CACHE_TTL/100 }.freeze
|
12
|
+
|
6
13
|
def initialize(docker:)
|
7
|
-
@
|
8
|
-
|
14
|
+
@logger = Logger.new($stdout)
|
15
|
+
|
16
|
+
@connection = ::Docker::Connection.new(docker, {})
|
17
|
+
@id_cache = ::ActiveSupport::Cache::MemoryStore.new
|
18
|
+
@ip_cache = ::ActiveSupport::Cache::MemoryStore.new
|
19
|
+
|
20
|
+
super()
|
21
|
+
|
22
|
+
@updater = Thread.new do
|
23
|
+
loop do
|
24
|
+
listen_event_stream
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def listen_event_stream
|
30
|
+
::Docker::Event.stream({}, @connection) do |event|
|
31
|
+
handle_event(event)
|
32
|
+
end
|
33
|
+
rescue
|
34
|
+
@logger.error('EvenStream') { $ERROR_INFO }
|
35
|
+
end
|
36
|
+
|
37
|
+
def handle_event(event)
|
38
|
+
@logger.debug("Processing event #{event}")
|
39
|
+
|
40
|
+
case event.status
|
41
|
+
when 'die'.freeze
|
42
|
+
@id_cache.clear
|
43
|
+
@ip_cache.delete(event.id)
|
44
|
+
when 'start'.freeze
|
45
|
+
@id_cache.clear
|
46
|
+
@ip_cache.write(event.id, ip(event.id))
|
47
|
+
when 'create'.freeze
|
48
|
+
end
|
9
49
|
end
|
10
50
|
|
11
51
|
def resolve(name)
|
12
|
-
|
52
|
+
id = id(name)
|
53
|
+
|
54
|
+
@logger.debug("#{name} has id #{id}")
|
55
|
+
@ip_cache.fetch(id, CACHE_OPTIONS) do
|
56
|
+
ip(id)
|
57
|
+
end
|
13
58
|
rescue Docker::Error::NotFoundError
|
59
|
+
@logger.warn("#{name} could not be found")
|
60
|
+
false
|
14
61
|
end
|
15
62
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
63
|
+
def id(name)
|
64
|
+
case name.length
|
65
|
+
when 64
|
66
|
+
name
|
67
|
+
else
|
68
|
+
@id_cache.fetch(name, CACHE_OPTIONS) do
|
69
|
+
container = Docker::Container.get(name, {}, @connection)
|
70
|
+
@logger.debug("Resolved #{name} to #{container.id}")
|
71
|
+
container.id
|
72
|
+
end
|
73
|
+
end
|
19
74
|
end
|
75
|
+
|
76
|
+
def ip(id)
|
77
|
+
container = Docker::Container.get(id, {}, @connection)
|
78
|
+
json = container.json
|
79
|
+
unless json['State']['Running']
|
80
|
+
@logger.warn("#{id} is not running")
|
81
|
+
raise Docker::Error::NotFoundError
|
82
|
+
end
|
83
|
+
ip = json['NetworkSettings']['IPAddress']
|
84
|
+
@logger.debug("#{id} has ip #{ip}")
|
85
|
+
ip
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
20
89
|
end
|
21
90
|
end
|
data/lib/kontejner/server.rb
CHANGED
@@ -3,33 +3,46 @@ require 'kontejner/resolver'
|
|
3
3
|
|
4
4
|
module Kontejner
|
5
5
|
class Server
|
6
|
-
GOOGLE_SERVERS = [ [:udp, '8.8.8.8', 53], [:tcp, '8.8.8.8', 53], [:udp, '8.8.4.4', 53], [:
|
6
|
+
GOOGLE_SERVERS = [ [:udp, '8.8.8.8', 53], [:tcp, '8.8.8.8', 53], [:udp, '8.8.4.4', 53], [:tcp, '8.8.4.4', 53] ]
|
7
7
|
|
8
8
|
IN = Resolv::DNS::Resource::IN
|
9
9
|
|
10
10
|
def initialize(domain:,docker:,ttl:)
|
11
|
+
@dns = RubyDNS::RuleBasedServer.new
|
12
|
+
|
13
|
+
@logger = @dns.logger
|
14
|
+
@logger.level = Logger::DEBUG
|
15
|
+
|
16
|
+
@matcher = /(?<subdomain>[^.]+)\.#{domain}$/
|
17
|
+
@ttl = ttl
|
18
|
+
|
11
19
|
@upstream = RubyDNS::Resolver.new(GOOGLE_SERVERS)
|
12
20
|
@docker = Kontejner::Resolver.new(docker: docker)
|
13
21
|
|
14
|
-
@
|
15
|
-
|
22
|
+
@logger.info("Docker Connection #{docker}")
|
23
|
+
end
|
24
|
+
|
25
|
+
def start(**options)
|
26
|
+
@logger.debug("Matching #{@matcher}")
|
16
27
|
|
17
|
-
@dns.match(
|
28
|
+
@dns.match(@matcher, IN::A) do |t, match|
|
18
29
|
ip = @docker.resolve(match[:subdomain])
|
19
30
|
|
20
31
|
if ip
|
21
|
-
t.respond!(ip, ttl: ttl)
|
32
|
+
t.respond!(ip, ttl: @ttl)
|
22
33
|
else
|
23
|
-
t.fail! :
|
34
|
+
t.fail! :NXDomain
|
24
35
|
end
|
25
36
|
end
|
26
37
|
|
27
38
|
@dns.otherwise do |q|
|
28
39
|
q.passthrough!(@upstream)
|
29
40
|
end
|
41
|
+
|
42
|
+
run(options)
|
30
43
|
end
|
31
44
|
|
32
|
-
def
|
45
|
+
def run(options)
|
33
46
|
EventMachine.run do
|
34
47
|
trap("INT") do
|
35
48
|
EventMachine.stop
|
data/lib/kontejner/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kontejner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michal Cichra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|