1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/bluebox/models/compute/server.rb

121 lines
3.3 KiB
Ruby
Raw Normal View History

require 'fog/compute/models/server'
module Fog
module Compute
class Bluebox
class BlockInstantiationError < StandardError; end
class Server < Fog::Compute::Server
identity :id
attribute :cpu
2010-10-12 16:01:17 -07:00
attribute :description
attribute :flavor_id, :aliases => :product, :squash => 'id'
2010-10-12 16:01:17 -07:00
attribute :hostname
attribute :image_id
2012-02-12 00:02:52 -08:00
attribute :location_id
2010-10-12 16:01:17 -07:00
attribute :ips
attribute :memory
2011-09-02 11:37:42 -07:00
attribute :state, :aliases => "status"
2010-10-12 16:01:17 -07:00
attribute :storage
attribute :template
attr_accessor :hostname, :password, :lb_applications, :lb_services, :lb_backends
def initialize(attributes={})
2012-02-12 00:02:52 -08:00
self.flavor_id ||= '94fd37a7-2606-47f7-84d5-9000deda52ae' # Block 1GB Virtual Server
self.image_id ||= 'a8f05200-7638-47d1-8282-2474ef57c4c3' # Scientific Linux 6
2012-02-16 18:42:00 -08:00
self.location_id ||= '37c2bd9a-3e81-46c9-b6e2-db44a25cc675' # Seattle, WA
super
end
def destroy
requires :id
connection.destroy_block(id)
true
end
def flavor
requires :flavor_id
connection.flavors.get(flavor_id)
end
def image
requires :image_id
connection.images.get(image_id)
end
2012-02-12 00:02:52 -08:00
def location
requires :location_id
connection.locations.get(location_id)
end
def private_ip_address
nil
end
def public_ip_address
ips.first
end
def ready?
self.state == 'running'
end
def reboot(type = 'SOFT')
requires :id
connection.reboot_block(id, type)
true
end
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
2012-02-12 00:02:52 -08:00
requires :flavor_id, :image_id, :location_id
2011-02-17 21:22:43 -08:00
options = {}
unless persisted? # new record
2011-02-17 21:22:43 -08:00
raise(ArgumentError, "password or public_key is required for this operation") if !password && !public_key
options['ssh_public_key'] = public_key if public_key
2011-02-17 21:22:43 -08:00
options['password'] = password if @password
end
2011-02-17 21:22:43 -08: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
2012-02-12 00:02:52 -08:00
options['username'] = username
options['hostname'] = hostname if @hostname
2012-02-12 00:02:52 -08:00
data = connection.create_block(flavor_id, image_id, location_id, options)
merge_attributes(data.body)
true
end
def setup(credentials = {})
requires :identity, :ips, :public_key, :username
Fog::SSH.new(public_ip_address, username, credentials).run([
%{mkdir .ssh},
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
%{passwd -l #{username}},
2012-04-25 10:31:28 -04:00
%{echo "#{Fog::JSON.encode(attributes)}" >> ~/attributes.json}
])
rescue Errno::ECONNREFUSED
sleep(1)
retry
end
def username
@username ||= 'deploy'
end
end
end
end
end