Skip to content

Commit

Permalink
[digitalocean|compute] allow bootstrapping with keys rather than paths
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Dave Donahue committed Dec 27, 2013
1 parent 53e6e06 commit 92ccbb9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
27 changes: 23 additions & 4 deletions lib/fog/digitalocean/models/compute/servers.rb
Expand Up @@ -17,22 +17,28 @@ def all(filters = {})
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

Expand All @@ -43,6 +49,19 @@ def get(id)
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
Expand Down
2 changes: 1 addition & 1 deletion tests/digitalocean/helper.rb
Expand Up @@ -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' }

Expand Down
23 changes: 19 additions & 4 deletions tests/digitalocean/models/compute/servers_tests.rb
Expand Up @@ -5,17 +5,32 @@
: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

0 comments on commit 92ccbb9

Please sign in to comment.