2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/model'
|
2010-09-08 18:00:47 -04:00
|
|
|
|
|
|
|
module Fog
|
2011-06-16 19:28:54 -04:00
|
|
|
module Compute
|
|
|
|
class Slicehost
|
2010-09-08 18:00:47 -04:00
|
|
|
|
|
|
|
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
|
2011-05-24 17:59:48 -04:00
|
|
|
attribute :state, :aliases => 'status'
|
2010-09-08 18:00:47 -04:00
|
|
|
|
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={})
|
2011-04-14 19:31:58 -04:00
|
|
|
self.flavor_id ||= 1 # 256 server
|
|
|
|
self.image_id ||= 49 # Ubuntu 10.04 LTS 64bit
|
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
|
|
|
|
|
2011-02-22 19:36:15 -05:00
|
|
|
def private_ip_address
|
|
|
|
nil
|
|
|
|
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
|
|
|
|
|
2011-02-22 19:36:15 -05:00
|
|
|
def public_ip_address
|
|
|
|
addresses.first
|
|
|
|
end
|
|
|
|
|
2010-09-27 23:38:17 -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-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?
|
2011-05-24 17:59:48 -04:00
|
|
|
self.state == '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},
|
2011-01-31 20:42:53 -05:00
|
|
|
%{passwd -l #{username}},
|
2010-09-27 23:38:17 -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 :addresses, :identity, :username
|
|
|
|
|
|
|
|
options = {}
|
|
|
|
options[:key_data] = [private_key] if private_key
|
|
|
|
Fog::SSH.new(addresses.first, username, options).run(commands)
|
2010-09-27 23:38:17 -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 :addresses, :username
|
|
|
|
|
2011-06-13 13:53:08 -04:00
|
|
|
scp_options = {}
|
|
|
|
scp_options[:key_data] = [private_key] if private_key
|
|
|
|
Fog::SCP.new(addresses.first, username, scp_options).upload(local_path, remote_path, upload_options)
|
2011-02-24 19:43:12 -05:00
|
|
|
end
|
|
|
|
|
2010-09-27 23:38:17 -04:00
|
|
|
def username
|
|
|
|
@username ||= 'root'
|
|
|
|
end
|
|
|
|
|
2010-09-08 18:00:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|