Class: Fog::Compute::XenServer::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/xenserver/compute.rb,
lib/fog/xenserver/requests/compute/scan_sr.rb,
lib/fog/xenserver/requests/compute/start_vm.rb,
lib/fog/xenserver/requests/compute/eject_vbd.rb,
lib/fog/xenserver/requests/compute/create_vbd.rb,
lib/fog/xenserver/requests/compute/create_vdi.rb,
lib/fog/xenserver/requests/compute/create_vif.rb,
lib/fog/xenserver/requests/compute/get_record.rb,
lib/fog/xenserver/requests/compute/insert_vbd.rb,
lib/fog/xenserver/requests/compute/unplug_vbd.rb,
lib/fog/xenserver/requests/compute/destroy_vdi.rb,
lib/fog/xenserver/requests/compute/destroy_vif.rb,
lib/fog/xenserver/requests/compute/get_records.rb,
lib/fog/xenserver/requests/compute/clone_server.rb,
lib/fog/xenserver/requests/compute/set_affinity.rb,
lib/fog/xenserver/requests/compute/start_server.rb,
lib/fog/xenserver/requests/compute/create_server.rb,
lib/fog/xenserver/requests/compute/reboot_server.rb,
lib/fog/xenserver/requests/compute/set_attribute.rb,
lib/fog/xenserver/requests/compute/destroy_server.rb,
lib/fog/xenserver/requests/compute/shutdown_server.rb,
lib/fog/xenserver/requests/compute/provision_server.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



65
66
67
68
69
70
71
72
# File 'lib/fog/xenserver/compute.rb', line 65

def initialize(options={})
  @host        = options[:xenserver_url]
  @username    = options[:xenserver_username]
  @password    = options[:xenserver_password]
  @defaults    = options[:xenserver_defaults] || {}
  @connection  = Fog::XenServer::Connection.new(@host)
  @connection.authenticate(@username, @password)
end

Instance Method Details

#clone_server(server_name, template_ref) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fog/xenserver/requests/compute/clone_server.rb', line 6

def clone_server( server_name, template_ref )
  # Clone the VM template
  if template_ref.kind_of? Fog::Compute::XenServer::Server
    template_ref = template_ref.reference
  end
  raise ArgumentError.new("Invalid template_ref") if template_ref.nil?
  raise ArgumentError.new("Invalid template_ref") if server_name.nil?

  ref = @connection.request(
    {:parser => Fog::Parsers::XenServer::Base.new, :method => 'VM.clone'},
    template_ref, server_name 
  )
end

#create_server(name_label, template = nil, networks = [], extra_args = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fog/xenserver/requests/compute/create_server.rb', line 74

def create_server( name_label, template = nil, networks = [], extra_args = {})
  if !networks.kind_of? Array
    raise "Invalid networks argument"
  end

  if template.kind_of? String
    template_string = template
    # try template by UUID 
    template = servers.templates.find { |s| s.uuid == template_string }
    if template.nil?
      # Try with the template name just in case
      template = servers.get get_vm_by_name(template_string)
    end
  end

  if template.nil?
    raise "Invalid template"
  end

  raise "Template #{template_string} does not exist" if template.allowed_operations.nil?
  raise 'Clone Operation not Allowed' unless template.allowed_operations.include?('clone')

  # Clone the VM template
  ref = clone_server name_label, template.reference
  # Add additional NICs
  networks.each do |n|
    create_vif ref, n.reference
  end
  if !extra_args[:auto_start] == false
    @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VM.provision'}, ref)
    start_vm( ref ) 
  end
  
  ref
end

#create_server_raw(config = {}) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fog/xenserver/requests/compute/create_server.rb', line 10

