jerakia 1.2.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,82 +0,0 @@
1
- require 'jerakia/cache/file'
2
-
3
- class Jerakia::Datasource
4
- module File
5
- attr_reader :file_format
6
-
7
- def load_format_handler
8
- format = options[:format] || :yaml
9
- class_name = format.to_s.capitalize
10
- require "jerakia/datasource/file/#{format}"
11
- @file_format = eval "Jerakia::Datasource::File::#{class_name}"
12
- end
13
-
14
- def cache
15
- Jerakia::Cache::File
16
- end
17
-
18
- def get_file_with_cache(diskname)
19
- if options[:enable_caching]
20
- Jerakia.log.debug("Querying cache for file #{diskname}")
21
- cache.retrieve(diskname)
22
- else
23
- ::File.read(diskname) if ::File.exists?(diskname)
24
- end
25
- end
26
-
27
- def list_fragments(prefix, extension)
28
- Dir["#{prefix}.d/*.#{extension}"] if ::File.directory?("#{prefix}.d")
29
- end
30
-
31
- def read_from_file(fname)
32
- fpath = []
33
- fpath << options[:docroot] unless fname[0] == '/'
34
- fpath << [fname, lookup.request.namespace]
35
-
36
- extension = options[:extension] || @file_format::EXTENSION
37
- diskname_prefix = ::File.join(fpath.flatten).gsub(/\/$/, '').to_s
38
- diskname = "#{diskname_prefix}.#{extension}"
39
-
40
- files = [diskname]
41
- files << list_fragments(diskname_prefix, extension)
42
-
43
- raw_data = ''
44
-
45
- files.flatten.compact.each do |f|
46
- Jerakia.log.debug("read_from_file() #{f}")
47
- file_contents = get_file_with_cache(f)
48
- raw_data << file_contents if file_contents
49
- end
50
-
51
- begin
52
- file_format.convert(raw_data)
53
- rescue Jerakia::FileParseError => e
54
- raise Jerakia::FileParseError, "While parsing #{diskname}: #{e.message}"
55
- end
56
- end
57
-
58
- def run
59
- #
60
- # Do the lookup
61
-
62
- Jerakia.log.debug("Searching key #{lookup.request.key} from file format #{options[:format]} (#{whoami})")
63
- option :searchpath, :type => Array, :mandatory => true
64
- option :format, :type => Symbol, :default => :yaml
65
- option :docroot, :type => String, :default => '/etc/jerakia/data'
66
- option :extension, :type => String
67
-
68
- load_format_handler
69
-
70
- options[:searchpath].flatten.each do |path|
71
- Jerakia.log.debug("Attempting to load data from #{path}")
72
- return unless response.want?
73
- data = read_from_file(path)
74
- Jerakia.log.debug("Datasource provided #{data} looking for key #{lookup.request.key}")
75
- unless data[lookup.request.key].nil?
76
- Jerakia.log.debug("Found data #{data[lookup.request.key]}")
77
- response.submit data[lookup.request.key]
78
- end
79
- end
80
- end
81
- end
82
- end
@@ -1,23 +0,0 @@
1
- class Jerakia
2
- class Policy
3
- class Registry
4
-
5
- @instances = {}
6
-
7
- class << self
8
- attr_reader :instances
9
-
10
- def register(name, instance)
11
- @instances[name] = instance unless @instances[name]
12
- end
13
- end
14
-
15
- attr_reader :instance
16
-
17
- def initialize(name, instance)
18
- @instance = instance
19
- self.class.register(name, self)
20
- end
21
- end
22
- end
23
- end
@@ -1,33 +0,0 @@
1
- require 'puppet/indirector/code'
2
- require 'jerakia'
3
- require 'json'
4
-
5
- class Puppet::DataBinding::Jerakia < Puppet::Indirector::Code
6
- desc 'Data binding for Jerakia'
7
-
8
- attr_reader :jerakia
9
- attr_reader :policy
10
-
11
- def initialize(*args)
12
- @jerakia = ::Jerakia.new
13
- @default_policy = 'default'
14
- super
15
- end
16
-
17
- def find(request)
18
- lookupdata = request.key.split(/::/)
19
- key = lookupdata.pop
20
- namespace = lookupdata
21
- metadata = request.options[:variables].to_hash.reject { |_k, v| v.is_a?(Puppet::Resource) }
22
- policy = metadata['jerakia_policy'] || @default_policy
23
- jacreq = Jerakia::Request.new(
24
- :key => key,
25
- :namespace => namespace,
26
- :policy => policy,
27
- :lookup_type => :first,
28
- :metadata => metadata
29
- )
30
- answer = jerakia.lookup(jacreq)
31
- answer.payload
32
- end
33
- end
@@ -1,44 +0,0 @@
1
- # This is a proof of concept, and highly experimental to enable a data binding
2
- # that talks to Jerakia's REST API. At present we only pass the environment
3
- # from the scope as the whole scope hash is too large for the HTTP request.
4
- #
5
- # This may or may not be supported in future versions but feel free to contribute :)
6
- #
7
- require 'puppet/indirector/code'
8
- require 'rest_client'
9
- require 'jerakia'
10
- require 'json'
11
-
12
- class Puppet::DataBinding::Jerakia_rest < Puppet::Indirector::Code
13
- desc 'Data binding for Jerakia'
14
-
15
- attr_reader :jerakia
16
- attr_reader :jerakia_url
17
- attr_reader :policy
18
-
19
- def initialize(*args)
20
- @jerakia = ::Jerakia.new
21
- @jerakia_url = @jerakia.config.server_url
22
- @policy = 'puppet'
23
- super
24
- end
25
-
26
- def find(request)
27
- lookupdata = request.key.split(/::/)
28
- key = lookupdata.pop
29
- namespace = lookupdata
30
-
31
- # metadata = request.options[:variables].to_hash
32
-
33
- metadata = {
34
- :environment => request.options[:variables].environment
35
- }
36
- payload = {
37
- :namespace => namespace,
38
- :lookup_type => :first,
39
- :metadata => metadata
40
- }.to_json
41
- response = RestClient.get "#{jerakia_url}/#{policy}/#{key}", :params => { :payload => payload }
42
- response
43
- end
44
- end