Class: VCenterDriver::VCenterVm

Inherits:
Object
  • Object
show all
Defined in:
lib/vcenter_driver.rb

Overview

This class is a high level abstraction of a VI VirtualMachine class with OpenNebula semantics.

Constant Summary collapse

POLL_ATTRIBUTE =
VirtualMachineDriver::POLL_ATTRIBUTE
VM_STATE =
VirtualMachineDriver::VM_STATE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, vm_vi) ⇒ VCenterVm

Creates a new VIVm using a RbVmomi::VirtualMachine object

@param client [VCenterClient] client to connect to vCenter
@param vm_vi [RbVmomi::VirtualMachine] it will be used if not nil


1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
# File 'lib/vcenter_driver.rb', line 1367

def initialize(client, vm_vi )
    @vm     = vm_vi
    @client = client

    @used_cpu    = 0
    @used_memory = 0

    @netrx = 0
    @nettx = 0
end

Instance Attribute Details

#vmObject (readonly)

Returns the value of attribute vm.



1357
1358
1359
# File 'lib/vcenter_driver.rb', line 1357

def vm
  @vm
end

Class Method Details

.attach_nic(deploy_id, mac, bridge, model, host) ⇒ Object

Attach NIC to a VM

@param deploy_id vcenter identifier of the VM
@param mac MAC address of the NIC to be attached
@param bridge name of the Network in vCenter
@param model model of the NIC to be attached
@param host hostname of the ESX where the VM is running


1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
# File 'lib/vcenter_driver.rb', line 1681

def self.attach_nic(deploy_id, mac, bridge, model, host)
    hid         = VIClient::translate_hostname(host)
    connection  = VIClient.new(hid)

    vm          = connection.find_vm_template(deploy_id)

    spec_hash   = calculate_addnic_spec(vm, mac, bridge, model)

    spec        = RbVmomi::VIM.VirtualMachineConfigSpec({:deviceChange =>
                                                          [spec_hash]})

    vm.ReconfigVM_Task(:spec => spec).wait_for_completion
end

.cancel(deploy_id, hostname, lcm_state, keep_disks, disks, to_template) ⇒ Object

Cancels a VM

@param deploy_id vcenter identifier of the VM
@param hostname name of the host (equals the vCenter cluster)
@param lcm_state state of the VM
@param keep_disks keep or not VM disks in datastore
@param disks VM attached disks


1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
# File 'lib/vcenter_driver.rb', line 1406

def self.cancel(deploy_id, hostname, lcm_state, keep_disks, disks, to_template)
    case lcm_state
        when "SHUTDOWN_POWEROFF", "SHUTDOWN_UNDEPLOY"
            shutdown(deploy_id, hostname, lcm_state, keep_disks, disks, to_template)
        when "CANCEL", "LCM_INIT", "CLEANUP_RESUBMIT", "SHUTDOWN", "CLEANUP_DELETE"
            hid         = VIClient::translate_hostname(hostname)
            connection  = VIClient.new(hid)
            vm          = connection.find_vm_template(deploy_id)

            begin
                if vm.summary.runtime.powerState == "poweredOn"
                    vm.PowerOffVM_Task.wait_for_completion
                end
            rescue
            end
            if keep_disks
                detach_all_disks(vm)
            else
                detach_attached_disks(vm, disks, hostname) if disks
            end

            # If the VM was instantiated to persistent, convert the VM to 
            # vCenter VM Template and update the OpenNebula new 
            # VM Template to point to the new vCenter VM Template
            if !to_template.nil?
                vm.MarkAsTemplate

                new_template = OpenNebula::Template.new_with_id(to_template,
                                                     OpenNebula::Client.new)
                new_template.info

                public_cloud_str = "PUBLIC_CLOUD=["

                new_template.to_hash["VMTEMPLATE"]["TEMPLATE"]["PUBLIC_CLOUD"].each{|k,v|
                    if k == "VM_TEMPLATE"
                        public_cloud_str += "VM_TEMPLATE=\"#{deploy_id}\",\n"
                    else
                        public_cloud_str += "#{k}=\"#{v}\",\n"
                    end
                }

                public_cloud_str = public_cloud_str + "]"

                new_template.update(public_cloud_str, true)
            else
                vm.Destroy_Task.wait_for_completion
            end
        else
            raise "LCM_STATE #{lcm_state} not supported for cancel"
    end
