From a986736523d1e11aaacae04029432430a99a9ae9 Mon Sep 17 00:00:00 2001 From: geemus Date: Thu, 23 Sep 2010 17:39:25 -0700 Subject: [PATCH] clean/fix bootstrap methods --- lib/fog/aws/models/compute/server.rb | 23 ++++++++++++------- lib/fog/aws/models/compute/servers.rb | 26 ++++++++-------------- lib/fog/rackspace/models/compute/server.rb | 25 ++++++++++++++------- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/lib/fog/aws/models/compute/server.rb b/lib/fog/aws/models/compute/server.rb index f3b628b34..7b0d89fed 100644 --- a/lib/fog/aws/models/compute/server.rb +++ b/lib/fog/aws/models/compute/server.rb @@ -33,7 +33,7 @@ module Fog attribute :user_data attr_accessor :password, :username - attr_writer :private_key_path, :public_key_path + attr_writer :private_key, :private_key_path, :public_key, :public_key_path def initialize(attributes={}) @groups ||= ["default"] unless attributes[:subnet_id] @@ -79,7 +79,7 @@ module Fog end def key_pair=(new_keypair) - @key_name = new_keypair.name + @key_name = new_keypair && new_keypair.name end def monitoring=(new_monitoring) @@ -99,13 +99,20 @@ module Fog end def private_key_path - @private_key_path ||= Fog.credentials[:private_key_path] + File.expand_path(@private_key_path ||= Fog.credentials[:private_key_path]) + end + + def private_key + @private_key ||= File.read(private_key_path) end def public_key_path - @public_key_path ||= Fog.credentials[:public_key_path] + File.expand_path(@public_key_path ||= Fog.credentials[:public_key_path]) end + def public_key + @public_key ||= File.read(public_key_path) + end def ready? @state == 'running' end @@ -147,11 +154,11 @@ module Fog end def setup(credentials = {}) - requires :ip_address, :identity, :public_key_path, :username + requires :identity, :ip_address, :public_key, :username sleep(10) # takes a bit before EC2 instances will play nice Fog::SSH.new(ip_address, username, credentials).run([ %{mkdir .ssh}, - %{echo "#{File.read(public_key_path)}" >> ~/.ssh/authorized_keys}, + %{echo "#{public_key}" >> ~/.ssh/authorized_keys}, %{passwd -l root}, %{echo "#{attributes.to_json}" >> ~/attributes.json} ]) @@ -161,8 +168,8 @@ module Fog end def ssh(commands) - requires :identity, :ip_address, :private_key_path, :username - @ssh ||= Fog::SSH.new(ip_address, username, :keys => [private_key_path]) + requires :identity, :ip_address, :private_key, :username + @ssh ||= Fog::SSH.new(ip_address, username, :key_data => [private_key]) @ssh.run(commands) end diff --git a/lib/fog/aws/models/compute/servers.rb b/lib/fog/aws/models/compute/servers.rb index f77be729d..f1bfca344 100644 --- a/lib/fog/aws/models/compute/servers.rb +++ b/lib/fog/aws/models/compute/servers.rb @@ -9,8 +9,6 @@ module Fog attribute :server_id - attr_writer :private_key_path, :public_key_path - model Fog::AWS::Compute::Server def initialize(attributes) @@ -31,10 +29,14 @@ module Fog end def bootstrap(new_attributes = {}) + server = connection.servers.new(new_attributes) + # 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) + unless server.key_pair = connection.key_pairs.get("fog_#{Fog.credential}") + server.key_pair = connection.key_pairs.create( + :name => "fog_#{Fog.credential}", + :public_key => server.public_key + ) end # make sure port 22 is open in the first security group @@ -49,11 +51,9 @@ module Fog security_group.authorize_port_range(22..22) end + server.save 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.setup(:key_data => [server.private_key]) server end @@ -65,14 +65,6 @@ 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 diff --git a/lib/fog/rackspace/models/compute/server.rb b/lib/fog/rackspace/models/compute/server.rb index 6134ab240..f67097735 100644 --- a/lib/fog/rackspace/models/compute/server.rb +++ b/lib/fog/rackspace/models/compute/server.rb @@ -18,7 +18,8 @@ module Fog attribute :progress attribute :status - attr_accessor :password, :private_key_path, :public_key_path, :username + attr_accessor :password, :username + attr_writer :private_key, :private_key_path, :public_key, :public_key_path def initialize(attributes={}) @flavor_id ||= 1 @@ -57,11 +58,19 @@ module Fog end def private_key_path - @private_key_path ||= Fog.credentials[:private_key_path] + File.expand_path(@private_key_path ||= Fog.credentials[:private_key_path]) + end + + def private_key + @private_key ||= File.read(private_key_path) end def public_key_path - @public_key_path ||= Fog.credentials[:public_key_path] + File.expand_path(@public_key_path ||= Fog.credentials[:public_key_path]) + end + + def public_key + @public_key ||= File.read(public_key_path) end def save @@ -78,10 +87,10 @@ module Fog end def setup(credentials = {}) - requires :addresses, :identity, :public_key_path, :username + requires :addresses, :identity, :public_key, :username Fog::SSH.new(addresses['public'].first, username, credentials).run([ %{mkdir .ssh}, - %{echo "#{File.read(File.expand_path(public_key_path))}" >> ~/.ssh/authorized_keys}, + %{echo "#{public_key}" >> ~/.ssh/authorized_keys}, %{passwd -l root}, %{echo "#{attributes.to_json}" >> ~/attributes.json}, %{echo "#{metadata.to_json}" >> ~/metadata.json} @@ -92,13 +101,13 @@ module Fog end def ssh(commands) - requires :addresses, :identity, :private_key_path, :username - @ssh ||= Fog::SSH.new(addresses['public'].first, username, :keys => [private_key_path]) + requires :addresses, :identity, :private_key, :username + @ssh ||= Fog::SSH.new(addresses['public'].first, username, :key_data => [private_key]) @ssh.run(commands) end def username - @username ||= root + @username ||= 'root' end private