This repository has been archived by the owner on Feb 9, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from christian-blades-cb/kick_start
Support ad-hoc chef runs
- Loading branch information
Showing
12 changed files
with
398 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module SilverSpurs | ||
class NodeNotFoundException < Exception | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'singleton' | ||
require 'forwardable' | ||
require 'ridley' | ||
require 'silver_spurs/chef_exceptions' | ||
|
||
module SilverSpurs | ||
class ChefInterface | ||
|
||
def initialize(options) | ||
@chef_config = options | ||
end | ||
|
||
def chef_run(node_name, run_list = []) | ||
node = find_node node_name | ||
if run_list.size > 0 | ||
command = "sudo chef-client -o '#{run_list.join(',')}'" | ||
ridley.node.execute_command node.public_hostname, command | ||
else | ||
node.chef_run | ||
end | ||
end | ||
|
||
private | ||
|
||
def ridley | ||
@ridley ||= Ridley.new(@chef_config) | ||
end | ||
|
||
def find_node(node_name) | ||
node = ridley.node.find(node_name) | ||
raise NodeNotFoundException.new unless node | ||
node | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'rest-client' | ||
require 'silver_spurs/client/exceptions' | ||
require 'erb' | ||
|
||
module SilverSpurs | ||
class ChefRun | ||
|
||
attr_reader :log, :status | ||
|
||
def initialize(response) | ||
@status = convert_status response | ||
@log = prettify_log response | ||
end | ||
|
||
private | ||
|
||
def convert_status(response) | ||
code = response[0] | ||
case code | ||
when 'ok' | ||
:success | ||
when 'error' | ||
:failed | ||
end | ||
end | ||
|
||
def prettify_log(response) | ||
run_info = response[1] | ||
stdout = run_info['stdout'] | ||
stderr = run_info['stderr'] | ||
exit_code = run_info['exit_code'] || run_info['exit_status'] | ||
|
||
template = ERB.new <<-END | ||
Exit Code: <%= exit_code %> | ||
--STDOUT----------------- | ||
<%= stdout %> | ||
--STDERR----------------- | ||
<%= stderr %> | ||
END | ||
template.result binding | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module SilverSpurs | ||
VERSION = "1.0.2" | ||
VERSION = "2.0.0.rc1" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
require 'spec_helper' | ||
require 'ridley' | ||
|
||
describe SilverSpurs::ChefInterface do | ||
|
||
describe :chef_run do | ||
|
||
before :each do | ||
@chef_i = SilverSpurs::ChefInterface.new({}) | ||
@ridley = double('ridley') | ||
@node_resource = double('node_resource') | ||
@node_obj = double('node_obj') | ||
|
||
@chef_i.stub(:ridley).and_return @ridley | ||
@ridley.stub(:node).and_return @node_resource | ||
end | ||
|
||
it 'finds a node and then launches a chef run' do | ||
@chef_i.should_receive(:find_node).and_return @node_obj | ||
@node_obj.should_receive(:chef_run) | ||
|
||
@chef_i.chef_run 'node' | ||
end | ||
|
||
context 'with a run list' do | ||
before :each do | ||
@chef_i.stub(:find_node).and_return @node_obj | ||
end | ||
|
||
after :each do | ||
@chef_i.chef_run 'node_name', ['recipe[one]', 'recipe[two]'] | ||
end | ||
|
||
it 'should call execute off of the node resource' do | ||
@node_obj.stub(:public_hostname).and_return 'hostname' | ||
@node_resource.should_receive(:execute_command) | ||
end | ||
|
||
it 'should pass in the host name from the node object' do | ||
@node_obj.should_receive(:public_hostname).and_return 'hostname' | ||
@node_resource.should_receive(:execute_command).with('hostname', anything) | ||
end | ||
|
||
it 'should pass in the run list as a comma-seperated string' do | ||
@node_obj.stub(:public_hostname).and_return 'hostname' | ||
@node_resource.should_receive(:execute_command).with(anything, %r{'recipe\[one\],recipe\[two\]'}) | ||
end | ||
end | ||
|
||
context 'without a run list' do | ||
after :each do | ||
@chef_i.chef_run 'node_name' | ||
end | ||
|
||
before :each do | ||
@chef_i.stub(:find_node).and_return @node_obj | ||
end | ||
|
||
it 'should call chef_run off of the node object' do | ||
@node_obj.should_receive :chef_run | ||
end | ||
end | ||
|
||
end | ||
|
||
describe :find_node do | ||
|
||
before :each do | ||
@chef_i = SilverSpurs::ChefInterface.new({}) | ||
@ridley = double('ridley') | ||
@node_resource = double('node_resource') | ||
|
||
@chef_i.stub(:ridley).and_return @ridley | ||
@ridley.stub(:node).and_return @node_resource | ||
end | ||
|
||
def call_find_node | ||
@chef_i.send(:find_node, 'node_name') | ||
end | ||
|
||
it 'asks ridley for the node' do | ||
@node_resource.should_receive(:find).with('node_name').and_return double('node_obj') | ||
call_find_node | ||
end | ||
|
||
context 'when node exists' do | ||
before :each do | ||
@node_obj = double('node_obj') | ||
@node_resource.stub(:find).and_return @node_obj | ||
end | ||
|
||
it 'should return the node' do | ||
call_find_node.should eq @node_obj | ||
end | ||
end | ||
|
||
context 'when node does not exist' do | ||
before :each do | ||
@node_resource.stub(:find).and_return nil | ||
end | ||
|
||
it 'should throw an error' do | ||
expect { call_find_node }.to raise_error | ||
end | ||
end | ||
|
||
end | ||
|
||
describe :ridley do | ||
before :each do | ||
@chef_i = SilverSpurs::ChefInterface.new({}) | ||
end | ||
|
||
def call_ridley | ||
@chef_i.send :ridley | ||
end | ||
|
||
context 'when @ridley has not been initialized' do | ||
it 'creates a new Ridley instance' do | ||
Ridley.should_receive(:new) | ||
call_ridley | ||
end | ||
end | ||
|
||
context 'when @ridley has been initialized' do | ||
it 're-uses the old ridley' do | ||
Ridley.should_receive(:new).once.and_return double('ridley') | ||
call_ridley | ||
call_ridley | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
Oops, something went wrong.