end

.create_snapshot(deploy_id, hostname, snapshot_name) ⇒ Object

Create VM snapshot

@param deploy_id vcenter identifier of the VM
@param hostname name of the host (equals the vCenter cluster)
@param snaphot_name name of the snapshot


1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
# File 'lib/vcenter_driver.rb', line 1588

def self.create_snapshot(deploy_id, hostname, snapshot_name)
    hid         = VIClient::translate_hostname(hostname)
    connection  = VIClient.new(hid)

    snapshot_hash = {
        :name => snapshot_name,
        :description => "OpenNebula Snapshot of VM #{deploy_id}",
        :memory => true,
        :quiesce => true
    }

    vm          = connection.find_vm_template(deploy_id)

    vm.CreateSnapshot_Task(snapshot_hash).wait_for_completion

    return snapshot_name
end

.delete_snapshot(deploy_id, hostname, snapshot_name) ⇒ Object

Delete VM snapshot

@param deploy_id vcenter identifier of the VM
@param hostname name of the host (equals the vCenter cluster)
@param snaphot_name name of the snapshot


1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
# File 'lib/vcenter_driver.rb', line 1630

def self.delete_snapshot(deploy_id, hostname, snapshot_name)
    hid         = VIClient::translate_hostname(hostname)
    connection  = VIClient.new(hid)

    vm          = connection.find_vm_template(deploy_id)

    list = vm.snapshot.rootSnapshotList

    snapshot = find_snapshot(list, snapshot_name)
    return nil if !snapshot

    delete_snapshot_hash = {
        :_this => snapshot,
        :removeChildren => false
    }

    snapshot.RemoveSnapshot_Task(delete_snapshot_hash).wait_for_completion
end

.deploy(xml_text, lcm_state, deploy_id, hostname, datastore = nil) ⇒ Object

Deploys a VM

@xml_text XML representation of the VM


1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
# File 'lib/vcenter_driver.rb', line 1382

def self.deploy(xml_text, lcm_state, deploy_id, hostname, datastore = nil)
    if lcm_state == "BOOT" || lcm_state == "BOOT_FAILURE"
        return clone_vm(xml_text, hostname, datastore)
    else
        hid         = VIClient::translate_hostname(hostname)
        connection  = VIClient.new(hid)
        vm          = connection.find_vm_template(deploy_id)
        xml         = REXML::Document.new xml_text

        reconfigure_vm(vm, xml, false, hostname)

        vm.PowerOnVM_Task.wait_for_completion
        return vm.config.uuid
    end
end

.detach_nic(deploy_id, mac, host) ⇒ Object

Detach NIC from a VM



1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
# File 'lib/vcenter_driver.rb', line 1698

def self.detach_nic(deploy_id, mac, host)
    hid         = VIClient::translate_hostname(host)
    connection  = VIClient.new(hid)

    vm   = connection.find_vm_template(deploy_id)

    nic  = vm.config.hardware.device.find { |d|
            is_nic?(d) && (d.macAddress ==  mac)
    }

    raise "Could not find NIC with mac address #{mac}" if nic.nil?

    spec = {
        :deviceChange => [
            :operation => :remove,
            :device => nic
        ]
    }

    vm.ReconfigVM_Task(:spec => spec).wait_for_completion
end

.find_snapshot(list, snapshot_name) ⇒ Object

Find VM snapshot

@param list root list of VM snapshots
@param snaphot_name name of the snapshot


1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
# File 'lib/vcenter_driver.rb', line 1611

def self.find_snapshot(list, snapshot_name)
    list.each do |i|
        if i.name == snapshot_name
            return i.snapshot
        elsif !i.childSnapshotList.empty?
            snap = find_snapshot(i.childSnapshotList, snapshot_name)
            return snap if snap
        end
    end

    nil
