hiera_zookeeper 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,18 +15,41 @@ class Hiera
15
15
  def lookup(key, scope, order_override, resolution_type)
16
16
  answer = nil
17
17
  Hiera.debug("Looking up #{key} in ZooKeeper backend")
18
- # Select a server
18
+ # Establish server(s) to check
19
+ servers = []
19
20
  unless Config[:zookeeper].nil? or Config[:zookeeper][:server].nil? then
20
- if Config[:zookeeper][:server].class == Array then
21
- # Note: not using Array#sample to keep it backwards compatible with ruby 1.8.7
22
- server = Backend.parse_string(Config[:zookeeper][:server][rand(Config[:zookeeper][:server].length)], scope)
21
+ if Config[:zookeeper][:server].kind_of?(Array) then
22
+ Config[:zookeeper][:server].shuffle!
23
+ Config[:zookeeper][:server].each do |server|
24
+ servers.push(Backend.parse_string(server, scope))
25
+ end
23
26
  else
24
- server = Backend.parse_string(Config[:zookeeper][:server], scope)
27
+ servers.push(Backend.parse_string(Config[:zookeeper][:server], scope))
25
28
  end
26
29
  else
27
- server = 'localhost:2181'
30
+ servers.push('localhost:2181')
28
31
  end
29
- zk = Zookeeper.new(server)
32
+ # Establish connection timeout
33
+ if Config[:zookeeper].nil? or Config[:zookeeper][:timeout].nil? then
34
+ timeout = 1
35
+ else
36
+ timeout = Config[:zookeeper][:timeout]
37
+ end
38
+ # Establish connection
39
+ zk = nil
40
+ servers.each do |s|
41
+ begin
42
+ zk = Zookeeper.new(s, timeout)
43
+ break if zk.connected?
44
+ rescue => e
45
+ Hiera.debug(e.message)
46
+ next
47
+ end
48
+ end
49
+ # If it did not work, raise that exception!
50
+ if zk.nil? or not zk.connected? then
51
+ raise(ZookeeperExceptions::ZookeeperException, "Could not connect to any server in configuration")
52
+ end
30
53
 
31
54
  Backend.datasources(scope, order_override) do |source|
32
55
  Hiera.debug("Looking for data source #{source}")
@@ -43,11 +66,11 @@ class Hiera
43
66
  end
44
67
  case resolution_type
45
68
  when :array
46
- raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of?(Array) or new_answer.kind_of?(String)
69
+ raise(Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}") unless new_answer.kind_of?(Array) or new_answer.kind_of?(String)
47
70
  answer ||= []
48
71
  answer << new_answer
49
72
  when :hash
50
- raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of?(Hash)
73
+ raise(Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}") unless new_answer.kind_of?(Hash)
51
74
  answer ||= {}
52
75
  answer = new_answer.merge(answer)
53
76
  else
@@ -55,12 +78,8 @@ class Hiera
55
78
  break
56
79
  end
57
80
  end
58
- zk.close if !zk.nil?
81
+ zk.close if not zk.nil? and zk.connected?
59
82
  return answer
60
-
61
- #rescue ZookeeperExceptions::ZookeeperException, RuntimeError
62
- # zk.close if !zk.nil?
63
- # return nil
64
83
  end
65
84
  end
66
85
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiera_zookeeper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - zahna
@@ -93,6 +93,6 @@ rubyforge_project:
93
93
  rubygems_version: 1.8.24
94
94
  signing_key:
95
95
  specification_version: 3
96
- summary: Hiera_zookeeper is a plugin for the Hiera database to get data from Zookeeper
96
+ summary: A backend plugin to Hiera to enable it to reference data from Zookeeper.
97
97
  test_files: []
98
98