Class: Rudy::CLI::AWS::EC2::Instances

Inherits:
CommandBase
  • Object
show all
Defined in:
lib/rudy/cli/aws/ec2/instances.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

#consolesObject



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

def consoles
  opts = {}
  opts[:group] = @option.group if @option.group
  opts[:id] = @argv.instid if @argv.instid
  opts[:id] &&= [opts[:id]].flatten
  
  lt = @rinst.list_group(opts[:group], :any, opts[:id]) do |inst|
    puts instance_separator(inst.dns_public || inst.state, inst.awsid)
    console = @rinst.console(inst.awsid)
    output = console ? Base64.decode64(console) : "Unavailable"
    
    # The linux console can include ANSI escape codes for color, 
    # clear screen etc... We strip them out to get rid of the 
    # clear specifically. Otherwise the display is messed!
    output &&= output.noansi 
    
    puts output 
    
    if output.match(/<Password>(.+)<\/Password>/m)  # /m, match multiple lines
      puts
      if @@global.pkey
        encrtypted_text = ($1 || '').strip
        k = Rye::Key.from_file(@@global.pkey)
        pword = k.decrypt(encrtypted_text)
        answer = "%s: %s" % ['password', pword] 
        Annoy.timed_display(answer, STDERR, 10)
        puts
      else
        puts "Please supply a private key path to decode the administrator password"
        puts "rudy-ec2 -k path/2/privatekey console [-g group] [instance ID]"
      end
    end
    
  end
  
end

#consoles_valid?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
# File 'lib/rudy/cli/aws/ec2/instances.rb', line 114

def consoles_valid?
  @rinst = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
  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
  raise "No instances" unless @rinst.any?
  true
end

#instances_createObject



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

def instances_create
  
  opts = {                 # Defaults
    :group => 'default',
    :size => 'm1.small',
    :zone => @@global.zone
  }
  
  radd = Rudy::AWS::EC2::Addresses.new(@@global.accesskey, @@global.secretkey, @@global.region)
  rinst = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
  
  if @option.address
    raise "Cannot specify both -a and -n" if @option.newaddress
    raise "#{@option.address} is not allocated to you" unless radd.exists?(@option.address)
    raise "#{@option.address} is already associated!" if radd.associated?(@option.address)
  end
  
  # These can be sent directly to EC2 class
  [:group, :ami, :size, :keypair, :private].each do |n|
    opts[n] = @option.send(n) if @option.send(n)
  end
  
  puts "Creating #{opts[:size]} instance in #{@@global.zone}"
  
  unless opts[:keypair]
    puts "You did not specify a keypair. Unless you've prepared a user account".bright
    puts "on this image (#{opts[:ami]}) you will not be able to log in to it.".bright
    exit unless Annoy.proceed?(:low)
  end
  
  instances = rinst.list_group(opts[:group], :running)
  
  if instances && instances.size > 0
    instance_count = (instances.size == 1) ? 'is 1 instance' : "are #{instances.size} instances"
    puts "There #{instance_count} running in the #{opts[:group]} group."
    exit unless Annoy.proceed?(:low)
  end
  
  if @option.newaddress
    print "Creating address... "
    address = radd.create
    puts "#{address.ipaddress}"
    @option.address = address.ipaddress
  end
     
  execute_action do
    first_instance = true
    rinst.create(opts) do |inst| # Rudy::AWS::EC2::Instance objects
    
      # Assign IP address to only the first instance
      if first_instance && @option.address
        puts "Associating #{@option.address} to #{inst.awsid}"
        radd.associate(@option.address, inst.awsid)
        first_instance = false
      end
    
      puts @@global.verbose > 0 ? inst.inspect : inst.dump(@@global.format)
    end
  end
end

#instances_create_valid?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rudy/cli/aws/ec2/instances.rb', line 14

def instances_create_valid?
  
  raise "Cannot supply an instance ID" if @option.instid
  
  if @option.group
    rgroup = Rudy::AWS::EC2::Groups.new(@@global.accesskey, @@global.secretkey, @@global.region)
    raise "Group #{@option.group} does not exist" unless rgroup.exists?(@option.group)
  end
  
  true
end

#instances_destroyObject



106
107
108
# File 'lib/rudy/cli/aws/ec2/instances.rb', line 106

def instances_destroy
  instances_action :destroy
end

#instances_restartObject



110
111
112
# File 'lib/rudy/cli/aws/ec2/instances.rb', line 110

def instances_restart
  instances_action :restart
end

#instances_restart_valid?Boolean Also known as: instances_destroy_valid?

Returns:

  • (Boolean)

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rudy/cli/aws/ec2/instances.rb', line 87

def instances_restart_valid?
  raise InstanceAndGroupError.new(nil, @alias) if @option.group && @argv.instid
  raise NoInstanceError.new(nil, @alias) if !@option.group && !@argv.instid
  
  if @option.group
    rgroup = Rudy::AWS::EC2::Groups.new(@@global.accesskey, @@global.secretkey, @@global.region)
    raise "Group #{@option.group} does not exist" unless rgroup.exists?(@option.group)
  end
  
  if @option.private
    raise Drydock::OptsError.new(nil, @alias, "Cannot allocate public IP for private instance") if @option.address || @option.newadress
  end
  
  @rinst = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
  raise "No instances" unless @rinst.any?
  true
end

#statusObject Also known as: instances



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rudy/cli/aws/ec2/instances.rb', line 160

def status
  opts = {}
  
  opts[:group] = @option.group if @option.group
  opts[:state] = @option.state if @option.state

  # A nil value forces the @ec2.instances.list to return all instances
  if @option.all
    opts[:state] = :any
    opts[:group] = :any
  end

  opts[:id] = @argv.instid if @argv.instid
  opts[:id] &&= [opts[:id]].flatten

  rudy = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
  lt = rudy.list_group(opts[:group], opts[:state], opts[:id]) do |inst|
    puts @@global.verbose > 0 ? inst.inspect : inst.dump(@@global.format)
  end
  puts "No instances running" if !lt || lt.empty?
end