Skip to content
Browse files

figuring out how to add the cap environment

  • Loading branch information...
1 parent 5416d2d commit 67e8e305a15cb9d6cc73aba26c0ac4a021319e3d @wbailey committed Jun 9, 2012
Showing with 59 additions and 2 deletions.
  1. +10 −0 lib/claws/capistrano.rb
  2. +6 −1 lib/claws/presenter/ec2.rb
  3. +10 −0 spec/capistrano_spec.rb
  4. +21 −0 spec/collection/ec2_spec.rb
  5. +12 −1 spec/presenter/ec2_spec.rb
View
10 lib/claws/capistrano.rb
@@ -27,6 +27,16 @@ def roles(host)
end
end
+ def environment(host)
+ data = self.all_host_roles
+
+ environments = data.keys
+
+ environments.each do |env|
+ return env.to_s if data[env].has_key?(host)
+ end
+ end
+
private
def get_roles(environment)
View
7 lib/claws/presenter/ec2.rb
@@ -4,19 +4,24 @@
module Claws
module EC2
class Presenter
- attr_writer :roles
+ attr_writer :roles, :environment
def initialize(instance, options = {})
@ec2 = instance.extend(Claws::Support)
@roles = options[:roles] || []
@region = options[:region]
+ @environment = options[:environment] || ''
freeze
end
def region
@region || 'N/A'
end
+ def environment
+ @environment.empty? ? 'N/A' : @environment
+ end
+
def roles
@roles.empty? ? 'N/A' : @roles.join(', ')
end
View
10 spec/capistrano_spec.rb
@@ -71,4 +71,14 @@
cap = Claws::Capistrano.new
cap.roles('ec2-263-56-231-91.compute-1.amazonaws.com').should == %w{app web}
end
+
+ it 'returns environment for host' do
+ Dir.should_receive(:glob).and_return(%W{#{@default_path}/staging.rb #{@default_path}/production.rb})
+ File.should_receive(:readlines).with("#{@default_path}/staging.rb").and_return(@roles[:staging])
+ File.should_receive(:readlines).with("#{@default_path}/production.rb").and_return(@roles[:production])
+
+ cap = Claws::Capistrano.new
+ cap.environment('ec2-263-56-231-91.compute-1.amazonaws.com').should == 'production'
+ cap.environment('ec2-147-32-151-54.compute-1.amazonaws.com').should == 'staging'
+ end
end
View
21 spec/collection/ec2_spec.rb
@@ -82,4 +82,25 @@
subject.new(config).get.size.should == 2
end
+
+ context 'capistrano' do
+ it 'adds environment to the instance definition' do
+ AWS.should_receive(:config).with(credentials).and_return(true)
+ AWS.should_receive(:start_memoizing).and_return(nil)
+
+ AWS::EC2.should_receive(:new).and_return(
+ double('AWS::EC2::RegionsCollection', :regions => regions)
+ )
+
+ config = double('Claws::Configuration',
+ :aws => credentials,
+ :ec2 => OpenStruct.new({:regions => %w(us-east-1)})
+ )
+
+ collection = subject.new(config).get
+
+ collection[0].environment.should == 'production'
+ end
+ end
+
end
View
13 spec/presenter/ec2_spec.rb
@@ -27,8 +27,9 @@
cap = double('Claws::Capistrano')
cap.stub(:roles).with(host).and_return(%w{app web})
+ cap.stub(:environment).with(host).and_return('production')
- @full_presenter = subject.new(full_instance, :region => 'us-east-1', :roles => cap.roles(full_instance.public_dns))
+ @full_presenter = subject.new(full_instance, :region => 'us-east-1', :roles => cap.roles(full_instance.public_dns), :environment => cap.environment(full_instance.public_dns))
less_instance = double(AWS::EC2, :tags => nil)
@less_presenter = subject.new(less_instance)
@@ -42,6 +43,16 @@
end
end
+ describe '#environment' do
+ it 'can be defined' do
+ @full_presenter.environment.should == 'production'
+ end
+
+ it 'is not required' do
+ @less_presenter.environment.should == 'N/A'
+ end
+ end
+
describe '#region' do
it 'can be defined' do
@full_presenter.region.should == 'us-east-1'

0 comments on commit 67e8e30

Please sign in to comment.
Something went wrong with that request. Please try again.