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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/capistrano/puppeteer/aws.rb', line 27
def self.extended(configuration)
configuration.load do
set(:cloud_provider) { abort "Please specify a cloud provider, set :cloud_provider, 'AWS'" } unless exists? :cloud_provider
set(:aws_ami) { abort "Please specify a AWS AMI, set :aws_ami, 'ami-a29943cb'" } unless exists? :aws_ami
set(:aws_secret_access_key) { abort "Please specify an AWS Access Key, set :aws_secret_access_key, 'XXXX'" } unless exists? :aws_secret_access_key
set(:aws_access_key_id) { abort "Please specify a AWS AMI, set :aws_access_key_id, 'ZZZ'" } unless exists? :aws_access_key_id
set(:aws_region) { abort "Please specify a AWS AMI, set :aws_region, 'us-west-1'" } unless exists? :aws_availability_zone
set(:aws_availability_zone) { abort "Please specify a AWS AMI, set :aws_availability_zone, 'us-west-1a'" } unless exists? :aws_availability_zone
set(:aws_key_name) { abort "Please specify a AWS AMI, set :aws_key_name, 'default'" } unless exists? :aws_key_name
set(:aws_ssh_key) { abort "Please specify a AWS AMI, set :aws_ssh_key, 'config/aws.pem'" } unless exists? :aws_ssh_key
namespace :aws do
desc <<-DESC
create an AWS instance.
cap aws:create [OPTIONS]
Available options:
flavour (required) - The type of EC2 instance to create
name (required) - The name of the instance, this will be used as the AWS tag
iam_role - An IAM role to apply to the instance
DESC
task :create do
flavour = ENV['flavour'] || abort('please specify a flavour')
name = ENV['name'] || abort('please specify name')
iam_role = ENV['iam_role']
puts "Creating Instance..."
instance_options = {
:image_id => aws_ami,
:availability_zone => aws_availability_zone,
:flavor_id => flavour,
:key_name => aws_key_name,
:tags => { 'Name' => name },
}
instance_options[:iam_instance_profile_name] = iam_role if iam_role
server = servers.create instance_options
server.wait_for { ready? }
server.reload
ENV['HOSTS'] = server.public_ip_address
end
desc 'List Instance types'
task :flavours do
puts "%-11s %-11s %-7s %-5s %s" % %w[Name Price/Month RAM Units IO]
Capistrano::Puppeteer::AWS::FLAVOURS.each do |flavor, opts|
puts "%-11s $ %7.2f %4.1f GB %4.1f %s" % [flavor, opts[:price] * 720, opts[:ram], opts[:compute], opts[:io]]
end
end
desc 'List current AWS instances'
task :list do
format = '%-15s %-10s %-8s %-10s %-43s %-15s %-10s %-s %s'
puts format % %w{Name ID State Zone DNS IP Type CreatedAt ImageID}
servers.sort {|a,b| (a.tags['Name'] || 'Unknown') <=> (b.tags['Name'] || 'Unknown') }.each do |server|
puts format % [server.tags['Name'], server.id, server.state, server.availability_zone, server.dns_name, server.private_ip_address, server.flavor_id, server.created_at, server.image_id]
end
end
desc 'Describe an instance'
desc <<-DESC
Describe an AWS instance.
cap aws:show instance_id=...
DESC
task :show do
instance_id = ENV['instance_id'] || abort('provide an instance_id')
server = servers.get instance_id
p server
end
desc <<-DESC
Start an AWS instance.
cap aws:start instance_id=...
DESC
task :start do
instance_id = ENV['instance_id'] || abort('provide an instance_id')
server = servers.get instance_id
server.start
end
desc <<-DESC
Stop an AWS instance.
cap aws:stop instance_id=...
Options:
force=true - Forces a stop for a hung instance
DESC
task :stop do
instance_id = ENV['instance_id'] || abort('provide an instance_id')
force = ENV['force'] =~ /^true$/i
server = servers.get instance_id
server.stop force
end
desc <<-DESC
Destroy an AWS instance.
cap aws:destroy instance_id=...
DESC
task :destroy do
instance_id = ENV['instance_id'] || abort('provide an instance_id')
server = servers.get instance_id
server.destroy
end
desc <<-DESC
Reboot an AWS instance.
cap aws:reboot instance_id=...
Options:
force=true - Forces a stop for a hung instance
DESC
task :reboot do
instance_id = ENV['instance_id'] || abort('provide an instance_id')
force = ENV['force'] =~ /^true$/i
server = servers.get instance_id
server.reboot force
end
def compute
@compute ||= Fog::Compute.new(
:provider => cloud_provider,
:region => aws_region,
:aws_secret_access_key => aws_secret_access_key,
:aws_access_key_id => aws_access_key_id,
)
end
def servers
@servers ||= compute.servers
end
end
end
end
|