def create_server_raw(config = {})
  config[:name_label] = config[:name] if config[:name]
  config.delete :name
  config[:affinity] = config[:__affinity] if config[:__affinity]
  config.delete :__affinity
  raise ArgumentError.new("Invalid :name_label attribute") \
    if !config[:name_label]
  raise ArgumentError.new("Invalid :affinity attribute") \
    if not config[:affinity]
  config[:affinity] = config[:affinity].reference \
    if config[:affinity].kind_of? Fog::Compute::XenServer::Host
  config.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
  %w{ VCPUs_at_startup 
      VCPUs_max 
      VCPUs_params
      PV_bootloader_args 
      PV_bootloader
      PV_kernel
      PV_ramdisk
      PV_legacy_args
      HVM_boot_params
      HVM_boot_params
  }.each do |k|
    if config[k.downcase.to_sym]
      config[k.to_sym] = config[k.downcase.to_sym]
      config.delete k.downcase.to_sym
    end
  end
  vm_record = {
    :name_label =>              '',
    :name_description =>        'description',
    :user_version =>            '0',
    :affinity =>                '',
    :is_a_template =>           true,
    :auto_power_on =>           false,
    :memory_static_max =>       '536870912',
    :memory_static_min =>       '536870912',
    :memory_dynamic_max =>      '536870912',
    :memory_dynamic_min =>      '536870912',
    :VCPUs_params =>            {},
    :VCPUs_max =>               '1',
    :VCPUs_at_startup =>        '1',
    :actions_after_shutdown =>  'Destroy',
    :actions_after_reboot =>    'Restart',
    :actions_after_crash =>     'Restart',
    :platform =>                { 'nx' => false, 'acpi' => true, 'apic' => 'true', 'pae' => true, 'viridian' => true},
    :platform =>                {},
    :other_config =>            {},
    :pool_name =>               '',
    :PV_bootloader =>           'pygrub', #pvgrub, eliloader
    :PV_kernel =>                '',
    :PV_ramdisk =>              '',
    :PV_args =>                 '-- quiet console=hvc0',
    :PV_bootloader_args =>      '',
    :PV_legacy_args =>          '',
    :HVM_boot_policy =>         '', 
    :HVM_boot_params =>         {},
    :PCI_bus =>                 '',
    :recommendations =>         '',
  }.merge config
  ref = @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VM.create' }, vm_record)
  ref
end

#create_vbd(vm_ref, vdi_ref, config = {}) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/xenserver/requests/compute/create_vbd.rb', line 7

def create_vbd( vm_ref, vdi_ref, config = {} )
  raise ArgumentError.new('Invalid config') if config.nil?
  vm_ref = vm_ref.reference if vm_ref.kind_of? Fog::Compute::XenServer::Server
  vdi_ref = vdi_ref.reference if vdi_ref.kind_of? Fog::Compute::XenServer::VDI
  config.reject! { |k,v| (k == :server) or (k == :vdi) }
  default_config = {
    :VM => vm_ref,
    :VDI => vdi_ref,
    :empty => false,
    :other_config => {'owner' => ''},
    :userdevice => "0",
    :bootable => true,
    :mode => 'RW',
    :qos_algorithm_type=> '',
    :qos_algorithm_params=> {},
    :qos_supported_algorithms=> [],
    :type => 'Disk'
  }.merge config

  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VBD.create'}, default_config )
end

#create_vdi(config) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fog/xenserver/requests/compute/create_vdi.rb', line 7

def create_vdi( config )
  raise ArgumentError.new('Invalid config') if config.nil?
  raise ArgumentError.new('Missing virtual_size attribute') if config[:virtual_size].nil?
  raise ArgumentError.new('Missing read_only attribute') if config[:read_only].nil?
  raise ArgumentError.new('Missing type attribute') if config[:type].nil?
  raise ArgumentError.new('Missing sharable attribute') if config[:sharable].nil?
  raise ArgumentError.new('Missing other_config attribute') if config[:other_config].nil?
  raise ArgumentError.new('Missing storage_repository attribute') if config[:storage_repository].nil?
  config[:SR] = config[:storage_repository].reference
  config[:name_label] = config[:name]
  config[:name_description] = config[:description] if config[:description]
  config.reject! { |k,v| (k == :__sr) or (k == :storage_repository) }
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VDI.create'}, config )
end

