Module: Jerakia::Datasource::File

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

Defined Under Namespace

Classes: Json, Yaml

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_formatObject (readonly)

Returns the value of attribute file_format.



5
6
7
# File 'lib/jerakia/datasource/file.rb', line 5

def file_format
  @file_format
end

Instance Method Details

#cacheObject



14
15
16
# File 'lib/jerakia/datasource/file.rb', line 14

def cache
  Jerakia::Cache::File
end

#get_file_with_cache(diskname) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/jerakia/datasource/file.rb', line 18

def get_file_with_cache(diskname)
  if options[:enable_caching]
    Jerakia.log.debug("Querying cache for file #{diskname}")
    cache.retrieve(diskname)
  else
    ::File.read(diskname) if ::File.exists?(diskname)
  end
end

#list_fragments(prefix, extension) ⇒ Object



27
28
29
# File 'lib/jerakia/datasource/file.rb', line 27

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

#load_format_handlerObject



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

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

#read_from_file(fname) ⇒ Object



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
# File 'lib/jerakia/datasource/file.rb', line 31

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(/\/$/, '').to_s
  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}")
    file_contents = get_file_with_cache(f)
    raw_data << file_contents if file_contents
  end

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

#runObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jerakia/datasource/file.rb', line 58

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