Skip to content

Commit

Permalink
Merge branch 'feature/chef_support'
Browse files Browse the repository at this point in the history
  • Loading branch information
danryan committed Sep 2, 2012
2 parents 5a1bff3 + 6a991ab commit 8567f62
Show file tree
Hide file tree
Showing 19 changed files with 205 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
/tmp
.env
mastermind.wiki
examples
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.2.0

- Chef API support (via spice)
- new `run_ssh_multi` participant

## 0.1.0

- definitions are now stored in the database.
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ gem 'cabin', '~> 0.4.4'
gem 'fog', '~> 1.5.0'
gem 'tinder'
gem 'net-ssh'
gem 'net-ssh-multi'
gem 'spice'

# Queue processing
gem 'sidekiq', '~> 2.1.1'
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,19 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.19)
mixlib-authentication (1.3.0)
mixlib-log
mixlib-log (1.4.1)
multi_json (1.3.6)
multipart-post (1.1.5)
net-scp (1.0.4)
net-ssh (>= 1.99.1)
net-ssh (2.5.2)
net-ssh-gateway (1.1.0)
net-ssh (>= 1.99.1)
net-ssh-multi (1.1)
net-ssh (>= 2.1.4)
net-ssh-gateway (>= 0.99.0)
nokogiri (1.5.5)
paper_trail (2.6.3)
activerecord (~> 3.0)
Expand Down Expand Up @@ -284,6 +292,10 @@ GEM
ruby2ruby (>= 1.2.5)
ruby_parser (>= 2.0.5)
sexp_processor (>= 3.0.5)
spice (1.0.4)
faraday (~> 0.8.0)
mixlib-authentication (>= 1.1.4)
multi_json (~> 1.3.6)
spork (1.0.0rc3)
sprockets (2.1.3)
hike (~> 1.2)
Expand Down Expand Up @@ -358,6 +370,7 @@ DEPENDENCIES
json_spec (>= 1.0.3)
multi_json (~> 1.3.6)
net-ssh
net-ssh-multi
paper_trail
pg
rack-test (>= 0.6.0)
Expand All @@ -372,6 +385,7 @@ DEPENDENCIES
shoulda-matchers (>= 1.0.0)
sidekiq (~> 2.1.1)
simple_form
spice
spork (>= 1.0.0rc2)
state_machine
tach (>= 0.0.8)
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ Definition.new(
}
).to_pdef

# Same as:
Definition.new(
:name => "plain ol' ruby!",
:content => %q{
run_ssh :host => "host1.example.com",
:user => '${user}',
:key_data => '${key_data}',
:command => '${command}'
run_ssh :host => "host2.example.com",
:user => '${user}',
:key_data => '${key_data}',
:command => '${command}'
run_ssh :host => "host3.example.com",
:user => '${user}',
:key_data => '${key_data}',
:command => '${command}'
}
).to_pdef

# => compiled definition
#
# ["define",
Expand Down
11 changes: 1 addition & 10 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
## Definitions

* definition names are based on their file names.
* example definition: (no more `Mastermind.define do`)


* defs are read in from the filesystem and parsed by the Mastermind::Reader


!! OR !!
# Allow specifying of a custom key in which to hold participant results.

21 changes: 21 additions & 0 deletions app/participants/cm/chef.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spice'

module Participant::CM
class Chef < Participant

# Not a real participant. Just a base class to inherit from.

option :server_url, ENV['CHEF_SERVER_URL']
option :client_name, ENV['CHEF_CLIENT_NAME']
option :client_key, Spice.read_key_file(File.expand_path(ENV['CHEF_CLIENT_KEY']))

def connection
Spice::Connection.new(
server_url: options[:server_url],
client_name: options[:client_name],
client_key: options[:client_key]
)
end

end
end
19 changes: 19 additions & 0 deletions app/participants/cm/chef/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'spice'
require Rails.root + 'app/participants/cm/chef'