#create_vif(vm_ref, network_ref, device = -1)) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
# File 'lib/fog/xenserver/requests/compute/create_vif.rb', line 7

def create_vif( vm_ref, network_ref, device = -1)
  raise ArgumentError.new('Invalid vm_ref') if vm_ref.nil?
  raise ArgumentError.new('Invalid network_ref') if network_ref.nil?
  vm_ref = vm_ref.reference if vm_ref.kind_of? Fog::Model
  network_ref = network_ref.reference if network_ref.kind_of? Fog::Model
  vif_config = default_vif_config(vm_ref, network_ref, device.to_s)
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VIF.create'}, vif_config )
end

#create_vif_custom(conf) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
# File 'lib/fog/xenserver/requests/compute/create_vif.rb', line 16

def create_vif_custom( conf )
  raise ArgumentError.new('VIF config is not a Hash') if not conf.kind_of?(Hash)
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VIF.create'}, conf )
end

#default_networkObject



89
90
91
# File 'lib/fog/xenserver/compute.rb', line 89

def default_network
  networks.find { |n| n.name == (@defaults[:network] || "Pool-wide network associated with eth0") }
end

#default_templateObject



82
83
84
85
86
87
# File 'lib/fog/xenserver/compute.rb', line 82

def default_template
  return nil if @defaults[:template].nil?
  (servers.custom_templates + servers.builtin_templates).find do |s|
    (s.name == @defaults[:template]) or (s.uuid == @defaults[:template])
  end
end

#default_template=(name) ⇒ Object



78
79
80
# File 'lib/fog/xenserver/compute.rb', line 78

def default_template=(name)
  @defaults[:template] = name
end

#default_vif_config(vm_ref, network_ref, device_number = '-1') ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/xenserver/requests/compute/create_vif.rb', line 21

def default_vif_config( vm_ref, network_ref, device_number = '-1' )
  conf = {
    'MAC_autogenerated' => 'True',
    'VM' => vm_ref,
    'network' => network_ref,
    'MAC' => '',
    'MTU' => '0',
    'other_config' => {},
    'qos_algorithm_type' => 'ratelimit',
    'qos_algorithm_params' => {}
  }
  if device_number.to_i >= 0
    conf['device'] = device_number
  else
    highest_dev = 0
    server = servers.get vm_ref
    server.vifs.each do |vif|
      dev = vif.device.to_i
      highest_dev = dev if dev > highest_dev
    end
    conf['device'] = (highest_dev + 1).to_s
  end
  conf
end

#destroy_server(vm_ref, extra_args = {}) ⇒ Object



7
8
9
# File 'lib/fog/xenserver/requests/compute/destroy_server.rb', line 7

def destroy_server( vm_ref , extra_args = {})
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VM.destroy'}, vm_ref)
end

#destroy_vdi(vdi_ref, extra_args = {}) ⇒ Object



7
8
9
# File 'lib/fog/xenserver/requests/compute/destroy_vdi.rb', line 7

def destroy_vdi( vdi_ref, extra_args = {})
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VDI.destroy'}, vdi_ref)
end

#destroy_vif(ref, extra_args = {}) ⇒ Object



7
8
9
# File 'lib/fog/xenserver/requests/compute/destroy_vif.rb', line 7

def destroy_vif( ref, extra_args = {})
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VIF.destroy'}, ref)
end

#eject_vbd(ref, extra_args = {}) ⇒ Object



7
8
9
# File 'lib/fog/xenserver/requests/compute/eject_vbd.rb', line 7

def eject_vbd(ref, extra_args = {})
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VBD.eject'}, ref)
end

#get_record(ref, klass, options = {}) ⇒ Object



9
10
11
# File 'lib/fog/xenserver/requests/compute/get_record.rb', line 9

def get_record( ref, klass, options = {} )
  get_record_by_ref ref, klass, options
end

#get_record_by_ref(ref, klass, options = {}) ⇒ Object



