Module: Jerakia::Datasource::File

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

Defined Under Namespace

Classes: Yaml

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_format ⇒ Object (readonly)

Returns the value of attribute file_format.



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

def file_format
  @file_format
end

Instance Method Details

#load_format_handler ⇒ Object



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

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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jerakia/datasource/file.rb', line 12

def read_from_file(fname)
  fpath = []
  fpath << options[:docroot] unless fname[0] == '/'
  fpath << [ fname, lookup.request.namespace ]
  diskname = ::File.join(fpath.flatten).gsub(/\/$/, '')

  Jerakia.log.debug("read_from_file() #{fname} using diskname #{diskname}")

  import_args=[]
  import_args << diskname
  import_args << options[:extension] if options[:extension]

  cache_index={ :diskname => diskname, :format => options[:format] }
  if options[:enable_caching]
    if in_bucket?(cache_index) 
      Jerakia.log.debug("returning cached data #{bucket[cache_index]}")
      bucket[cache_index]
    else
      Jerakia.log.debug("adding to cache #{@file_format.import_file(*import_args)}")
      bucket_add(cache_index,@file_format.import_file(*import_args))
    end
  else
    @file_format.import_file(*import_args)
  end
end

#run ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jerakia/datasource/file.rb', line 39

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].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}")
    if data[lookup.request.key]
      Jerakia.log.debug("Found data #{data[lookup.request.key]}")
      response.submit data[lookup.request.key]
    end
  end
  
end