vagrant-dnsdock-hostupdater 0.0.20 → 0.0.21
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/Gemfile +14 -0
- data/lib/client.rb +1 -2
- data/lib/event-watcher.rb +41 -48
- data/lib/host-manager.rb +45 -28
- data/lib/version.rb +1 -1
- metadata +5 -4
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: e3573580bb55bd09385772c7bcdd7cff5564d6e4
         | 
| 4 | 
            +
              data.tar.gz: 7aa965373fe6f88c2c4347895a1d69bcb6a98f7c
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 882ec6bc80ec52c62c1b2106209df03f8b6c00a0951dca92ca1901c750e6d87ba4e691c0313cc6643d48d084c67a15fc6a29d5c7568473a349d3a8483b9bf617
         | 
| 7 | 
            +
              data.tar.gz: a6dbbf6cab2d0dafda674da90e5cb9700bb17276a86166727ad67e198b14e90c934c0d35fd821c32951eb8f1eb79b220bc444689b62ca3e111b20726832aaf80
         | 
    
        data/Gemfile
    ADDED
    
    | @@ -0,0 +1,14 @@ | |
| 1 | 
            +
            source "http://rubygems.org"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            gemspec
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # group :development do
         | 
| 6 | 
            +
            #   gem 'vagrant',
         | 
| 7 | 
            +
            #       git: 'https://github.com/mitchellh/vagrant.git',
         | 
| 8 | 
            +
            #       ref: ENV.fetch('VAGRANT_VERSION', 'v1.9.1')
         | 
| 9 | 
            +
            # end
         | 
| 10 | 
            +
            #
         | 
| 11 | 
            +
            # group :plugins do
         | 
| 12 | 
            +
            #   gem "vagrant-dnsdock-hostupdater", path: "."
         | 
| 13 | 
            +
            # end
         | 
| 14 | 
            +
            #
         | 
    
        data/lib/client.rb
    CHANGED
    
    
    
        data/lib/event-watcher.rb
    CHANGED
    
    | @@ -1,58 +1,51 @@ | |
| 1 1 | 
             
            require_relative 'host-manager'
         | 
| 2 2 | 
             
            require 'yaml'
         | 
| 3 | 
            -
             | 
| 4 | 
            -
             | 
| 5 | 
            -
             | 
| 6 | 
            -
               | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
                   | 
| 20 | 
            -
             | 
| 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)
         | 
| 21 27 | 
             
                  begin
         | 
| 22 | 
            -
                     | 
| 23 | 
            -
                       | 
| 24 | 
            -
             | 
| 25 | 
            -
                      # puts event.action
         | 
| 28 | 
            +
                    Docker::Event.stream { |event|
         | 
| 29 | 
            +
                      HostManager.log.info 'Connected to docker socket. Listening for events.'
         | 
| 30 | 
            +
                      # respond to all container events
         | 
| 26 31 |  | 
| 27 | 
            -
                       | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 32 | 
            +
                      begin
         | 
| 33 | 
            +
                        if event.type == 'container' && %w(start die).include?(event.action)
         | 
| 34 | 
            +
                          self.update(ip, port, host_suffix)
         | 
| 35 | 
            +
                        end
         | 
| 31 36 |  | 
| 32 | 
            -
                       | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
                      # To handle updating single entries
         | 
| 36 | 
            -
                      # if event.action == 'start'
         | 
| 37 | 
            -
                      #   client.send_action('create', container_name, ip, container_id)
         | 
| 38 | 
            -
                      # elsif event.action == 'die'
         | 
| 39 | 
            -
                      #   client.send_action('delete', container_name, ip, container_id)
         | 
| 40 | 
            -
                      # end
         | 
| 41 | 
            -
             | 
| 42 | 
            -
                      if event.action == 'start' || event.action == 'die'
         | 
| 43 | 
            -
                        update
         | 
| 37 | 
            +
                      rescue => e
         | 
| 38 | 
            +
                        puts e.inspect
         | 
| 44 39 | 
             
                      end
         | 
| 45 40 |  | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 48 | 
            -
                    puts e.inspect
         | 
| 49 | 
            -
                  end
         | 
| 41 | 
            +
                      break
         | 
| 42 | 
            +
                    }
         | 
| 50 43 |  | 
| 51 | 
            -
                   | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
             | 
| 55 | 
            -
                HostManager.log.info 'Docker socket connection timed out whilst listening for events. Reconnecting...'
         | 
| 44 | 
            +
                  rescue Docker::Error::TimeoutError
         | 
| 45 | 
            +
                    HostManager.log.info 'Docker socket connection timed out whilst listening for events. Reconnecting...'
         | 
| 46 | 
            +
                  end
         | 
| 47 | 
            +
                end
         | 
| 56 48 | 
             
              end
         | 
| 57 | 
            -
             | 
| 49 | 
            +
            end
         | 
| 58 50 |  | 
| 51 | 
            +
            HostManager::DockerEvent.run('192.168.200.1', 2991, '.local.signal.sh')
         | 
    
        data/lib/host-manager.rb
    CHANGED
    
    | @@ -53,10 +53,35 @@ module HostManager | |
| 53 53 | 
             
                  if container_name.include?('.')
         | 
| 54 54 | 
             
                    container_name
         | 
| 55 55 | 
             
                  else
         | 
| 56 | 
            -
                    container_name | 
| 56 | 
            +
                    sanitize_host_value(container_name) + host_suffix
         | 
| 57 57 | 
             
                  end
         | 
| 58 58 | 
             
                end
         | 
| 59 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 | 
            +
             | 
| 60 85 | 
             
                def host_suffix(suffix = nil)
         | 
| 61 86 | 
             
                  if suffix != nil
         | 
