/
ec2.rb
113 lines (99 loc) · 3.61 KB
/
ec2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
Capistrano::Configuration.instance(:must_exist).load do
namespace :ec2 do
desc "Set up a new EC2 instance and provision"
task :default, :roles => :web do
ec2.key_pair.install
ec2.instance.create
ec2.security.group.setup if yes("Set up the default security group? You only need to do this once.")
exit unless yes("Add the instance's IP address to config/deploy.rb. Continue?")
puts space(msg(:ec2_finished))
end
namespace :instance do
desc "Create a fresh Hardy instance"
task :create do
options = {
:image_id => ask('Press enter for Ubuntu Hardy or enter an AMI image id: ', 'ami-1c5db975'),
:key_name => "#{application}"
}
instance = ec2_api.run_instances(options).instancesSet.item[0]
instance_id = instance.instanceId
pp instance
ip = ec2_api.allocate_address.publicIp
ec2_api.associate_address(:instance_id => instance_id, :public_ip => ip)
puts "\nYour instance id is: #{instance_id}"
puts "Your IP address is: #{ip}"
end
desc "Restart an instance"
task :restart do
ec2.instances
pp ec2_api.reboot_instances(:instance_id => ask("Restart which instance ids?"))
end
desc "Destroy an instance"
task :destroy do
ec2.instances
instance_id = ask("Terminate which instance ids?")
ec2_api.terminate_instances(:instance_id => instance_id)
ip = ec2_api.describe_addresses.addressesSet.item.select { |x| x.instanceId == instance_id }.first
ec2_api.release_address(:public_ip => ip.publicIp) if ip
end
end
desc "List your EC2 instances"
task :instances do
pp ec2_api.describe_instances
end
desc "List IPs for this EC2 account"
task :ips do
pp ec2_api.describe_addresses
end
namespace :key_pair do
desc "Install key pair for SSH"
task :install do
begin
out = ec2_api.create_keypair(:key_name => application.to_s)
key = out.keyMaterial
rescue EC2::InvalidKeyPairDuplicate
ec2.key_pair.remove
ec2.key_pair.install
end
File.open(File.expand_path("~/.ssh/id_rsa-#{application}"), 'w') { |f| f.write key }
end
desc "Install key pair for SSH"
task :remove do
ec2_api.delete_keypair(:key_name => application.to_s)
`rm ~/.ssh/id_rsa-#{application}`
end
end
namespace :security do
namespace :group do
desc "Open standard ports for default security group"
task :setup do
[ 22, 80, 443 ].each do |port|
ec2_api.authorize_security_group_ingress(
:group_name => 'default', :cidr_ip => '0.0.0.0/0', :from_port => port, :to_port => port, :ip_protocol => 'tcp'
)
end
end
desc "Describe default security group"
task :describe do
pp ec2_api.describe_security_groups(:group_name => 'default')
end
end
end
namespace :api_tools do
desc "Install ec2 api tools locally"
task :install, :roles => :web do
`cd ~ && curl http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip -O`
`cd ~ && unzip ec2-api-tools.zip`
`cd ~ && rm ec2-api-tools.zip`
`mv ~/ec2-api-tools-* ~/.ec2`
end
desc "Install ec2 api tools locally"
task :remove, :roles => :web do
`rm -Rf ~/.ec2`
end
end
def ec2_api
@ec2_api ||= EC2::Base.new(:access_key_id => ec2_access_key, :secret_access_key => ec2_secret_key)
end
end
end