Skip to content
This repository has been archived by the owner on Mar 8, 2018. It is now read-only.

Commit

Permalink
Refactor some of the key importing code to work both on OpenStack and…
Browse files Browse the repository at this point in the history
… AWS
  • Loading branch information
R. Tyler Croy committed Jul 14, 2012
1 parent 869496f commit 1e1e8ef
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
10 changes: 9 additions & 1 deletion lib/blimpy/boxes/aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ def fog

private

def import_key
material = Blimpy::Keys.public_key
begin
fog.import_key_pair(Blimpy::Keys.key_name, material)
rescue Fog::Compute::AWS::Error => e
end
end

def create_host
tags = @tags.merge({:Name => @name, :CreatedBy => 'Blimpy', :BlimpyFleetId => @fleet_id})

Blimpy::Keys.import_key(fog)
import_key
generated_group = Blimpy::SecurityGroups.ensure_group(fog, @ports + [22])
groups = [@group, generated_group].compact
fog.servers.create(:image_id => @image_id,
Expand Down
11 changes: 10 additions & 1 deletion lib/blimpy/boxes/openstack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,22 @@ def flavor_id(name)

private

def import_key
material = Blimpy::Keys.public_key
begin
fog.create_key_pair(Blimpy::Keys.key_name, material)
rescue Excon::Errors::Conflict => e
end
end

def create_host
tags = @tags.merge({:Name => @name, :CreatedBy => 'Blimpy', :BlimpyFleetId => @fleet_id})

groups = [@group]
import_key
fog.servers.create(:image_ref => @image_id,
:flavor_ref => flavor_id(@flavor),
:key_name => @key_name,
:key_name => Blimpy::Keys.key_name,
:groups => groups,
:name => @name,
:tags => tags)
Expand Down
10 changes: 3 additions & 7 deletions lib/blimpy/keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Blimpy
module Keys
def self.import_key(fog)
def self.public_key
filename = File.expand_path('~/.ssh/id_rsa.pub')
unless File.exists? filename
filename = File.expand_path('~/.ssh/id_dsa.pub')
Expand All @@ -12,15 +12,11 @@ def self.import_key(fog)
end
end

material = File.open(filename, 'r').read
begin
fog.import_key_pair(key_name, material)
rescue Fog::Compute::AWS::Error => e
end
File.open(filename, 'r').read
end

def self.key_name
"Blimpy-#{ENV['USER']}@#{Socket.gethostname}"
"Blimpy-#{ENV['USER']}-#{Socket.gethostname}"
end
end
end
6 changes: 3 additions & 3 deletions spec/blimpy/keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
require 'blimpy/keys'

describe Blimpy::Keys do
describe '#import_key' do
describe '#public_key' do
context 'with no SSH keys' do
it 'should raise a SSHKeyNotFoundError' do
File.stub(:exists?).and_return(false)
expect {
subject.import_key(nil)
subject.public_key
}.to raise_error(Blimpy::SSHKeyNotFoundError)
end
end
Expand All @@ -21,7 +21,7 @@
it do
hostname = 'rspec'
Socket.should_receive(:gethostname).and_return(hostname)
subject.key_name.should == "Blimpy-tester@#{hostname}"
subject.key_name.should == "Blimpy-tester-#{hostname}"
end
end
end

0 comments on commit 1e1e8ef

Please sign in to comment.