Skip to content

Commit

Permalink
bring registry back; move top namespace methods to mixins; general fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
danryan committed Sep 13, 2012
1 parent 69c528b commit 20dd8a4
Show file tree
Hide file tree
Showing 34 changed files with 533 additions and 497 deletions.
8 changes: 5 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ PATH
cabin (~> 0.4.4)
faraday (~> 0.8.4)
fog (~> 1.5.0)
multi_json (~> 1.3.6)
multi_xml
json (~> 1.7.5)
net-ssh (~> 2.5.2)
net-ssh-multi (~> 1.1)
ruote (~> 2.3.0)
Expand All @@ -36,6 +35,7 @@ GEM
json
diff-lcs (1.1.3)
eventmachine (1.0.0)
eventmachine (1.0.0-java)
excon (0.16.2)
factory_girl (4.1.0)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -73,14 +73,15 @@ GEM
spork (>= 0.8.4)
hashie (1.2.0)
http_parser.rb (0.5.3)
http_parser.rb (0.5.3-java)
i18n (0.6.1)
json (1.7.5)
json (1.7.5-java)
mime-types (1.19)
mixlib-authentication (1.3.0)
mixlib-log
mixlib-log (1.4.1)
multi_json (1.3.6)
multi_xml (0.5.1)
multipart-post (1.1.5)
net-scp (1.0.4)
net-ssh (>= 1.99.1)
Expand All @@ -91,6 +92,7 @@ GEM
net-ssh (>= 2.1.4)
net-ssh-gateway (>= 0.99.0)
nokogiri (1.5.5)
nokogiri (1.5.5-java)
parslet (1.4.0)
blankslate (~> 2.0)
rb-fsevent (0.9.1)
Expand Down
13 changes: 13 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ignore %r{^spec/fixtures/}

guard 'rspec', :version => 2, :cli => "--color --format doc" do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
watch(%r{^spec/support/.+.rb$}) { "spec" }
end

guard 'spork' do
watch('spec/spec_helper.rb')
watch(%r{^spec/support/.+.rb$}) { "spec" }
end
20 changes: 20 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

desc "Run specs"
RSpec::Core::RakeTask.new(:spec) {|t|}

namespace :spec do
desc "Clean up rbx compiled files and run spec suite"
RSpec::Core::RakeTask.new(:ci) { |t| Dir.glob("**/*.rbc").each {|f| FileUtils.rm_f(f) } }
end

desc "Run guard"
task :guard do
sh %{bundle exec guard start}
end

desc "Run spork"
task :spork do
sh %{bundle exec spork}
end
119 changes: 36 additions & 83 deletions lib/mastermind.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'ruote'
require 'ruote-redis'

require 'json'
require 'cabin'

require 'active_support/json'
Expand All @@ -16,88 +14,43 @@
require 'faraday'
require 'addressable/uri'
require 'active_attr'
# require 'state_machine'

$VERBOSE = nil

json_lib = defined?(RUBY_PLATFORM) && RUBY_PLATFORM == "java" ? 'json' : 'yajl'

begin
require json_lib
Rufus::Json.backend = json_lib.to_sym
rescue LoadError => e
raise LoadError, "Please install #{json_lib}: `gem install #{json_lib}` (#{e.message})", e.backtrace
end

JSON.create_id = nil
# require 'state_machine'

module Mastermind
autoload :AWS, 'mastermind/aws'
autoload :Resource, 'mastermind/resource'
autoload :Provider, 'mastermind/provider'
autoload :DSL, 'mastermind/dsl'
autoload :Job, 'mastermind/job'
autoload :JobObserver, 'mastermind/job_observer'

def self.logger
cabin = Cabin::Channel.new
cabin.subscribe(STDOUT)
@logger ||= cabin
@logger
end

def self.logger=(logger)
@logger = logger
end

def self.dashboard
url = ENV['REDIS_URL'] || ENV['REDISTOGO_URL'] || 'redis://localhost:6379/1'
storage = Ruote::Redis::Storage.new(::Redis.connect(url: url, thread_safe: true))
# worker = Ruote::Worker.new(storage)
@dashboard ||= Ruote::Dashboard.new(storage)
@dashboard.configure('ruby_eval_allowed', true)
@dashboard
end

def self.resources
@resources ||= Hash.new.with_indifferent_access
autoload :JobObserver, 'mastermind/job_observer'
autoload :Logger, 'mastermind/logger'
autoload :Mixins, 'mastermind/mixins'
autoload :Registry, 'mastermind/registry'
autoload :Resource, 'mastermind/resource'
autoload :Provider, 'mastermind/provider'
autoload :DSL, 'mastermind/dsl'
autoload :Job, 'mastermind/job'

extend Mixins::Registry
extend Mixins::Ruote
extend Mixins::Logger

resources do
register :mock, Mastermind::Resource::Mock
register :chef_node, Mastermind::Resource::CM::Chef::Node
register :campfire, Mastermind::Resource::Notification::Campfire
register :email, Mastermind::Resource::Notification::Email
register :http, Mastermind::Resource::Remote::HTTP
register :ssh, Mastermind::Resource::Remote::SSH
register :ssh_multi, Mastermind::Resource::Remote::SSHMulti
register :ec2_server, Mastermind::Resource::Server::EC2
end

