Class Rudy::CLI::AWS::EC2::Candy

  1. lib/rudy/cli/aws/ec2/candy.rb

Public instance methods

copy ()
[show source]
     # File lib/rudy/cli/aws/ec2/candy.rb, line 116
116:     def copy
117:       
118:       opts = {}
119:       opts[:group] = @option.group if @option.group
120:       opts[:group] = :any if @option.all
121: 
122:       opts[:id] = @argv.shift if Rudy::Utils.is_id?(:instance, @argv.first)
123:       opts[:id] &&= [opts[:id]].flatten
124:       
125:       # Options to be sent to Net::SSH
126:       rye_opts = { :user => @global.user || Rudy.sysinfo.user, :debug => nil  }
127:       if @@global.pkey 
128:         raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
129:         raise InsecureKeyPermissions, @@global.pkey unless File.stat(@@global.pkey).mode == 33152
130:         rye_opts[:keys] = @@global.pkey 
131:       end
132:       
133:       opts[:paths] = @argv
134:       opts[:dest] = opts[:paths].pop
135:     
136:       opts[:task] = :download if %w(dl download).member?(@alias) || @option.download
137:       opts[:task] = :upload if %w(ul upload).member?(@alias)
138:       opts[:task] ||= :upload
139:       opts[:user] = @global.user || Rudy.sysinfo.user
140:     
141:     
142:       # Options to be sent to Rye::Box
143:       info = @@global.quiet ? nil : STDERR
144:       rye_opts = { :user => @global.user || Rudy.sysinfo.user, :info => info }
145:       if @@global.pkey 
146:         raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
147:         raise InsecureKeyPermissions, @@global.pkey unless File.stat(@@global.pkey).mode == 33152
148:         rye_opts[:keys] = @@global.pkey 
149:       end
150:       
151: 
152:       checked = false
153:       lt = Rudy::AWS::EC2::Instances.list_group(opts[:group], :running, opts[:id]) do |inst|
154:         
155:         if @option.print
156:           scp_command inst.dns_public, @@global.pkey, opts[:user], opts[:paths], opts[:dest], (opts[:task] == :download), false, @option.print
157:           next
158:         end
159:         
160:         # Print header
161:         if @@global.quiet
162:           print "You are #{rye_opts[:user].bright}. " if !checked # only the 1st
163:         else
164:           print "Connecting #{rye_opts[:user].bright}@#{inst.dns_public} "
165:           li "(#{inst.awsid}, groups: #{inst.groups.join(', ')})"
166:         end
167:         
168:         # Make sure we want to run this command on all instances
169:         if !checked
170:           #execute_check(:medium) if opts[:user] == "root"
171:           checked = true
172:         end
173:         
174:         # Open the connection and run the command
175:         rbox = Rye::Box.new(inst.dns_public, rye_opts)
176:         rbox.send(opts[:task], opts[:paths], opts[:dest])
177:       end
178: 
179:     end
copy_valid? ()
[show source]
     # File lib/rudy/cli/aws/ec2/candy.rb, line 110
