Class: Hostlist

Inherits:
Object
  • Object
show all
Defined in:
lib/mofa/hostlist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



10
11
12
# File 'lib/mofa/hostlist.rb', line 10

def api_key
  @api_key
end

#filterObject

Returns the value of attribute filter.



6
7
8
# File 'lib/mofa/hostlist.rb', line 6

def filter
  @filter
end

#listObject

Returns the value of attribute list.



5
6
7
# File 'lib/mofa/hostlist.rb', line 5

def list
  @list
end

#service_hostObject

Returns the value of attribute service_host.



7
8
9
# File 'lib/mofa/hostlist.rb', line 7

def service_host
  @service_host
end

#service_urlObject

Returns the value of attribute service_url.



8
9
10
# File 'lib/mofa/hostlist.rb', line 8

def service_url
  @service_url
end

Class Method Details

.create(filter = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/mofa/hostlist.rb', line 12

def self.create(filter = nil)
  hl = Hostlist.new
  filter ||= Mofa::Config.config['service_hostlist_default_filter']
  hl.filter = filter
  hl.service_host = Mofa::Config.config['service_hostlist_url'].gsub(/^http:\/\//, '').gsub(/\/.*$/, '').gsub(/:.*$/, '')
  hl.service_url = Mofa::Config.config['service_hostlist_url']
  hl.api_key = Mofa::Config.config['service_hostlist_api_key']
  hl
end

Instance Method Details

#apply_filterObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/mofa/hostlist.rb', line 38

def apply_filter
  # building matcher
  regex = @filter.gsub(/\*/, '__ASTERISK__')
  regex = Regexp.escape(regex).gsub(/__ASTERISK__/, '.*')
  regex = '^' + regex + '$'

  puts "regex=#{regex}"

  @list.select! {|hostname| hostname.match(regex) }
end

#filter_by_runlist_map(runlist_map) ⇒ Object



57
58
59
# File 'lib/mofa/hostlist.rb', line 57

def filter_by_runlist_map(runlist_map)
  @list.select! { |hostname| runlist_map.mp.key?(hostname)}
end

#retrieveObject



22
23
24
25
26
27
28
29
30
# File 'lib/mofa/hostlist.rb', line 22

def retrieve
  fail "Hostlist Service not reachable! (cannot ping #{service_host})" unless up?
  response = RestClient.get(@service_url, { :params => {:key => api_key}})
  hosts_list_json = JSON.parse response.body
  @list = hosts_list_json['data'].collect { |i| i['cname'] }

  apply_filter
  sort_by_domainname
end

#sort_by_domainnameObject



49
50
51
52
53
54
55
# File 'lib/mofa/hostlist.rb', line 49

def sort_by_domainname
  sortable = {}
  @list.each do |hostname|
    sortable.store(hostname.split(/\./).reverse.join('.'), hostname)
  end
  @list = sortable.keys.sort.collect { |s| sortable[s] }
end

#up?Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/mofa/hostlist.rb', line 33

def up?
  p = Net::Ping::TCP.new(@service_host, 'http')
  p.ping?
end