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

Inherits:
Base
  • Object
show all
Defined in:
lib/mcollective/util/playbook/data_stores/file_data_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #lock, #members, #prepare, #release

Constructor Details

This class inherits a constructor from MCollective::Util::Playbook::DataStores::Base

Instance Attribute Details

#createObject

Returns the value of attribute create.



11
12
13
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 11

def create
  @create
end

#fileObject

Returns the value of attribute file.



11
12
13
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 11

def file
  @file
end

#formatObject

Returns the value of attribute format.



11
12
13
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 11

def format
  @format
end

Instance Method Details

#dataObject



43
44
45
46
47
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 43

def data
  @file_mutex.synchronize do
    parse_data
  end
end

#delete(key) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 89

def delete(key)
  @file_mutex.synchronize do
    raw_data = parse_data

    raw_data.delete(key)

    save_data(raw_data)
  end
end

#from_hash(properties) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 17

def from_hash(properties)
  @file = properties["file"]
  @format = properties["format"]
  @create = !!properties["create"]

  validate_configuration!

  self
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 99

def include?(key)
  data.include?(key)
end

#parse_dataObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 49

def parse_data
  return({}) if File.size(@file) == 0

  case @format
  when "json"
    JSON.parse(File.read(@file))
  when "yaml"
    YAML.load(File.read(@file))
  end
end

#read(key) ⇒ Object



71
72
73
74
75
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 71

def read(key)
  raise("No such key %s" % [key]) unless include?(key)

  data[key]
end

#save_data(raw_data) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 60

def save_data(raw_data)
  File.open(@file, "w") do |f|
    case @format
    when "json"
      f.print(JSON.dump(raw_data))
    when "yaml"
      f.print(YAML.dump(raw_data))
    end
  end
end

#startup_hookObject



13
14
15
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 13

def startup_hook
  @file_mutex = Mutex.new
end

#validate_configuration!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 27

def validate_configuration!
  raise("No file given to use as data source") unless @file
  raise("No file format given") unless @format
  raise("File format has to be one of 'json' or 'yaml'") unless ["json", "yaml"].include?(@format)

  @file = File.expand_path(@file)

  FileUtils.touch(@file) if @create && !File.exist?(@file)

  raise("Cannot find data file %s" % @file) unless File.exist?(@file)
  raise("Cannot read data file %s" % @file) unless File.readable?(@file)
  raise("Cannot write data file %s" % @file) unless File.writable?(@file)

  raise("The data file must contain a Hash or be empty") unless data.is_a?(Hash)
end

#write(key, value) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mcollective/util/playbook/data_stores/file_data_store.rb', line 77

def write(key, value)
  @file_mutex.synchronize do
    raw_data = parse_data

    raw_data[key] = value

    save_data(raw_data)
  end

  read(key)
end