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

212 lines
5.8 KiB
Ruby
Raw Normal View History

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
2012-04-02 07:25:03 -04:00
identity :reference
2012-04-02 07:25:03 -04:00
attribute :uuid
attribute :name, :aliases => :name_label
attribute :__affinity, :aliases => :affinity
2012-04-02 07:25:03 -04:00
attribute :allowed_operations
attribute :consoles
attribute :domarch
attribute :domid
attribute :tags
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
attribute :pv_args, :aliases => :PV_args
attribute :pv_bootloader, :aliases => :PV_bootloader
attribute :pv_bootloader_args, :aliases => :PV_bootloader_args
attribute :pv_kernel, :aliases => :PV_kernel
attribute :pv_ramdisk, :aliases => :PV_ramdisk
attribute :pv_legacy_args, :aliases => :PV_legacy_args
attribute :__resident_on, :aliases => :resident_on
2012-04-02 07:25:03 -04:00
# Virtual Block Devices
attribute :__vbds, :aliases => :VBDs
# Virtual CPUs
attribute :vcpus_at_startup, :aliases => :VCPUs_at_startup
attribute :vcpus_max, :aliases => :VCPUs_max
attribute :vcpus_params, :aliases => :VCPUs_params
2012-04-02 07:25:03 -04:00
# Virtual Interfaces (NIC)
attribute :__vifs, :aliases => :VIFs
attribute :template_name
attribute :hvm_boot_policy, :aliases => :HVM_boot_policy
attribute :hvm_boot_params, :aliases => :HVM_boot_params
attribute :pci_bus, :aliases => :PCI_bus
2012-04-02 07:25:03 -04:00
def initialize(attributes={})
super
end
def vbds
__vbds.collect {|vbd| service.vbds.get vbd }
2012-04-02 07:25:03 -04:00
end
def affinity
service.hosts.get __affinity
end
2012-04-02 07:25:03 -04:00
def destroy
# Make sure it's halted
stop('hard')
2012-04-02 07:25:03 -04:00
vbds.each do |vbd|
if vbd.type == "Disk"
2012-04-10 02:01:17 -04:00
vbd.unplug \
if vbd.allowed_operations.include?("unplug")
2012-04-10 02:01:17 -04:00
vbd.vdi.destroy \
if vbd.vdi.allowed_operations.include?("destroy")
end
2012-04-02 07:25:03 -04:00
end
service.destroy_server( reference )
2012-04-02 07:25:03 -04:00
true
end
def set_attribute(name, *val)
data = service.set_attribute( 'VM', reference, name, *val )
# Do not reload automatically for performance reasons
# We can set multiple attributes at the same time and
# then reload manually
#reload
end
2012-04-02 07:25:03 -04:00
def refresh
data = service.get_record( reference, 'VM' )
2012-04-02 07:25:03 -04:00
merge_attributes( data )
true
end
def vifs
__vifs.collect { |vif| service.vifs.get vif }
2012-04-02 07:25:03 -04:00
end
# associations
def networks
vifs.collect { |v| v.network }
end
def resident_on
service.hosts.get __resident_on
2012-04-02 07:25:03 -04:00
end
#
# This is not always present in XenServer VMs
# Guest needs XenTools installed to report this AFAIK
def guest_metrics
return nil unless __guest_metrics
rec = service.get_record( __guest_metrics, 'VM_guest_metrics' )
Fog::Compute::XenServer::GuestMetrics.new(rec)
end
def tools_installed?
!guest_metrics.nil?
2012-04-02 07:25:03 -04:00
end
2012-04-02 07:25:03 -04:00
def home_hypervisor
service.hosts.first
2012-04-02 07:25:03 -04:00
end
2012-04-02 07:25:03 -04:00
def mac_address
networks.first.MAC
end
2012-04-02 07:25:03 -04:00
def running?
reload
power_state == "Running"
2012-04-02 07:25:03 -04:00
end
2012-04-02 07:25:03 -04:00
def halted?
reload
power_state == "Halted"
2012-04-02 07:25:03 -04:00
end
2012-04-02 07:25:03 -04:00
# operations
def start
return false if running?
service.start_server( reference )
2012-04-02 07:25:03 -04:00
true
end
def save(params = {})
requires :name
nets = attributes[:networks] || []
if params[:auto_start].nil?
auto_start = true
else
auto_start = params[:auto_start]
end
if template_name
attr = service.get_record(
service.create_server( name, template_name, nets, :auto_start => auto_start),
'VM'
)
else
attr = service.get_record(
service.create_server_raw(attributes),
'VM'
)
end
merge_attributes attr
2012-04-02 07:25:03 -04:00
true
end
def reboot(stype = 'clean')
service.reboot_server(reference, stype)
2012-04-02 07:25:03 -04:00
true
end
def hard_reboot
reboot 'hard'
end
def clean_reboot
reboot 'clean'
end
def stop(stype = 'clean')
return false if !running?
service.shutdown_server( reference, stype )
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 provision
service.provision_server reference
end
2012-04-02 07:25:03 -04:00
# def snapshot
# requires :reference, :name_label
# data = service.snapshot_server(@reference, @name_label)
2012-04-02 07:25:03 -04:00
# merge_attributes(data.body)
# true
# end
end
end
end
end