2010-10-04 14:02:08 -07:00
|
|
|
require 'fog/core/model'
|
2010-03-16 15:46:21 -07:00
|
|
|
|
2009-08-12 22:33:35 -07:00
|
|
|
module Fog
|
2011-06-16 16:28:54 -07:00
|
|
|
module Compute
|
|
|
|
class AWS
|
2009-08-12 22:33:35 -07:00
|
|
|
|
|
|
|
class KeyPair < Fog::Model
|
|
|
|
|
2010-09-07 11:30:02 -07:00
|
|
|
identity :name, :aliases => 'keyName'
|
2009-10-23 22:23:55 -07:00
|
|
|
|
2010-09-07 11:30:02 -07:00
|
|
|
attribute :fingerprint, :aliases => 'keyFingerprint'
|
2010-09-22 12:06:25 -07:00
|
|
|
attribute :private_key, :aliases => 'keyMaterial'
|
|
|
|
|
|
|
|
attr_accessor :public_key
|
2009-08-12 22:33:35 -07:00
|
|
|
|
2009-09-20 09:21:03 -07:00
|
|
|
def destroy
|
2009-11-21 13:56:39 -08:00
|
|
|
requires :name
|
|
|
|
|
2010-11-19 13:45:45 -08:00
|
|
|
connection.delete_key_pair(name)
|
2009-09-05 20:50:40 -07:00
|
|
|
true
|
2009-08-12 22:33:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
2009-11-21 13:56:39 -08:00
|
|
|
requires :name
|
|
|
|
|
2010-09-22 12:06:25 -07:00
|
|
|
data = if public_key
|
|
|
|
connection.import_key_pair(name, public_key).body
|
|
|
|
else
|
|
|
|
connection.create_key_pair(name).body
|
|
|
|
end
|
2009-08-14 09:19:40 -07:00
|
|
|
new_attributes = data.reject {|key,value| !['keyFingerprint', 'keyMaterial', 'keyName'].include?(key)}
|
2009-09-27 21:31:15 -07:00
|
|
|
merge_attributes(new_attributes)
|
2009-09-05 20:50:40 -07:00
|
|
|
true
|
2011-04-02 10:06:22 -06:00
|
|
|
|
2009-08-12 22:33:35 -07:00
|
|
|
end
|
|
|
|
|
2011-03-31 02:09:53 -06:00
|
|
|
def write(path="#{ENV['HOME']}/.ssh/fog_#{Fog.credential.to_s}_#{name}.pem")
|
2011-04-02 10:06:22 -06:00
|
|
|
|
2011-03-31 01:31:33 -06:00
|
|
|
if writable?
|
|
|
|
split_private_key = private_key.split(/\n/)
|
2011-04-02 10:06:22 -06:00
|
|
|
File.open(path, "w") do |f|
|
|
|
|
split_private_key.each {|line| f.puts line}
|
|
|
|
f.chmod 0600
|
|
|
|
end
|
2011-03-31 01:31:33 -06:00
|
|
|
"Key file built: #{path}"
|
|
|
|
else
|
|
|
|
"Invalid private key"
|
|
|
|
end
|
|
|
|
end
|
2010-09-22 12:06:25 -07:00
|
|
|
|
2011-03-31 01:48:34 -06:00
|
|
|
def writable?
|
2011-04-02 10:06:22 -06:00
|
|
|
!!(private_key && ENV.has_key?('HOME'))
|
2011-03-31 01:48:34 -06:00
|
|
|
end
|
|
|
|
|
2009-08-12 22:33:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|