Class: Awscli::EC2::KeyPairs

Inherits:
Object
  • Object
show all
Defined in:
lib/awscli/ec2.rb

Overview

> EC2

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ KeyPairs

Returns a new instance of KeyPairs.



221
222
223
# File 'lib/awscli/ec2.rb', line 221

def initialize(connection)
  @conn = connection
end

Instance Method Details

#create_keypair(options) ⇒ Object



229
230
231
232
233
234
235
236
# File 'lib/awscli/ec2.rb', line 229

def create_keypair(options)
  #validate keypair
  Fog.credential = 'awscli'
  abort "KeyPair '#{options[:name]}' already exists" if @conn.key_pairs.get(options[:name])
  kp = @conn.key_pairs.create(options)
  puts "Created keypair: #{options[:name]}"
  p kp.write  #save the key to disk
end

#delete_keypair(keypair) ⇒ Object



238
239
240
241
242
# File 'lib/awscli/ec2.rb', line 238

def delete_keypair(keypair)
  abort "KeyPair '#{keypair}' does not exist" unless @conn.key_pairs.get(keypair)
  @conn.key_pairs.get(keypair).destroy
  puts "Deleted Keypair: #{keypair}"
end

#fingerprint(keypair) ⇒ Object



244
245
246
247
248
# File 'lib/awscli/ec2.rb', line 244

def fingerprint(keypair)
  response = @conn.key_pairs.get(keypair)
  abort "Cannot find key pair: #{keypair}" unless response
  puts "Fingerprint for the key (#{keypair}): #{response.fingerprint}"
end

#import_keypair(options) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/awscli/ec2.rb', line 250

def import_keypair(options)
  #validate if the file exists
  private_key_path = if options[:private_key_path]
                      File.expand_path(options[:private_key_path])
                     else
                      File.expand_path("~/.ssh/#{options[:name]}")
                     end
  public_key_path  = if options[:public_key_path]
                      File.expand_path(options[:public_key_path])
                     else
                      File.expand_path("~/.ssh/#{options[:name]}.pub")
                     end
  abort "Cannot find private_key_path: #{private_key_path}" unless File.exist?(private_key_path)
  abort "Cannot find public_key_path: #{public_key_path}" unless File.exist?(public_key_path)
  #validate if the key pair name exists
  Fog.credentials = Fog.credentials.merge({ :private_key_path => private_key_path, :public_key_path => public_key_path })
  @conn.import_key_pair(options[:name], IO.read(public_key_path)) if @conn.key_pairs.get(options[:name]).nil?
  puts "Imported KeyPair with name: #{options[:name]} sucessfully, using public_key: #{public_key_path} and private_key: #{private_key_path}"
end

#list_keypairsObject



225
226
227
# File 'lib/awscli/ec2.rb', line 225

def list_keypairs
  @conn.key_pairs.table
end