2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/model'
|
2010-09-08 18:00:47 -04:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Slicehost
|
|
|
|
class Compute
|
|
|
|
|
|
|
|
class Server < Fog::Model
|
|
|
|
|
|
|
|
identity :id
|
|
|
|
|
|
|
|
attribute :addresses
|
|
|
|
attribute :backup_id, :aliases => 'backup-id'
|
|
|
|
attribute :bandwidth_in, :aliases => 'bw-in'
|
|
|
|
attribute :bandwidth_out, :aliases => 'bw-out'
|
|
|
|
attribute :flavor_id, :aliases => 'flavor-id'
|
|
|
|
attribute :image_id, :aliases => 'image-id'
|
|
|
|
attribute :name
|
|
|
|
attribute :progress
|
|
|
|
attribute :status
|
|
|
|
|
2010-09-27 23:38:17 -04:00
|
|
|
attr_accessor :password
|
|
|
|
alias_method :'root-password=', :password=
|
|
|
|
attr_writer :private_key, :private_key_path, :public_key, :public_key_path, :username
|
|
|
|
|
2010-09-08 18:00:47 -04:00
|
|
|
def initialize(attributes={})
|
2010-11-19 16:45:45 -05:00
|
|
|
self.flavor_id ||= 1
|
2010-09-08 18:00:47 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
requires :id
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.delete_slice(id)
|
2010-09-08 18:00:47 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def flavor
|
|
|
|
requires :flavor_id
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.flavors.get(flavor_id)
|
2010-09-08 18:00:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def image
|
|
|
|
requires :image_id
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.images.get(image_id)
|
2010-09-08 18:00:47 -04:00
|
|
|
end
|
|
|
|
|
2010-09-27 23:38:17 -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-27 23:38:17 -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-27 23:38:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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-27 23:38:17 -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-27 23:38:17 -04:00
|
|
|
end
|
|
|
|
|
2010-09-08 18:00:47 -04:00
|
|
|
def ready?
|
2010-11-19 16:45:45 -05:00
|
|
|
status == 'active'
|
2010-09-08 18:00:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reboot(type = 'SOFT')
|
|
|
|
requires :id
|
2010-11-19 16:45:45 -05:00
|
|
|
connection.reboot_slice(id, type)
|
2010-09-08 18:00:47 -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-08 18:00:47 -04:00
|
|
|
requires :flavor_id, :image_id, :name
|
2010-10-04 19:08:34 -04:00
|
|
|
|
2010-11-19 16:45:45 -05:00
|
|
|
data = connection.create_slice(flavor_id, image_id, name)
|
2010-09-08 18:00:47 -04:00
|
|
|
merge_attributes(data.body)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-09-27 23:38:17 -04:00
|
|
|
def setup(credentials = {})
|
|
|
|
requires :addresses, :identity, :public_key, :username
|
|
|
|
Fog::SSH.new(addresses.first, 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 :addresses, :identity, :private_key, :username
|
2010-10-20 19:22:31 -04:00
|
|
|
Fog::SSH.new(addresses.first, username, :key_data => [private_key]).run(commands)
|
2010-09-27 23:38:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
@username ||= 'root'
|
|
|
|
end
|
|
|
|
|
2010-09-08 18:00:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|