solutious-rudy 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/CHANGES.txt +19 -1
  2. data/README.rdoc +66 -35
  3. data/Rakefile +1 -0
  4. data/Rudyfile +97 -6
  5. data/bin/ird +7 -4
  6. data/bin/rudy +120 -212
  7. data/bin/rudy-ec2 +240 -0
  8. data/bin/rudy-s3 +76 -0
  9. data/bin/rudy-sdb +67 -0
  10. data/lib/annoy.rb +61 -18
  11. data/lib/console.rb +10 -5
  12. data/lib/rudy/aws/ec2/address.rb +76 -40
  13. data/lib/rudy/aws/ec2/group.rb +138 -78
  14. data/lib/rudy/aws/ec2/image.rb +134 -12
  15. data/lib/rudy/aws/ec2/instance.rb +116 -89
  16. data/lib/rudy/aws/ec2/keypair.rb +26 -14
  17. data/lib/rudy/aws/ec2/snapshot.rb +23 -12
  18. data/lib/rudy/aws/ec2/volume.rb +198 -202
  19. data/lib/rudy/aws/ec2/zone.rb +77 -0
  20. data/lib/rudy/aws/ec2.rb +56 -25
  21. data/lib/rudy/aws/s3.rb +54 -0
  22. data/lib/rudy/aws/sdb/error.rb +46 -0
  23. data/lib/rudy/aws/sdb.rb +298 -0
  24. data/lib/rudy/aws.rb +29 -57
  25. data/lib/rudy/{metadata/backup.rb → backup.rb} +8 -8
  26. data/lib/rudy/cli/aws/ec2/addresses.rb +105 -0
  27. data/lib/rudy/cli/aws/ec2/candy.rb +208 -0
  28. data/lib/rudy/cli/aws/ec2/groups.rb +121 -0
  29. data/lib/rudy/cli/aws/ec2/images.rb +196 -0
  30. data/lib/rudy/cli/aws/ec2/instances.rb +194 -0
  31. data/lib/rudy/cli/aws/ec2/keypairs.rb +53 -0
  32. data/lib/rudy/cli/aws/ec2/snapshots.rb +49 -0
  33. data/lib/rudy/cli/aws/ec2/volumes.rb +104 -0
  34. data/lib/rudy/cli/aws/ec2/zones.rb +22 -0
  35. data/lib/rudy/cli/aws/s3/buckets.rb +50 -0
  36. data/lib/rudy/cli/aws/s3/store.rb +22 -0
  37. data/lib/rudy/cli/aws/sdb/domains.rb +41 -0
  38. data/lib/rudy/cli/{deploy.rb → candy.rb} +2 -6
  39. data/lib/rudy/cli/config.rb +25 -20
  40. data/lib/rudy/cli/disks.rb +18 -108
  41. data/lib/rudy/cli/machines.rb +94 -0
  42. data/lib/rudy/cli/routines.rb +47 -70
  43. data/lib/rudy/cli.rb +104 -89
  44. data/lib/rudy/config/objects.rb +5 -43
  45. data/lib/rudy/config.rb +8 -24
  46. data/lib/rudy/disks.rb +248 -0
  47. data/lib/rudy/global.rb +121 -0
  48. data/lib/rudy/huxtable.rb +232 -147
  49. data/lib/rudy/machines.rb +245 -0
  50. data/lib/rudy/metadata.rb +136 -0
  51. data/lib/rudy/routines/helpers/diskhelper.rb +101 -0
  52. data/lib/rudy/routines/helpers/scripthelper.rb +91 -0
  53. data/lib/rudy/routines/release.rb +27 -8
  54. data/lib/rudy/routines/shutdown.rb +47 -32
  55. data/lib/rudy/routines/startup.rb +47 -37
  56. data/lib/rudy/routines.rb +30 -37
  57. data/lib/rudy/scm/svn.rb +1 -1
  58. data/lib/rudy/utils.rb +262 -4
  59. data/lib/rudy.rb +76 -248
  60. data/lib/storable.rb +19 -16
  61. data/lib/sysinfo.rb +1 -1
  62. data/rudy.gemspec +88 -68
  63. data/support/rudy-ec2-startup +5 -5
  64. data/test/05_config/00_setup_test.rb +3 -7
  65. data/test/20_sdb/00_setup_test.rb +2 -17
  66. data/test/20_sdb/10_domains_test.rb +18 -16
  67. data/test/25_ec2/00_setup_test.rb +5 -10
  68. data/test/25_ec2/10_keypairs_test.rb +13 -5
  69. data/test/25_ec2/20_groups_test.rb +48 -56
  70. data/test/25_ec2/30_addresses_test.rb +13 -10
  71. data/test/25_ec2/40_volumes_test.rb +11 -8
  72. data/test/25_ec2/50_snapshots_test.rb +17 -12
  73. data/test/26_ec2_instances/00_setup_test.rb +3 -8
  74. data/test/26_ec2_instances/10_instances_test.rb +21 -19
  75. data/test/30_sdb_metadata/00_setup_test.rb +2 -9
  76. data/test/30_sdb_metadata/10_disks_test.rb +47 -37
  77. data/test/30_sdb_metadata/20_backups_test.rb +9 -9
  78. data/test/helper.rb +5 -3
  79. data/vendor/highline-1.5.1/CHANGELOG +222 -0
  80. data/vendor/highline-1.5.1/INSTALL +35 -0
  81. data/vendor/highline-1.5.1/LICENSE +7 -0
  82. data/vendor/highline-1.5.1/README +63 -0
  83. data/vendor/highline-1.5.1/Rakefile +82 -0
  84. data/vendor/highline-1.5.1/TODO +6 -0
  85. data/vendor/highline-1.5.1/examples/ansi_colors.rb +38 -0
  86. data/vendor/highline-1.5.1/examples/asking_for_arrays.rb +18 -0
  87. data/vendor/highline-1.5.1/examples/basic_usage.rb +75 -0
  88. data/vendor/highline-1.5.1/examples/color_scheme.rb +32 -0
  89. data/vendor/highline-1.5.1/examples/limit.rb +12 -0
  90. data/vendor/highline-1.5.1/examples/menus.rb +65 -0
  91. data/vendor/highline-1.5.1/examples/overwrite.rb +19 -0
  92. data/vendor/highline-1.5.1/examples/page_and_wrap.rb +322 -0
  93. data/vendor/highline-1.5.1/examples/password.rb +7 -0
  94. data/vendor/highline-1.5.1/examples/trapping_eof.rb +22 -0
  95. data/vendor/highline-1.5.1/examples/using_readline.rb +17 -0
  96. data/vendor/highline-1.5.1/lib/highline/color_scheme.rb +120 -0
  97. data/vendor/highline-1.5.1/lib/highline/compatibility.rb +17 -0
  98. data/vendor/highline-1.5.1/lib/highline/import.rb +43 -0
  99. data/vendor/highline-1.5.1/lib/highline/menu.rb +395 -0
  100. data/vendor/highline-1.5.1/lib/highline/question.rb +463 -0
  101. data/vendor/highline-1.5.1/lib/highline/system_extensions.rb +193 -0
  102. data/vendor/highline-1.5.1/lib/highline.rb +758 -0
  103. data/vendor/highline-1.5.1/setup.rb +1360 -0
  104. data/vendor/highline-1.5.1/test/tc_color_scheme.rb +56 -0
  105. data/vendor/highline-1.5.1/test/tc_highline.rb +823 -0
  106. data/vendor/highline-1.5.1/test/tc_import.rb +54 -0
  107. data/vendor/highline-1.5.1/test/tc_menu.rb +429 -0
  108. data/vendor/highline-1.5.1/test/ts_all.rb +15 -0
  109. metadata +81 -69
  110. data/lib/aws_sdb/error.rb +0 -42
  111. data/lib/aws_sdb/service.rb +0 -215
  112. data/lib/aws_sdb.rb +0 -3
  113. data/lib/rudy/aws/simpledb.rb +0 -71
  114. data/lib/rudy/cli/addresses.rb +0 -85
  115. data/lib/rudy/cli/backups.rb +0 -175
  116. data/lib/rudy/cli/domains.rb +0 -17
  117. data/lib/rudy/cli/groups.rb +0 -77
  118. data/lib/rudy/cli/images.rb +0 -111
  119. data/lib/rudy/cli/instances.rb +0 -142
  120. data/lib/rudy/cli/keypairs.rb +0 -47
  121. data/lib/rudy/cli/manager.rb +0 -51
  122. data/lib/rudy/cli/release.rb +0 -174
  123. data/lib/rudy/cli/volumes.rb +0 -121
  124. data/lib/rudy/command/addresses.rb +0 -69
  125. data/lib/rudy/command/backups.rb +0 -65
  126. data/lib/rudy/command/disks-old.rb +0 -322
  127. data/lib/rudy/command/disks.rb +0 -9
  128. data/lib/rudy/command/domains.rb +0 -34
  129. data/lib/rudy/command/groups.rb +0 -118
  130. data/lib/rudy/command/instances.rb +0 -278
  131. data/lib/rudy/command/keypairs.rb +0 -149
  132. data/lib/rudy/command/manager.rb +0 -65
  133. data/lib/rudy/command/volumes.rb +0 -127
  134. data/lib/rudy/metadata/disk.rb +0 -149
  135. data/lib/rudy/metadata/machine.rb +0 -34
  136. data/lib/rudy/routines/disk_handler.rb +0 -190
  137. data/lib/rudy/routines/script_runner.rb +0 -65
  138. data/test/50_commands/00_setup_test.rb +0 -11
  139. data/test/50_commands/10_keypairs_test.rb +0 -79
  140. data/test/50_commands/20_groups_test.rb +0 -77
  141. data/test/50_commands/40_volumes_test.rb +0 -55
  142. data/test/50_commands/50_instances_test.rb +0 -110
  143. data/tryouts/console_tryout.rb +0 -91
  144. data/tryouts/disks.rb +0 -55
  145. data/tryouts/nested_methods.rb +0 -36
  146. data/tryouts/session_tryout.rb +0 -46