end

.reboot(deploy_id, hostname) ⇒ Object

Reboots a VM

@param deploy_id vcenter identifier of the VM
@param hostname name of the host (equals the vCenter cluster)


1495
1496
1497
1498
1499
1500
1501
1502
# File 'lib/vcenter_driver.rb', line 1495

def self.reboot(deploy_id, hostname)
    hid         = VIClient::translate_hostname(hostname)
    connection  = VIClient.new(hid)

    vm          = connection.find_vm_template(deploy_id)

    vm.RebootGuest.wait_for_completion
end

.reconfigure(deploy_id, hostname, xml_text) ⇒ Object

Reconfigures a VM (context data)

@param deploy_id vcenter identifier of the VM
@param hostname name of the host (equals the vCenter cluster)
@param xml_text XML repsentation of the VM


1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
# File 'lib/vcenter_driver.rb', line 1726

def self.reconfigure(deploy_id, hostname, xml_text)
    hid         = VIClient::translate_hostname(hostname)
    connection  = VIClient.new(hid)
    vm          = connection.find_vm_template(deploy_id)

    xml = REXML::Document.new xml_text
    context = xml.root.elements["//TEMPLATE/CONTEXT"]

    if context
        context_text = create_context(context)
        context_spec = {
            :extraConfig => [
                { :key=>"guestinfo.opennebula.context",
                  :value=> Base64.encode64(context_text) }
            ]
        }

        spec      = RbVmomi::VIM.VirtualMachineConfigSpec(context_spec)
        vm.ReconfigVM_Task(:spec => spec).wait_for_completion
    end
end

.reset(deploy_id, hostname) ⇒ Object

Resets a VM

@param deploy_id vcetranslate_hostnamnter identifier of the VM
@param hostname name of the host (equals the vCenter cluster)


1509
1510
1511
1512
1513
1514
1515
1516
# File 'lib/vcenter_driver.rb', line 1509

def self.reset(deploy_id, hostname)
    hid         = VIClient::translate_hostname(hostname)
    connection  = VIClient.new(hid)

    vm          = connection.find_vm_template(deploy_id)

    vm.ResetVM_Task.wait_for_completion
end

.resume(deploy_id, hostname) ⇒ Object

Resumes a VM

@param deploy_id vcenter identifier of the VM
@param hostname name of the host (equals the vCenter cluster)


1482
1483
1484
1485
1486
1487
1488
# File 'lib/vcenter_driver.rb', line 1482

def self.resume(deploy_id, hostname)
    hid         = VIClient::translate_hostname(hostname)
    connection  = VIClient.new(hid)
    vm          = connection.find_vm_template(deploy_id)

    vm.PowerOnVM_Task.wait_for_completion
end

.revert_snapshot(deploy_id, hostname, snapshot_name) ⇒ Object

Revert VM snapshot

@param deploy_id vcenter identifier of the VM
@param hostname name of the host (equals the vCenter cluster)
@param snaphot_name name of the snapshot


1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
# File 'lib/vcenter_driver.rb', line 1655

def self.revert_snapshot(deploy_id, hostname, snapshot_name)
    hid         = VIClient::translate_hostname(hostname)
    connection  = VIClient.new(hid)

    vm          = connection.find_vm_template(deploy_id)

    list = vm.snapshot.rootSnapshotList

    snapshot = find_snapshot(list, snapshot_name)
    return nil if !snapshot

    revert_snapshot_hash = {
        :_this => snapshot
    }

    snapshot.RevertToSnapshot_Task(revert_snapshot_hash).wait_for_completion
end

.save(deploy_id, hostname, lcm_state) ⇒ Object

Saves a VM

@param deploy_id vcenter identifier of the VM
@param hostname name of the host (equals the vCenter cluster)


1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
# File 'lib/vcenter_driver.rb', line 1464

