Module: CapistranoPlugin

Defined in:
lib/capazon/capistrano_plugin.rb

Instance Method Summary collapse

Instance Method Details

#authorize_access(auth, group_name, ip_protocol, from_port, to_port, cidr_ip = "0.0.0.0/0") ⇒ Object



49
50
51
52
53
54
# File 'lib/capazon/capistrano_plugin.rb', line 49

def authorize_access(auth,group_name,ip_protocol,from_port,to_port,cidr_ip="0.0.0.0/0")
  amazon = connect(auth)
  web_security_response = amazon.authorize_security_group_ingress("", :groupName => group_name, :ipProtocol => ip_protocol, :fromPort => from_port, :toPort => to_port, :cidrIp => cidr_ip).parse.to_s
  raise "Failed Authorizing Web Access" unless web_security_response == "Ingress authorized."
  puts "Access Granted for #{group_name} group on interface #{cidr_ip} for #{ip_protocol} port(s) #{from_port} to #{to_port}."
end

#connect(auth) ⇒ Object

returns an EC2::AWSAuthConnection object

Raises:

  • (Exception)


12
13
14
15
16
# File 'lib/capazon/capistrano_plugin.rb', line 12

def connect(auth)
  amazon = EC2::AWSAuthConnection.new(auth[:AWS_ACCESS_KEY_ID], auth[:AWS_SECRET_ACCESS_KEY])
  raise Exception, "Connection to AWS failed. Check your Access Key ID and Secret Access Key - http://aws.amazon.com/" if amazon.nil?
  return amazon
end

#delete_keypair(auth, keypair_name) ⇒ Object

Raises:

  • (Exception)


40
41
42
43
44
45
46
47
# File 'lib/capazon/capistrano_plugin.rb', line 40

def delete_keypair(auth,keypair_name)
  amazon = connect(auth)
      
  raise Exception, "Keypair #{keypair_name} does not exist." if amazon.describe_keypairs(keypair_name).parse.empty?
  keypair_response = amazon.delete_keypair(keypair_name).parse.to_s
  raise Exception, "Failed Authorizing Web Access" unless keypair_response == "Keypair deleted."
  puts "Keypair \"#{keypair_name}\" deleted"
end

#describe_images(auth, imageIds = [], owners = [], executableBy = []) ⇒ Object



85
86
87
88
# File 'lib/capazon/capistrano_plugin.rb', line 85

def describe_images(auth,imageIds=[], owners=[], executableBy=[])
  amazon = connect(auth)
  amazon.describe_images(imageIds,owners,executableBy).parse
end

#describe_instances(auth, instanceIds = []) ⇒ Object



90
91
92
93
# File 'lib/capazon/capistrano_plugin.rb', line 90

def describe_instances(auth,instanceIds=[])
  amazon = connect(auth)
  amazon.describe_instances(instanceIds).parse
end

#describe_keypairs(auth, keyNames = []) ⇒ Object

def reboot_instances(*instance_ids)

puts "not yet implmented"

end



99
100
101
102
# File 'lib/capazon/capistrano_plugin.rb', line 99

def describe_keypairs(auth,keyNames=[])
  amazon = connect(auth)    
  amazon.describe_keypairs(keyNames).parse
end

#run_instance(auth, ami_id, keypair_name, group_ids = [], min_count = 1, max_count = 1, user_data = nil, base64_encoded = false) ⇒ Object

Raises:

  • (Exception)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/capazon/capistrano_plugin.rb', line 56

def run_instance(auth,ami_id,keypair_name,group_ids=[],min_count=1,max_count=1,user_data=nil,base64_encoded=false)
  amazon = connect(auth)
  instance = amazon.run_instances(ami_id, :minCount=>min_count, :maxCount=>max_count, :keyname=>keypair_name, :groupIds=>group_ids, :userData=>user_data, :base64Encoded=>base64_encoded).parse[1]
  raise Exception, "Instance did not start" unless instance[4] == "pending"
  instance_id = instance[1]
  puts "Instance #{instance_id} Startup Pending"
  
  #loop checking for instance startup
  puts "Checking every 10 seconds to detect startup for up to 5 minutes"
  tries = 0
  begin
    instance_desc = amazon.describe_instances.parse.select { |i| i[1] == instance_id.to_s }[0]
    raise "Server Not Running" unless instance_desc[4] == "running"
    sleep 5
    return instance_desc
  rescue
    puts "."
    sleep 10
    tries += 1
    retry unless tries == 35
    raise "Server Not Running"
  end
end

#setup_keypair(auth, keypair_name, private_key_path = nil) ⇒ Object

Raises:

  • (Exception)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/capazon/capistrano_plugin.rb', line 18

def setup_keypair(auth,keypair_name,private_key_path=nil)
  amazon = connect(auth)
            
  #verify keypair doesn't already exist
  raise Exception, "Keypair #{keypair_name} already exists." unless amazon.describe_keypairs(keypair_name).parse.empty?
  
  #create keypair
  private_key = amazon.create_keypair(keypair_name)
  raise Exception, "Private Key not correctly generated" unless private_key.parse[0][0] == "KEYPAIR"
  puts "Keypair \"#{keypair_name}\" generated"
  
  unless private_key_path == nil
    #write private key to file
    text_private_key = private_key.parse.inject("") { |text_private_key, a| a.join("\t") + "\n" }
    File.open(private_key_path, 'w') do |file|
      file.write(text_private_key)
    end
    system "chmod 600 #{private_key_path}"
    puts "Written to ./#{private_key_path}"
  end
end

#terminate_instance(auth, instance_id) ⇒ Object



80
81
82
83
# File 'lib/capazon/capistrano_plugin.rb', line 80

def terminate_instance(auth,instance_id)
  amazon = connect(auth)    
  amazon.terminate_instances(instance_id).parse
end