Class: Hiera::Backend::Zookeeper_backend
- Inherits:
-
Object
- Object
- Hiera::Backend::Zookeeper_backend
- Defined in:
- lib/hiera/backend/zookeeper_backend.rb
Instance Method Summary collapse
-
#initialize ⇒ Zookeeper_backend
constructor
ZooKeeper backend for Hiera.
- #lookup(key, scope, order_override, resolution_type) ⇒ Object
Constructor Details
#initialize ⇒ Zookeeper_backend
ZooKeeper backend for Hiera. Recognizes :server and :datadir config options. If no :server is specified, uses localhost:2181. The node data is expected to be in YAML or JSON (YAML is the default).
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/hiera/backend/zookeeper_backend.rb', line 7 def initialize require 'rubygems' require 'zookeeper' # Set default values Config[:zookeeper] = Hash.new() if Config[:zookeeper].nil? Config[:zookeeper][:timeout] = 1 if Config[:zookeeper][:timeout].nil? Config[:zookeeper][:datadir] = '/hiera' if Config[:zookeeper][:datadir].nil? Config[:zookeeper][:format] = 'yaml' if Config[:zookeeper][:format].nil? Config[:zookeeper][:format].downcase! if Config[:zookeeper][:server].nil? then Config[:zookeeper][:servers] = ['localhost:2181'] if Config[:zookeeper][:servers].nil? else Hiera.warn("The :server option is deprecated. Use :servers. Option :server will go away very soon.") Config[:zookeeper][:servers] = Config[:zookeeper][:server] end case Config[:zookeeper][:format] when 'json' require 'json' else require 'yaml' end Hiera.debug("Hiera ZooKeeper backend starting") end |
Instance Method Details
#lookup(key, scope, order_override, resolution_type) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/hiera/backend/zookeeper_backend.rb', line 33 def lookup(key, scope, order_override, resolution_type) answer = nil Hiera.debug("Looking up #{key} in ZooKeeper backend") # Establish server(s) to check servers = [] Config[:zookeeper][:servers].shuffle! Config[:zookeeper][:servers].each do |s| servers.push(Backend.parse_string(s, scope)) end # Establish connection zk = nil servers.each do |s| begin zk = Zookeeper.new(s, Config[:zookeeper][:timeout]) break if zk.connected? rescue => e Hiera.debug(e.) next end end # If it did not work, raise that exception! if zk.nil? or not zk.connected? then raise(ZookeeperExceptions::ZookeeperException, "Could not connect to any zookeeper server in configuration") end Backend.datasources(scope, order_override) do |source| Hiera.debug("Looking for data source #{source}") # Determine the zookeeper path to get data from datadir = "#{Backend.parse_string(Config[:zookeeper][:datadir], scope)}/#{source}" data = zk.get(:path => "#{datadir}/#{key}") if data[:stat].exists then Hiera.debug("Found #{key} in #{source}") case Config[:zookeeper][:format] when 'json' new_answer = Backend.resolve_answer(JSON.load(data[:data]), resolution_type) else new_answer = Backend.resolve_answer(YAML.load(data[:data]), resolution_type) end case resolution_type when :array raise(Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}") unless new_answer.kind_of?(Array) or new_answer.kind_of?(String) answer ||= [] answer << new_answer when :hash raise(Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}") unless new_answer.kind_of?(Hash) answer ||= {} answer = new_answer.merge(answer) else answer = new_answer break end end end zk.close if not zk.nil? and zk.connected? return answer end |