Class: EasyType::NodeGroupData

Inherits:
Object
  • Object
show all
Includes:
CatalogData, Encryption
Defined in:
lib/easy_type/node_group_data.rb

Overview

Class to read and write from the NodeGroup cache

Instance Method Summary collapse

Methods included from Encryption

#cipher, #decrypt, #decrypted_value, #encrypt, #encrypted?, #encryption_key

Methods included from CatalogData

#catalog, #get_catalog_data, #get_secured_catalog_data

Constructor Details

#initialize(cache_file) ⇒ NodeGroupData

Returns a new instance of NodeGroupData.



17
18
19
20
21
# File 'lib/easy_type/node_group_data.rb', line 17

def initialize(cache_file)
  @cache_file = cache_file
  @pdb = EasyType::Pdb.new
  @classifier = EasyType::Classifier.new
end

Instance Method Details

#load_from_cacheObject

This function fetches the node group data from the cache file. If the node group data is older then 30 minutes, we fail and fall back to an empty list. Thus failing the license checks.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/easy_type/node_group_data.rb', line 51

def load_from_cache
  encrypted_data = File.read(@cache_file) 
  node_group_data = JSON.parse(decrypt(encrypted_data))
  if Time.parse(node_group_data['time']) < Time.now - 90 * 60  # Older then 1,5 hour
    Puppet.warn_once(:license, :node_group_cache_expired, "Node group cache older the 1,5 hour. Please run puppet agent to update the license information.")
    @data = {}
  else
    Puppet.debug "Using cached node group data."
    @data = node_group_data['data']
  end
rescue
  Puppet.debug "Error loading from cache, returning empty list."
  @data = {}
end

#load_from_catalogObject

Load the node group information from the catalog. If there is np catalog available, load the node group information from the cache



27
28
29
# File 'lib/easy_type/node_group_data.rb', line 27

def load_from_catalog
  @data = get_secured_catalog_data('node_group_data')
end

#load_from_serverObject

Load the node group information from the puppet server



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/easy_type/node_group_data.rb', line 34

def load_from_server
  Puppet.debug "Fetching node group information from classifier."
  groups = @classifier.em_groups
  @data = {}
  if groups.nil?
    Puppet.debug "Node group '#{group_name}' not found."
  else
    groups.each {|g| get_nodes(g, @data)}
  end
  @data
end

#save_node_group_cacheObject

This function saves the fetched node group data to the node group cache file. It also stores the time the cache is created so when we use it, we can check how old it is.

Because it is called multiple times. We only rewrite the file if it is older then 1 minute.



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/easy_type/node_group_data.rb', line 72

def save_node_group_cache
  dir = File.dirname(@cache_file)
  FileUtils.mkdir_p(dir) unless File.exist?(dir)
  if !File.exist?(@cache_file) || (File.exist?(@cache_file) && File.ctime(@cache_file) < Time.now - 60)
    hashed_data = {
      :time => Time.now,
      :data => @data
    }
    encrypted_data = encrypt(hashed_data.to_json)
    File.write(@cache_file,encrypted_data) 
  end
end