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

#concrete_targetObject

Returns the value of attribute concrete_target.



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

def concrete_target
  @concrete_target
end

#filterObject

Returns the value of attribute filter.



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

def filter
  @filter
end

#listObject

Returns the value of attribute list.



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

def list
  @list
end

#service_hostObject

Returns the value of attribute service_host.



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

def service_host
  @service_host
end

#service_urlObject

Returns the value of attribute service_url.



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

def service_url
  @service_url
end

Class Method Details

.create(filter = nil, service_hostlist_url = nil, concrete_target = nil) ⇒ Object



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

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

.get_role(hostname) ⇒ Object



29
30
31
# File 'lib/mofa/hostlist.rb', line 29

def self.get_role(hostname)
  Hostlist::get_shortname(hostname).gsub(/\d+$/, '')
end

.get_shortname(hostname) ⇒ Object



25
26
27
# File 'lib/mofa/hostlist.rb', line 25

def self.get_shortname(hostname)
  hostname.gsub(/\..*$/, '')
end

Instance Method Details

#apply_filterObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mofa/hostlist.rb', line 68

def apply_filter
  unless @filter.nil?
    if @filter[0] == '/' && @filter[-1] == '/' && @filter.length > 2
      regex = @filter[1..-2]
    else
      # building matcher
      regex = @filter.gsub(/\*/, '__ASTERISK__')
      regex = Regexp.escape(regex).gsub(/__ASTERISK__/, '.*')
      regex = '^' + regex + '$'
    end
    @list.select! { |hostname| hostname.match(regex) }
  end
end

#filter_by_runlist_map(runlist_map) ⇒ Object



90
91
92
# File 'lib/mofa/hostlist.rb', line 90

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

#has_concrete_targetObject



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

def has_concrete_target
  return (@concrete_target.nil?) ? false : true
end

#retrieveObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mofa/hostlist.rb', line 37

def retrieve
  case
    when has_concrete_target
      @list = [@concrete_target]
    when @service_url.match(/^http/)
      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'] }
    when @service_url.match(/^file:/)
      json_file = @service_url.gsub(/^file:\/\//, '')
      if File.exist?(json_file)
        hosts_list_json = JSON.parse(File.read(json_file))
        @list = hosts_list_json['data'].collect { |i| i['cname'] }
      else
        fail "Hostlist JSON-File not found: #{json_file}"
      end

    else
      fail "Hostlist Service Url either has to be a http(s):// or a file:/// Url!"
  end
  apply_filter
  sort_by_domainname
end

#sort_by_domainnameObject



82
83
84
85
86
87
88
# File 'lib/mofa/hostlist.rb', line 82

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)


63
64
65
66
# File 'lib/mofa/hostlist.rb', line 63

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