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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

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

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



8
9
10
# File 'lib/mcollective/util/playbook/data_stores/shell_data_store.rb', line 8

def command
  @command
end

#cwdObject (readonly)

Returns the value of attribute cwd.



8
9
10
# File 'lib/mcollective/util/playbook/data_stores/shell_data_store.rb', line 8

def cwd
  @cwd
end

#environmentObject (readonly)

Returns the value of attribute environment.



8
9
10
# File 'lib/mcollective/util/playbook/data_stores/shell_data_store.rb', line 8

def environment
  @environment
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



8
9
10
# File 'lib/mcollective/util/playbook/data_stores/shell_data_store.rb', line 8

def timeout
  @timeout
end

Instance Method Details

#delete(key) ⇒ Object



16
17
18
19
20
# File 'lib/mcollective/util/playbook/data_stores/shell_data_store.rb', line 16

def delete(key)
  run("delete", key)

  nil
end

#from_hash(properties) ⇒ Object



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

def from_hash(properties)
  @command = properties["command"]
  @timeout = properties.fetch("timeout", 10)
  @environment = properties.fetch("environment", {})
  @cwd = properties["cwd"]

  self
end

#read(key) ⇒ Object



22
23
24
# File 'lib/mcollective/util/playbook/data_stores/shell_data_store.rb', line 22

def read(key)
  run("read", key).stdout.chomp
end

#run(action, key, environment = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mcollective/util/playbook/data_stores/shell_data_store.rb', line 26

def run(action, key, environment={})
  validate_key(key)

  command = "%s --%s" % [@command, action]
  options = shell_options

  options["environment"].merge!(
    environment.merge(
      "CHORIA_DATA_KEY" => key,
      "CHORIA_DATA_ACTION" => action
    )
  )

  shell = run_command(command, options)

  unless shell.status.exitstatus == 0
    Log.warn("While running command %s: %s" % [command, shell.stderr])
    raise("Could not %s key %s, got exitcode %d" % [action, key, shell.status.exitstatus])
  end

  shell
end

#run_command(command, options) ⇒ Object



49
50
51
52
53
# File 'lib/mcollective/util/playbook/data_stores/shell_data_store.rb', line 49

def run_command(command, options)
  shell = Shell.new(command, options)
  shell.runcommand
  shell
end

#shell_optionsObject



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

def shell_options
  unless @__options
    @__options = {}
    @__options["cwd"] = @cwd if @cwd
    @__options["environment"] = @environment
    @__options["timeout"] = Integer(@timeout)
  end

  # bacause environment is being edited
  Marshal.load(Marshal.dump(@__options))
end

#validate_configuration!Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mcollective/util/playbook/data_stores/shell_data_store.rb', line 70

def validate_configuration!
  raise("A command is required") unless @command
  raise("Command %s is not executable" % @command) unless File.executable?(@command)
  raise("Timeout should be an integer") unless @timeout.to_i.to_s == @timeout.to_s

  if @environment
    raise("Environment should be a hash") unless @environment.is_a?(Hash)

    all_strings = @environment.map {|k, v| k.is_a?(String) && v.is_a?(String)}.all?
    raise("All keys and values in the environment must be strings") unless all_strings
  end

  if @cwd
    raise("cwd %s does not exist" % @cwd) unless File.exist?(@cwd)
    raise("cwd %s is not a directory" % @cwd) unless File.directory?(@cwd)
  end
end

#validate_key(key) ⇒ Object



55
56
57
58
59
# File 'lib/mcollective/util/playbook/data_stores/shell_data_store.rb', line 55

def validate_key(key)
  raise("Valid keys must match ^[a-zA-Z0-9_-]+$") unless key =~ /^[a-zA-Z0-9_-]+$/

  true
end

#write(key, value) ⇒ Object



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

def write(key, value)
  run("write", key, "CHORIA_DATA_VALUE" => value)

  nil
end