| 62 87 | 
             
                    @domain_suffix = suffix
         | 
| @@ -98,13 +123,15 @@ module HostManager | |
| 98 123 | 
             
                          ip = container.info['NetworkSettings']['IPAddress']
         | 
| 99 124 |  | 
| 100 125 | 
             
                          if ip
         | 
| 101 | 
            -
                             | 
| 102 | 
            -
             | 
| 103 | 
            -
             | 
| 104 | 
            -
             | 
| 105 | 
            -
             | 
| 106 | 
            -
             | 
| 107 | 
            -
             | 
| 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
         | 
| 108 135 | 
             
                          else
         | 
| 109 136 | 
             
                            log.warn("Ignored request to create hosts entry due to no IP being returned from container: #{name}")
         | 
| 110 137 | 
             
                          end
         | 
| @@ -150,9 +177,10 @@ module HostManager | |
| 150 177 | 
             
                end
         | 
| 151 178 |  | 
| 152 179 | 
             
                def add_entry(entry, hosts)
         | 
| 153 | 
            -
                  if entry[ | 
| 154 | 
            -
                    comment = HostManager.comment_prefix + (entry['containerId'] ? " Container ID: #{entry[ | 
| 155 | 
            -
                     | 
| 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)
         | 
| 156 184 |  | 
| 157 185 | 
             
                  else
         | 
| 158 186 | 
             
                    log.warn 'Unable to write entry due to missing values [ip,containerId,hostname]: ' + entry.to_json
         | 
| @@ -162,9 +190,6 @@ module HostManager | |
| 162 190 | 
             
                def execute(action, hostname, ip, container_id = nil)
         | 
| 163 191 | 
             
                  hosts = get_hosts
         | 
| 164 192 |  | 
| 165 | 
            -
             | 
| 166 | 
            -
                  # todo: remove all entries and then re-add (sync hosts every time)
         | 
| 167 | 
            -
             | 
| 168 193 | 
             
                  if action == 'create'
         | 
| 169 194 | 
             
                    hosts.elements.each do |element|
         | 
| 170 195 | 
             
                      if valid_entry?(element) && hostname == element.name
         | 
| @@ -193,6 +218,12 @@ module HostManager | |
| 193 218 | 
             
                  end
         | 
| 194 219 | 
             
                end
         | 
| 195 220 |  | 
| 221 | 
            +
                private
         | 
| 222 | 
            +
             | 
| 223 | 
            +
                def sanitize_host_value(value)
         | 
| 224 | 
            +
                  value.sub(/^\//, '').gsub(/_/, '-')
         | 
| 225 | 
            +
                end
         | 
| 226 | 
            +
             | 
| 196 227 | 
             
              end
         | 
| 197 228 |  | 
| 198 229 | 
             
              class Server < Base
         | 
| @@ -239,20 +270,6 @@ module HostManager | |
| 239 270 | 
             
                  @socket.close
         | 
| 240 271 | 
             
                end
         | 
| 241 272 |  | 
| 242 | 
            -
                def send_action(action, hostname, ip, container_id = nil)
         | 
| 243 | 
            -
                  data = {
         | 
| 244 | 
            -
                      :action => action,
         | 
| 245 | 
            -
                      :hostname => host_name(hostname),
         | 
| 246 | 
            -
                      :ip => ip,
         | 
| 247 | 
            -
                  }
         | 
| 248 | 
            -
             | 
| 249 | 
            -
                  if container_id
         | 
| 250 | 
            -
                    data[:containerId] = container_id
         | 
| 251 | 
            -
                  end
         | 
| 252 | 
            -
             | 
| 253 | 
            -
                  send(data)
         | 
| 254 | 
            -
                end
         | 
| 255 | 
            -
             | 
| 256 273 | 
             
                def sync_hosts(container_data)
         | 
| 257 274 | 
             
                  data = build_hosts_data(container_data)
         | 
| 258 275 |  | 
    
        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.21
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Brian Coit
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2017- | 
| 11 | 
            +
            date: 2017-03-18 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: docker-api
         | 
| @@ -61,6 +61,7 @@ executables: [] | |
| 61 61 | 
             
            extensions: []
         | 
| 62 62 | 
             
            extra_rdoc_files: []
         | 
| 63 63 | 
             
            files:
         | 
| 64 | 
            +
            - Gemfile
         | 
| 64 65 | 
             
            - Rakefile
         | 
| 65 66 | 
             
            - lib/client.rb
         | 
| 66 67 | 
             
            - lib/event-watcher.rb
         | 
| @@ -83,7 +84,7 @@ files: | |
| 83 84 | 
             
            - lib/vagrant-dnsdock-hostupdater/command.rb
         | 
| 84 85 | 
             
            - lib/vagrant-dnsdock-hostupdater/plugin.rb
         | 
| 85 86 | 
             
            - lib/version.rb
         | 
| 86 | 
            -
            homepage: https://bitbucket.org/ | 
| 87 | 
            +
            homepage: https://bitbucket.org/briancoit
         | 
| 87 88 | 
             
            licenses:
         | 
| 88 89 | 
             
            - ISC
         | 
| 89 90 | 
             
            metadata: {}
         | 
| @@ -103,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 103 104 | 
             
                  version: '0'
         | 
| 104 105 | 
             
            requirements: []
         | 
| 105 106 | 
             
            rubyforge_project: 
         | 
| 106 | 
            -
            rubygems_version: 2. | 
| 107 | 
            +
            rubygems_version: 2.5.2
         | 
| 107 108 | 
             
            signing_key: 
         | 
| 108 109 | 
             
            specification_version: 4
         | 
| 109 110 | 
             
            summary: Update hosts
         |