mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[aws][compute] add import_key_pair stuff
This commit is contained in:
parent
7e67e4aec5
commit
ad2a921ea6
6 changed files with 123 additions and 27 deletions
|
@ -52,6 +52,7 @@ module Fog
|
|||
request :detach_volume
|
||||
request :disassociate_address
|
||||
request :get_console_output
|
||||
request :import_key_pair
|
||||
request :modify_image_attributes
|
||||
request :modify_snapshot_attribute
|
||||
request :reboot_instances
|
||||
|
@ -183,7 +184,7 @@ module Fog
|
|||
:aws_access_key_id => @aws_access_key_id,
|
||||
:hmac => @hmac,
|
||||
:host => @host,
|
||||
:version => '2009-11-30'
|
||||
:version => '2010-08-31'
|
||||
}
|
||||
)
|
||||
|
||||
|
|
|
@ -5,11 +5,15 @@ module Fog
|
|||
class Compute
|
||||
|
||||
class KeyPair < Fog::Model
|
||||
extend Fog::Deprecation
|
||||
deprecate(:material, :private_key)
|
||||
|
||||
identity :name, :aliases => 'keyName'
|
||||
|
||||
attribute :fingerprint, :aliases => 'keyFingerprint'
|
||||
attribute :material, :aliases => 'keyMaterial'
|
||||
attribute :private_key, :aliases => 'keyMaterial'
|
||||
|
||||
attr_accessor :public_key
|
||||
|
||||
def destroy
|
||||
requires :name
|
||||
|
@ -21,12 +25,18 @@ module Fog
|
|||
def save
|
||||
requires :name
|
||||
|
||||
data = connection.create_key_pair(@name).body
|
||||
data = if public_key
|
||||
connection.import_key_pair(name, public_key).body
|
||||
else
|
||||
connection.create_key_pair(name).body
|
||||
end
|
||||
new_attributes = data.reject {|key,value| !['keyFingerprint', 'keyMaterial', 'keyName'].include?(key)}
|
||||
merge_attributes(new_attributes)
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -32,7 +32,8 @@ module Fog
|
|||
attribute :subnet_id, :aliases => 'subnetId'
|
||||
attribute :user_data
|
||||
|
||||
attr_accessor :password, :private_key_path, :public_key_path, :username
|
||||
attr_accessor :password, :username
|
||||
attr_writer :private_key_path, :public_key_path
|
||||
|
||||
def initialize(attributes={})
|
||||
@groups ||= ["default"] unless attributes[:subnet_id]
|
||||
|
@ -150,7 +151,7 @@ module Fog
|
|||
sleep(10) # takes a bit before EC2 instances will play nice
|
||||
Fog::SSH.new(ip_address, username, credentials).run([
|
||||
%{mkdir .ssh},
|
||||
%{echo "#{File.read(File.expand_path(public_key_path))}" >> ~/.ssh/authorized_keys},
|
||||
%{echo "#{File.read(public_key_path)}" >> ~/.ssh/authorized_keys},
|
||||
%{passwd -l root},
|
||||
%{echo "#{attributes.to_json}" >> ~/attributes.json}
|
||||
])
|
||||
|
|
|
@ -9,6 +9,8 @@ module Fog
|
|||
|
||||
attribute :server_id
|
||||
|
||||
attr_writer :private_key_path, :public_key_path
|
||||
|
||||
model Fog::AWS::Compute::Server
|
||||
|
||||
def initialize(attributes)
|
||||
|
@ -29,29 +31,29 @@ module Fog
|
|||
end
|
||||
|
||||
def bootstrap(new_attributes = {})
|
||||
begin
|
||||
tmp_key_pair = connection.key_pairs.create(:name => "tmp_#{Time.now.to_f.to_s.gsub('.','')}")
|
||||
server = create(new_attributes.merge(:key_pair => tmp_key_pair))
|
||||
|
||||
# make sure port 22 is open in the first security group
|
||||
security_group = connection.security_groups.get(server.groups.first)
|
||||
ip_permission = security_group.ip_permissions.detect do |ip_permission|
|
||||
ip_permission['ipRanges'].first && ip_permission['ipRanges'].first['cidrIp'] == '0.0.0.0/0' &&
|
||||
ip_permission['fromPort'] == 22 &&
|
||||
ip_permission['ipProtocol'] == 'tcp' &&
|
||||
ip_permission['toPort'] == 22
|
||||
end
|
||||
unless ip_permission
|
||||
security_group.authorize_port_range(22..22)
|
||||
end
|
||||
|
||||
server.wait_for { ready? }
|
||||
server.setup(:key_data => [tmp_key_pair.material])
|
||||
ensure
|
||||
if tmp_key_pair
|
||||
tmp_key_pair.destroy
|
||||
end
|
||||
# first or create fog_#{credential} keypair
|
||||
unless key_pair = connection.key_pairs.get("fog_#{Fog.credential}")
|
||||
public_key = File.read(public_key_path)
|
||||
key_pair = connection.key_pairs.create(:name => "fog_#{Fog.credential}", :public_key => public_key)
|
||||
end
|
||||
|
||||
# make sure port 22 is open in the first security group
|
||||
security_group = connection.security_groups.get(server.groups.first)
|
||||
ip_permission = security_group.ip_permissions.detect do |ip_permission|
|
||||
ip_permission['ipRanges'].first && ip_permission['ipRanges'].first['cidrIp'] == '0.0.0.0/0' &&
|
||||
ip_permission['fromPort'] == 22 &&
|
||||
ip_permission['ipProtocol'] == 'tcp' &&
|
||||
ip_permission['toPort'] == 22
|
||||
end
|
||||
unless ip_permission
|
||||
security_group.authorize_port_range(22..22)
|
||||
end
|
||||
|
||||
server.wait_for { ready? }
|
||||
private_key = File.read(private_key_path)
|
||||
server.setup(:key_data => [private_key])
|
||||
|
||||
server.merge_attributes(:private_key_path => private_key_path, :public_key_path => public_key_path)
|
||||
server
|
||||
end
|
||||
|
||||
|
@ -63,6 +65,14 @@ module Fog
|
|||
nil
|
||||
end
|
||||
|
||||
def private_key_path
|
||||
@private_key_path ||= Fog.credentials[:private_key_path]
|
||||
end
|
||||
|
||||
def public_key_path
|
||||
@public_key_path ||= Fog.credentials[:public_key_path]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
20
lib/fog/aws/parsers/compute/import_key_pair.rb
Normal file
20
lib/fog/aws/parsers/compute/import_key_pair.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module AWS
|
||||
module Compute
|
||||
|
||||
class ImportKeyPair < Fog::Parsers::Base
|
||||
|
||||
def end_element(name)
|
||||
case name
|
||||
when 'keyFingerprint', 'keyName', 'requestId'
|
||||
@response[name] = @value
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
54
lib/fog/aws/requests/compute/import_key_pair.rb
Normal file
54
lib/fog/aws/requests/compute/import_key_pair.rb
Normal file
|
@ -0,0 +1,54 @@
|
|||
module Fog
|
||||
module AWS
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
require 'fog/aws/parsers/compute/import_key_pair'
|
||||
|
||||
# Import an existing public key to create a new key pair
|
||||
#
|
||||
# ==== Parameters
|
||||
# * key_name<~String> - Unique name for key pair.
|
||||
# * public_key_material<~String> - RSA public key
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
# * body<~Hash>:
|
||||
# * 'keyFingerprint'<~String> - SHA-1 digest of DER encoded private key
|
||||
# * 'keyName'<~String> - Name of key
|
||||
# * 'requestId'<~String> - Id of request
|
||||
def import_key_pair(key_name, public_key_material)
|
||||
request(
|
||||
'Action' => 'ImportKeyPair',
|
||||
'KeyName' => key_name,
|
||||
'PublicKeyMaterial' => Base64::encode64(public_key_material),
|
||||
:parser => Fog::Parsers::AWS::Compute::ImportKeyPair.new
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def import_key_pair(key_name, public_key_material)
|
||||
response = Excon::Response.new
|
||||
unless @data[:key_pairs][key_name]
|
||||
response.status = 200
|
||||
data = {
|
||||
'keyFingerprint' => Fog::AWS::Mock.key_fingerprint,
|
||||
'keyName' => key_name
|
||||
}
|
||||
@data[:key_pairs][key_name] = data
|
||||
response.body = {
|
||||
'requestId' => Fog::AWS::Mock.request_id
|
||||
}.merge!(data)
|
||||
response
|
||||
else
|
||||
raise Fog::AWS::Compute::Error.new("InvalidKeyPair.Duplicate => The keypair '#{key_name}' already exists.")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue