hiera-redis 0.2.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/hiera/backend/redis_backend.rb +89 -52
  2. metadata +23 -9
@@ -2,79 +2,66 @@ class Hiera
2
2
  module Backend
3
3
  class Redis_backend
4
4
 
5
- VERSION="0.2.2"
5
+ VERSION="1.0.0"
6
6
 
7
- attr_reader :redis
7
+ attr_reader :redis, :options
8
8
 
9
9
  def initialize
10
-
11
- require 'redis'
12
- Hiera.debug("Hiera Redis backend #{VERSION} starting")
13
-
14
- # default values
15
- options = {:host => 'localhost', :port => 6379, :db => 0, :password => nil, :timeout => 3, :path => nil}
16
-
17
- # config overrides default values
18
- options.each_key do |k|
19
- options[k] = Config[:redis][k] if Config[:redis].is_a?(Hash) && Config[:redis].has_key?(k)
20
- end
21
-
22
- @redis = Redis.new(options)
23
-
24
- @soft_connection_failure = false # Thus we are preventing the current behaviour
25
- @soft_connection_failure = Config[:redis][:soft_connection_failure] if Config[:redis].is_a?(Hash) && Config[:redis].has_key?(:soft_connection_failure)
10
+ Hiera.debug("Hiera Redis backend %s starting" % VERSION)
11
+ @redis = connect
26
12
  end
27
13
 
28
- def redis_query(args = {})
29
-
30
- # convert our seperator in order to maintain yaml compatibility
31
- redis_key = args[:source].gsub('/', ':')
32
-
33
- begin
34
- if redis.type(redis_key) == "hash"
35
- redis.hget(redis_key, args[:key])
36
- else
37
- redis_key << ":#{args[:key]}"
38
- case redis.type(redis_key)
39
- when "set"
40
- redis.smembers(redis_key)
41
- when "hash"
42
- redis.hgetall(redis_key)
43
- when "list"
44
- redis.lrange(redis_key, 0, -1)
45
- when "string"
46
- redis.get(redis_key)
47
- when "zset"
48
- redis.zrange(redis_key, 0, -1)
49
- else
50
- Hiera.debug("No such key: #{redis_key}")
51
- nil
52
- end
53
- end
54
- rescue Redis::CannotConnectError => e
55
- raise e unless @soft_connection_failure
56
- Hiera.warn("Cannot connect to Redis server")
57
- nil
14
+ def deserialize(args = {})
15
+ return nil if args[:string].nil?
16
+
17
+ Hiera.debug("Found %s key in %s source" % [args[:key], args[:source]])
18
+ return args[:string] unless options.include? :deserialize
19
+
20
+ case options[:deserialize]
21
+ when :json
22
+ Hiera.debug("Deserializing JSON")
23
+ require 'json'
24
+
25
+ JSON.parse(args[:string])
26
+ when :yaml
27
+ Hiera.debug("Deserializing YAML")
28
+ require 'yaml'
29
+ YAML::load(args[:string])
30
+ else
31
+ Hiera.warn("Invalid configuration for :deserialize; found %s" % options[:deserialize])
32
+ args[:string]
58
33
  end
34
+ # when we try to deserialize a string
35
+ rescue JSON::ParserError
36
+ args[:string]
37
+ rescue => e
38
+ Hiera.warn("Exception raised: %s: %s" % [e.class, e.message])
59
39
  end
60
40
 
61
41
  def lookup(key, scope, order_override, resolution_type)
42
+ answer = nil
62
43
 
63
- answer = Backend.empty_answer(resolution_type)
44
+ Hiera.debug("Looking up %s in Redis backend" % key)
64
45
 
65
46
  Backend.datasources(scope, order_override) do |source|
47
+ Hiera.debug("Looking for data source %s" % source)
66
48
 
67
- data = redis_query(:source => source, :key => key)
49
+ data = deserialize(:string => redis_query(:source => source, :key => key),
50
+ :source => source,
51
+ :key => key)
68
52
 
69
53
  next unless data
54
+
70
55
  new_answer = Backend.parse_answer(data, scope)
71
56
 
72
57
  case resolution_type
73
58
  when :array
74
- raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.is_a?(Array) or new_answer.is_a?(String)
59
+ raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.is_a? Array or new_answer.is_a? String
60
+ answer ||= []
75
61
  answer << new_answer
76
62
  when :hash
77
- raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.is_a?(Hash)
63
+ raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.is_a? Hash
64
+ answer ||= {}
78
65
  answer = new_answer.merge answer
79
66
  else
80
67
  answer = new_answer
@@ -84,6 +71,56 @@ class Hiera
84
71
 
85
72
  answer
86
73
  end
74
+
75
+ private
76
+
77
+ def connect
78
+
79
+ # override default options
80
+ @options = {
81
+ :host => 'localhost',
82
+ :port => 6379,
83
+ :db => 0,
84
+ :password => nil,
85
+ :timeout => 3,
86
+ :path => nil,
87
+ :soft_connection_failure => false,
88
+ :separator => ':',
89
+ :json => false
90
+ }.merge Config[:redis] || {}
91
+
92
+ require 'redis'
93
+
94
+ Redis.new(options)
95
+ rescue LoadError
96
+ retry if require 'rubygems'
97
+ end
98
+
99
+ def redis_query(args = {})
100
+
101
+ # convert our seperator in order to maintain yaml compatibility
102
+ redis_key = "%s" % [args[:source].split('/'), args[:key]].join(options[:separator])
103
+
104
+ case redis.type(redis_key)
105
+ when 'set'
106
+ redis.smembers(redis_key)
107
+ when 'hash'
108
+ redis.hgetall(redis_key)
109
+ when 'list'
110
+ redis.lrange(redis_key, 0, -1)
111
+ when 'string'
112
+ redis.get(redis_key)
113
+ when 'zset'
114
+ redis.zrange(redis_key, 0, -1)
115
+ else
116
+ Hiera.debug("No such key: %s" % redis_key)
117
+ nil
118
+ end
119
+ rescue Redis::CannotConnectError => e
120
+ Hiera.warn('Cannot connect to Redis server')
121
+ raise e unless options.has_key?(:soft_connection_failure)
122
+ nil
123
+ end
87
124
  end
88
125
  end
89
126
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiera-redis
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
+ - 1
7
8
  - 0
8
- - 2
9
- - 2
10
- version: 0.2.2
9
+ - 0
10
+ version: 1.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adam Kosmin c/o Reliant Security, Inc.
@@ -15,9 +15,23 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-07-23 00:00:00 Z
19
- dependencies: []
20
-
18
+ date: 2012-09-14 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hiera
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 15
29
+ segments:
30
+ - 1
31
+ - 0
32
+ version: "1.0"
33
+ type: :runtime
34
+ version_requirements: *id001
21
35
  description: Allows hiera functions to pull data from a Redis database.
22
36
  email: heira-redis@reliantsecurity.com
23
37
  executables: []
@@ -55,8 +69,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
69
  segments:
56
70
  - 0
57
71
  version: "0"
58
- requirements:
59
- - hiera
72
+ requirements: []
73
+
60
74
  rubyforge_project:
61
75
  rubygems_version: 1.8.15
62
76
  signing_key: