Class: EndPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/model/endpoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(puppetdb_helper, cache_timeout, thread_count) ⇒ EndPoint

Returns a new instance of EndPoint.



10
11
12
13
14
# File 'lib/model/endpoint.rb', line 10

def initialize(puppetdb_helper, cache_timeout, thread_count)
  @db_helper = puppetdb_helper
  @cache_timeout = cache_timeout
  @thread_count = thread_count
end

Instance Attribute Details

#cache_timeoutObject

Returns the value of attribute cache_timeout.



7
8
9
# File 'lib/model/endpoint.rb', line 7

def cache_timeout
  @cache_timeout
end

#thread_countObject

Returns the value of attribute thread_count.



8
9
10
# File 'lib/model/endpoint.rb', line 8

def thread_count
  @thread_count
end

Instance Method Details

#clear_cacheObject



129
130
131
132
133
134
135
136
137
138
# File 'lib/model/endpoint.rb', line 129

def clear_cache
  cache_files = ['/tmp/puppetdb-resource.json','/tmp/puppetdb-resource.yaml']

  cache_files.each do |file|
    if File.exist?(file)
      FileUtils.rm file
    end
  end
  "Cached cleared"
end

#parse(type) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/model/endpoint.rb', line 108

def parse(type)
  per_type_cache = "/tmp/puppetdb-resource.#{type}"

  if File.exist?(per_type_cache)
    file = File.new(per_type_cache)
    t_now = Time.at(Time.now.to_i)
    t_file = Time.at(file.mtime.to_i)

    if t_now < (t_file + @cache_timeout)
      p "reading from cache for #{type}"
      @rundeck_data = File.new(per_type_cache, 'r').read
    else
      p "not reading from cache for: #{type}"
      reload(type)
    end
  else
    p "not reading from cache for: #{type}"
    reload(type)
  end
end

#reload(type) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/model/endpoint.rb', line 16

def reload(type)
  p "reloading data"
  if @nodes.nil? or @nodes.empty?
    @nodes = @db_helper.get_nodes
  end

  @rundeck_data = Hash.new

  helper = Helpers::Process.new

  mutex = Mutex.new

  per_type_cache = "/tmp/puppetdb-resource.#{type}"

  data_elements = []
  process_threads = []

  @thread_count.times.map {
    t = Thread.new(@nodes) do |nodes|
      while node = mutex.synchronize { @nodes.pop }
        host = node['name']
        facts = @db_helper.get_facts(host)
        if !facts.nil?
          mutex.synchronize do
            data_elements.push(helper.add_facts(facts, host))
          end
        end
      end
    end
    process_threads.push(t)
  }

  ThreadsWait.all_waits(*process_threads)

  data_elements.each do |item|
    #sleep(Random.new.rand(1..10))
    @rundeck_data.merge!(item) if @rundeck_data.is_a?(Hash)
  end

  data = case type
    when 'json' then self.to_json(false)
    when 'yaml' then self.to_yaml(false)
    when 'xml' then to_xml(false)
    else 'unknown'
  end

  File.open(per_type_cache, 'w') { |file| file.write(data) }

  return data
end

#to_json(parse_data) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/model/endpoint.rb', line 67

def to_json(parse_data)
  p "parse_data is: #{parse_data}"
  parse('json') if parse_data == true
  if @rundeck_data.is_a?(String)
    @rundeck_data
  else
    @rundeck_data.to_json
  end
end

#to_xml(parse_data = true) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/model/endpoint.rb', line 87

def to_xml(parse_data=true)
  parse('xml') if parse_data == true
  if @rundeck_data.is_a?(String)
    @rundeck_data
  else
    data = Array.new
    data << "<project>\n"
    @rundeck_data.keys.each {|n|
      data << "<node name=\"#{n}\""
      @rundeck_data[n].each {|k,v|
        data << "#{k}=\"#{v}\""
      }
      data << "/>\n"
    }
    data << "</project>"
    xml = data.join(" ")

    @rundeck_data = xml
  end
end

#to_yaml(parse_data) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/model/endpoint.rb', line 77

def to_yaml(parse_data)
  p "parse_data is: #{parse_data}"
  parse('yaml') if parse_data == true
  if @rundeck_data.is_a?(String)
    @rundeck_data
  else
    @rundeck_data.to_yaml
  end
end