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/compute/models/rackspace/server.rb

146 lines
3.7 KiB
Ruby
Raw Normal View History

2010-10-04 17:02:08 -04:00
require 'fog/core/model'
module Fog
module Rackspace
class Compute
class Server < Fog::Model
identity :id
attribute :addresses
attribute :flavor_id, :aliases => 'flavorId'
attribute :host_id, :aliases => 'hostId'
attribute :image_id, :aliases => 'imageId'
attribute :metadata
attribute :name
attribute :personality
attribute :progress
attribute :status
attr_reader :password
attr_writer :private_key, :private_key_path, :public_key, :public_key_path, :username
2010-06-06 02:05:06 -04:00
def initialize(attributes={})
self.flavor_id ||= 1
super
end
def destroy
requires :id
connection.delete_server(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
def images
requires :id
connection.images(:server => self)
end
def private_ip_address
nil
end
2010-06-06 02:05:06 -04:00
def private_key_path
@private_key_path ||= Fog.credentials[:private_key_path]
@private_key_path &&= File.expand_path(@private_key_path)
2010-09-23 20:39:25 -04:00
end
def private_key
@private_key ||= private_key_path && File.read(private_key_path)
2010-06-06 02:05:06 -04:00
end
def public_ip_address
addresses.first
end
2010-06-06 02:05:06 -04:00
def public_key_path
@public_key_path ||= Fog.credentials[:public_key_path]
@public_key_path &&= File.expand_path(@public_key_path)
2010-09-23 20:39:25 -04:00
end
def public_key
@public_key ||= public_key_path && File.read(public_key_path)
2010-06-06 02:05:06 -04:00
end
def ready?
status == 'ACTIVE'
end
def reboot(type = 'SOFT')
requires :id
connection.reboot_server(id, type)
true
end
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
requires :flavor_id, :image_id
options = {
'metadata' => metadata,
'name' => name,
'personality' => personality
}
2009-10-15 18:06:50 -04:00
options = options.reject {|key, value| value.nil?}
data = connection.create_server(flavor_id, image_id, options)
merge_attributes(data.body['server'])
true
end
def setup(credentials = {})
2010-09-23 20:39:25 -04:00
requires :addresses, :identity, :public_key, :username
Fog::SSH.new(addresses['public'].first, username, credentials).run([
2010-06-06 02:05:06 -04:00
%{mkdir .ssh},
2010-09-23 20:39:25 -04:00
%{echo "#{public_key}" >> ~/.ssh/authorized_keys},
%{passwd -l #{username}},
2010-06-06 02:05:06 -04:00
%{echo "#{attributes.to_json}" >> ~/attributes.json},
%{echo "#{metadata.to_json}" >> ~/metadata.json}
])
rescue Errno::ECONNREFUSED
sleep(1)
retry
end
def ssh(commands)
requires :addresses, :identity, :username
options = {}
options[:key_data] = [private_key] if private_key
Fog::SSH.new(addresses['public'].first, username, options).run(commands)
end
2011-02-24 19:43:12 -05:00
def scp(local_path, remote_path)
requires :addresses, :username
options = {}
options[:key_data] = [private_key] if private_key
Fog::SCP.new(addresses['public'].first, username, options).upload(local_path, remote_path)
end
def username
2010-09-23 20:39:25 -04:00
@username ||= 'root'
end
2010-06-06 02:05:06 -04:00
private
def adminPass=(new_admin_pass)
@password = new_admin_pass
end
end
end
end
2010-06-06 02:05:06 -04:00
end