def self.save(deploy_id, hostname, lcm_state)
    case lcm_state
        when "SAVE_MIGRATE"
            raise "Migration between vCenters cluster not supported"
        when "SAVE_SUSPEND", "SAVE_STOP"
            hid         = VIClient::translate_hostname(hostname)
            connection  = VIClient.new(hid)
            vm          = connection.find_vm_template(deploy_id)

            vm.SuspendVM_Task.wait_for_completion
    end
end

.shutdown(deploy_id, hostname, lcm_state, keep_disks, disks, to_template) ⇒ Object

Shutdown a VM

@param deploy_id vcenter identifier of the VM
@param hostname name of the host (equals the vCenter cluster)
@param lcm_state state of the VM
@param keep_disks keep or not VM disks in datastore
@param disks VM attached disks
@param to_template whether this VM has been instantiated as persistent


1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
# File 'lib/vcenter_driver.rb', line 1527

def self.shutdown(deploy_id, hostname, lcm_state, keep_disks, disks, to_template)
    hid         = VIClient::translate_hostname(hostname)
    connection  = VIClient.new(hid)

    vm          = connection.find_vm_template(deploy_id)

    case lcm_state
        when "SHUTDOWN"
            begin
                vm.ShutdownGuest.wait_for_completion
            rescue
            end
            vm.PowerOffVM_Task.wait_for_completion
            if keep_disks
                detach_all_disks(vm)
            else
                detach_attached_disks(vm, disks, hostname) if disks
            end

            # If the VM was instantiated to persistent, convert the VM to 
            # vCenter VM Template and update the OpenNebula new 
            # VM Template to point to the new vCenter VM Template
            if !to_template.nil?
                vm.MarkAsTemplate

                new_template = OpenNebula::Template.new_with_id(to_template,
                                                    OpenNebula::Client.new)
                new_template.info

                public_cloud_str = "PUBLIC_CLOUD=["

                new_template.to_hash["VMTEMPLATE"]["TEMPLATE"]["PUBLIC_CLOUD"].each{|k,v|
                    if k == "VM_TEMPLATE"
                        public_cloud_str += "VM_TEMPLATE=\"#{deploy_id}\"\n"
                    else
                        public_cloud_str += "#{k}=\"#{v}\",\n"
                    end
                }

                public_cloud_str = public_cloud_str + "]"

                new_template.update(public_cloud_str, true)
            else
                vm.Destroy_Task.wait_for_completion
            end

        when "SHUTDOWN_POWEROFF", "SHUTDOWN_UNDEPLOY"
            begin
                vm.ShutdownGuest.wait_for_completion
            rescue
            end
            vm.PowerOffVM_Task.wait_for_completion
    end
end

Instance Method Details

#infoObject

Generates a OpenNebula IM Driver valid string with the monitor info



1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
# File 'lib/vcenter_driver.rb', line 1797

def info
  return 'STATE=d' if @state == 'd'

  str_info = ""

  str_info << "GUEST_IP=" << @guest_ip.to_s << " " if @guest_ip
  if @guest_ip_addresses && !@guest_ip_addresses.empty?
      str_info << "GUEST_IP_ADDRESSES=\\\"" <<
          @guest_ip_addresses.to_s << "\\\" "
  end
  str_info << "#{POLL_ATTRIBUTE[:state]}="  << @state                << " "
  str_info << "#{POLL_ATTRIBUTE[:cpu]}="    << @used_cpu.to_s        << " "
  str_info << "#{POLL_ATTRIBUTE[:memory]}=" << @used_memory.to_s     << " "
  str_info << "#{POLL_ATTRIBUTE[:netrx]}="  << @netrx.to_s           << " "
  str_info << "#{POLL_ATTRIBUTE[:nettx]}="  << @nettx.to_s           << " "
  str_info << "ESX_HOST=\\\""               << @esx_host.to_s        << "\\\" "
  str_info << "GUEST_STATE="                << @guest_state.to_s     << " "
  str_info << "VMWARETOOLS_RUNNING_STATUS=" << @vmware_tools.to_s    << " "
  str_info << "VMWARETOOLS_VERSION="        << @vmtools_ver.to_s     << " "
  str_info << "VMWARETOOLS_VERSION_STATUS=" << @vmtools_verst.to_s   << " "
  str_info << "RESOURCE_POOL=\\\""          << @vm.resourcePool.name << "\\\" "