13
14
15
# File 'lib/fog/xenserver/requests/compute/get_record.rb', line 13

def get_record_by_ref( ref, klass, options = {} )
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => "#{klass}.get_record"}, ref).merge(:reference => ref)
end

#get_records(klass, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/fog/xenserver/requests/compute/get_records.rb', line 9

def get_records( klass, options = {} )
  begin
    res = @connection.request(:parser => Fog::Parsers::XenServer::GetRecords.new, :method => "#{klass}.get_all_records")
    res
  rescue Fog::XenServer::RequestFailed => e
    []
  end
end

#get_vm_by_name(label) ⇒ Object



6
7
8
# File 'lib/fog/xenserver/requests/compute/create_server.rb', line 6

def get_vm_by_name(label)
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VM.get_by_name_label' }, label)
end

#insert_vbd(ref, vdi_ref, extra_args = {}) ⇒ Object



7
8
9
# File 'lib/fog/xenserver/requests/compute/insert_vbd.rb', line 7

def insert_vbd(ref, vdi_ref, extra_args = {})
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VBD.insert'}, ref, vdi_ref)
end

#provision_server(ref) ⇒ Object



6
7
8
# File 'lib/fog/xenserver/requests/compute/provision_server.rb', line 6

def provision_server( ref )
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VM.provision'}, ref)
end

#reboot_server(ref, stype = 'clean') ⇒ Object



7
8
9
# File 'lib/fog/xenserver/requests/compute/reboot_server.rb', line 7

def reboot_server( ref, stype = 'clean' )
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => "VM.#{stype}_shutdown"}, ref)
end

#reloadObject



74
75
76
# File 'lib/fog/xenserver/compute.rb', line 74

def reload
  @connection.authenticate(@username, @password)
end

#scan_sr(ref, extra_args = {}) ⇒ Object



7
8
9
# File 'lib/fog/xenserver/requests/compute/scan_sr.rb', line 7

def scan_sr( ref, extra_args = {})
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'SR.scan'}, ref)
end

#set_affinity(host_ref) ⇒ Object



9
10
11
# File 'lib/fog/xenserver/requests/compute/set_affinity.rb', line 9

def set_affinity( host_ref )
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VM.set_affinity'}, host_ref)
end

#set_attribute(klass, ref, attr_name, *value) ⇒ Object



9
10
11
# File 'lib/fog/xenserver/requests/compute/set_attribute.rb', line 9

def set_attribute( klass, ref, attr_name, *value )
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => "#{klass}.set_#{attr_name.gsub('-','_')}"}, ref, *value)
end

#shutdown_server(vm_ref, stype = 'clean') ⇒ Object



7
8
9
# File 'lib/fog/xenserver/requests/compute/shutdown_server.rb', line 7

def shutdown_server( vm_ref, stype = 'clean' )
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => "VM.#{stype}_shutdown"}, vm_ref)
end

#start_server(vm_ref) ⇒ Object



7
8
9
# File 'lib/fog/xenserver/requests/compute/start_server.rb', line 7

def start_server( vm_ref )
  start_vm( vm_ref )
end

#start_vm(vm_ref) ⇒ Object

bit.ly/8ZPyCN VM.start( session, VM_ref, start_paused, force)



9
10
11
# File 'lib/fog/xenserver/requests/compute/start_vm.rb', line 9

def start_vm( vm_ref )
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VM.start'}, vm_ref, false, false)
end

#unplug_force_vbd(ref, extra_args = {}) ⇒ Object



11
12
13
# File 'lib/fog/xenserver/requests/compute/unplug_vbd.rb', line 11

def unplug_force_vbd(ref, extra_args = {})
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VBD.unplug_force'}, ref)
end

#unplug_vbd(vbd_ref, extra_args = {}) ⇒ Object



7
8
9
# File 'lib/fog/xenserver/requests/compute/unplug_vbd.rb', line 7

def unplug_vbd( vbd_ref, extra_args = {})
  @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VBD.unplug'}, vbd_ref)
end