@@ -0,0 +1,208 @@
1
+
2
+ module Rudy; module CLI;
3
+ module AWS; module EC2;
4
+
5
+ class Candy < Rudy::CLI::CommandBase
6
+
7
+ def status_valid?
8
+ avail = Rudy::Utils.service_available?('status.aws.amazon.com', 80, 5)
9
+ raise ServiceUnavailable, 'status.aws.amazon.com' unless avail
10
+ true
11
+ end
12
+ def status
13
+ url = 'http://status.aws.amazon.com/rss/EC2.rss'
14
+ # TODO: Move to Rudy::AWS
15
+ ec2 = Rudy::Utils::RSSReader.run(url) || {}
16
+
17
+ # TODO: Create Storable object
18
+ if @@global.format == 'yaml'
19
+ puts ec2.to_yaml
20
+ elsif @@global.format == 'json'
21
+ require 'json'
22
+ puts ec2.to_json
23
+ else
24
+ puts "Updated: #{ec2[:pubdate]} (updated every #{ec2[:ttl]} minutes)"
25
+ ec2[:items].each do |i|
26
+ puts
27
+ puts '%s' % i[:title]
28
+ puts ' %s: %s' % [i[:pubdate], i[:description]]
29
+
30
+ end
31
+ end
32
+ end
33
+
34
+ def ssh_valid?
35
+ if @@global.pkey
36
+ raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
37
+ raise "Insecure permissions for #{@@global.pkey}" unless (File.stat(@@global.pkey).mode & 600) == 0
38
+ end
39
+ if @option.group
40
+ rgroup = Rudy::AWS::EC2::Groups.new(@@global.accesskey, @@global.secretkey, @@global.region)
41
+ raise "Cannot supply group and instance ID" if @option.instid
42
+ raise "Group #{@option.group} does not exist" unless rgroup.exists?(@option.group)
43
+ end
44
+ if @option.instid && !Rudy::Utils.is_id?(:instance, @option.instid)
45
+ raise "#{@option.instid} is not an instance ID"
46
+ end
47
+ true
48
+ end
49
+ def ssh
50
+ opts = {}
51
+ opts[:group] = @option.group if @option.group
52
+ opts[:group] = :any if @option.all
53
+ opts[:id] = @option.instid if @option.instid
54
+
55
+ # Options to be sent to Net::SSH
56
+ ssh_opts = { :user => @global.user || Rudy.sysinfo.user, :debug => nil }
57
+ if @@global.pkey
58
+ raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
59
+ raise InsecureKeyPermissions, @@global.pkey unless File.stat(@@global.pkey).mode == 33152
60
+ ssh_opts[:keys] = @@global.pkey
61
+ end
62
+
63
+
64
+ # The user specified a command to run. We won't create an interactive
65
+ # session so we need to prepare the command and its arguments
66
+ if @argv.first
67
+ command, command_args = @argv.shift, @argv || []
68
+ puts "#{command} #{command_args.join(' ')}" if @@global.verbose > 1
69
+
70
+ # otherwise, we'll open an ssh session or print command
71
+ else
72
+ command, command_args = :interactive_ssh, @option.print.nil?
73
+ end
74
+
75
+ checked = false
76
+ rudy = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
77
+ lt = rudy.list_group(opts[:group], :running, opts[:id]) do |inst|
78
+
79
+ # Print header
80
+ if @@global.quiet
81
+ print "You are #{ssh_opts[:user].bright}. " if !checked # only the 1st
82
+ else
83
+ print "Connecting #{ssh_opts[:user].bright}@#{inst.dns_public} "
84
+ puts "(#{inst.awsid}, groups: #{inst.groups.join(', ')})"
85
+ end
86
+
87
+ # Make sure we want to run this command on all instances
88
+ if !checked && command != :interactive_ssh
89
+ execute_check(:medium) if ssh_opts[:user] == "root"
90
+ checked = true
91
+ end
92
+
93
+ # Open the connection and run the command
94
+ rbox = Rye::Box.new(inst.dns_public, ssh_opts)
95
+ ret = rbox.send(command, command_args)
96
+ puts ret unless command == :interactive_ssh
97
+ end
98
+ end
99
+
100
+ def copy_valid?
101
+ raise "You must supply a source and a target. See rudy-ec2 #{@alias} -h" unless @argv.size >= 2
102
+ raise "You cannot download and upload at the same time" if @option.download && @alias == 'upload'
103
+ raise "You cannot download and upload at the same time" if @option.upload && @alias == 'download'
104
+ true
105
+ end
106
+ def copy
107
+
108
+ opts = {}
109
+ opts[:group] = @option.group if @option.group
110
+ opts[:group] = :any if @option.all
111
+
112
+ opts[:id] = @argv.shift if Rudy::Utils.is_id?(:instance, @argv.first)
113
+ opts[:id] &&= [opts[:id]].flatten
114
+
115
+ # * +:recursive: recursively transfer directories (default: false)
116
+ # * +:preserve: preserve atimes and ctimes (default: false)
117
+ # * +:task+ one of: :upload (default), :download.
118
+ # * +:paths+ an array of paths to copy. The last element is the "to" path.
119
+ opts[:recursive] = @option.recursive ? true : false
120
+ opts[:preserve] = @option.preserve ? true : false
121
+
122
+ opts[:paths] = @argv
123
+ opts[:dest] = opts[:paths].pop
124
+
125
+ opts[:task] = :download if %w(dl download).member?(@alias) || @option.download
126
+ opts[:task] = :upload if %w(ul upload).member?(@alias)
127
+ opts[:task] ||= :upload
128
+ opts[:user] = @global.user || Rudy.sysinfo.user
129
+
130
+ # Options to be sent to Net::SSH
131
+ ssh_opts = { :user => opts[:user], :debug => nil }
132
+ ssh_opts[:keys] = @@global.pkey if @@global.pkey
133
+
134
+ if @@global.pkey
135
+ raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
136
+ raise "Insecure permissions for #{@@global.pkey}" unless (File.stat(@@global.pkey).mode & 600) == 0
137
+ end
138
+
139
+ checked = false
140
+ rudy = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
141
+ lt = rudy.list_group(opts[:group], :running, opts[:id]) do |inst|
142
+
143
+ if @option.print
144
+ Rudy::Utils.scp_command inst.dns_public, @@global.pkey, opts[:user], opts[:paths], opts[:dest], (opts[:task] == :download), false, @option.print
145
+ next
146
+ end
147
+
148
+ # Print header
149
+ if @@global.quiet
150
+ print "You are #{ssh_opts[:user].bright}. " if !checked # only the 1st
151
+ else
152
+ print "Connecting #{ssh_opts[:user].bright}@#{inst.dns_public} "
153
+ puts "(#{inst.awsid}, groups: #{inst.groups.join(', ')})"
154
+ end
155
+
156
+ # Make sure we want to run this command on all instances
157
+ if !checked
158
+ #execute_check(:medium) if opts[:user] == "root"
159
+ checked = true
160
+ end
161
+
162
+ scp_opts = {
163
+ :recursive => opts[:recursive],
164
+ :preserve => opts[:preserve],
165
+ :chunk_size => 16384
166
+ }
167
+
168
+ Candy.scp(opts[:task], inst.dns_public, opts[:user], @@global.pkey, opts[:paths], opts[:dest], scp_opts)
169
+ puts
170
+ puts unless @@global.quiet
171
+ end
172
+
173
+ end
174
+
175
+
176
+ private
177
+
178
+ def Candy.scp(task, host, user, keypairpath, paths, dest, opts)
179
+
180
+ connect_opts = {}
181
+ connect_opts[:keys] = [keypairpath] if keypairpath
182
+
183
+ Net::SCP.start(host, user, connect_opts) do |scp|
184
+
185
+ paths.each do |path|
186
+ prev_path = nil
187
+ scp.send("#{task}", path, dest, opts) do |ch, name, sent, total|
188
+ #print "#{name}: #{sent}/#{total}\r"
189
+ msg = ((prev_path == name) ? "\r" : "\n") # new line for new file
190
+ msg << "#{name}: #{sent}/#{total}" # otherwise, update the same line
191
+ print msg
192
+ STDOUT.flush # update the screen every cycle
193
+ prev_path = name
194
+ break if sent == total
195
+ end
196
+ puts unless prev_path == path
197
+ end
198
+
199
+ end
200
+ end
201
+
202
+
203
+
204
+ end
205
+
206
+
207
+ end; end
208
+ end; end
@@ -0,0 +1,121 @@
1
+
2
+ module Rudy; module CLI;
3
+ module AWS; module EC2;
4
+
5
+ class Groups < Rudy::CLI::CommandBase
6
+
7
+
8
+
9
+ def create_groups_valid?
10
+ @rgroups = Rudy::AWS::EC2::Groups.new(@@global.accesskey, @@global.secretkey, @@global.region)
11
+ raise Drydock::ArgError.new('group name', @alias) unless @argv.name
12
+ raise "Group #{@argv.name} alread exists" if @rgroups.exists?(@argv.name)
13
+ true
14
+ end
15
+ def create_groups
16
+ opts = check_options
17
+ puts "Creating #{@argv.name}"
18
+
19
+ execute_action {
20
+ @rgroups.create(@argv.name, @option.description, opts[:addresses], opts[:ports], opts[:protocols])
21
+ }
22
+
23
+ @rgroups.list(@argv.name) do |group|
24
+ puts @@global.verbose > 0 ? group.inspect : group.dump(@@global.format)
25
+ end
26
+ end
27
+
28
+
29
+ def destroy_groups_valid?
30
+ @rgroups = Rudy::AWS::EC2::Groups.new(@@global.accesskey, @@global.secretkey, @@global.region)
31
+ raise Drydock::ArgError.new('group name', @alias) unless @argv.name
32
+ raise "Group #{@argv.name} does not exist" unless @rgroups.exists?(@argv.name)
33
+ true
34
+ end
35
+
36
+ def destroy_groups
37
+ puts "Destroying group: #{@argv.name}"
38
+ execute_check(:medium)
39
+ execute_action { @rgroups.destroy(@argv.name) }
40
+ @argv.clear # so groups will print all other groups
41
+ groups
42
+ end
43
+
44
+ def revoke_groups_valid?; modify_group_valid?; end
45
+ def revoke_groups; modify_group(:revoke); end
46
+
47
+ def authorize_groups_valid?; modify_group_valid?; end
48
+ def authorize_groups; modify_group(:authorize); end
49
+
50
+ def groups
51
+ opts = {}
52
+ name = @option.all ? nil : @argv.name
53
+ rgroups = Rudy::AWS::EC2::Groups.new(@@global.accesskey, @@global.secretkey, @@global.region)
54
+ rgroups.list(name).each do |group|
55
+ puts @@global.verbose > 0 ? group.inspect : group.dump(@@global.format)
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def modify_group_valid?
62
+ if @option.owner == 'self'
63
+ raise "AWS_ACCOUNT_NUMBER not set" unless @@global.accountnum
64
+ @option.owner = @@global.accountnum
65
+ end
66
+
67
+ if (@option.addresses || @option.ports) && (@option.group || @option.owner)
68
+ raise Drydock::OptError.new('', @alias, "Cannot mix group and network authorization")
69
+ end
70
+ if @option.owner && !@option.group
71
+ raise Drydock::OptError.new('', @alias, "Must provide -g with -o")
72
+ end
73
+
74
+ raise Drydock::ArgError.new('group name', @alias) unless @argv.name
75
+ @groups = Rudy::AWS::EC2::Groups.new(@@global.accesskey, @@global.secretkey, @@global.region)
76
+ end
77
+
78
+ def modify_group(action)
79
+ opts = check_options
80
+ if (@option.group || @option.owner)
81
+ g = [opts[:owner], opts[:group]].join(':')
82
+ puts "#{action.to_s.capitalize} access to #{@argv.name.bright} from #{g.bright}"
83
+ else
84
+ print "#{action.to_s.capitalize} access to #{@argv.name.bright}"
85
+ puts " from #{opts[:addresses].join(', ').bright}"
86
+ print "on #{opts[:protocols].join(', ').bright} "
87
+ puts "ports: #{opts[:ports].map { |p| "#{p.join(' to ').bright}" }.join(', ')}"
88
+ end
89
+ rgroups = Rudy::AWS::EC2::Groups.new(@@global.accesskey, @@global.secretkey, @@global.region)
90
+ execute_check(:medium)
91
+ execute_action {
92
+ if (@option.group || @option.owner)
93
+ rgroups.send("#{action.to_s}_group", @argv.name, opts[:group], opts[:owner])
94
+ else
95
+ rgroups.send(action, @argv.name, opts[:addresses], opts[:ports], opts[:protocols])
96
+ end
97
+ }
98
+ groups # prints on the modified group b/c of @argv.name
99
+ end
100
+
101
+ def check_options
102
+ opts = {}
103
+ [:addresses, :protocols, :owner, :group, :ports].each do |opt|
104
+ opts[opt] = @option.send(opt) if @option.respond_to?(opt)
105
+ end
106
+ unless @option.group || @option.owner
107
+ opts[:ports].collect! { |port| port.split(/[:-]/) } if opts[:ports]
108
+ opts[:ports] ||= [[22,22],[80,80],[443,443]]
109
+ opts[:addresses] ||= [Rudy::Utils::external_ip_address]
110
+ opts[:protocols] ||= [:tcp]
111
+ else
112
+ opts[:owner] ||= @@global.accountnum
113
+ end
114
+ opts
115
+ end
116
+
117
+ end
118
+
119
+
120
+ end; end
121
+ end; end
@@ -0,0 +1,196 @@
1
+
2
+
3
+ module Rudy; module CLI;
4
+ module AWS; module EC2;
5
+
6
+ class Images < Rudy::CLI::CommandBase
7
+
8
+ #def print_header
9
+ # puts @global.print_header, @@global.print_header
10
+ #end
11
+
12
+
13
+ def images_valid?
14
+ if @option.owner == 'self'
15
+ raise "AWS_ACCOUNT_NUMBER not set" unless @@global.accountnum
16
+ @option.owner = @@global.accountnum
17
+ end
18
+
19
+ true
20
+ end
21
+ def images
22
+
23
+ rimages = Rudy::AWS::EC2::Images.new(@@global.accesskey, @@global.secretkey, @@global.region)
24
+ unless @option.all
25
+ @option.owner ||= 'amazon'
26
+ puts "Images owned by #{@option.owner.bright}" unless @argv.awsid
27
+ end
28
+
29
+ images = rimages.list(@option.owner, @argv) || []
30
+ images.each do |img|
31
+ puts @@global.verbose > 0 ? img.inspect : img.dump(@@global.format)
32
+ end
33
+ puts "No images" if images.empty?
34
+ end
35
+
36
+ def prepare_images_valid?
37
+ true
38
+ end
39
+ def prepare_images
40
+ opts = {}
41
+ opts[:group] = @option.group if @option.group
42
+ opts[:group] = :any if @option.all
43
+ opts[:id] = @option.instid if @option.instid
44
+
45
+ puts "This will do the following:"
46
+ puts "- Clear bash history"
47
+ # NOTE: We can't delete the host keys here. Otherwise we can't create the image.
48
+ #puts "- Delete host SSH keys (this is permanent!)"
49
+ puts ""
50
+
51
+ # Options to be sent to Net::SSH
52
+ ssh_opts = { :user => @global.user || Rudy.sysinfo.user, :debug => STDERR }
53
+ if @@global.pkey
54
+ raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
55
+ raise InsecureKeyPermissions, @@global.pkey unless File.stat(@@global.pkey).mode == 33152
56
+ ssh_opts[:keys] = @@global.pkey
57
+ end
58
+
59
+ execute_check(:medium)
60
+
61
+ rudy = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
62
+ lt = rudy.list_group(opts[:group], :running, opts[:id]) do |inst|
63
+
64
+ puts "Preparing #{inst.dns_public}..."
65
+
66
+ # Open the connection and run the command
67
+ rbox = Rye::Box.new(inst.dns_public, ssh_opts)
68
+ rbox.safe = false
69
+ # We need to explicitly add the rm command for rbox so we
70
+ # can delete the SSH host keys. This is will force the instance
71
+ # to re-create it's SSH keys on first boot.
72
+ def rbox.rm(*args); cmd('rm', args); end
73
+ p ret = rbox.history(:c)
74
+ p ret.exit_code
75
+ p ret.stderr
76
+ p ret.stdout
77
+
78
+ end
79
+
80
+ puts "done"
81
+ end
82
+
83
+ def create_images_valid?
84
+ raise "No account number" unless @@global.accountnum
85
+ raise "No Amazon cert-***.pem" unless @@global.cert
86
+ raise "No Amazon pk-***.pem" unless @@global.privatekey
87
+ true
88
+ end
89
+
90
+ def create_images
91
+ opts = {}
92
+ opts[:group] = @option.group if @option.group
93
+ opts[:group] = :any if @option.all
94
+ opts[:id] = @option.instid if @option.instid
95
+
96
+ puts "You may want to run rudy-ec2 #{@alias} --prepare before this.".color(:blue)
97
+ puts "This feature is experimental. Make sure you enter the bucket"
98
+ puts "and image names correctly because if they're wrong the image"
99
+ puts "won't get created and you'll be annoyed that you waited."
100
+
101
+ # Options to be sent to Net::SSH
102
+ ssh_opts = { :user => @global.user || Rudy.sysinfo.user, :debug => STDERR }
103
+ if @@global.pkey
104
+ raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
105
+ raise InsecureKeyPermissions, @@global.pkey unless File.stat(@@global.pkey).mode == 33152
106
+ ssh_opts[:keys] = @@global.pkey
107
+ end
108
+
109
+ unless @option.name
110
+ print "Enter the image name: "
111
+ @option.image_name = gets.chomp
112
+ end
113
+
114
+ unless @option.bucket
115
+ print "Enter the S3 bucket that will store the image: "
116
+ @option.bucket_name = gets.chomp
117
+ end
118
+
119
+ execute_check(:medium)
120
+
121
+ rudy = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
122
+ lt = rudy.list_group(opts[:group], :running, opts[:id]) do |inst|
123
+
124
+ puts inst.dns_public
125
+
126
+ # Open the connection and run the command
127
+ rbox = Rye::Box.new(inst.dns_public, ssh_opts)
128
+
129
+ # ~/.rudy, /etc/motd, history -c, /etc/hosts, /var/log/rudy*
130
+ cert = File.read(@@global.cert)
131
+ pk = File.read(@@global.privatekey)
132
+ rbox.safe = false
133
+ rbox.echo("'#{cert}' > /mnt/cert-temporary.pem")
134
+ rbox.echo("'#{pk}' > /mnt/pk-temporary.pem")
135
+ rbox.safe = true
136
+ rbox.touch("/root/firstrun")
137
+
138
+ # TODO:
139
+ # We have to delete the host keys just before we run the bundle command.
140
+ # The problem is that if we lose the connection we won't be able to connect
141
+ # to the instance again. A better solution is to ass the keys to the ignore
142
+ # list for the bundle command.
143
+
144
+ #ret = rbox.rm('/etc/ssh/ssh_host_*_key*')
145
+ #puts "Starting bundling process...".bright
146
+ #puts ssh_command(machine[:dns_name], keypairpath, @global.user, "ec2-bundle-vol -r i386 -p #{@option.image_name} -k /mnt/pk-*pem -c /mnt/cert*pem -u #{@option.account}", @option.print)
147
+ #puts ssh_command(machine[:dns_name], keypairpath, @global.user, "ec2-upload-bundle -b #{@option.bucket_name} -m /tmp/#{@option.image_name}.manifest.xml -a #{@global.accesskey} -s #{@global.secretkey}", @option.print)
148
+ #
149
+ #@ec2.images.register("#{@option.bucket_name}/#{@option.image_name}.manifest.xml") unless @option.print
150
+
151
+ break
152
+ end
153
+
154
+ end
155
+
156
+ #def create_images_valid?
157
+ # puts "Make sure the machine is clean. I don't want archive no crud!"
158
+ # switch_user("root")
159
+ #
160
+ # raise "No EC2 .pem keys provided" unless has_pem_keys?
161
+ # raise "No SSH key provided for #{@global.user}!" unless has_keypair?
162
+ # raise "No SSH key provided for root!" unless has_keypair?(:root)
163
+ # true
164
+ #end
165
+ #
166
+ #
167
+ #def prepare_images
168
+ # # TODO: Avail hooks for clean an instance
169
+ # # Clean off Rudy specific crap.
170
+ #end
171
+ #
172
+ #
173
+
174
+ #
175
+ #def deregister
176
+ # ami = @argv.first
177
+ # raise "You must supply an AMI ID (ami-XXXXXXX)" unless ami
178
+ # puts "Deregistering AMI: #{ami}"
179
+ #
180
+ # exit unless Annoy.are_you_sure?
181
+ #
182
+ # if @ec2.images.deregister(ami)
183
+ # puts "Done!"
184
+ # else
185
+ # puts "There was an unknown problem!"
186
+ # end
187
+ #
188
+ #end
189
+
190
+ end
191
+
192
+
193
+ end; end
194
+ end; end
195
+
196
+