end

#monitor(host) ⇒ Object

Initialize the vm monitor information



1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
# File 'lib/vcenter_driver.rb', line 1751

def monitor(host)
    @summary = @vm.summary
    @state   = state_to_c(@summary.runtime.powerState)

    if @state != VM_STATE[:active]
        @used_cpu    = 0
        @used_memory = 0

        @netrx = 0
        @nettx = 0

        return
    end

    @used_memory = @summary.quickStats.hostMemoryUsage * 1024
    cpuMhz       = @vm.runtime.host.summary.hardware.cpuMhz.to_f

    @used_cpu   =
            ((@summary.quickStats.overallCpuUsage.to_f / cpuMhz) * 100).to_s
    @used_cpu   = sprintf('%.2f',@used_cpu).to_s

    # Check for negative values
    @used_memory = 0 if @used_memory.to_i < 0
    @used_cpu    = 0 if @used_cpu.to_i < 0

    @esx_host       = @vm.summary.runtime.host.name
    @guest_ip       = @vm.guest.ipAddress
    @guest_state    = @vm.guest.guestState
    @vmware_tools   = @vm.guest.toolsRunningStatus
    @vmtools_ver    = @vm.guest.toolsVersion
    @vmtools_verst  = @vm.guest.toolsVersionStatus

    guest_ip_addresses = []

    @vm.guest.net.each do |net|
        net.ipConfig.ipAddress.each do |ip|
            guest_ip_addresses << ip.ipAddress
        end if net.ipConfig && net.ipConfig.ipAddress
    end if @vm.guest.net

    @guest_ip_addresses = guest_ip_addresses.join(',')
end

#to_one(host) ⇒ Object

Generates an OpenNebula Template for this VCenterVm



1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
# File 'lib/vcenter_driver.rb', line 1823

def to_one(host)
    cluster_name = host.cluster_name

    str = "NAME   = \"#{@vm.name} - #{cluster_name}\"\n"\
          "CPU    = \"#{@vm.config.hardware.numCPU}\"\n"\
          "vCPU   = \"#{@vm.config.hardware.numCPU}\"\n"\
          "MEMORY = \"#{@vm.config.hardware.memoryMB}\"\n"\
          "HYPERVISOR = \"vcenter\"\n"\
          "PUBLIC_CLOUD = [\n"\
          "  TYPE        =\"vcenter\",\n"\
          "  VM_TEMPLATE =\"#{@vm.config.uuid}\",\n"\
          "  HOST        =\"#{cluster_name}\"\n"\
          "]\n"\
          "GRAPHICS = [\n"\
          "  TYPE     =\"vnc\",\n"\
          "  LISTEN   =\"0.0.0.0\"\n"\
          "]\n"\
     "SCHED_REQUIREMENTS=\"NAME=\\\"#{cluster_name}\\\"\"\n"\
          "CONTEXT = ["\
          "  NETWORK = \"YES\","\
          "  SSH_PUBLIC_KEY = \"$USER[SSH_PUBLIC_KEY]\" ]"

    if @vm.config.annotation.nil? || @vm.config.annotation.empty?
        str << "DESCRIPTION = \"vCenter Template imported by OpenNebula"\
            " from Cluster #{@vm.runtime.host.parent.name}\"\n"
    else
        notes = @vm.config.annotation.gsub("\\", "\\\\").gsub("\"", "\\\"")
        str << "DESCRIPTION = \"#{notes}\"\n"
    end

    case @vm.guest.guestFullName
        when /CentOS/i
            str << "LOGO=images/logos/centos.png"
        when /Debian/i
            str << "LOGO=images/logos/debian.png"
        when /Red Hat/i
            str << "LOGO=images/logos/redhat.png"
        when /Ubuntu/i
            str << "LOGO=images/logos/ubuntu.png"
        when /Windows XP/i
            str << "LOGO=images/logos/windowsxp.png"
        when /Windows/i
            str << "LOGO=images/logos/windows8.png"
        when /Linux/i
            str << "LOGO=images/logos/linux.png"
    end
    return str
