Class: MCollective::Util::Playbook::DataStores

Inherits:
Object
  • Object
show all
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

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

Parameters:

  • store (String)

    a store name

Raises:

  • (StandardError)

    for unknown stores



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

Parameters:

  • path (String)

    example data_store/key_name

Raises:

  • (StandardError)

    when reading from the key fails

  • (StandardError)

    for unknown stores



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

Parameters:

  • data (Hash)

    playbook format data

Returns:



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

Returns:

  • (Boolean)


153
154
155
# File 'lib/mcollective/util/playbook/data_stores.rb', line 153

def include?(store)
  @stores.include?(store)
end

#keysArray<String>

List of known store names

Returns:



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

Parameters:

  • path (String)

    example data_store/key_name

Raises:

  • (StandardError)

    when obtaining a lock fails

  • (StandardError)

    when obtaining a lock timesout

  • (StandardError)

    for unknown stores



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

Parameters:

Returns:

  • (Integer)


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

Parameters:

  • path (String)

    example data_store/key_name

Returns:

Raises:

  • (StandardError)

    when reading from the key fails

  • (StandardError)

    for unknown stores



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

Parameters:

  • path (String)

    example data_store/key_name

Returns:

Raises:

  • (StandardError)

    for incorrectly formatted paths



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

#prepareObject

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

Parameters:

  • path (String)

    example data_store/key_name

Returns:

  • (Object)

    data from the data store

Raises:

  • (StandardError)

    when reading from the key fails

  • (StandardError)

    for unknown stores



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

Parameters:

  • path (String)

    example data_store/key_name

Raises:

  • (StandardError)

    when reading from the key fails

  • (StandardError)

    for unknown stores



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

Parameters:

  • name (String)

    the store instance name

  • type (String)

    store type

Returns:

Raises:

  • (NameError)

    for unknown types



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

Parameters:

  • path (String)

    example data_store/key_name

Returns:

  • (Boolean)


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

#write(path, value) ⇒ Object

Writes to a path

Parameters:

  • path (String)

    example data_store/key_name

  • value (Object)

    data to write to the path

Raises:

  • (StandardError)

    when reading from the key fails

  • (StandardError)

    for unknown stores



63
64
65
66
67
68
# File 'lib/mcollective/util/playbook/data_stores.rb', line 63

def write(path, value)
  store, key = parse_path(path)

  Log.debug("Storing data in key %s within data store %s" % [key, store])
  self[store].write(key, value)
end