Skip to content

Commit

Permalink
Merge pull request choria-io#108 from ripienaar/102
Browse files Browse the repository at this point in the history
(choria-io#102) support arguments to scripts used in shell nodes
  • Loading branch information
ripienaar authored Dec 26, 2016
2 parents 6398256 + 078a8f8 commit 0f28f94
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 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
2 changes: 1 addition & 1 deletion lib/mcollective/util/playbook/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def prepare
def resolve_nodes(node_set)
node_props = @nodes[node_set]
node_props[:resolver].prepare
node_props[:discovered] = node_props[:resolver].discover
node_props[:discovered] = node_props[:resolver].discover.uniq
end

# Checks if the agents on the nodes matches the desired versions
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
2 changes: 1 addition & 1 deletion spec/unit/mcollective/util/playbook/nodes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Playbook
seq = sequence(:nodes)
resolver = nodes.nodes["load_balancers"][:resolver]
resolver.expects(:prepare).in_sequence(seq)
resolver.expects(:discover).in_sequence(seq).returns(["rspec1", "rspec2"])
resolver.expects(:discover).in_sequence(seq).returns(["rspec1", "rspec2", "rspec1"])

nodes.resolve_nodes("load_balancers")

Expand Down

0 comments on commit 0f28f94

Please sign in to comment.