2012-04-02 07:25:03 -04:00
|
|
|
require 'fog/compute/models/server'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class XenServer
|
|
|
|
|
|
|
|
class Server < Fog::Compute::Server
|
|
|
|
# API Reference here:
|
|
|
|
# http://docs.vmd.citrix.com/XenServer/5.6.0/1.0/en_gb/api/?c=VM
|
|
|
|
|
|
|
|
identity :reference
|
|
|
|
|
|
|
|
attribute :uuid
|
2012-04-04 03:01:55 -04:00
|
|
|
attribute :name, :aliases => :name_label
|
2012-04-02 07:25:03 -04:00
|
|
|
attribute :affinity
|
|
|
|
attribute :allowed_operations
|
|
|
|
attribute :consoles
|
|
|
|
attribute :domarch
|
|
|
|
attribute :domid
|
2012-04-04 03:01:55 -04:00
|
|
|
attribute :__guest_metrics, :aliases => :guest_metrics
|
2012-04-02 07:25:03 -04:00
|
|
|
attribute :is_a_snapshot
|
|
|
|
attribute :is_a_template
|
|
|
|
attribute :is_control_domain
|
|
|
|
attribute :memory_dynamic_max
|
|
|
|
attribute :memory_dynamic_min
|
|
|
|
attribute :memory_static_max
|
|
|
|
attribute :memory_static_min
|
|
|
|
attribute :memory_target
|
|
|
|
attribute :metrics
|
|
|
|
attribute :name_description
|
|
|
|
attribute :other_config
|
|
|
|
attribute :power_state
|
2012-04-04 03:01:55 -04:00
|
|
|
attribute :pv_args, :aliases => :PV_args
|
|
|
|
attribute :__resident_on, :aliases => :resident_on
|
2012-04-02 07:25:03 -04:00
|
|
|
# Virtual Block Devices
|
|
|
|
attribute :__vbds, :aliases => :VBDs
|
|
|
|
# Virtual CPUs
|
2012-04-04 03:01:55 -04:00
|
|
|
attribute :vcpus_at_startup, :aliases => :VCPUs_at_startup
|
|
|
|
attribute :vcpus_max, :aliases => :VCPUs_max
|
2012-04-02 07:25:03 -04:00
|
|
|
# Virtual Interfaces (NIC)
|
|
|
|
attribute :__vifs, :aliases => :VIFs
|
|
|
|
attribute :template_name
|
|
|
|
|
|
|
|
def initialize(attributes={})
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def vbds
|
|
|
|
__vbds.collect {|vbd| connection.vbds.get vbd }
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2012-04-04 13:48:21 -04:00
|
|
|
# Make sure it's halted
|
|
|
|
stop('hard')
|
2012-04-02 07:25:03 -04:00
|
|
|
vbds.each do |vbd|
|
2012-04-04 13:48:21 -04:00
|
|
|
if vbd.type == "Disk"
|
2012-04-10 02:01:17 -04:00
|
|
|
vbd.unplug \
|
2012-04-04 13:48:21 -04:00
|
|
|
if vbd.allowed_operations.include?("unplug")
|
2012-04-10 02:01:17 -04:00
|
|
|
vbd.vdi.destroy \
|
2012-04-04 13:48:21 -04:00
|
|
|
if vbd.vdi.allowed_operations.include?("destroy")
|
|
|
|
end
|
2012-04-02 07:25:03 -04:00
|
|
|
end
|
|
|
|
connection.destroy_server( reference )
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def refresh
|
|
|
|
data = connection.get_record( reference, 'VM' )
|
|
|
|
merge_attributes( data )
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def vifs
|
2012-04-04 13:48:21 -04:00
|
|
|
__vifs.collect { |vif| connection.vifs.get vif }
|
2012-04-02 07:25:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# associations
|
|
|
|
def networks
|
2012-04-04 13:48:21 -04:00
|
|
|
vifs.collect { |v| v.network }
|
2012-04-04 03:01:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def resident_on
|
|
|
|
connection.hosts.get __resident_on
|
2012-04-02 07:25:03 -04:00
|
|
|
end
|
|
|
|
|
2012-04-04 03:01:55 -04:00
|
|
|
#
|
|
|
|
# This is not always present in XenServer VMs
|
|
|
|
# Guest needs XenTools installed to report this AFAIK
|
|
|
|
def guest_metrics
|
|
|
|
begin
|
|
|
|
rec = connection.get_record( __guest_metrics, 'VM_guest_metrics' )
|
|
|
|
Fog::Compute::XenServer::GuestMetrics.new(rec)
|
|
|
|
rescue Fog::XenServer::RequestFailed
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tools_installed?
|
|
|
|
!guest_metrics.nil?
|
2012-04-02 07:25:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def home_hypervisor
|
|
|
|
connection.hosts.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def mac_address
|
|
|
|
networks.first.MAC
|
|
|
|
end
|
|
|
|
|
|
|
|
def running?
|
2012-04-04 13:48:21 -04:00
|
|
|
reload
|
|
|
|
power_state == "Running"
|
2012-04-02 07:25:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def halted?
|
2012-04-04 13:48:21 -04:00
|
|
|
reload
|
|
|
|
power_state == "Halted"
|
2012-04-02 07:25:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# operations
|
|
|
|
def start
|
|
|
|
return false if running?
|
|
|
|
connection.start_server( reference )
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def save(params = {})
|
|
|
|
requires :name
|
2012-04-09 06:36:08 -04:00
|
|
|
nets = attributes[:networks] || []
|
2012-04-09 13:29:17 -04:00
|
|
|
if params[:auto_start].nil?
|
|
|
|
auto_start = true
|
|
|
|
else
|
|
|
|
auto_start = params[:auto_start]
|
|
|
|
end
|
|
|
|
attributes = connection.get_record(
|
|
|
|
connection.create_server( name, template_name, nets, :auto_start => auto_start),
|
|
|
|
'VM'
|
|
|
|
)
|
2012-04-04 13:48:21 -04:00
|
|
|
merge_attributes attributes
|
2012-04-02 07:25:03 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def reboot(stype = 'clean')
|
|
|
|
connection.reboot_server(reference, stype)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def hard_reboot
|
|
|
|
reboot 'hard'
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_reboot
|
|
|
|
reboot 'clean'
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop(stype = 'clean')
|
|
|
|
return false if !running?
|
|
|
|
connection.shutdown_server( reference, stype )
|
2012-04-04 13:48:21 -04:00
|
|
|
wait_for { power_state == 'Halted' }
|
2012-04-02 07:25:03 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def hard_shutdown
|
|
|
|
stop 'hard'
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_shutdown
|
|
|
|
stop 'clean'
|
|
|
|
end
|
|
|
|
|
|
|
|
# def snapshot
|
|
|
|
# requires :reference, :name_label
|
|
|
|
# data = connection.snapshot_server(@reference, @name_label)
|
|
|
|
# merge_attributes(data.body)
|
|
|
|
# true
|
|
|
|
# end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|