Module: Jerakia::Datasource::File

Defined in:
lib/jerakia/datasource/file.rb,
lib/jerakia/datasource/file/json.rb,
lib/jerakia/datasource/file/yaml.rb

Defined Under Namespace

Classes: Json, Yaml

Constant Summary collapse

@@cache =
Jerakia::Cache::File.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_formatObject (readonly)

Returns the value of attribute file_format.



7
8
9
# File 'lib/jerakia/datasource/file.rb', line 7

def file_format
  @file_format
end

Instance Method Details

#cacheObject



18
19
20
# File 'lib/jerakia/datasource/file.rb', line 18

def cache
  @@cache
end

#get_file_with_cache(diskname) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jerakia/datasource/file.rb', line 31

def get_file_with_cache(diskname)
  if options[:enable_caching]
    if cache.valid?(diskname)
      Jerakia.log.debug("Returning cached data")
      cache.get(diskname)
    else
      Jerakia.log.debug("Adding contents of #{diskname} to cache")
      cache.add(diskname,import_file(diskname))
    end
  else
    import_file(diskname)
  end
end

#import_file(filename) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/jerakia/datasource/file.rb', line 22

def import_file(filename)
  Jerakia.log.debug("import_file() Importing #{filename}")
  if ::File.exists?(filename)
    ::File.read(filename)
  else
    ""
  end
end

#list_fragments(prefix, extension) ⇒ Object



45
46
47
48
49
# File 'lib/jerakia/datasource/file.rb', line 45

def list_fragments(prefix,extension)
  if ::File.directory?("#{prefix}.d")
    Dir["#{prefix}.d/*.#{extension}"]
  end
end

#load_format_handlerObject



10
11
12
13
14
15
# File 'lib/jerakia/datasource/file.rb', line 10

def load_format_handler
  format = options[:format] || :yaml
  class_name=format.to_s.capitalize
  require "jerakia/datasource/file/#{format.to_s}"
  @file_format = eval "Jerakia::Datasource::File::#{class_name}"
end

#read_from_file(fname) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jerakia/datasource/file.rb', line 51

def read_from_file(fname)
  fpath = []
  fpath << options[:docroot] unless fname[0] == '/'
  fpath << [ fname, lookup.request.namespace ]

  extension = options[:extension] || @file_format::EXTENSION
  diskname_prefix = "#{::File.join(fpath.flatten).gsub(/\/$/, '')}"
  diskname = "#{diskname_prefix}.#{extension}"
  
  files = [ diskname ]
  files << list_fragments(diskname_prefix, extension)
  
  raw_data=""

  files.flatten.compact.each do |f|
    Jerakia.log.debug("read_from_file()  #{f}")
    raw_data << get_file_with_cache(f)
  end

  begin
    file_format.convert(raw_data)
  rescue Jerakia::FileParseError => e
    raise Jerakia::FileParseError, "While parsing #{diskname}: #{e.message}"
  end

end

#runObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/jerakia/datasource/file.rb', line 79

def run
  #
  # Do the lookup

  Jerakia.log.debug("Searching key #{lookup.request.key} from file format #{options[:format]} (#{whoami})")
  option :searchpath, { :type => Array,  :mandatory => true }
  option :format,     { :type => Symbol, :default => :yaml }
  option :docroot,    { :type => String, :default => "/etc/jerakia/data" }
  option :extension,  { :type => String }

  load_format_handler

  options[:searchpath].flatten.each do |path|
    Jerakia.log.debug("Attempting to load data from #{path}")
    return unless response.want?
    data=read_from_file(path)
    Jerakia.log.debug("Datasource provided #{data} looking for key #{lookup.request.key}")
    unless data[lookup.request.key].nil?
      Jerakia.log.debug("Found data #{data[lookup.request.key]}")
      response.submit data[lookup.request.key]
    end
  end
  
end