public
Rubygem
Description: A Capistrano extension for managing and running your app on Amazon EC2.
Clone URL: git://github.com/jnewland/capsize.git
Search Repo:
on launch of an instance, execute scripts for each security group the 
instance is in
jnewland (author)
Sat Apr 12 13:58:09 -0700 2008
commit  e902ac06ca94865412751d240baff8ed3f709415
tree    8976413990f28f4ed8adbc199bcef1bae622d111
parent  5ea37faebf24cd9559c64d31e54e30f164782697
...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
 
 
 
 
258
259
260
261
262
263
 
264
265
 
266
267
268
...
280
281
282
 
283
284
 
285
286
287
288
289
290
 
291
292
293
294
295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
297
 
298
299
300
...
389
390
391
 
 
 
 
 
 
 
 
392
393
394
...
422
423
424
 
 
 
 
 
 
 
 
425
426
427
...
241
242
243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
...
272
273
274
275
276
 
277
278
279
280
281
 
282
283
284
285
286
287
 
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
...
404
405
406
407
408
409
410
411
412
413
414
415
416
417
...
445
446
447
448
449
450
451
452
453
454
455
456
457
458
0
@@ -241,28 +241,20 @@ module Capsize
0
       response = amazon.run_instances(options)
0
 
0
       instance_id = response.instancesSet.item[0].instanceId
0
- puts "Instance #{instance_id} startup in progress..."
0
-
0
- # loop checking for instance pending notification
0
- tries = 0
0
- begin
0
- instance = amazon.describe_instances(:instance_id => instance_id)
0
- raise "Waiting." unless response.instancesSet.item[0].instanceState.name == "pending"
0
- puts "Instance #{instance_id} entered state 'pending'"
0
- rescue
0
- sleep(10)
0
- tries += 1
0
- retry unless tries == 35
0
- raise "Instance #{instance_id} never moved to state 'pending'!"
0
- end
0
+ puts "Instance #{instance_id} startup in progress"
0
+
0
+ #set scope outside of block
0
+ instance = nil
0
 
0
       #loop checking for confirmation that instance is running
0
       tries = 0
0
       begin
0
         instance = amazon.describe_instances(:instance_id => instance_id)
0
         raise "Server Not Running" unless instance.reservationSet.item[0].instancesSet.item[0].instanceState.name == "running"
0
+ puts ""
0
         puts "Instance #{instance_id} entered state 'running'"
0
       rescue
0
+ $stdout.print '.'
0
         sleep(10)
0
         tries += 1
0
         retry unless tries == 35
0
@@ -280,21 +272,44 @@ module Capsize
0
         rescue Timeout::Error
0
           raise "SSH timed out..."
0
         end
0
+ puts ""
0
         puts "SSH is up! Grabbing the public key..."
0
- if system "scp -i #{get_key_file} root@#{hostname_from_instance_id(instance_id)}:/mnt/openssh_id.pub #{get_key_file}.pub"
0
+ if system "scp -o StrictHostKeyChecking=no -i #{get_key_file} root@#{hostname_from_instance_id(instance_id)}:/mnt/openssh_id.pub #{get_key_file}.pub"
0
           puts "Public key saved at #{get_key_file}.pub"
0
         else
0
           puts "Error grabbing public key"
0
         end
0
- return instance
0
       rescue Exception => e
0
+ $stdout.print '.'
0
         sleep(10)
0
         tries += 1
0
         retry unless tries == 35
0
         puts "We couldn't ever SSH in!"
0
- return instance
0
+ end
0
+
0
+ #scripts
0
+ if File.exists?(fetch(:capsize_config_dir)+"/scripts")
0
+ begin
0
+ instance = amazon.describe_instances(:instance_id => instance_id)
0
+ instance.reservationSet.item.first.groupSet.item.map { |g| g.groupId }.sort.each do |group|
0
+ script_path = fetch(:capsize_config_dir)+"/scripts/#{group}"
0
+ if File.exists?(script_path)
0
+ begin
0
+ puts "Found script for security group #{group}, running"
0
+ system("scp -o StrictHostKeyChecking=no -i #{get_key_file} #{script_path} root@#{hostname_from_instance_id(instance_id)}:/tmp/") or raise "SCP ERROR"
0
+ system("ssh -o StrictHostKeyChecking=no -i #{get_key_file} root@#{hostname_from_instance_id(instance_id)} chmod o+x /tmp/#{group}") or raise "Error changing script permissions"
0
+ system("ssh -o StrictHostKeyChecking=no -i #{get_key_file} root@#{hostname_from_instance_id(instance_id)} /tmp/#{group}") or raise "Error running script"
0
+ rescue Exception => e
0
+ puts e
0
+ end
0
+ end
0
+ end
0
+ rescue Exception => e
0
+ puts e
0
+ end
0
       end
0
 
0
+ return instance
0
     end
0
 
0
 
0
@@ -389,6 +404,14 @@ module Capsize
0
         set :to_port, options[:from_port] if options[:to_port].nil? || options[:to_port].empty?
0
         options[:to_port] = to_port if options[:to_port].nil? || options[:to_port].empty?
0
       end
0
+
0
+ #if source_security_group_name and source_security_group_owner_id are specified, unset the incompatible options
0
+ if !options[:source_security_group_name].nil? && !options[:source_security_group_owner_id].nil?
0
+ options.delete(:ip_protocol)
0
+ options.delete(:from_port)
0
+ options.delete(:to_port)
0
+ options.delete(:cidr_ip)
0
+ end
0
 
0
       amazon.authorize_security_group_ingress(options)
0
 
0
@@ -422,6 +445,14 @@ module Capsize
0
         set :to_port, options[:from_port] if options[:to_port].nil? || options[:to_port].empty?
0
         options[:to_port] = to_port if options[:to_port].nil? || options[:to_port].empty?
0
       end
0
+
0
+ #if source_security_group_name and source_security_group_owner_id are specified, unset the incompatible options
0
+ if !options[:source_security_group_name].nil? && !options[:source_security_group_owner_id].nil?
0
+ options.delete(:ip_protocol)
0
+ options.delete(:from_port)
0
+ options.delete(:to_port)
0
+ options.delete(:cidr_ip)
0
+ end
0
 
0
       amazon.revoke_security_group_ingress(options)
0
 

Comments

    No one has commented yet.