bguthrie-awsymandias 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/.specification +57 -0
  2. data/README.rdoc +25 -21
  3. data/Rakefile +20 -4
  4. data/VERSION +1 -1
  5. data/awsymandias.gemspec +37 -12
  6. data/lib/awsymandias.rb +36 -331
  7. data/lib/awsymandias/addons/right_elb_interface.rb +375 -0
  8. data/lib/awsymandias/ec2.rb +49 -0
  9. data/lib/awsymandias/ec2/application_stack.rb +261 -0
  10. data/lib/awsymandias/extensions/class_extension.rb +18 -0
  11. data/lib/awsymandias/extensions/net_http_extension.rb +9 -0
  12. data/lib/awsymandias/instance.rb +149 -0
  13. data/lib/awsymandias/load_balancer.rb +157 -0
  14. data/lib/awsymandias/right_aws.rb +73 -0
  15. data/lib/awsymandias/right_elb.rb +16 -0
  16. data/lib/awsymandias/simple_db.rb +46 -0
  17. data/lib/awsymandias/snapshot.rb +33 -0
  18. data/lib/awsymandias/stack_definition.rb +60 -0
  19. data/lib/awsymandias/volume.rb +70 -0
  20. data/spec/integration/instance_spec.rb +37 -0
  21. data/spec/unit/addons/right_elb_interface_spec.rb +45 -0
  22. data/spec/unit/awsymandias_spec.rb +61 -0
  23. data/spec/unit/ec2/application_stack_spec.rb +634 -0
  24. data/spec/unit/instance_spec.rb +365 -0
  25. data/spec/unit/load_balancer_spec.rb +250 -0
  26. data/spec/unit/right_aws_spec.rb +90 -0
  27. data/spec/unit/simple_db_spec.rb +63 -0
  28. data/spec/unit/snapshot_spec.rb +39 -0
  29. data/spec/unit/stack_definition_spec.rb +113 -0
  30. data/tags +368 -0
  31. metadata +39 -13
  32. data/spec/awsymandias_spec.rb +0 -815
  33. data/vendor/aws-sdb/LICENSE +0 -19
  34. data/vendor/aws-sdb/README +0 -1
  35. data/vendor/aws-sdb/Rakefile +0 -20
  36. data/vendor/aws-sdb/lib/aws_sdb.rb +0 -3
  37. data/vendor/aws-sdb/lib/aws_sdb/error.rb +0 -42
  38. data/vendor/aws-sdb/lib/aws_sdb/service.rb +0 -191
  39. data/vendor/aws-sdb/spec/aws_sdb/service_spec.rb +0 -183
  40. data/vendor/aws-sdb/spec/spec_helper.rb +0 -4
