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

Inherits:
CommandBase
  • Object
show all
Defined in:
lib/rudy/cli/aws/ec2/candy.rb

Instance Attribute Summary

Attributes inherited from CommandBase

#config

Instance Method Summary collapse

Methods included from Huxtable

change_environment, change_position, change_region, change_role, change_zone, #check_keys, #config_dirname, create_domain, #current_group_name, #current_machine_address, #current_machine_count, #current_machine_group, #current_machine_hostname, #current_machine_image, #current_machine_name, #current_machine_size, #current_user, #current_user_keypairpath, debug?, #debug?, domain, domain_exists?, #group_metadata, #has_keypair?, #has_keys?, #has_pem_keys?, #has_root_keypair?, keypair_path_to_name, #known_machine_group?, #root_keypairname, #root_keypairpath, #switch_user, update_config, update_global, update_logger, #user_keypairname, #user_keypairpath

Instance Method Details

#copyObject



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
# File 'lib/rudy/cli/aws/ec2/candy.rb', line 118

def copy
  
  opts = {}
  opts[:group] = @option.group if @option.group
  opts[:group] = :any if @option.all

  opts[:id] = @argv.shift if Rudy::Utils.is_id?(:instance, @argv.first)
  opts[:id] &&= [opts[:id]].flatten
  
  # Options to be sent to Net::SSH
  ssh_opts = { :user => @global.user || Rudy.sysinfo.user, :debug => nil  }
  if @@global.pkey 
    raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
    raise InsecureKeyPermissions, @@global.pkey unless File.stat(@@global.pkey).mode == 33152
    ssh_opts[:keys] = @@global.pkey 
  end
  
  opts[:paths] = @argv
  opts[:dest] = opts[:paths].pop

  opts[:task] = :download if %w(dl download).member?(@alias) || @option.download
  opts[:task] = :upload if %w(ul upload).member?(@alias)
  opts[:task] ||= :upload
  opts[:user] = @global.user || Rudy.sysinfo.user


  # Options to be sent to Rye::Box
  info = @@global.quiet ? nil : STDERR
  ssh_opts = { :user => @global.user || Rudy.sysinfo.user, :info => info }
  if @@global.pkey 
    raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
    raise InsecureKeyPermissions, @@global.pkey unless File.stat(@@global.pkey).mode == 33152
    ssh_opts[:keys] = @@global.pkey 
  end
  

  checked = false
  rudy = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
  lt = rudy.list_group(opts[:group], :running, opts[:id]) do |inst|
    
    if @option.print
      Rudy::Utils.scp_command inst.dns_public, @@global.pkey, opts[:user], opts[:paths], opts[:dest], (opts[:task] == :download), false, @option.print
      next
    end
    
    # Print header
    if @@global.quiet
      print "You are #{ssh_opts[:user].bright}. " if !checked # only the 1st
    else
      print "Connecting #{ssh_opts[:user].bright}@#{inst.dns_public} "
      puts "(#{inst.awsid}, groups: #{inst.groups.join(', ')})"
    end
    
    # Make sure we want to run this command on all instances
    if !checked
      #execute_check(:medium) if opts[:user] == "root"
      checked = true
    end
    
    # Open the connection and run the command
    rbox = Rye::Box.new(inst.dns_public, ssh_opts)
    rbox.send(opts[:task], opts[:paths], opts[:dest])
  end

end

#copy_valid?Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
# File 'lib/rudy/cli/aws/ec2/candy.rb', line 112

def copy_valid?
  raise "You must supply a source and a target. See rudy-ec2 #{@alias} -h" unless @argv.size >= 2
  raise "You cannot download and upload at the same time" if @option.download && @alias == 'upload'
  raise "You cannot download and upload at the same time" if @option.upload && @alias == 'download'
  true
end

#sshObject



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
# File 'lib/rudy/cli/aws/ec2/candy.rb', line 61

