1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[digitalocean|compute] allow bootstrapping with keys rather than paths

* Allows specifying public_key and private_key rather than public_key_path and private_key_path. Useful for Heroku where you don't have direct access to the filesystem and need to specify keys in environment variables.
This commit is contained in:
Dave Donahue 2013-12-27 14:02:38 -06:00
parent 53e6e0693b
commit 92ccbb9e10
3 changed files with 43 additions and 9 deletions

View file

@ -17,22 +17,28 @@ module Fog
def bootstrap(new_attributes = {})
server = new(new_attributes)
raise(ArgumentError, "public_key_path is required to configure the server.") unless new_attributes[:public_key_path]
raise(ArgumentError, "private_key_path is required to configure the server.") unless new_attributes[:private_key_path]
check_keys(new_attributes)
credential = Fog.respond_to?(:credential) && Fog.credential || :default
name = "fog_#{credential}"
ssh_key = service.ssh_keys.detect { |key| key.name == name }
if ssh_key.nil?
ssh_key = service.ssh_keys.create(
:name => name,
:ssh_pub_key => File.read(new_attributes[:public_key_path])
:ssh_pub_key => (new_attributes[:public_key] || File.read(new_attributes[:public_key_path]))
)
end
server.ssh_keys = [ssh_key]
server.save
server.wait_for { ready? }
server.setup :keys => [new_attributes[:private_key_path]]
if new_attributes[:private_key]
server.setup :key_data => [new_attributes[:private_key]]
else
server.setup :keys => [new_attributes[:private_key_path]]
end
server
end
@ -43,6 +49,19 @@ module Fog
nil
end
protected
def check_keys(attributes)
check_key :public, attributes[:public_key], attributes[:public_key_path]
check_key :private, attributes[:private_key], attributes[:private_key_path]
end
def check_key(name, data, path)
if [data, path].all?(&:nil?)
raise ArgumentError, "either #{name}_key or #{name}_key_path is required to configure the server"
end
end
end
end

View file

@ -5,7 +5,7 @@ def service
end
def fog_test_server_attributes
image = service.images.find { |i| i.name == 'Ubuntu 12.04 x64' }
image = service.images.find { |i| i.name == 'Ubuntu 13.10 x64' }
region = service.regions.find { |r| r.name == 'New York 1' }
flavor = service.flavors.find { |r| r.name == '512MB' }

View file

@ -5,17 +5,32 @@ Shindo.tests('Fog::Compute[:digitalocean] | servers collection', ['digitalocean'
:name => "#{fog_server_name}-#{Time.now.to_i.to_s}"
}.merge fog_test_server_attributes
public_key_path = File.join(File.dirname(__FILE__), '../../fixtures/id_rsa.pub')
private_key_path = File.join(File.dirname(__FILE__), '../../fixtures/id_rsa')
collection_tests(service.servers, options, true) do
@instance.wait_for { ready? }
end
tests("#bootstrap").succeeds do
tests("#bootstrap with public/private_key_path").succeeds do
pending if Fog.mocking?
@server = service.servers.bootstrap({
:public_key_path => File.join(File.dirname(__FILE__), '../../fixtures/id_rsa.pub'),
:private_key_path => File.join(File.dirname(__FILE__), '../../fixtures/id_rsa')
:public_key_path => public_key_path,
:private_key_path => private_key_path
}.merge(options))
@server.destroy
end
@server.destroy if @server
tests("#bootstrap with public/private_key").succeeds do
pending if Fog.mocking?
@server = service.servers.bootstrap({
:public_key => File.read(public_key_path),
:private_key => File.read(private_key_path)
}.merge(options))
@server.destroy
end
tests("#bootstrap with no public/private keys") do
raises(ArgumentError, 'raises ArgumentError') { service.servers.bootstrap(options) }
end
end