Class: Awscli::EC2::KeyPairs
- Inherits:
-
Object
- Object
- Awscli::EC2::KeyPairs
- Defined in:
- lib/awscli/ec2.rb
Overview
> EC2
Instance Method Summary collapse
- #create_keypair(options) ⇒ Object
- #delete_keypair(keypair) ⇒ Object
- #fingerprint(keypair) ⇒ Object
- #import_keypair(options) ⇒ Object
-
#initialize(connection) ⇒ KeyPairs
constructor
A new instance of KeyPairs.
- #list_keypairs ⇒ Object
Constructor Details
#initialize(connection) ⇒ KeyPairs
Returns a new instance of KeyPairs.
233 234 235 |
# File 'lib/awscli/ec2.rb', line 233 def initialize(connection) @conn = connection end |
Instance Method Details
#create_keypair(options) ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'lib/awscli/ec2.rb', line 241 def create_keypair() #validate keypair Fog.credential = 'awscli' abort "KeyPair '#{[:name]}' already exists" if @conn.key_pairs.get([:name]) kp = @conn.key_pairs.create() puts "Created keypair: #{[:name]}" p kp.write #save the key to disk end |
#delete_keypair(keypair) ⇒ Object
250 251 252 253 254 |
# File 'lib/awscli/ec2.rb', line 250 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
256 257 258 259 260 |
# File 'lib/awscli/ec2.rb', line 256 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
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/awscli/ec2.rb', line 262 def import_keypair() #validate if the file exists private_key_path = if [:private_key_path] File.([:private_key_path]) else File.("~/.ssh/#{[:name]}") end public_key_path = if [:public_key_path] File.([:public_key_path]) else File.("~/.ssh/#{[: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([:name], IO.read(public_key_path)) if @conn.key_pairs.get([:name]).nil? puts "Imported KeyPair with name: #{[:name]} sucessfully, using public_key: #{public_key_path} and private_key: #{private_key_path}" end |
#list_keypairs ⇒ Object
237 238 239 |
# File 'lib/awscli/ec2.rb', line 237 def list_keypairs @conn.key_pairs.table end |