2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/model'
|
2010-09-07 19:26:01 -04:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Bluebox
|
2010-09-08 15:07:45 -04:00
|
|
|
class Compute
|
2010-09-07 19:26:01 -04:00
|
|
|
|
|
|
|
class BlockInstantiationError < StandardError; end
|
|
|
|
|
|
|
|
class Server < Fog::Model
|
|
|
|
|
|
|
|
identity :id
|
|
|
|
|
|
|
|
attribute :cpu
|
2010-10-12 19:01:17 -04:00
|
|
|
attribute :description
|
2010-11-19 16:45:45 -05:00
|
|
|
attribute :flavor_id, :aliases => :product, :squash => 'id'
|
2010-10-12 19:01:17 -04:00
|
|
|
attribute :hostname
|
2010-09-28 01:12:41 -04:00
|
|
|
attribute :image_id
|
2010-10-12 19:01:17 -04:00
|
|
|
attribute :ips
|
|
|
|
attribute :memory
|
2011-05-24 17:59:48 -04:00
|
|
|
attribute :state, :aliases => :status
|
2010-10-12 19:01:17 -04:00
|
|
|
attribute :storage
|
2010-09-07 19:26:01 -04:00
|
|
|
attribute :template
|
|
|
|
|
2011-04-07 16:18:35 -04:00
|
|
|
attr_accessor :password, :lb_applications, :lb_services, :lb_backends
|
|
|
|
attr_writer :private_key, :private_key_path, :public_key, :public_key_path, :username
|
2010-09-28 01:12:41 -04:00
|
|
|
|
|
|
|
def initialize(attributes={})
|
2011-04-14 19:31:58 -04:00
|
|
|
self.flavor_id ||= '94fd37a7-2606-47f7-84d5-9000deda52ae' # Block 1GB Virtual Server
|
|
|
|
self.image_id ||= '03807e08-a13d-44e4-b011-ebec7ef2c928' # Ubuntu LTS 10.04 64bit
|
2010-09-28 01:12:41 -04:00
|
|
|
super
|
|
|
|
end
|
2010-09-07 19:26:01 -04:00
|
|
|
|
|
|
|
def destroy
|
|
|
|
requires :id
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.destroy_block(id)
|
2010-09-07 19:26:01 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def flavor
|
|
|
|
requires :flavor_id
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.flavors.get(flavor_id)
|
2010-09-07 19:26:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def image
|
|
|
|
requires :image_id
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.images.get(image_id)
|
2010-09-07 19:26:01 -04:00
|
|
|
end
|
|
|
|
|
2011-02-22 19:36:15 -05:00
|
|
|
def private_ip_address
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2010-09-28 01:12:41 -04:00
|
|
|
def private_key_path
|
2010-10-20 17:32:30 -04:00
|
|
|
@private_key_path ||= Fog.credentials[:private_key_path]
|
|
|
|
@private_key_path &&= File.expand_path(@private_key_path)
|
2010-09-28 01:12:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def private_key
|
2010-10-20 17:32:30 -04:00
|
|
|
@private_key ||= private_key_path && File.read(private_key_path)
|
2010-09-28 01:12:41 -04:00
|
|
|
end
|
|
|
|
|
2011-02-22 19:36:15 -05:00
|
|
|
def public_ip_address
|
|
|
|
ips.first
|
|
|
|
end
|
|
|
|
|
2010-09-28 01:12:41 -04:00
|
|
|
def public_key_path
|
2010-10-20 17:32:30 -04:00
|
|
|
@public_key_path ||= Fog.credentials[:public_key_path]
|
|
|
|
@public_key_path &&= File.expand_path(@public_key_path)
|
2010-09-28 01:12:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def public_key
|
2010-10-20 17:32:30 -04:00
|
|
|
@public_key ||= public_key_path && File.read(public_key_path)
|
2010-09-28 01:12:41 -04:00
|
|
|
end
|
|
|
|
|
2010-09-07 19:26:01 -04:00
|
|
|
def ready?
|
2011-05-24 17:59:48 -04:00
|
|
|
self.state == 'running'
|
2010-09-07 19:26:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reboot(type = 'SOFT')
|
|
|
|
requires :id
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.reboot_block(id, type)
|
2010-09-07 19:26:01 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
2010-10-04 19:08:34 -04:00
|
|
|
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
|
2010-09-07 19:26:01 -04:00
|
|
|
requires :flavor_id, :image_id
|
2011-02-18 00:22:43 -05:00
|
|
|
options = {}
|
|
|
|
|
|
|
|
if identity.nil? # new record
|
|
|
|
raise(ArgumentError, "password or public_key is required for this operation") if !password && !public_key
|
2011-03-26 15:17:52 -04:00
|
|
|
options['ssh_public_key'] = public_key if public_key
|
2011-02-18 00:22:43 -05:00
|
|
|
options['password'] = password if @password
|
2010-09-07 19:26:01 -04:00
|
|
|
end
|
2011-02-18 00:22:43 -05:00
|
|
|
|
2011-04-07 14:06:04 -04:00
|
|
|
if @lb_backends
|
|
|
|
options['lb_backends'] = lb_backends
|
|
|
|
elsif @lb_services
|
|
|
|
options['lb_services'] = lb_services
|
|
|
|
elsif @lb_applications
|
|
|
|
options['lb_applications'] = lb_applications
|
|
|
|
end
|
|
|
|
|
2010-09-28 01:12:41 -04:00
|
|
|
options['username'] = username
|
|
|
|
data = connection.create_block(flavor_id, image_id, options)
|
2010-09-07 19:26:01 -04:00
|
|
|
merge_attributes(data.body)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-09-28 01:12:41 -04:00
|
|
|
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},
|
2011-01-31 20:42:53 -05:00
|
|
|
%{passwd -l #{username}},
|
2010-09-28 01:12:41 -04:00
|
|
|
%{echo "#{attributes.to_json}" >> ~/attributes.json}
|
|
|
|
])
|
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
sleep(1)
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
|
|
|
def ssh(commands)
|
2011-01-22 04:11:19 -05:00
|
|
|
requires :identity, :ips, :username
|
|
|
|
|
|
|
|
options = {}
|
|
|
|
options[:key_data] = [private_key] if private_key
|
|
|
|
Fog::SSH.new(ips.first['address'], username, options).run(commands)
|
2010-09-28 01:12:41 -04:00
|
|
|
end
|
|
|
|
|
2011-06-13 13:53:08 -04:00
|
|
|
def scp(local_path, remote_path, upload_options = {})
|
2011-02-24 19:43:12 -05:00
|
|
|
requires :ips, :username
|
|
|
|
|
2011-06-13 13:53:08 -04:00
|
|
|
scp_options = {}
|
|
|
|
scp_options[:key_data] = [private_key] if private_key
|
|
|
|
Fog::SCP.new(ips.first['address'], username, scp_options).upload(local_path, remote_path, upload_options)
|
2011-02-24 19:43:12 -05:00
|
|
|
end
|
|
|
|
|
2010-09-28 01:12:41 -04:00
|
|
|
def username
|
|
|
|
@username ||= 'deploy'
|
|
|
|
end
|
|
|
|
|
2010-09-07 19:26:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|