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

[bluebox][compute] first pass at bootstrap/setup

This commit is contained in:
geemus 2010-09-27 22:12:41 -07:00
parent e0a5341534
commit b5025c939b
2 changed files with 66 additions and 14 deletions

View file

@ -7,6 +7,11 @@ module Fog
class BlockInstantiationError < StandardError; end
class Server < Fog::Model
extend Fog::Deprecation
deprecate(:ssh_key, :public_key)
deprecate(:ssh_key=, :public_key=)
deprecate(:user, :username)
deprecate(:user=, :username=)
identity :id
@ -17,13 +22,16 @@ module Fog
attribute :ips
attribute :status
attribute :flavor_id
# attribute :image_id
attr_accessor :image_id
attribute :image_id
attribute :template
# Not reported by the API, but used at create time
attr_accessor :password, :ssh_key, :user
attr_accessor :password
attr_writer :private_key, :private_key_path, :public_key, :public_key_path, :username
def initialize(attributes={})
@flavor_id ||= '94fd37a7-2606-47f7-84d5-9000deda52ae'
super
end
def destroy
requires :id
@ -41,6 +49,22 @@ module Fog
connection.images.get(@image_id)
end
def 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
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?
@status == 'running'
end
@ -53,21 +77,42 @@ module Fog
def save
requires :flavor_id, :image_id
options = if !@password && !@ssh_key
raise(ArgumentError, "password or ssh_key is required for this operation")
elsif @ssh_key
{'ssh_public_key' => @ssh_key}
options = if !password && !public_key
raise(ArgumentError, "password or public_key is required for this operation")
elsif public_key
{'ssh_public_key' => public_key}
elsif @password
{'password' => @password}
{'password' => password}
end
if @user
options['user'] = @user
end
data = connection.create_block(@flavor_id, @image_id, options)
options['username'] = username
data = connection.create_block(flavor_id, image_id, options)
merge_attributes(data.body)
true
end
def setup(credentials = {})
requires :identity, :ips, :public_key, :username
Fog::SSH.new(ips.first['address'], username, credentials).run([
%{mkdir .ssh},
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
%{passwd -l root},
%{echo "#{attributes.to_json}" >> ~/attributes.json}
])
rescue Errno::ECONNREFUSED
sleep(1)
retry
end
def ssh(commands)
requires :identity, :ips, :private_key, :username
@ssh ||= Fog::SSH.new(ips.first['address'], username, :key_data => [private_key])
@ssh.run(commands)
end
def username
@username ||= 'deploy'
end
private
def product=(new_product)

View file

@ -14,6 +14,13 @@ module Fog
load(data)
end
def bootstrap(new_attributes = {})
server = create(new_attributes)
server.wait_for { ready? }
server.setup(:key_data => [server.private_key])
server
end
def get(server_id)
if server_id && server = connection.get_block(server_id).body
new(server)