Skip to content

Commit

Permalink
(choria-io#102) support arguments to scripts used in shell nodes
Browse files Browse the repository at this point in the history
Now uses the M::Shell system to run the commands and additionally checks
for exit code 0, only accepts STDOUT specifically
  • Loading branch information
ripienaar committed Dec 26, 2016
1 parent 6398256 commit 91d7f11
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
|Date |Issue |Description |
|----------|------|---------------------------------------------------------------------------------------------------------|
|2016/12/26|102 |Support arguments to commands ran by the shell node set in playbooks |
|2016/12/26| |Release 0.0.12 |
|2016/12/26|104 |Support posting messages to slack as a task step |
|2016/12/25|95 |Support running shell commands as task steps |
Expand Down
12 changes: 10 additions & 2 deletions lib/mcollective/util/playbook/nodes/shell_nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def validate_configuration!

def from_hash(data)
@script = data["script"]
@_data = nil

self
end
Expand All @@ -28,10 +29,17 @@ def valid_hostname?(host)
def data
return @_data if @_data

@_data = `#{@script}`.lines.map do |line|
shell = Shell.new(@script, "timeout" => 10)
shell.runcommand

exitcode = shell.status.exitstatus

raise("Could not disocver nodes via shell method, command exited with code %d" % [exitcode]) unless exitcode == 0

@_data = shell.stdout.lines.map do |line|
line.chomp!

raise("%s is not a valid certname" % line) unless valid_hostname?(line)
raise("%s is not a valid hostname" % line) unless valid_hostname?(line)

line
end
Expand Down
10 changes: 9 additions & 1 deletion spec/unit/mcollective/util/playbook/nodes/shell_nodes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ class Nodes
describe "#data" do
it "should detect corrupt data" do
nodes.from_hash("script" => corrupt_fixture)
expect { nodes.data }.to raise_error("node1 example.net is not a valid certname")
expect { nodes.data }.to raise_error("node1 example.net is not a valid hostname")
end

it "should only accept outputs that exit with status 0" do
nodes.from_hash("script" => "echo 'example.net';exit 0")
expect(nodes.data).to eq(["example.net"])

nodes.from_hash("script" => "echo 'example.net';exit 1")
expect { nodes.data }.to raise_error("Could not disocver nodes via shell method, command exited with code 1")
end
end

Expand Down

0 comments on commit 91d7f11

Please sign in to comment.