def ssh
  opts = {}
  opts[:group] = @option.group if @option.group
  opts[:group] = :any if @option.all
  opts[:id] = @option.instid if @option.instid
  
  # Options to be sent to Rye::Box
  ssh_opts = { :user => @global.user || Rudy.sysinfo.user, :debug => nil  }
  if @@global.pkey 
    raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
    raise InsecureKeyPermissions, @@global.pkey unless File.stat(@@global.pkey).mode == 33152
    ssh_opts[:keys] = @@global.pkey 
  end
  
  
  # The user specified a command to run. We won't create an interactive
  # session so we need to prepare the command and its arguments
  if @argv.first
    command, command_args = @argv.shift, @argv || []
    puts "#{command} #{command_args.join(' ')}" if @@global.verbose > 1
  
  # otherwise, we'll open an ssh session or print command
  else
    command, command_args = :interactive_ssh, @option.print.nil?
  end
  
  checked = false
  rudy = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
  lt = rudy.list_group(opts[:group], :running, opts[:id]) do |inst|
    
    # Print header
    if @@global.quiet
      print "You are #{ssh_opts[:user].bright}. " if !checked # only the 1st
    else
      print "Connecting #{ssh_opts[:user].bright}@#{inst.dns_public} "
      puts "(#{inst.awsid}, groups: #{inst.groups.join(', ')})"
    end
    
    # Make sure we want to run this command on all instances
    if !checked && command != :interactive_ssh 
      execute_check(:medium) if ssh_opts[:user] == "root"
      checked = true
    end
    
    # Open the connection and run the command
    rbox = Rye::Box.new(inst.dns_public, ssh_opts)
    ret = rbox.send(command, command_args)
    puts ret unless command == :interactive_ssh
  end
end

#ssh_valid?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rudy/cli/aws/ec2/candy.rb', line 46

def ssh_valid?
  if @@global.pkey
    raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
    raise "Insecure permissions for #{@@global.pkey}" unless (File.stat(@@global.pkey).mode & 600) == 0
  end
  if @option.group
    rgroup = Rudy::AWS::EC2::Groups.new(@@global.accesskey, @@global.secretkey, @@global.region)
    raise "Cannot supply group and instance ID" if @option.instid
    raise "Group #{@option.group} does not exist" unless rgroup.exists?(@option.group)
  end
  if @option.instid && !Rudy::Utils.is_id?(:instance, @option.instid)
    raise "#{@option.instid} is not an instance ID" 
  end
  true
end

#statusObject



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
# File 'lib/rudy/cli/aws/ec2/candy.rb', line 12

def status
  url = 'http://status.aws.amazon.com/rss/EC2.rss'
  
  if (@@global.region || '').to_s.strip.match(/\Aeu/)
    url = 'http://status.aws.amazon.com/rss/EC2EU.rss'
  end
  
  # TODO: Move to Rudy::AWS
  ec2 = Rudy::Utils::RSSReader.run(url) || {}
  
  # TODO: Create Storable object
  if @@global.format == 'yaml'
    puts ec2.to_yaml
  elsif @@global.format == 'json'
    require 'json'
    puts ec2.to_json
  else
    puts "#{ec2[:title]}"
    puts "Updated: #{ec2[:pubdate]}"
    (ec2[:items] || []).each do |i|
      puts
      puts '%s' % i[:title]
      puts '  %s: %s' % [i[:pubdate], i[:description]]
    end
    if ec2.empty? || ec2[:items].empty?
      puts "No announcements" 
      return
    end
  end
  
  
  
end

#status_valid?Boolean

Returns:

  • (Boolean)

Raises:



7
8
9
10
11
# File 'lib/rudy/cli/aws/ec2/candy.rb', line 7

def status_valid?
  avail = Rudy::Utils.service_available?('status.aws.amazon.com', 80, 5)
  raise ServiceUnavailable, 'status.aws.amazon.com' unless @@global.offline || avail
  true
end