/
rudy-ec2
executable file
·271 lines (240 loc) · 10.4 KB
/
rudy-ec2
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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
269
270
#!/usr/bin/ruby
# = Rudy EC2
#
# === A Rudy interface to Amazon EC2
#
#
#
# See rudy-ec2 -h for usage
#
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib') # Put our local lib in first place
$:.unshift File.join(File.dirname(__FILE__), '..', 'vendor', 'highline-1.5.1', 'lib')
%w{caesars rye}.each { |dir| $:.unshift File.join(File.dirname(__FILE__), '..', '..', dir, 'lib') }
require 'rubygems'
require 'rudy'
require 'rudy/cli'
require 'drydock'
module RudyEC2CLI
extend Drydock
# -------------------------------------- RUDY-EC2 GLOBALS --------
# ------------------------------------------------------------------
global :A, :accesskey, String, "AWS Access Key"
global :S, :secretkey, String, "AWS Secret Access Key"
global :F, :format, String, "Output format"
#global :R, :region, String, "Connect to a specific EC2 region (ie: #{Rudy::DEFAULT_REGION})"
global :n, :nocolor, "Disable output colors"
global :f, :config, String, "Specify another configuration file to read (ie: #{Rudy::CONFIG_FILE})"
global :z, :zone, String, "Connect to a specific EC2 zone (ie: #{Rudy::DEFAULT_ZONE})"
global :q, :quiet, "Run with less output"
global :v, :verbose, "Increase verbosity of output (i.e. -v or -vv or -vvv)" do
@verbose ||= 0
@verbose += 1
end
global :V, :version, "Display version number" do
puts "Rudy version: #{Rudy::VERSION}"
exit 0
end
# ----------------------------------- AMAZON EC2 COMMANDS --------
# ------------------------------------------------------------------
usage "rudy-ec2 addresses"
usage "rudy-ec2 addresses -A -i instance-id address"
usage "rudy-ec2 addresses -A -i instance-id -n"
usage "rudy-ec2 addresses -N address"
usage "rudy-ec2 addresses -D address"
usage "rudy-ec2 addresses -C"
desc "Amazon EC2 Amazon Elastic IP addresses"
option :i, :instance, String, "Instance ID"
option :n, :newaddress, "Create and assign a new elastic IP"
action :A, :associate, "Associate an IP address to a running instance"
action :N, :disassociate, "Disassociate an IP address from an instance"
action :C, :create, "Request an IP address from Amazon"
action :D, :destroy, "Return an IP address to Amazon (this is permanent!)"
argv :ipaddress
command :addresses => Rudy::CLI::AWS::EC2::Addresses
command_alias :addresses, :address
command_alias :addresses, :a
usage "rudy-ec2 console [-g group] [instance ID]"
desc "Displays system console output for given instance(s)"
option :g, :group, String, "A security group name"
argv :awsid
command :consoles => Rudy::CLI::AWS::EC2::Instances
command_alias :consoles, :console
command_alias :consoles, :c
usage "rudy-ec2 copy [-p] [-r] [-u user] [-k path/2/privatekey] source target"
usage "rudy-ec2 upload -k path/2/privatekey -u username local-source target"
usage "rudy-ec2 download remote-source target"
usage "NOTE: Use quotes when using a tilda or asterisk in remote paths ('~/')."
usage "rudy-ec2 download '~/*' /tmp/"
desc "Copy files to or from machines."
option :r, :recursive, "Recursively copy entire directories"
option :p, :preserve, "Preserve atimes and ctimes."
option :d, :download, "Download FROM the remote machine to the local machine"
option :print, "Only print the SSH command, don't connect"
option :k, :pkey, String, "Path to the private SSH key"
option :g, :group, String, "A security group name"
option :i, :awsid, String, "An instance ID"
option :u, :user, String, "Username"
command :copy => Rudy::CLI::AWS::EC2::Candy
command_alias :copy, :scp
command_alias :copy, :upload
command_alias :copy, :download
usage "rudy-ec2 groups"
usage "rudy-ec2 groups -C [-p ports] [-a addresses] [-r protocols] name"
usage "rudy-ec2 groups -A [-p ports] [-a addresses] [-r protocols] name"
usage "rudy-ec2 groups -A -p 81,8000-9000 -a 127.0.0.1,127.0.0.2 name"
usage "rudy-ec2 groups -A -g default -o 123456789012 name"
usage "rudy-ec2 groups -R -g default name"
usage "rudy-ec2 groups -D name"
desc "Amazon EC2 EC2 Security Groups"
option :all, "Display all security groups"
option :r, :protocols, Array, "List of protocols. One of: tcp (default), udp, icmp"
option :p, :ports, Array, "List of port ranges (default: 22,80,443)"
option :a, :addresses, Array, "List of IP addresses (default: your current external IP)"
option :g, :group, String, "Other group to authorize or revoke. Use with -o!"
option :o, :owner, String, "Other group owner ID (account number). Use with -g!"
option :d, :description, String, "A group description"
action :C, :create, "Create a security group"
action :D, :destroy, "Destroy a security group"
action :A, :authorize, "Authorize a rule for a security group"
action :R, :revoke, "Revoke a rule for a security group"
argv :name
command :groups => Rudy::CLI::AWS::EC2::Groups
command_alias :groups, :group
command_alias :groups, :g
usage "rudy-ec2 im"
usage "rudy-ec2 image"
usage "rudy-ec2 images image-id"
usage "rudy-ec2 images -o 123456789012"
usage "rudy-ec2 images -o amazon"
usage "rudy-ec2 images -o self"
desc "Amazon EC2 Machine Images (AMIs)"
option :o, :owner, String, "Amazon Account Number or one of: amazon, self"
#option :p, :private, "Private images only"
#option :b, :bucket_name, String, "The name of the bucket that will store the image"
#action :C, :create, "Create an image"
#action :P, :prepare, "Prepare a running instance to be used as an image"
#action :D, :destroy, "Deregister an image (does not remove image files from S3)"
argv :ami
command :images => Rudy::CLI::AWS::EC2::Images
command_alias :images, :image
command_alias :images, :im
desc "Amazon EC2 Instances"
usage "rudy-ec2 i"
usage "rudy-ec2 instance"
usage "rudy-ec2 instances"
usage "rudy-ec2 instances [-g group-name] [instance-ID]"
usage "rudy-ec2 instances -C -g group-name -s m1.small -k keypair-name"
usage "rudy-ec2 instances -C -a 255.255.255.255 -k keypair-name"
usage "rudy-ec2 instances -R instance-ID"
usage "rudy-ec2 instances -D instance-ID"
usage "rudy-ec2 instances -R -g group-name"
usage "rudy-ec2 instances -D -g group-name"
option :g, :group, String, "The security group name"
option :m, :ami, String, "The machine image ID (ami)"
option :s, :size, String, "The instance size (default: m1.small)"
option :k, :keypair, String, "The SSH keypair to use for launch"
option :a, :address, String, "The IP address to associate to the new instance"
option :n, :newaddress, "Create and assign a new elastic IP"
option :p, :private, "Do not give the instance a public IP address"
action :C, :create, "Create a machine instance"
action :R, :restart, "Restart a machine instance"
action :D, :destroy, "Destroy a machine instance"
argv :instid
command :instances => Rudy::CLI::AWS::EC2::Instances
command_alias :instances, :instance
command_alias :instances, :in
command_alias :instances, :i
desc "Amazon EC2 KeyPairs"
usage "rudy-ec2 keypairs"
usage "rudy-ec2 keypairs -C name"
usage "rudy-ec2 keypairs -D name"
action :D, :destroy, "Unregister keypair from EC2 (this is permanent!)"
action :C, :create, "Create and register a keypair with EC2"
argv :name
command :keypairs => Rudy::CLI::AWS::EC2::KeyPairs
command_alias :keypairs, :keypair
command_alias :keypairs, :k
command :money
desc "Amazon EC2 Snapshots"
usage "rudy-ec2 snapshots"
usage "rudy-ec2 snapshots -C -v volume-id"
usage "rudy-ec2 snapshots -D snapshot-id"
option :v, :volume, String, "Volume ID"
action :D, :destroy, "Destroy a snapshot (this is permanent!)"
action :C, :create, "Create a snapshot"
argv :snapid
command :snapshots => Rudy::CLI::AWS::EC2::Snapshots
command_alias :snapshots, :snapshot
command_alias :snapshots, :t
usage "rudy-ec2 ssh"
usage "rudy-ec2 ssh -k path/2/key -u root"
usage "rudy-ec2 ssh -k path/2/key -u root uname -a"
usage "rudy-ec2 ssh -g group-name"
usage "rudy-ec2 ssh -i instance-id"
desc "Open an SSH connection"
option :print, "Only print the SSH command, don't connect"
option :g, :group, String, "A security group name"
option :i, :instid, String, "An instance ID"
option :k, :pkey, String, "Path to the private SSH key"
option :u, :user, String, "Username to connect as"
option :print, "Print SSH command. Don't connect."
argv :cmd
command :ssh => Rudy::CLI::AWS::EC2::Candy
command_alias :ssh, :connect
desc "Amazon EC2 status"
command :status => Rudy::CLI::AWS::EC2::Instances
desc "Amazon EC2 Volumes"
usage "rudy-ec2 volumes"
usage "rudy-ec2 volumes -C -s size [-d device-path]"
usage "rudy-ec2 volumes -A volume-id instance-id"
usage "rudy-ec2 volumes -N volume-id"
usage "rudy-ec2 volumes -D volume-id"
option :i, :instance, String, "Instance ID"
option :s, :size, String, "Size (in GB)"
option :t, :snapshot, String, "Snapshot ID (snap)"
option :d, :device, String, "Device path (default: /dev/sdh)"
action :D, :destroy, "Destroy a volume (this is permanent!)"
action :C, :create, "Create a volume"
action :A, :attach, "Attach a volume to a running instance"
action :N, :detach, "Detach a volume from an instance"
argv :volid
command :volumes => Rudy::CLI::AWS::EC2::Volumes
command_alias :volumes, :volume
command_alias :volumes, :v
desc "Availability Zones"
argv :name
command :zones => Rudy::CLI::AWS::EC2::Zones
command_alias :zones, :zone
command_alias :zones, :z
# -------------------------------- RUDY-EC2 MISCELLANEOUS --------
# ------------------------------------------------------------------
default :instances
debug :on
before do |obj|
obj.global.print_header = false # Don't print Rudy Header
@start = Time.now
end
after do |obj|
if obj.global.verbose > 0
puts
@elapsed = Time.now - @start
puts "Elapsed: %.2f seconds" % @elapsed.to_f if @elapsed > 0.1
end
end
end
puts Rudy.banner("THIS IS RUBY #{RUBY_VERSION}") if Drydock.debug?
# We call Drydock specifically otherwise it will run at_exit. Rye also
# uses at_exit for shutting down the ssh-agent. Ruby executes at_exit
# blocks in reverse order so if Drydock is required first, it's block
# will run after Rye shuts down the ssh-agent.
begin
Drydock.run!(ARGV, STDIN) if Drydock.run? && !Drydock.has_run?
rescue Drydock::ArgError, Drydock::OptError => ex
STDERR.puts ex.message
STDERR.puts ex.usage
rescue => ex
STDERR.puts "ERROR (#{ex.class.to_s}): #{ex.message}"
STDERR.puts ex.backtrace if Drydock.debug?
rescue SystemExit
# Don't balk
end