providers do
register :mock, Mastermind::Provider::Mock
register :chef_node, Mastermind::Provider::CM::Chef::Node
register :campfire, Mastermind::Provider::Notification::Campfire
register :email, Mastermind::Provider::Notification::Email
register :http, Mastermind::Provider::Remote::HTTP
register :ssh, Mastermind::Provider::Remote::SSH
register :ssh_multi, Mastermind::Provider::Remote::SSHMulti
register :ec2_server, Mastermind::Provider::Server::EC2
end

def self.providers
@providers ||= Hash.new.with_indifferent_access
end

def self.launch(job_or_pdef, fields={}, variables={})
if job_or_pdef.kind_of?(Job) && fields.empty? && variables.empty?
# someone launched a job manually
Mastermind.dashboard.launch(job_or_pdef.pdef, job_or_pdef.fields, { job_id: job_or_pdef.id })
else
# assume we're dealing with a ruote-compiled pdef
Mastermind.dashboard.launch(job_or_pdef, fields, variables)
end
end

def self.ps(wfid)
Mastermind.dashboard.process(wfid)
end

def self.define(attributes, &block)
Ruote.define(attributes, &block)
end
end

Mastermind.dashboard.add_service('job_observer', Mastermind::JobObserver)

Mastermind.dashboard.context.logger.noisy = ENV['MASTERMIND_NOISY'] || false
Mastermind.logger.level = ENV['LOG_LEVEL'] && ENV['LOG_LEVEL'].to_sym || :info

# # require our resources
# Dir[Rails.root + 'app/models/resource/**/*.rb'].each do |file|
# require file
# end

# # require our providers
# Dir[Rails.root + 'app/models/provider/**/*.rb'].each do |file|
# require file
# end

end
32 changes: 0 additions & 32 deletions lib/mastermind/aws.rb

This file was deleted.

14 changes: 14 additions & 0 deletions lib/mastermind/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'cabin'

module Mastermind
class Logger < Cabin::Channel

def initialize(*args)
super()

subscribe(args[0])

metrics.channel = Cabin::Channel.new
end
end
end
9 changes: 9 additions & 0 deletions lib/mastermind/mixins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Mastermind
module Mixins
autoload :Resources, 'mastermind/mixins/resources'
autoload :AWS, 'mastermind/mixins/aws'
autoload :Registry, 'mastermind/mixins/registry'
autoload :Ruote, 'mastermind/mixins/ruote'
autoload :Logger, 'mastermind/mixins/logger'
end
end
34 changes: 34 additions & 0 deletions lib/mastermind/mixins/aws.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Mastermind
module Mixins
module AWS
FLAVORS = ['m1.small', 'm1.medium', 'm1.large', 'm1.xlarge', 't1.micro', 'm2.xlarge', 'm2.2xlarge', 'm2.4xlarge', 'c1.medium', 'c1.xlarge', 'cc1.4xlarge', 'cc2.8xlarge', 'cg1.4xlarge', 'hi1.4xlarge']

REGIONS = ['ap-northeast-1', 'ap-southeast-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1']

ZONES = {
'us-east-1' => [
'us-east-1a', 'us-east-1b', 'us-east-1c', 'us-east-1d', 'us-east-1e'
],
'us-west-1' => [
'us-west-1a', 'us-west-1b', 'us-west-1c'
],
'us-west-2' => [
'us-west-2a', 'us-west-2b', 'us-west-2c'
],
'ap-northeast-1' => [
'ap-northeast-1a', 'ap-northeast-1b'
],
'ap-southeast-1' => [
'ap-southeast-1a', 'ap-southeast-1b'
],
'eu-west-1' => [
'eu-west-1a', 'eu-west-1b', 'eu-west-1c'
],
'sa-east-1' => [
'sa-east-1a', 'sa-east-1b'
]
}

end
end
end
19 changes: 19 additions & 0 deletions lib/mastermind/mixins/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Mastermind
module Mixins
module Logger

def self.extended(base)
base.logger.level = ENV['LOG_LEVEL'] && ENV['LOG_LEVEL'].to_sym || :info
base.dashboard.context.logger.noisy = ENV['MASTERMIND_NOISY'] || false
end

def logger
@logger ||= Mastermind::Logger.new(::STDOUT)
end

def logger=(logger)
@logger = logger
end
end
end
end
26 changes: 26 additions & 0 deletions lib/mastermind/mixins/registry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Mastermind
module Mixins
module Registry

def resources(&block)
@resources ||= Mastermind::Registry.new

unless block.nil?
@resources.instance_eval(&block)
end

return @resources
end

def providers(&block)
@providers ||= Mastermind::Registry.new

unless block.nil?
@providers.instance_eval(&block)
end

return @providers
end
end
end
end
2 changes: 1 addition & 1 deletion lib/mastermind/mixins/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def attribute(name, value=nil)
module ClassMethods
def register(type)
@type = type.to_s
Mastermind.resources[type.to_sym] = self
# Mastermind.resources[type.to_sym] = self
end

def provider(provider_class)
Expand Down
Loading

0 comments on commit 20dd8a4

Please sign in to comment.