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 =
OpenNebula::VirtualMachine::Driver::POLL_ATTRIBUTE
VM_STATE =
OpenNebula::VirtualMachine::Driver::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


1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
# File 'lib/vcenter_driver.rb', line 1448

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.



1438
1439
1440
# File 'lib/vcenter_driver.rb', line 1438

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


1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
# File 'lib/vcenter_driver.rb', line 1784

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


1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
# File 'lib/vcenter_driver.rb', line 1490

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


1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
# File 'lib/vcenter_driver.rb', line 1691

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


1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
# File 'lib/vcenter_driver.rb', line 1733

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, ops = {}) ⇒ Object

Deploys a VM

@xml_text XML representation of the VM


1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
# File 'lib/vcenter_driver.rb', line 1463

def self.deploy(xml_text, lcm_state, deploy_id, hostname, datastore = nil,
                ops = {})
    if lcm_state == "BOOT" || lcm_state == "BOOT_FAILURE"
        return clone_vm(xml_text, hostname, datastore, ops)
    else
        hid         = VIClient::translate_hostname(hostname)
        connection  = VIClient.new(hid)
        vm          = connection.find_vm_fast(deploy_id,
                                              ops[:ref],
                                              ops[:name])
        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



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

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


1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
# File 'lib/vcenter_driver.rb', line 1714

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)


1579
1580
1581
1582
1583
1584
1585
1586
# File 'lib/vcenter_driver.rb', line 1579

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


1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
# File 'lib/vcenter_driver.rb', line 1829

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)


1593
1594
1595
1596
1597
1598
1599
1600
# File 'lib/vcenter_driver.rb', line 1593

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)


1566
1567
1568
1569
1570
1571
1572
# File 'lib/vcenter_driver.rb', line 1566

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


1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
# File 'lib/vcenter_driver.rb', line 1758

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)


1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
# File 'lib/vcenter_driver.rb', line 1548

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


1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
# File 'lib/vcenter_driver.rb', line 1611

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
                counter = 60*10 # 10 minutes
                while counter > 0
                    break if vm.runtime.powerState == "poweredOff"
                    counter -= 1
                    sleep 1
                end
            rescue
            end

            if vm.runtime.powerState != "poweredOff"
                vm.PowerOffVM_Task.wait_for_completion
            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

        when "SHUTDOWN_POWEROFF", "SHUTDOWN_UNDEPLOY"
            begin
                vm.ShutdownGuest
                counter = 60*10 # 10 minutes
                while counter > 0
                    break if vm.runtime.powerState == "poweredOff"
                    counter -= 1
                    sleep 1
                end
            rescue
            end

            if vm.runtime.powerState != "poweredOff"
                vm.PowerOffVM_Task.wait_for_completion
            end
    end
end

Instance Method Details

#infoObject

Generates a OpenNebula IM Driver valid string with the monitor info



1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
# File 'lib/vcenter_driver.rb', line 1967

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



1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
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
# File 'lib/vcenter_driver.rb', line 1854

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(',')

    # Network metrics - Realtime retrieved by perfManager
    pm = @client.vim.serviceInstance.content.perfManager

    provider = pm.provider_summary [@vm].first
    refresh_rate = provider.refreshRate

    vmid = -1
    extraconfig_vmid = @vm.config.extraConfig.select{|val|
                                    val[:key]=="opennebula.vm.id"}
    if extraconfig_vmid.size > 0 and extraconfig_vmid[0]
        vmid = extraconfig_vmid[0][:value].to_i
    end

    if vmid < 0
        @nettx = 0
        @netrx = 0
        id_not_found = "Could not retrieve VM ID from extra configuration for "\
                       "vCenter's VM UUID #{@vm.config.uuid}"
    else
        one_vm = OpenNebula::VirtualMachine.new_with_id(vmid, OpenNebula::Client.new)
        one_vm.info
        stats = []

        if(one_vm["LAST_POLL"] && one_vm["LAST_POLL"].to_i != 0 )
            #Real time data stores max 1 hour. 1 minute has 3 samples
            interval = (Time.now.to_i - one_vm["LAST_POLL"].to_i)

            #If last poll was more than hour ago get 3 minutes,
            #else calculate how many samples since last poll
            samples =  interval > 3600 ? 9 : interval / refresh_rate
            max_samples = samples > 0 ? samples : 1

            stats = pm.retrieve_stats(
                [@vm],
                ['net.transmitted','net.bytesRx','net.bytesTx','net.received'],
                {interval:refresh_rate, max_samples: max_samples}
            )
        else
            # First poll, get at least latest 3 minutes = 9 samples
            stats = pm.retrieve_stats(
                [@vm],
                ['net.transmitted','net.bytesRx'],
                {interval:refresh_rate, max_samples: 9}
            )
        end

        if stats.empty? || stats.first[1][:metrics].empty?
            @nettx = 0
            @netrx = 0
        else
            metrics = stats.first[1][:metrics]

            nettx_kbpersec = 0
            metrics['net.transmitted'].each { |sample|
                nettx_kbpersec += sample
            }

            netrx_kbpersec = 0
            metrics['net.bytesRx'].each { |sample|
                netrx_kbpersec += sample
            }

            @nettx = (nettx_kbpersec * 1024 * refresh_rate).to_i
            @netrx = (netrx_kbpersec * 1024 * refresh_rate).to_i
        end
    end
end

#to_one(host) ⇒ Object

Generates an OpenNebula Template for this VCenterVm



1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
# File 'lib/vcenter_driver.rb', line 1993

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"\
          "  VCENTER_REF =\"#{@vm.ref}\",\n"\
          "  VCENTER_NAME=\"#{@vm.name}\",\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



2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
# File 'lib/vcenter_driver.rb', line 2047

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



2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
# File 'lib/vcenter_driver.rb', line 2062

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



2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
# File 'lib/vcenter_driver.rb', line 2079

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