diff --git a/lib/fog/credentials.rb b/lib/fog/credentials.rb index 16ff8c5b7..6d0ea9aa0 100644 --- a/lib/fog/credentials.rb +++ b/lib/fog/credentials.rb @@ -34,6 +34,8 @@ module Fog :bluebox_api_key: INTENTIONALLY_LEFT_BLANK :bluebox_customer_id: INTENTIONALLY_LEFT_BLANK :local_root: INTENTIONALLY_LEFT_BLANK + :public_key_path: INTENTIONALLY_LEFT_BLANK + :private_key_path: INTENTIONALLY_LEFT_BLANK :rackspace_api_key: INTENTIONALLY_LEFT_BLANK :rackspace_username: INTENTIONALLY_LEFT_BLANK :slicehost_password: INTENTIONALLY_LEFT_BLANK diff --git a/lib/fog/rackspace/models/servers/server.rb b/lib/fog/rackspace/models/servers/server.rb index 0bfbf74d3..2c59d849a 100644 --- a/lib/fog/rackspace/models/servers/server.rb +++ b/lib/fog/rackspace/models/servers/server.rb @@ -9,7 +9,6 @@ module Fog identity :id attribute :addresses - attribute :password, 'adminPass' attribute :flavor_id, 'flavorId' attribute :host_id, 'hostId' attribute :image_id, 'imageId' @@ -19,6 +18,8 @@ module Fog attribute :progress attribute :status + attr_accessor :password, :private_key_path, :public_key_path + def destroy requires :id connection.delete_server(@id) @@ -50,6 +51,14 @@ module Fog true 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 + def save requires :flavor_id, :image_id options = { @@ -63,8 +72,35 @@ module Fog true end + def ssh(commands) + requires :addresses, :identity, :private_key_path + @ssh ||= Fog::SSH.new(@addresses['public'].first, 'root', :keys => [private_key_path]) + @ssh.run(commands) + end + + def setup + requires :addresses, :identity, :password, :public_key_path + Fog::SSH.new(@addresses['public'].first, 'root', :password => password).run([ + %{mkdir .ssh}, + %{echo "#{File.read(File.expand_path(public_key_path))}" >> ~/.ssh/authorized_keys}, + %{passwd -l root}, + %{echo "#{attributes.to_json}" >> ~/attributes.json}, + %{echo "#{metadata.to_json}" >> ~/metadata.json} + ]) + rescue Errno::ECONNREFUSED + sleep(1) + retry + end + + private + + def adminPass=(new_admin_pass) + @password = new_admin_pass + end + end end end + end diff --git a/lib/fog/rackspace/models/servers/servers.rb b/lib/fog/rackspace/models/servers/servers.rb index 68f45dc43..9096f7e79 100644 --- a/lib/fog/rackspace/models/servers/servers.rb +++ b/lib/fog/rackspace/models/servers/servers.rb @@ -20,6 +20,13 @@ module Fog load(data) end + def bootstrap(new_attributes = {}) + server = create(new_attributes) + server.wait_for { ready? } + server.setup + server + end + def get(server_id) if server = connection.get_server_details(server_id).body['server'] new(server) diff --git a/lib/fog/rackspace/servers.rb b/lib/fog/rackspace/servers.rb index a39c68185..67a3bcc29 100644 --- a/lib/fog/rackspace/servers.rb +++ b/lib/fog/rackspace/servers.rb @@ -77,7 +77,6 @@ module Fog def request(params) @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}") - begin response = @connection.request(params.merge!({ :headers => { diff --git a/lib/fog/ssh.rb b/lib/fog/ssh.rb index f225fa765..8deb10f61 100644 --- a/lib/fog/ssh.rb +++ b/lib/fog/ssh.rb @@ -54,7 +54,7 @@ module Fog sudoable_command = command.sub(/^sudo/, %{sudo -p 'fog sudo password:'}) escaped_command = sudoable_command.sub(/'/, %{'"'"'}) channel.request_pty - result = Result.new(command) + result = Result.new(escaped_command) channel.exec(%{bash -lc '#{escaped_command}'}) do |channel, success| unless success raise "Could not execute command: #{command.inspect}"