Class: MCollective::Util::Playbook::DataStores
- Inherits:
-
Object
- Object
- MCollective::Util::Playbook::DataStores
- Defined in:
- lib/mcollective/util/playbook/data_stores.rb,
lib/mcollective/util/playbook/data_stores/base.rb,
lib/mcollective/util/playbook/data_stores/etcd_data_store.rb,
lib/mcollective/util/playbook/data_stores/file_data_store.rb,
lib/mcollective/util/playbook/data_stores/shell_data_store.rb,
lib/mcollective/util/playbook/data_stores/consul_data_store.rb,
lib/mcollective/util/playbook/data_stores/environment_data_store.rb
Defined Under Namespace
Classes: Base, ConsulDataStore, EnvironmentDataStore, EtcdDataStore, FileDataStore, ShellDataStore
Instance Method Summary collapse
-
#[](store) ⇒ Object
Finds a named store instance.
-
#delete(path) ⇒ Object
Deletes a path.
-
#from_hash(data) ⇒ DataStores
Initialize the data stores from playbook data.
-
#include?(store) ⇒ Boolean
Determines if a store is known.
-
#initialize(playbook) ⇒ DataStores
constructor
A new instance of DataStores.
-
#keys ⇒ Array<String>
List of known store names.
-
#lock(path) ⇒ Object
Attempts to lock a named semaphore, waits until it succeeds.
-
#lock_timeout(store) ⇒ Integer
Retrieves the configured lock timeout for a store.
-
#members(path) ⇒ Array<String>
Members registered in a service.
-
#parse_path(path) ⇒ String
Parse a normal format data path.
-
#prepare ⇒ Object
Prepares all the stores.
-
#read(path) ⇒ Object
Reads from a path.
-
#release(path) ⇒ Object
Attempts to unlock a named semaphore.
-
#store_for(name, type) ⇒ DataStores::Base
Creates a store instance for a given type.
-
#valid_path?(path) ⇒ Boolean
Determines if a path is in the valid form and a known store.
-
#write(path, value) ⇒ Object
Writes to a path.
Constructor Details
#initialize(playbook) ⇒ DataStores
Returns a new instance of DataStores.
12 13 14 15 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 12 def initialize(playbook) @playbook = playbook @stores = {} end |
Instance Method Details
#[](store) ⇒ Object
Finds a named store instance
137 138 139 140 141 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 137 def [](store) raise("Unknown data store %s" % store) unless include?(store) @stores[store][:store] end |
#delete(path) ⇒ Object
Deletes a path
75 76 77 78 79 80 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 75 def delete(path) store, key = parse_path(path) Log.debug("Deleting key %s from data store %s" % [key, store]) self[store].delete(key) end |
#from_hash(data) ⇒ DataStores
Initialize the data stores from playbook data
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 182 def from_hash(data) @stores.clear data.each do |store, props| Log.debug("Loading data store %s" % [store]) @stores[store] = { :properties => props, :type => props["type"], :lock_timeout => Integer(props.fetch("timeout", 120)), :store => store_for(store, props["type"]) } end self end |
#include?(store) ⇒ Boolean
Determines if a store is known
153 154 155 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 153 def include?(store) @stores.include?(store) end |
#keys ⇒ Array<String>
List of known store names
146 147 148 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 146 def keys @stores.keys end |
#lock(path) ⇒ Object
Attempts to lock a named semaphore, waits until it succeeds
101 102 103 104 105 106 107 108 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 101 def lock(path) store, key = parse_path(path) timeout = lock_timeout(store) Log.debug("Obtaining lock %s on data store %s with timeout %d" % [key, store, timeout]) self[store].lock(key, timeout) end |
#lock_timeout(store) ⇒ Integer
Retrieves the configured lock timeout for a store
127 128 129 130 131 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 127 def lock_timeout(store) raise("Unknown data store %s" % store) unless include?(store) @stores[store][:lock_timeout] end |
#members(path) ⇒ Array<String>
Members registered in a service
88 89 90 91 92 93 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 88 def members(path) store, service = parse_path(path) Log.debug("Retrieving service members for service %s from data store %s" % [service, store]) self[store].members(service) end |
#parse_path(path) ⇒ String
Parse a normal format data path
33 34 35 36 37 38 39 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 33 def parse_path(path) if path =~ /^([a-zA-Z0-9\-_]+)\/(.+)$/ [$1, $2] else raise("Invalid data store path %s" % [path]) end end |
#prepare ⇒ Object
Prepares all the stores
158 159 160 161 162 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 158 def prepare @stores.each_value do |properties| properties[:store].from_hash(properties[:properties]).prepare end end |
#read(path) ⇒ Object
Reads from a path
47 48 49 50 51 52 53 54 55 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 47 def read(path) store, key = parse_path(path) Log.debug("Reading key %s from data store %s" % [key, store]) self[store].read(key) rescue raise("Could not read key %s: %s: %s" % [path, $!.class, $!.to_s]) end |
#release(path) ⇒ Object
Attempts to unlock a named semaphore
115 116 117 118 119 120 121 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 115 def release(path) store, key = parse_path(path) Log.debug("Releasing lock %s on data store %s" % [key, store]) self[store].release(key) end |
#store_for(name, type) ⇒ DataStores::Base
Creates a store instance for a given type
170 171 172 173 174 175 176 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 170 def store_for(name, type) klass_name = "%sDataStore" % type.capitalize DataStores.const_get(klass_name).new(name, @playbook) rescue NameError raise("Cannot find a handler for Data Store type %s" % type) end |
#valid_path?(path) ⇒ Boolean
Determines if a path is in the valid form and a known store
21 22 23 24 25 26 |
# File 'lib/mcollective/util/playbook/data_stores.rb', line 21 def valid_path?(path) store, _ = parse_path(path) include?(store) rescue false end |