@@ -0,0 +1,33 @@
1
+ module Awsymandias
2
+ class Snapshot < ActiveResource::Base
3
+ hash_initializer :aws_progress, :aws_status, :aws_id, :aws_volume_id, :aws_started_at, :stack
4
+ attr_reader :aws_progress, :aws_status, :aws_id, :aws_volume_id, :aws_started_at
5
+
6
+ self.site = "mu"
7
+
8
+ def self.find(*ids)
9
+ Awsymandias::RightAws.connection.describe_snapshots(ids).map { |s| Awsymandias::Snapshot.new s }
10
+ end
11
+
12
+ def self.find_by_tag(name)
13
+ tagged_snapshot = SimpleDB.get('snapshots', name)
14
+ find(tagged_snapshot[:snapshot_id]) if tagged_snapshot
15
+ end
16
+
17
+ def id; @aws_id; end
18
+ def snapshot_id; @aws_id; end
19
+
20
+ def size
21
+ Awsymandias::RightAws.connection.describe_volumes([connection.describe_snapshots([snapshot_id]).first[:aws_volume_id]]).first[:aws_size]
22
+ end
23
+
24
+ def tag(name)
25
+ SimpleDB.put('snapshots', name, :snapshot_id => id)
26
+ end
27
+
28
+ def to_simpledb
29
+ id
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,60 @@
1
+ module Awsymandias
2
+ class StackDefinition
3
+ attr_reader :name, :defined_instances, :defined_volumes, :defined_roles, :defined_load_balancers
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ @defined_instances = {}
8
+ @defined_volumes = {}
9
+ @defined_roles = {}
10
+ @defined_load_balancers = {}
11
+ end
12
+
13
+ def instance(name, config={})
14
+ extract_roles(config).each { |r| role(r, name) }
15
+ @defined_instances[name] = config
16
+ end
17
+
18
+ def instances(*names)
19
+ config = names.extract_options!
20
+ roles = extract_roles(config)
21
+ names.each do |name|
22
+ roles.each { |r| role(r, name) }
23
+ instance(name, config)
24
+ end
25
+ end
26
+
27
+ def load_balancer(name, configuration = {})
28
+ @defined_load_balancers[name] = configuration
29
+ end
30
+
31
+ def role(name, *instance_names)
32
+ @defined_roles[name] ||= []
33
+ @defined_roles[name] += instance_names
34
+ end
35
+
36
+ def volume(name, configuration={})
37
+ @defined_volumes[name] = configuration
38
+ end
39
+
40
+ def volumes(*names)
41
+ configuration = names.extract_options!
42
+ names.each { |name| volume(name, configuration) }
43
+ end
44
+
45
+ def build_stack
46
+ Awsymandias::EC2::ApplicationStack.new(name,
47
+ :instances => defined_instances,
48
+ :volumes => defined_volumes,
49
+ :roles => defined_roles,
50
+ :load_balancers => defined_load_balancers
51
+ )
52
+ end
53
+
54
+ private
55
+ def extract_roles(config)
56
+ [config.delete(:roles), config.delete(:role)].flatten.compact
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,70 @@
1
+ module Awsymandias
2
+ class Volume < ActiveResource::Base
3
+ hash_initializer :aws_size, :aws_device, :aws_attachment_status, :zone, :snapshot_id, :aws_attached_at, :aws_status, :aws_id, :aws_created_at, :aws_instance_id, :stack
4
+ attr_reader :aws_size, :aws_device, :aws_attachment_status, :zone, :snapshot_id, :aws_attached_at, :aws_status, :aws_id, :aws_created_at, :aws_instance_id
5
+
6
+ self.site = "mu"
7
+
8
+ def id; aws_id; end
9
+ def volume_id; aws_id; end
10
+
11
+ def attach_to_once_running(instance, unix_device)
12
+ Awsymandias.wait_for("#{instance.name} (#{instance.id}) to become available to attach #{id}", 5) do
13
+ instance.reload.running?
14
+ end
15
+ attach_to(instance.id, unix_device)
16
+ end
17
+
18
+ def attach_to(instance_id, unix_device)
19
+ if attached_to?(instance_id)
20
+ Awsymandias.verbose_output "\tVolume #{volume_id} is already attached to #{instance_id}."
21
+ return
22
+ end
23
+ raise "Volume #{volume_id} is already attached to #{aws_instance_id}. Can't attach to #{instance_id}." if attached_to_an_instance_other_than?(instance_id)
24
+
25
+ Awsymandias.verbose_output "\tTrying to attach volume #{volume_id} to #{instance_id} at #{unix_device}"
26
+ Awsymandias::RightAws.connection.attach_volume(volume_id, instance_id, unix_device)
27
+
28
+ Awsymandias.wait_for "volume #{volume_id} to attach to instance #{instance_id} on device #{unix_device}", 3 do
29
+ reload.attached?
30
+ end
31
+ end
32
+
33
+ def detach
34
+ Awsymandias::RightAws.connection.detach_volume volume_id, aws_instance_id, aws_device
35
+ Awsymandias.wait_for "volume #{volume_id} to detach..", 3 do
36
+ reload.available?
37
+ end
38
+ end
39
+
40
+ def attached?
41
+ aws_attachment_status == 'attached'
42
+ end
43
+
44
+ def attached_to?(instance_id)
45
+ attached? && aws_instance_id == instance_id
46
+ end
47
+
48
+ def attached_to_an_instance_other_than?(instance_id)
49
+ attached? && aws_instance_id != instance_id
50
+ end
51
+
52
+ def available?
53
+ aws_status == 'available'
54
+ end
55
+
56
+ def reload
57
+ data = Awsymandias::RightAws.connection.describe_volumes(self.aws_id).first
58
+ data.symbolize_keys!
59
+ data.keys.each do |attribute_name|
60
+ instance_variable_set "@#{attribute_name}", data[attribute_name]
61
+ end
62
+ self
63
+ end
64
+
65
+ def to_simpledb
66
+ aws_id
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require File.expand_path(File.dirname(__FILE__) + "/../../lib/awsymandias")
4
+
5
+ describe 'a launched instance' do
6
+
7
+ before :all do
8
+ Awsymandias.access_key_id = ENV['AMAZON_ACCESS_KEY_ID']
9
+ Awsymandias.secret_access_key = ENV['AMAZON_SECRET_ACCESS_KEY']
10
+
11
+ @stack = Awsymandias::EC2::ApplicationStack.define('instances') do |s|
12
+ s.instance :box, :image_id => 'ami-20b65349'
13
+ end
14
+
15
+ @stack.launch
16
+ Awsymandias.wait_for('stack to start', 5) { @stack.reload.running? }
17
+ end
18
+
19
+ after :all do
20
+ @stack.terminate!
21
+ end
22
+
23
+ it "should be available through a method on stack" do
24
+ @stack.box.should_not be_nil
25
+ end
26
+
27
+ it "should be running" do
28
+ @stack.box.running?.should be_true
29
+ end
30
+
31
+ it "should be saved in simple db" do
32
+ found_stack = Awsymandias::EC2::ApplicationStack.find('instances')
33
+ found_stack.box.should_not be_nil
34
+ found_stack.box.running?.should be_true
35
+ end
36
+
37
+ end
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require File.expand_path(File.dirname(__FILE__) + "/../../../lib/awsymandias")
4
+
5
+ if !"".respond_to?(:camelize)
6
+ class String
7
+ def camelize(first_letter = :upper)
8
+ case first_letter
9
+ when :upper
10
+ self.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
11
+ when :lower
12
+ self.first + camelize(lower_case_and_underscored_word)[1..-1]
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ module RightAws
19
+ describe ElbInterface do
20
+ describe "rehash_params_for_request" do
21
+ it "returns properly formatted request params" do
22
+ starting_params = {:load_balancer_name => 'Bob',
23
+ :some_hash => {:key => 'val'},
24
+ :some_array => ['array_val_1', 'array_val_2'],
25
+ :some_array_of_hashes => [ { :key1 => 'val1',
26
+ :key2 => 'val2' },
27
+ { :key3 => 'val3',
28
+ :key4 => 'val4' },
29
+ ]
30
+ }
31
+ expected_params = {"LoadBalancerName" => "Bob",
32
+ "SomeHash.Key" => "val",
33
+ "SomeArray.member.1" => "array_val_1",
34
+ "SomeArray.member.2" => "array_val_2",
35
+ "SomeArrayOfHashes.member.1.Key1" => "val1",
36
+ "SomeArrayOfHashes.member.1.Key2" => "val2",
37
+ "SomeArrayOfHashes.member.2.Key3" => "val3",
38
+ "SomeArrayOfHashes.member.2.Key4" => "val4"}
39
+
40
+ elb = ElbInterface.new(:some_key, :some_secret_key)
41
+ elb.send(:rehash_params_for_request,starting_params).should == expected_params
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,61 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require File.expand_path(File.dirname(__FILE__) + "/../../lib/awsymandias")
4
+
5
+ describe Awsymandias do
6
+ describe "stack names" do
7
+ it "returns an array of stack names fetched from SimpleDB" do
8
+ Awsymandias.access_key_id = "configured key"
9
+ Awsymandias.secret_access_key = "configured secret"
10
+
11
+ Awsymandias::SimpleDB.should_receive(:connection).and_return(connection = mock("connection"))
12
+ connection.should_receive(:query).with('application-stack','', nil, nil).and_return( { :items => ['x','y','z'] } )
13
+ Awsymandias.stack_names.should == ['x','y','z']
14
+ end
15
+
16
+ it "remove blank stack names from the returned array" do
17
+ Awsymandias.access_key_id = "configured key"
18
+ Awsymandias.secret_access_key = "configured secret"
19
+
20
+ Awsymandias::SimpleDB.should_receive(:connection).and_return(connection = mock("connection"))
21
+ connection.should_receive(:query).with('application-stack','', nil, nil).and_return( { :items => ['x','','y','z' ] } )
22
+ Awsymandias.stack_names.should == ['x','y','z']
23
+ end
24
+ end
25
+
26
+ describe Awsymandias::SimpleDB do
27
+ describe "connection" do
28
+ it "configure an instance of RightAws::SdbInterface" do
29
+ Awsymandias.access_key_id = "configured key"
30
+ Awsymandias.secret_access_key = "configured secret"
31
+
32
+ ::RightAws::SdbInterface.should_receive(:new).
33
+ with("configured key", "configured secret", anything).
34
+ and_return(:a_connection)
35
+
36
+ Awsymandias::SimpleDB.connection.should == :a_connection
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ describe Awsymandias::RightAws do
43
+ def zero_dollars
44
+ Money.new(0)
45
+ end
46
+
47
+ describe "connection" do
48
+ it "should configure an instance of RightAws::Ec2" do
49
+ Awsymandias.access_key_id = "configured key"
50
+ Awsymandias.secret_access_key = "configured secret"
51
+
52
+ ::RightAws::Ec2.should_receive(:new).
53
+ with("configured key", "configured secret", anything).
54
+ and_return(:a_connection)
55
+
56
+ Awsymandias::RightAws.connection.should == :a_connection
57
+ end
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,634 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require File.expand_path(File.dirname(__FILE__) + "/../../../lib/awsymandias")
4
+
5
+ module Awsymandias
6
+ module EC2
7
+ describe ApplicationStack do
8
+ class SimpleDBStub
9
+ def initialize
10
+ @store = {}
11
+ end
12
+
13
+ def list_domains
14
+ [ @store.keys ]
15
+ end
16
+
17
+ def put_attributes(domain, name, attributes)
18
+ @store[domain][name] = attributes
19
+ end
20
+
21
+ def get_attributes(domain, name)
22
+ @store[domain][name]
23
+ end
24
+
25
+ def delete_attributes(domain, name)
26
+ @store[domain][name] = nil
27
+ end
28
+
29
+ def create_domain(domain)
30
+ @store[domain] = {}
31
+ end
32
+ end
33
+
34
+ attr_accessor :simpledb
35
+
36
+ def stub_instance(stubs={})
37
+ instance_defaults = {:aws_instance_type=>"m1.large",
38
+ :ami_launch_index=>"0",
39
+ :aws_reason=>"",
40
+ :aws_launch_time=>"2009-07-23T13:57:22.000Z",
41
+ :aws_owner=>"423319072129",
42
+ :ssh_key_name=>"gsg-keypair",
43
+ :aws_reservation_id=>"r-dd6733b4",
44
+ :aws_kernel_id=>"aki-b51cf9dc",
45
+ :aws_instance_id=>"i-12345a3c",
46
+ :aws_availability_zone=>"us-east-1b",
47
+ :aws_state=>"running",
48
+ :aws_groups=>["default"],
49
+ :aws_ramdisk_id=>"ari-b31cf9da",
50
+ :aws_image_id=>"ami-some-image",
51
+ :dns_name=>"ec2-174-129-118-52.compute-1.amazonaws.com",
52
+ :aws_state_code=>"16",
53
+ :aws_product_codes=>[],
54
+ :private_dns_name=>"ip-10-244-226-239.ec2.internal"}
55
+ instance = Instance.new(instance_defaults.merge(stubs))
56
+ instance.should_receive(:attached_volumes).any_number_of_times.and_return([])
57
+ instance
58
+ end
59
+
60
+ def stub_load_balancer(stubs={})
61
+ lb_defaults = {:aws_created_at => "Tue Aug 04 11:14:27 UTC 2009",
62
+ :availability_zones=>["us-east-1b"],
63
+ :dns_name=>nil,
64
+ :name=>"RobTest",
65
+ :instances=>["i-5752453e"],
66
+ :listeners=> [{:protocol=>"HTTP", :load_balancer_port=>80, :instance_port=>3080}],
67
+ }
68
+ LoadBalancer.new(lb_defaults.merge(stubs))
69
+ end
70
+
71
+ before :each do
72
+ @simpledb = SimpleDBStub.new
73
+ SimpleDB.stub!(:connection).and_return @simpledb
74
+ end
75
+
76
+ it "should have a name" do
77
+ ApplicationStack.new("foo").name.should == "foo"
78
+ end
79
+
80
+ describe "instances" do
81
+ it "should be empty by default" do
82
+ ApplicationStack.new("foo").unlaunched_instances.should be_empty
83
+ end
84
+
85
+ it "should be settable through the initializer" do
86
+ stack = ApplicationStack.new("foo", :instances => { :app => {:some_key => :some_value} })
87
+ stack.unlaunched_instances[:app].should == {:some_key => :some_value}
88
+ end
89
+
90
+ it "should create a method on the application stack to access the running instance" do
91
+ stack = ApplicationStack.new("foo", :instances => { :app => {:some_key => :some_value} })
92
+ stack.respond_to?(:app).should be_true
93
+ end
94
+ end
95
+
96
+ describe "instance" do
97
+ it "should allow the definition of a basic, default instance" do
98
+ stack = ApplicationStack.define("foo") do |s|
99
+ s.instance :app
100
+ end
101
+ stack.unlaunched_instances[:app].should == {}
102
+ end
103
+
104
+ it "should use the parameters given to the instance definition" do
105
+ stack = ApplicationStack.define("foo") do |s|
106
+ s.instance :app, :foo => "bar"
107
+ end
108
+ stack.unlaunched_instances[:app].should == { :foo => "bar" }
109
+ end
110
+
111
+
112
+ it "should allow for the creation of multiple instances" do
113
+ stack = ApplicationStack.define("foo") do |s|
114
+ s.instance :app, :foo => "bar"
115
+ s.instance :db, :foo => "baz"
116
+ end
117
+ stack.unlaunched_instances[:app].should == { :foo => "bar" }
118
+ stack.unlaunched_instances[:db].should == { :foo => "baz" }
119
+ end
120
+
121
+ it "should map multiple instances to the same set of parameters" do
122
+ stack = ApplicationStack.define("foo") do |s|
123
+ s.instances :app, :db, :foo => "bar"
124
+ end
125
+ stack.unlaunched_instances[:app].should == { :foo => "bar" }
126
+ stack.unlaunched_instances[:db].should == { :foo => "bar" }
127
+ end
128
+
129
+ it "should create an accessor mapped to the new instance, nil by default" do
130
+ stack = ApplicationStack.define("foo") do |s|
131
+ s.instance :app, :foo => "bar"
132
+ end
133
+ stack.app.should == nil
134
+ end
135
+
136
+ end
137
+
138
+ describe "volumes" do
139
+ it "should be empty by default" do
140
+ ApplicationStack.define("foo").volumes.should be_empty
141
+ end
142
+
143
+ it "should be settable through the initializer" do
144
+ stack = ApplicationStack.new("foo", :volumes => { :db => {} })
145
+ stack.volumes[:db].should == {}
146
+ end
147
+
148
+ it "should not allow invalid options" do
149
+ lambda do
150
+ stack = ApplicationStack.new("foo", :volumes => { :db => {:some_key => "123"} })
151
+ end.should raise_error
152
+ end
153
+ end
154
+
155
+ describe "volume" do
156
+ it "should use the parameters given to the volume definition" do
157
+ stack = ApplicationStack.define("foo") do |s|
158
+ s.volume :some_volume, :volume_id => "vol-123"
159
+ end
160
+ stack.volumes[:some_volume].should == { :volume_id => "vol-123" }
161
+ end
162
+
163
+ it "should allow multiple volumes" do
164
+ stack = ApplicationStack.define("foo") do |s|
165
+ s.volume :volume_1, :volume_id => "vol-123"
166
+ s.volume :volume_2, :volume_id => "vol-456"
167
+ end
168
+ stack.volumes[:volume_1].should == { :volume_id => "vol-123" }
169
+ stack.volumes[:volume_2].should == { :volume_id => "vol-456" }
170
+ end
171
+
172
+ it "should allow volume_id, instance, and unix_device as options" do
173
+ stack = ApplicationStack.define("foo") do |s|
174
+ s.volumes :volume_1, :volume_id => "vol-123", :instance => "foo", :unix_device => "/dev/sdj"
175
+ end
176
+ stack.volumes[:volume_1].should == { :volume_id => "vol-123", :instance => "foo", :unix_device => "/dev/sdj" }
177
+ end
178
+
179
+ it "should not allow invalid options" do
180
+ lambda do
181
+ stack = ApplicationStack.define("foo") do |s|
182
+ s.volume :volume_1, :something_else => "foo"
183
+ end
184
+ end.should raise_error
185
+ end
186
+ end
187
+
188
+
189
+ describe "simpledb_domain" do
190
+ it "should map to ApplicationStack::DEFAULT_SIMPLEDB_DOMAIN upon creation" do
191
+ ApplicationStack.new("foo").simpledb_domain.should == ApplicationStack::DEFAULT_SIMPLEDB_DOMAIN
192
+ end
193
+
194
+ it "should be configurable" do
195
+ ApplicationStack.new("foo", :simpledb_domain => "a domain").simpledb_domain.should == "a domain"
196
+ end
197
+ end
198
+
199
+ describe 'define' do
200
+ it "should store the stack name" do
201
+ ApplicationStack.define('name').name.should == 'name'
202
+ end
203
+
204
+ it "should allow defining instances with a block" do
205
+ definition = ApplicationStack.define('name') do |s|
206
+ s.instance :foo, :image_id => 'foo'
207
+ end
208
+ definition.unlaunched_instances[:foo].should == { :image_id => 'foo' }
209
+ end
210
+
211
+ it "should create a method for each role created" do
212
+ stack = ApplicationStack.define('test') do |s|
213
+ s.instance :foo, :image_id => 'foo', :role => :app
214
+ end
215
+ stack.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
216
+ foo_instance = stub_instance :image_id => 'foo'
217
+ Instance.should_receive(:launch).with(:image_id => 'foo').and_return(foo_instance)
218
+ stack.launch
219
+
220
+ stack.app.should == [foo_instance]
221
+ end
222
+
223
+ end
224
+
225
+ describe "launch" do
226
+ it "should launch its instances when launched" do
227
+ s = ApplicationStack.define("test") do |s|
228
+ s.instance :db, :instance_type => InstanceTypes::C1_XLARGE
229
+ s.instance :app, :instance_type => InstanceTypes::M1_LARGE
230
+ end
231
+
232
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
233
+ inst1 = stub_instance :instance_type => InstanceTypes::C1_XLARGE
234
+
235
+ Instance.should_receive(:launch).
236
+ with({ :instance_type => InstanceTypes::C1_XLARGE }).
237
+ and_return(inst1)
238
+
239
+ inst2 = stub_instance :instance_type => InstanceTypes::M1_LARGE
240
+
241
+ Instance.should_receive(:launch).
242
+ with({ :instance_type => InstanceTypes::M1_LARGE }).
243
+ and_return(inst2)
244
+
245
+ s.launch
246
+ end
247
+
248
+ it "should set the getter for the particular instance to the return value of launching the instance" do
249
+ s = ApplicationStack.define("test") do |s|
250
+ s.instance :db, :instance_type => InstanceTypes::C1_XLARGE
251
+ s.instance :app, :instance_type => InstanceTypes::M1_LARGE
252
+ end
253
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
254
+
255
+ instance_1 = stub_instance
256
+ instance_2 = stub_instance
257
+
258
+ Instance.stub!(:launch).
259
+ with({ :instance_type => InstanceTypes::C1_XLARGE }).
260
+ and_return instance_1
261
+ Instance.stub!(:launch).
262
+ with({ :instance_type => InstanceTypes::M1_LARGE }).
263
+ and_return instance_2
264
+
265
+ s.db.should be_nil
266
+ s.app.should be_nil
267
+
268
+ s.launch
269
+
270
+ s.db.should == instance_1
271
+ s.app.should == instance_2
272
+ end
273
+
274
+ it "should remove the instance from unlaunched_instances after it has been launched" do
275
+ s = ApplicationStack.define("test") do |s|
276
+ s.instance :db, :instance_type => InstanceTypes::C1_XLARGE
277
+ end
278
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
279
+
280
+ inst = mock("instance1", :aws_instance_id => "a", :attached_volumes => [])
281
+ inst.should_receive(:name=)
282
+ Instance.should_receive(:launch).
283
+ with({ :instance_type => InstanceTypes::C1_XLARGE }).
284
+ and_return(inst)
285
+
286
+ s.unlaunched_instances[:db].should_not be_nil
287
+ s.launch
288
+ s.unlaunched_instances[:db].should be_nil
289
+ end
290
+
291
+ it "should store details about the newly launched instances" do
292
+ Instance.stub!(:launch).and_return stub_instance(:aws_instance_id => "abc123")
293
+ stack = ApplicationStack.define("test") do |s|
294
+ s.instance :db, :instance_type => InstanceTypes::C1_XLARGE
295
+ end
296
+ stack.should_receive(:store_app_stack_metadata!).at_least(:once)
297
+ stack.launch
298
+ end
299
+
300
+ it "should attach volumes when launched" do
301
+ s = ApplicationStack.define("test") do |s|
302
+ s.instance :db, :instance_type => InstanceTypes::M1_LARGE
303
+ s.volume :production_data, :volume_id => "vol-123", :instance => :db, :unix_device => "/dev/sdj"
304
+ end
305
+
306
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
307
+
308
+ instance = stub_instance
309
+
310
+ Instance.should_receive(:launch).and_return(instance)
311
+
312
+ Awsymandias::RightAws.should_receive(:describe_volumes).with(["vol-123"]).and_return([volume = mock])
313
+ volume.should_receive(:attach_to_once_running).with(instance, "/dev/sdj")
314
+
315
+ s.launch
316
+ end
317
+
318
+ it "should create volumes from snapshots and attach them to each instance of the appropriate role" do
319
+ s = ApplicationStack.define("test") do |s|
320
+ s.instance :app1, :instance_type => InstanceTypes::M1_LARGE, :role => :app
321
+ s.instance :app2, :instance_type => InstanceTypes::M1_LARGE, :role => :app
322
+ s.instance :app3, :instance_type => InstanceTypes::M1_LARGE, :role => :app
323
+ s.instance :no_attached_volume, :instance_type => InstanceTypes::M1_SMALL, :role => :other_role
324
+
325
+ s.volume :production_data, :snapshot_id => "snap-123", :role => :app, :unix_device => "/dev/sdj"
326
+ end
327
+
328
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
329
+ s.should_receive(:sleep).any_number_of_times
330
+
331
+ instances = stub_instance, stub_instance, stub_instance
332
+
333
+ Instance.should_receive(:launch).
334
+ with(hash_including(:instance_type => InstanceTypes::M1_LARGE)).exactly(3).times.
335
+ and_return(*instances)
336
+
337
+ Instance.should_receive(:launch).
338
+ with(hash_including(:instance_type => InstanceTypes::M1_SMALL)).once.
339
+ and_return(stub_instance)
340
+
341
+ Awsymandias::RightAws.should_receive(:wait_for_create_volume).exactly(3).times.
342
+ with("snap-123", instances.first.aws_availability_zone).
343
+ and_return(volume_1 = mock(:aws_id => "vol-123-1"), volume_2 = mock(:aws_id => "vol-123-2"), volume_3 = mock(:aws_id => "vol-123-3"))
344
+
345
+ volume_1.should_receive(:attach_to_once_running).with(instances[0], "/dev/sdj")
346
+ volume_2.should_receive(:attach_to_once_running).with(instances[1], "/dev/sdj")
347
+ volume_3.should_receive(:attach_to_once_running).with(instances[2], "/dev/sdj")
348
+
349
+ s.launch
350
+ end
351
+
352
+ it "should create volumes from snapshots and attach them to all instances across roles" do
353
+ s = ApplicationStack.define("test") do |s|
354
+ s.instance :app, :instance_type => InstanceTypes::M1_LARGE
355
+ s.instance :db, :instance_type => InstanceTypes::M1_LARGE
356
+ s.volume :volume_for_all, :snapshot_id => "snap-123", :all_instances => true, :unix_device => "/dev/sdj"
357
+ end
358
+
359
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
360
+ s.should_receive(:sleep).any_number_of_times
361
+
362
+ instances = stub_instance, stub_instance
363
+
364
+ Instance.should_receive(:launch).exactly(2).times.and_return(*instances)
365
+
366
+ Awsymandias::RightAws.should_receive(:wait_for_create_volume).exactly(2).times.
367
+ with("snap-123", instances.first.aws_availability_zone).
368
+ and_return(volume_1 = mock(:aws_id => "vol-123-1"), volume_2 = mock(:aws_id => "vol-123-2"))
369
+
370
+ volume_1.should_receive(:attach_to_once_running).with(instances[0], "/dev/sdj")
371
+ volume_2.should_receive(:attach_to_once_running).with(instances[1], "/dev/sdj")
372
+
373
+ s.launch
374
+ end
375
+
376
+ it "should launch its load balancers when launched" do
377
+ lb_params = {:availability_zones=>["us-east-1b"],
378
+ :instances=>[:db],
379
+ :listeners=> [{:protocol=>"HTTP", :load_balancer_port=>80, :instance_port=>3080}]
380
+ }
381
+ s = ApplicationStack.define("test") do |s|
382
+ s.instance :db, :instance_type => InstanceTypes::C1_XLARGE
383
+ s.load_balancer :lb, lb_params
384
+ end
385
+
386
+ Instance.should_receive(:launch).and_return(stub_instance)
387
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
388
+ lb = stub_load_balancer
389
+
390
+ LoadBalancer.should_receive(:launch).with(lb_params).and_return(lb)
391
+ s.launch
392
+ end
393
+
394
+ it "should remove the load balancer from unlaunched_load_balancers after it has been launched" do
395
+ lb_params = {:availability_zones=>["us-east-1b"],
396
+ :instances=>[:db],
397
+ :listeners=> [{:protocol=>"HTTP", :load_balancer_port=>80, :instance_port=>3080}]
398
+ }
399
+ s = ApplicationStack.define("test") do |s|
400
+ s.instance :db, :instance_type => InstanceTypes::C1_XLARGE
401
+ s.load_balancer :lb, lb_params
402
+ end
403
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
404
+
405
+ Instance.should_receive(:launch).and_return(stub_instance)
406
+
407
+ lb = stub_load_balancer
408
+ LoadBalancer.should_receive(:launch).with(lb_params).and_return(lb)
409
+
410
+ s.unlaunched_load_balancers[:lb].should_not be_nil
411
+ s.launch
412
+ s.unlaunched_load_balancers[:lb].should be_nil
413
+ end
414
+
415
+ end
416
+
417
+ describe "launched?" do
418
+ it "should be false initially" do
419
+ s = ApplicationStack.define("test") { |s| s.instance :db, :instance_type => InstanceTypes::M1_LARGE }
420
+ s.launched?.should be_false
421
+ end
422
+
423
+ it "should be true if launched and instances are non-empty" do
424
+ s = ApplicationStack.define("test") { |s| s.instance :db }
425
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
426
+ Instance.stub!(:launch).and_return stub_instance
427
+ s.launch
428
+ s.launched?.should be_true
429
+ end
430
+ end
431
+
432
+ describe "running?" do
433
+ it "should be false initially" do
434
+ ApplicationStack.define("test") { |s| s.instance :db }.should_not be_running
435
+ end
436
+
437
+ it "should be false if launched but all instances are pending" do
438
+ Instance.stub!(:launch).and_return stub_instance(:aws_state => { :name => "pending" })
439
+ s = ApplicationStack.define("test") { |s| s.instance :db }
440
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
441
+ s.launch.should_not be_running
442
+ end
443
+
444
+ it "should be false if launched and some instances are pending" do
445
+ s = ApplicationStack.define("test") do |s|
446
+ s.instance :db, :instance_type => InstanceTypes::C1_XLARGE
447
+ s.instance :app, :instance_type => InstanceTypes::M1_LARGE
448
+ end
449
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
450
+
451
+ Instance.stub!(:launch).
452
+ with({ :instance_type => InstanceTypes::C1_XLARGE }).
453
+ and_return stub_instance(:aws_state => { :name => "pending" })
454
+
455
+ Instance.stub!(:launch).
456
+ with({ :instance_type => InstanceTypes::M1_LARGE }).
457
+ and_return stub_instance(:aws_state => { :name => "running" })
458
+
459
+ s.launch
460
+ s.should_not be_running
461
+ end
462
+
463
+ it "should be true if launched and all instances are running" do
464
+ s = ApplicationStack.define("test") do |s|
465
+ s.instance :db, :instance_type => InstanceTypes::C1_XLARGE
466
+ s.instance :app, :instance_type => InstanceTypes::M1_LARGE
467
+ end
468
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
469
+
470
+ Instance.stub!(:launch).
471
+ with({ :instance_type => InstanceTypes::C1_XLARGE }).
472
+ and_return stub_instance(:aws_state => "running")
473
+
474
+ Instance.stub!(:launch).
475
+ with({ :instance_type => InstanceTypes::M1_LARGE }).
476
+ and_return stub_instance(:aws_state => "running")
477
+
478
+ s.launch
479
+ s.should be_running
480
+ end
481
+ end
482
+
483
+ describe "port_open?" do
484
+ it "should return true if there is one instance with the port open" do
485
+ s = ApplicationStack.define("test") do |s|
486
+ s.instance "app", :instance_type => InstanceTypes::C1_XLARGE
487
+ end
488
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
489
+
490
+ instance = stub_instance
491
+ instance.should_receive(:port_open?).with(100).and_return(true)
492
+ Instance.stub!(:launch).and_return(instance)
493
+
494
+ s.launch
495
+ s.port_open?(100).should be_true
496
+ end
497
+
498
+ it "should return false if there is one instance with the port closed" do
499
+ s = ApplicationStack.define("test") do |s|
500
+ s.instance "app", :instance_type => InstanceTypes::C1_XLARGE
501
+ end
502
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
503
+
504
+ instance = stub_instance
505
+ instance.should_receive(:port_open?).with(100).and_return(false)
506
+ Instance.stub!(:launch).and_return(instance)
507
+
508
+ s.launch
509
+ s.port_open?(100).should be_false
510
+ end
511
+
512
+ it "should return true if there are multiple instances all with the port open" do
513
+ s = ApplicationStack.define("test") do |s|
514
+ s.instance "app1", :instance_type => InstanceTypes::C1_XLARGE
515
+ s.instance "app2", :instance_type => InstanceTypes::C1_XLARGE
516
+ end
517
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
518
+
519
+ instance1, instance2 = [stub_instance, stub_instance]
520
+ instance1.should_receive(:port_open?).with(100).and_return(true)
521
+ instance2.should_receive(:port_open?).with(100).and_return(true)
522
+ Instance.stub!(:launch).and_return(instance1, instance2)
523
+
524
+ s.launch
525
+ s.port_open?(100).should be_true
526
+ end
527
+
528
+ it "should return false if there are multiple instances with at least one port closed" do
529
+ s = ApplicationStack.define("test") do |s|
530
+ s.instance "app1", :instance_type => InstanceTypes::C1_XLARGE
531
+ s.instance "app2", :instance_type => InstanceTypes::C1_XLARGE
532
+ end
533
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
534
+
535
+ instance1, instance2 = [stub_instance, stub_instance]
536
+ instance1.should_receive(:port_open?).with(100).and_return(true)
537
+ instance2.should_receive(:port_open?).with(100).and_return(false)
538
+ Instance.stub!(:launch).and_return(instance1, instance2)
539
+
540
+ s.launch
541
+ s.port_open?(100).should be_false
542
+ end
543
+
544
+ end
545
+
546
+ describe "terminate!" do
547
+ it "should not do anything if not running" do
548
+ s = ApplicationStack.define("test") { |s| s.instance "db" }
549
+ s.should_receive(:remove_app_stack_metadata!).once.and_return(nil)
550
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
551
+ RightAws.should_receive(:connection).never
552
+ s.terminate!
553
+ end
554
+
555
+ it "should terminate all instances if running" do
556
+ s = ApplicationStack.define("test") { |s| s.instance "db" }
557
+ s.should_receive(:remove_app_stack_metadata!).once.and_return(nil)
558
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
559
+ mock_instance = mock("an_instance", :aws_instance_id => "i-foo", :running? => true, :attached_volumes => [])
560
+ mock_instance.should_receive(:name=).any_number_of_times
561
+ mock_instance.should_receive(:name).any_number_of_times
562
+ mock_instance.should_receive(:terminate!)
563
+ Instance.stub!(:launch).and_return(mock_instance)
564
+ s.launch
565
+ s.terminate!
566
+ end
567
+
568
+ it "should remove any stored instance name mappings" do
569
+ s = ApplicationStack.define("test") { |s| s.instance "db_1" }
570
+ s.should_receive(:remove_app_stack_metadata!).once.and_return(nil)
571
+ s.should_receive(:store_app_stack_metadata!).any_number_of_times.and_return(nil)
572
+ mock_instance = mock("an_instance", :aws_instance_id => "i-foo", :running? => true, :attached_volumes => [])
573
+ mock_instance.should_receive(:name=).any_number_of_times
574
+ mock_instance.should_receive(:name).any_number_of_times
575
+ mock_instance.should_receive(:terminate!)
576
+ Instance.stub!(:launch).and_return(mock_instance)
577
+ s.launch
578
+ s.terminate!
579
+ end
580
+ end
581
+
582
+ describe "running_cost" do
583
+ it "should be zero if the stack has not been launched" do
584
+ s = ApplicationStack.define("test") { |s| s.instance "db", :instance_type => InstanceTypes::M1_LARGE}
585
+ s.running_cost.should == Money.new(0)
586
+ end
587
+
588
+ it "should be the sum total of the running cost of its constituent instances" do
589
+ inst1, inst2 = [
590
+ Instance.new(
591
+ :aws_instance_type => 'm1.small',
592
+ :aws_state => 'running',
593
+ :aws_launch_time => 5.minutes.ago.strftime("%Y-%m-%dT%H:%M:00.000EDT")
594
+ ),
595
+ Instance.new(
596
+ :aws_instance_type => 'c1.medium',
597
+ :aws_state => 'running',
598
+ :aws_launch_time => 5.minutes.ago.strftime("%Y-%m-%dT%H:%M:00.000EDT")
599
+ )
600
+ ]
601
+
602
+ stack = ApplicationStack.new "test"
603
+ stack.should_receive(:launched?).and_return(true)
604
+ stack.instance_variable_set :"@instances", {'inst1' => inst1, 'inst2' => inst2}
605
+
606
+ stack.running_cost.should == inst1.running_cost + inst2.running_cost
607
+ end
608
+ end
609
+
610
+ describe "create_and_attach_volumes_to_instances" do
611
+ it "should raise an error if a volume is already attached to the specified unix device" do
612
+ instance = stub_instance
613
+ unix_device = "/dev/sdj"
614
+ instance.should_receive(:volume_attached_to_unix_device, unix_device).and_return(mock(:aws_id => "vol-123"))
615
+
616
+ lambda { ApplicationStack.new("test").create_and_attach_volumes_to_instances([instance], :unix_device => unix_device) }.
617
+ should raise_error(RuntimeError)
618
+ end
619
+ end
620
+
621
+ describe "find" do
622
+ it "should have more than just a pending test"
623
+ end
624
+
625
+ describe "reload_from_metadata!" do
626
+ it "should have more than just a pending test"
627
+ end
628
+
629
+ describe "store_app_stack_metadata!" do
630
+ it "should have more than just a pending test"
631
+ end
632
+ end
633
+ end
634
+ end