end

#to_one_ds(host, default_ds) ⇒ Object

Generates a Datastore user input



1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
# File 'lib/vcenter_driver.rb', line 1875

def to_one_ds(host, default_ds)
   # Datastores User Input
   str = ""

   if host.ds_list != ""
       str    =  "M|list|Which datastore you want this VM to run on?|"\
              << "#{host.ds_list}|#{default_ds}"
   end

   return str
end

#to_one_rp(host) ⇒ Object

Generates a Resource Pool user input



1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
# File 'lib/vcenter_driver.rb', line 1890

def to_one_rp(host)
    # Resource Pool User Input
    str = ""

    if host.rp_list != ""
        str    =  "M|list|Which resource pool you want this VM to run"\
                  " in?|#{host.rp_list}|#{host.rp_list.split(",")[0]}"
    end

    return str
end

#vm_to_one(host) ⇒ Object

Generates an OpenNebula VirtualMachine for this VCenterVm



1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
# File 'lib/vcenter_driver.rb', line 1907

def vm_to_one(host)
    cluster_name = host.cluster_name

    state = case state_to_c(@summary.runtime.powerState)
                when 'a'
                    "RUNNING"
                when 'd'
                    "POWEROFF"
            end

    str = "NAME   = \"#{@vm.name} - #{cluster_name}\"\n"\
          "CPU    = \"#{@vm.config.hardware.numCPU}\"\n"\
          "vCPU   = \"#{@vm.config.hardware.numCPU}\"\n"\
          "MEMORY = \"#{@vm.config.hardware.memoryMB}\"\n"\
          "HYPERVISOR = \"vcenter\"\n"\
          "PUBLIC_CLOUD = [\n"\
          "  TYPE        =\"vcenter\",\n"\
          "  VM_TEMPLATE =\"#{@vm.config.uuid}\",\n"\
          "  HOST        =\"#{cluster_name}\"\n"\
          "]\n"\
          "IMPORT_VM_ID    = \"#{@vm.config.uuid}\"\n"\
          "IMPORT_STATE   = \"#{state}\"\n"\
          "SCHED_REQUIREMENTS=\"NAME=\\\"#{cluster_name}\\\"\"\n"

    vp     = @vm.config.extraConfig.select{|v|
                                       v[:key].downcase=="remotedisplay.vnc.port"}
    keymap = @vm.config.extraConfig.select{|v|
                                       v[:key].downcase=="remotedisplay.vnc.keymap"}

    if vp.size > 0
        str << "GRAPHICS = [\n"\
               "  TYPE     =\"vnc\",\n"\
               "  LISTEN   =\"0.0.0.0\",\n"\
               "  PORT     =\"#{vp[0][:value]}\"\n"
        str << " ,KEYMAP   =\"#{keymap[0][:value]}\"\n" if keymap[0]
        str << "]\n"
    end

    if @vm.config.annotation.nil? || @vm.config.annotation.empty?
        str << "DESCRIPTION = \"vCenter Virtual Machine imported by"\
            " OpenNebula from Cluster #{cluster_name}\"\n"
    else
        notes = @vm.config.annotation.gsub("\\", "\\\\").gsub("\"", "\\\"")
        str << "DESCRIPTION = \"#{notes}\"\n"
    end

    case @vm.guest.guestFullName
        when /CentOS/i
            str << "LOGO=images/logos/centos.png"
        when /Debian/i
            str << "LOGO=images/logos/debian.png"
        when /Red Hat/i
            str << "LOGO=images/logos/redhat.png"
        when /Ubuntu/i
            str << "LOGO=images/logos/ubuntu.png"
        when /Windows XP/i
            str << "LOGO=images/logos/windowsxp.png"
        when /Windows/i
            str << "LOGO=images/logos/windows8.png"
        when /Linux/i
            str << "LOGO=images/logos/linux.png"
    end

    return str
end