module Participant::CM
class Chef::Node < Chef
option :server_url, ENV['CHEF_SERVER_URL']
option :client_name, ENV['CHEF_CLIENT_NAME']
option :client_key, Spice.read_key_file(File.expand_path(ENV['CHEF_CLIENT_KEY']))

register :chef_node

action :list do
chef_nodes = connection.get('/nodes').keys

{ chef_nodes: chef_nodes }
end

end
end
72 changes: 72 additions & 0 deletions app/participants/remote/ssh_multi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'net/ssh'

module Participant::Remote
class SSHMulti < Participant
register :ssh_multi

action :run do
requires :hosts, :user, :key_data, :command

output = run_ssh(target.command)

{ output: output }
end


def run_ssh(command)
output = {}

session = Net::SSH::Multi.start()

opts = {}
opts[:user] = target.user
opts[:key_data] = [target.key_data]

target.hosts.each do |host|
session.use(host, opts)
end

session.open_channel do |ch|
ch.request_pty
ch.exec command do |ch, success|
raise ArgumentError, "Cannot execute #{command}" unless success
ch.on_data do |ichannel, data|
output[ichannel[:host]] = data
end
end
end

session.loop
session.close
return output if output
end

def tcp_test_ssh(hostname)
tcp_socket = TCPSocket.new(hostname, 22)
readable = IO.select([tcp_socket], nil, nil, 5)
if readable
Mastermind.logger.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
yield
true
else
false
end
rescue SocketError
sleep 2
false
rescue Errno::ETIMEDOUT
false
rescue Errno::EPERM
false
rescue Errno::ECONNREFUSED
sleep 2
false
# This happens on EC2 quite often
rescue Errno::EHOSTUNREACH
sleep 2
false
ensure
tcp_socket && tcp_socket.close
end
end
end
12 changes: 12 additions & 0 deletions app/participants/server/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,17 @@ def connection(region=nil)
target.attributes
end

action :reboot do
requires :instance_id

server = connection(target.region).servers.get(target.instance_id)
server.reboot

server.wait_for { state == 'running' }

target.attributes = server.attributes
target.attributes
end

end
end
7 changes: 7 additions & 0 deletions app/targets/cm/chef.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Target::CM
class Chef < Target

# not a real target

end
end
18 changes: 18 additions & 0 deletions app/targets/cm/chef/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require Rails.root + 'app/targets/cm/chef'

module Target::CM
class Chef::Node < Target
register :chef_node

attribute :name, type: String
attribute :run_list, type: Object, default: []
attribute :automatic, type: Object, default: {}
attribute :default, type: Object, default: {}
attribute :normal, type: Object, default: {}
attribute :override, type: Object, default: {}
attribute :_rev, type: String
attribute :chef_type, type: String, default: "node"
attribute :json_class, type: String, default: "Chef::Node"

end
end
14 changes: 14 additions & 0 deletions app/targets/remote/ssh_multi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Target::Remote
class SSHMulti < Target
register :ssh_multi

attribute :command, type: String
attribute :hosts, type: Object
attribute :user, type: String
attribute :key_data, type: String
attribute :output, type: String

validates! :command, :hosts, :user, :key_data,
presence: true
end
end
14 changes: 0 additions & 14 deletions examples/definitions/create_and_destroy_ec2_server.rb

This file was deleted.

1 change: 0 additions & 1 deletion examples/definitions/mock_fail.rb

This file was deleted.

1 change: 0 additions & 1 deletion examples/definitions/mock_pass.rb

This file was deleted.

6 changes: 0 additions & 6 deletions examples/definitions/notification/campfire.rb

This file was deleted.

6 changes: 0 additions & 6 deletions examples/definitions/remote/ssh.rb

This file was deleted.

3 changes: 0 additions & 3 deletions examples/definitions/remote/test.rb

This file was deleted.

0 comments on commit 8567f62

Please sign in to comment.