110:     def copy_valid?
111:       raise "You must supply a source and a target. See rudy-ec2 #{@alias} -h" unless @argv.size >= 2
112:       raise "You cannot download and upload at the same time" if @option.download && @alias == 'upload'
113:       raise "You cannot download and upload at the same time" if @option.upload && @alias == 'download'
114:       true
115:     end
ssh ()
[show source]
     # File lib/rudy/cli/aws/ec2/candy.rb, line 60
 60:     def ssh
 61:       opts = {}
 62:       opts[:group] = @option.group if @option.group
 63:       opts[:group] = :any if @option.all
 64:       opts[:id] = @option.instid if @option.instid
 65:       
 66:       # Options to be sent to Rye::Box
 67:       rye_opts = { :user => @global.user || Rudy.sysinfo.user, :debug => nil, :info => STDOUT  }
 68:       if @@global.pkey 
 69:         raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
 70:         raise InsecureKeyPermissions, @@global.pkey unless File.stat(@@global.pkey).mode == 33152
 71:         rye_opts[:keys] = @@global.pkey 
 72:       end
 73:       
 74:       
 75:       # The user specified a command to run. We won't create an interactive
 76:       # session so we need to prepare the command and its arguments
 77:       if @argv.first
 78:         command, command_args = @argv.shift, @argv || []
 79:         li "#{command} #{command_args.join(' ')}" if @@global.verbose > 1
 80:       
 81:       # otherwise, we'll open an ssh session or print command
 82:       else
 83:         command, command_args = :interactive_ssh, @option.print.nil?
 84:       end
 85:       
 86:       checked = false
 87:       lt = Rudy::AWS::EC2::Instances.list_group(opts[:group], :running, opts[:id]) do |inst|
 88:         
 89:         # Print header
 90:         if @@global.quiet
 91:           print "You are #{rye_opts[:user].bright}. " if !checked # only the 1st
 92:         else
 93:           print "Connecting #{rye_opts[:user].bright}@#{inst.dns_public} "
 94:           li "(#{inst.awsid}, groups: #{inst.groups.join(', ')})"
 95:         end
 96:         
 97:         # Make sure we want to run this command on all instances
 98:         if !checked && command != :interactive_ssh 
 99:           execute_check(:medium) if rye_opts[:user] == "root"
100:           checked = true
101:         end
102:         
103:         # Open the connection and run the command
104:         rbox = Rye::Box.new(inst.dns_public, rye_opts)
105:         ret = rbox.send(command, command_args)
106:         li ret unless command == :interactive_ssh
107:       end
108:     end
ssh_valid? ()
[show source]
    # File lib/rudy/cli/aws/ec2/candy.rb, line 46
46:     def ssh_valid?
47:       if @@global.pkey
48:         raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
49:         raise "Insecure permissions for #{@@global.pkey}" unless (File.stat(@@global.pkey).mode & 600) == 0
50:       end
51:       if @option.group
52:         raise "Cannot supply group and instance ID" if @option.instid
53:         raise "Group #{@option.group} does not exist" unless Rudy::AWS::EC2::Groups.exists?(@option.group)
54:       end
55:       if @option.instid && !Rudy::Utils.is_id?(:instance, @option.instid)
56:         raise "#{@option.instid} is not an instance ID" 
57:       end
58:       true
59:     end
status ()
[show source]
    # File lib/rudy/cli/aws/ec2/candy.rb, line 12
12:     def status
13:       url = 'http://status.aws.amazon.com/rss/EC2.rss'
14:       
15:       if (@@global.region || '').to_s.strip.match(/\Aeu/)
16:         url = 'http://status.aws.amazon.com/rss/EC2EU.rss'
17:       end
18:       
19:       # TODO: Move to Rudy::AWS
20:       ec2 = Rudy::Utils::RSSReader.run(url) || {}
21:       
22:       # TODO: Create Storable object
23:       if @@global.format == 'yaml'
24:         li ec2.to_yaml
25:       elsif @@global.format == 'json'
26:         require 'json'
27:         li ec2.to_json
28:       else
29:         li "#{ec2[:title]}"
30:         li "Updated: #{ec2[:pubdate]}"
31:         (ec2[:items] || []).each do |i|
32:           li
33:           li '%s' % i[:title]
34:           li '  %s: %s' % [i[:pubdate], i[:description]]
35:         end
36:         if ec2.empty? || ec2[:items].empty?
37:           li "No announcements" 
38:           return
39:         end
40:       end
41:       
42:       
43:       
44:     end
status_valid? ()
[show source]
    # File lib/rudy/cli/aws/ec2/candy.rb, line 7
 7:     def status_valid?
 8:       avail = Rudy::Utils.service_available?('status.aws.amazon.com', 80, 5)
 9:       raise ServiceUnavailable, 'status.aws.amazon.com' unless @@global.offline || avail
10:       true
11:     end