Class: HostManager::Base
- Inherits:
-
Object
- Object
- HostManager::Base
- Defined in:
- lib/host-manager.rb
Instance Method Summary collapse
- #add_entry(entry, hosts) ⇒ Object
- #build_hosts_data(container_data) ⇒ Object
- #compose_host_names(container) ⇒ Object
- #execute(action, hostname, ip, container_id = nil) ⇒ Object
- #get_hosts ⇒ Object
- #host_name(container_name) ⇒ Object
- #host_suffix(suffix = nil) ⇒ Object
- #log ⇒ Object
- #update_hosts_file(data) ⇒ Object
-
#valid_entry?(element) ⇒ Boolean
Determine if entry is managed by this utility or not.
Instance Method Details
#add_entry(entry, hosts) ⇒ Object
179 180 181 182 183 184 185 186 187 188 |
# File 'lib/host-manager.rb', line 179 def add_entry(entry, hosts) if entry[:ip] && entry[:containerId] && entry[:hostname] comment = HostManager.comment_prefix + (entry['containerId'] ? " Container ID: #{entry[:containerId]}" : '') log.info "Adding entry: #{entry[:hostname]} => #{entry[:ip]}" hosts.elements << Hosts::Entry.new(entry[:ip], entry[:hostname], :comment => comment) else log.warn 'Unable to write entry due to missing values [ip,containerId,hostname]: ' + entry.to_json end end |
#build_hosts_data(container_data) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/host-manager.rb', line 112 def build_hosts_data(container_data) data = [] if container_data && container_data.is_a?(Array) container_data.each do |container_info| container = Docker::Container.get(container_info.info['id']) if container.info['State']['Running'] name = container.info['Name'] if name ip = container.info['NetworkSettings']['IPAddress'] if ip compose_host_names(container).each do |hostname| item = { :hostname => hostname, :ip => ip, :containerId => container.info['id'] } data.push(item) end else log.warn("Ignored request to create hosts entry due to no IP being returned from container: #{name}") end end end end end data end |
#compose_host_names(container) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/host-manager.rb', line 60 def compose_host_names(container) names = [] labels = container.info['Config']['Labels'] name = labels['com.docker.compose.project'] service = labels['com.docker.compose.service'] if name != nil && service != nil service = sanitize_host_value(service) name = sanitize_host_value(name) container_num = labels['com.docker.compose.container-number'] if container_num == '1' names.push("#{service}.#{name}#{host_suffix}") else names.push("#{service}-#{container_num}.#{name}#{host_suffix}") end else names.push(host_name(container.info['Name'])) end names end |
#execute(action, hostname, ip, container_id = nil) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/host-manager.rb', line 190 def execute(action, hostname, ip, container_id = nil) hosts = get_hosts if action == 'create' hosts.elements.each do |element| if valid_entry?(element) && hostname == element.name raise "Entry already exists! (#{ip} #{hostname})" end end comment = HostManager.comment_prefix + (container_id ? " Container ID: #{container_id}" : '') hosts.elements << Hosts::Entry.new(ip, hostname, :comment => comment) hosts.write elsif action == 'delete' hosts.elements.delete_if do |element| if valid_entry?(element) if hostname == element.name log.info 'Removing entry for ' + element.name true end end end hosts.write else raise 'No valid action specified.' end end |
#get_hosts ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/host-manager.rb', line 104 def get_hosts unless @host_manager @host_manager = Hosts::File.read(HostManager.host_file_path) end @host_manager end |
#host_name(container_name) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/host-manager.rb', line 52 def host_name(container_name) if container_name.include?('.') container_name else sanitize_host_value(container_name) + host_suffix end end |
#host_suffix(suffix = nil) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/host-manager.rb', line 85 def host_suffix(suffix = nil) if suffix != nil @domain_suffix = suffix else @domain_suffix end end |
#log ⇒ Object
48 49 50 |
# File 'lib/host-manager.rb', line 48 def log HostManager.log end |
#update_hosts_file(data) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/host-manager.rb', line 147 def update_hosts_file(data) if data.is_a?(Hash) required_keys = %w(action hostname ip containerId) required_keys.each do |key| unless data.key?(key) raise "Data for key #{key} not provided." end end execute(data['action'], data['hostname'], data['ip'], data['containerId']) elsif data.is_a?(Array) hosts = get_hosts # initially remove all entries hosts.elements.delete_if do |element| if valid_entry?(element) log.info 'Removing entry for ' + element.name true end end # add recieved entries data.each do |entry| add_entry(entry, hosts) hosts.write end hosts.write end end |
#valid_entry?(element) ⇒ Boolean
Determine if entry is managed by this utility or not
94 95 96 97 98 99 100 101 102 |
# File 'lib/host-manager.rb', line 94 def valid_entry?(element) # check it's a managed host first if element.respond_to?(:comment) && element.comment.to_s.start_with?(HostManager.comment_prefix) # check we have hostname and ip if element.respond_to?(:name) && element.respond_to?(:address) return true end end end |