Class: VCenterDriver::Template
- Inherits:
-
Object
- Object
- VCenterDriver::Template
- Includes:
- Memoize
- Defined in:
- lib/vm_template.rb
Overview
Class Template
Direct Known Subclasses
Instance Attribute Summary collapse
-
#item ⇒ Object
Returns the value of attribute item.
Class Method Summary collapse
- .get_xml_template(template, vcenter_uuid, vi_client, dc_name = nil, rp_cache = {}) ⇒ Object
-
.new_from_ref(ref, vi_client) ⇒ Object
TODO: check with uuid.
- .template_to_one(template, vc_uuid, ccr_ref, ccr_name, import_name) ⇒ Object
Instance Method Summary collapse
- #cdrom?(device) ⇒ Boolean
-
#create_ar(nic, with_id = false, ipv4 = nil, ipv6 = nil) ⇒ Object
Create AR * in case of IPv6 we use a standard PREFIX_LENGTH = 64 * if we pass ipv4 we force nic use that IPv4 * if we pass ipv6 we force nic use that IPv6.
-
#create_delta_disks ⇒ Object
Linked Clone over existing template.
-
#create_network_for_import(opts) ⇒ Object
Creates an OpenNebula Virtual Network as part of the VM Template import process.
- #create_template_copy(template_name) ⇒ Object
- #datacenter ⇒ Object
- #delete_template ⇒ Object
-
#disk?(device) ⇒ Boolean
Checks if a RbVmomi::VIM::VirtualDevice is a disk.
-
#disk_or_cdrom?(device) ⇒ Boolean
Checks if a RbVmomi::VIM::VirtualDevice is a disk or a cdrom.
-
#disk_or_iso?(device) ⇒ Boolean
Checks if a RbVmomi::VIM::VirtualDevice is a disk or an iso file.
- #esx_name ⇒ Object
- #fill_nic(ip_addresses, nic) ⇒ Object
- #find_alias_ips_in_network(network, vm_object, alias_ipv4 = nil, alias_ipv6 = nil) ⇒ Object
- #find_ip_in_ar(ip, ar_array) ⇒ Object
- #find_ips_in_network(network, vm_object, nic, force = false, first_ip = false) ⇒ Object
- #get_ipv6_prefix(ipv6, prefix_length) ⇒ Object
- #get_vcenter_disk_key(unit_number, controller_key) ⇒ Object
- #identify_network(identifier, network) ⇒ Object
-
#import_vcenter_disks(vc_uuid, dpool, ipool, type) ⇒ Object
Import vcenter disks identifier(:id).
- #import_vcenter_nics(opts, vm_id = nil, dc_name = nil) ⇒ Object
-
#initialize(item = nil, vi_client = nil) ⇒ Template
constructor
A new instance of Template.
-
#lock ⇒ Object
Locking function.
- #nic_alias_from_nic(id, nic, nic_index, network_found, vm_object) ⇒ Object
- #nic_from_network_created(one_vn, nic, nic_index, vm_object, _ar_ids) ⇒ Object
- #nic_from_network_found(network_found, vm_object, nic, _ar_ids, nic_index) ⇒ Object
- #online? ⇒ Boolean
-
#resource_pool ⇒ Object
RbVmomi::VIM::ResourcePool, first resource pool in cluster.
- #retrieve_from_device(device) ⇒ Object
- #save_ar_ids(network_found, nic, ar_ids) ⇒ Object
- #save_as_linked_clones(name) ⇒ Object
-
#unlock ⇒ Object
Unlock driver execution mutex.
- #vcenter_disks_get ⇒ Object
- #vcenter_instance_uuid ⇒ Object
- #vcenter_nics_hash ⇒ Object
- #vcenter_nics_list ⇒ Object
- #vm? ⇒ Boolean
-
#vm_template_ds_ref ⇒ Object
Gets MOREF from Datastore used by the VM.
- #vm_to_one(vm_name) ⇒ Object
Methods included from Memoize
#[], #[]=, #clear, #clear_all
Constructor Details
#initialize(item = nil, vi_client = nil) ⇒ Template
Returns a new instance of Template.
32 33 34 35 36 37 |
# File 'lib/vm_template.rb', line 32 def initialize(item = nil, vi_client = nil) @item = item check_item(@item, nil) if @item @vi_client = vi_client @locking = true end |
Instance Attribute Details
#item ⇒ Object
Returns the value of attribute item.
28 29 30 |
# File 'lib/vm_template.rb', line 28 def item @item end |
Class Method Details
.get_xml_template(template, vcenter_uuid, vi_client, dc_name = nil, rp_cache = {}) ⇒ Object
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 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 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 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 |
# File 'lib/vm_template.rb', line 1760 def self.get_xml_template( template, vcenter_uuid, vi_client, dc_name = nil, rp_cache = {} ) begin template_ref = template['_ref'] template_name = template['name'] template_ccr = template['runtime.host.parent'] template_ccr_ref = template_ccr._ref template_ccr_name = template_ccr.name # Get datacenter info if !dc_name dc = datacenter dc_name = dc.item.name end # Get resource pools and generate a list if !rp_cache[template_ccr_name] tmp_cluster = VCenterDriver::ClusterComputeResource .new_from_ref( template_ccr_ref, vi_client ) rp_list = tmp_cluster.get_resource_pool_list rp = '' if !rp_list.empty? rp_name_list = [] rp_list.each do |rp_hash| rp_name_list << rp_hash[:name] end rp = 'O|list|Which resource pool \ you want this VM to run in? ' rp << "|#{rp_name_list.join(',')}" # List of RP rp << "|#{rp_name_list.first}" # Default RP end rp_cache[template_ccr_name] = {} rp_cache[template_ccr_name][:rp] = rp rp_cache[template_ccr_name][:rp_list] = rp_list end rp = rp_cache[template_ccr_name][:rp] rp_list = rp_cache[template_ccr_name][:rp_list] # Determine the location path for the template vcenter_template = VCenterDriver::VirtualMachine .new_without_id( vi_client, template_ref ) item = vcenter_template.item folders = [] until item.instance_of? RbVmomi::VIM::Datacenter item = item.parent first_condition = !(item.instance_of? RbVmomi::VIM::Datacenter) second_condition = item.name != 'vm' if first_condition && second_condition folders << item.name end if item.nil? raise 'Could not find the templates parent location' end end location = folders.reverse.join('/') location = '/' if location.empty? # Generate a crypto hash for the template # name and take the first 12 chars import_name = VCenterDriver::VIHelper .one_name( OpenNebula::TemplatePool, template_name, template_ref+vcenter_uuid ) template_name = template_name.tr("\u007F", '') template_ccr_name = template_ccr_name.tr("\u007F", '') # Prepare the Hash that will be used by importers to display # the object being imported one_tmp = {} one_tmp[:name] = import_name one_tmp[:ref] = template_ref one_tmp[:dc_name] = dc_name one_tmp[:template_name] = template_name one_tmp[:sunstone_template_name]= "#{template_name} [ Cluster: #{template_ccr_name}" \ " - Template location: #{location} ]" one_tmp[:template_location] = location one_tmp[:vcenter_ccr_ref] = template_ccr_ref one_tmp[:vcenter_ref] = template_ref one_tmp[:vcenter_instance_uuid] = vcenter_uuid one_tmp[:cluster_name] = template_ccr_name one_tmp[:rp] = rp one_tmp[:rp_list] = rp_list one_tmp[:template] = template # By default we import disks and nics one_tmp[:import_disks_and_nics] = true # Get the host ID of the OpenNebula host # which represents the vCenter Cluster one_host = VCenterDriver::VIHelper .find_by_ref( OpenNebula::HostPool, 'TEMPLATE/VCENTER_CCR_REF', template_ccr_ref, vcenter_uuid ) host_id = one_host['ID'] unless host_id raise "Could not find the host's ID associated \ to template being imported" end # Get the OpenNebula's template hash one_tmp[:one] = template_to_one( template, vcenter_uuid, template_ccr_ref, template_ccr_name, import_name ) one_tmp rescue StandardError nil end end |
.new_from_ref(ref, vi_client) ⇒ Object
TODO: check with uuid
1899 1900 1901 |
# File 'lib/vm_template.rb', line 1899 def self.new_from_ref(ref, vi_client) new(RbVmomi::VIM::VirtualMachine.new(vi_client.vim, ref), vi_client) end |
.template_to_one(template, vc_uuid, ccr_ref, ccr_name, import_name) ⇒ Object
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 |
# File 'lib/vm_template.rb', line 1696 def self.template_to_one( template, vc_uuid, ccr_ref, ccr_name, import_name ) num_cpu, memory, annotation, guest_fullname = template .item .collect( 'config.hardware.numCPU', 'config.hardware.memoryMB', 'config.annotation', 'guest.guestFullName' ) str = "NAME = \"#{import_name}\"\n"\ "CPU = \"#{num_cpu}\"\n"\ "vCPU = \"#{num_cpu}\"\n"\ "MEMORY = \"#{memory}\"\n"\ "HYPERVISOR = \"vcenter\"\n"\ "CONTEXT = [\n"\ " NETWORK = \"YES\",\n"\ " SSH_PUBLIC_KEY = \"$USER[SSH_PUBLIC_KEY]\"\n"\ "]\n"\ "VCENTER_INSTANCE_ID =\"#{vc_uuid}\"\n" str << "VCENTER_TEMPLATE_REF =\"#{template['_ref']}\"\n" str << "VCENTER_CCR_REF =\"#{ccr_ref}\"\n" str << "GRAPHICS = [\n"\ " TYPE =\"vnc\",\n" str << " LISTEN =\"0.0.0.0\"\n" str << "]\n" if annotation.nil? || annotation.empty? str << 'DESCRIPTION = "vCenter Template imported by OpenNebula'\ " from Cluster #{ccr_name}\"\n" else notes = annotation.gsub('\\', '\\\\').gsub('"', '\\"') str << "DESCRIPTION = \"#{notes}\"\n" end case guest_fullname when /CentOS/i str << "LOGO=images/logos/centos.png\n" when /Debian/i str << "LOGO=images/logos/debian.png\n" when /Red Hat/i str << "LOGO=images/logos/redhat.png\n" when /Ubuntu/i str << "LOGO=images/logos/ubuntu.png\n" when /Windows XP/i str << "LOGO=images/logos/windowsxp.png\n" when /Windows/i str << "LOGO=images/logos/windows8.png\n" when /Linux/i str << "LOGO=images/logos/linux.png\n" end str end |
Instance Method Details
#cdrom?(device) ⇒ Boolean
1569 1570 1571 |
# File 'lib/vm_template.rb', line 1569 def cdrom?(device) device.backing.is_a? RbVmomi::VIM::VirtualCdromIsoBackingInfo end |
#create_ar(nic, with_id = false, ipv4 = nil, ipv6 = nil) ⇒ Object
Create AR
-
in case of IPv6 we use a standard PREFIX_LENGTH = 64
-
if we pass ipv4 we force nic use that IPv4
-
if we pass ipv6 we force nic use that IPv6
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'lib/vm_template.rb', line 521 def create_ar(nic, with_id = false, ipv4 = nil, ipv6 = nil) ar_tmp = "AR=[\n" # if ipv4 and ipv6 are defined create a IPv4 address with a static # IPv6 address if ipv4 && ipv6 ar_tmp << "TYPE=\"IP4_6_STATIC\",\n" ar_tmp << "IP=\"#{ipv4}\",\n" ar_tmp << "IP6=\"#{ipv6}\",\n" ar_tmp << "PREFIX_LENGTH=\"64\",\n" # if just ipv4 is defined create a AR with just a IPv4 address elsif ipv4 ar_tmp << "TYPE=\"IP4\",\n" ar_tmp << "IP=\"#{ipv4}\",\n" # if just ipv6 is defined create a AR with just a IPv4 address elsif ipv6 ar_tmp << "TYPE=\"IP6_STATIC\",\n" ar_tmp << "IP6=\"#{ipv6}\",\n" ar_tmp << "PREFIX_LENGTH=\"64\",\n" # in case nic have defined mac, ipv4 and ipv6 create a AR with # this configuration elsif nic[:mac] && nic[:ipv4] && nic[:ipv6] ar_tmp << "AR_ID=0,\n" if with_id ar_tmp << "TYPE=\"IP4_6_STATIC\",\n" ar_tmp << "IP=\"#{nic[:ipv4]}\",\n" ar_tmp << "MAC=\"#{nic[:mac]}\",\n" ar_tmp << "IP6=\"#{nic[:ipv6]}\",\n" ar_tmp << "PREFIX_LENGTH=\"64\",\n" # in case nic have defined mac and ipv6 create a AR with # this configuration elsif nic[:mac] && nic[:ipv6] ar_tmp << "AR_ID=0,\n" if with_id ar_tmp << "TYPE=\"IP6_STATIC\",\n" ar_tmp << "MAC=\"#{nic[:mac]}\",\n" ar_tmp << "IP6=\"#{nic[:ipv6]}\",\n" ar_tmp << "PREFIX_LENGTH=\"64\",\n" # in case nic have defined mac and ipv4 create a AR with # this configuration elsif nic[:mac] && nic[:ipv4] ar_tmp << "AR_ID=0,\n" if with_id ar_tmp << "TYPE=\"IP4\",\n" ar_tmp << "IP=\"#{nic[:ipv4]}\",\n" ar_tmp << "MAC=\"#{nic[:mac]}\",\n" # in case nic not have any default configuration create ETHER else ar_tmp << "AR_ID=0,\n" if with_id ar_tmp << "TYPE=\"ETHER\",\n" ar_tmp << "MAC=\"#{nic[:mac]}\",\n" end ar_tmp << "SIZE=\"1\"\n" ar_tmp << "]\n" ar_tmp end |
#create_delta_disks ⇒ Object
Linked Clone over existing template
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/vm_template.rb', line 256 def create_delta_disks begin disks = @item['config.hardware.device'] .grep(RbVmomi::VIM::VirtualDisk) disk_without_snapshots = disks.select do |x| x.backing.parent.nil? end rescue StandardError error = 'Cannot extract existing disks on template.' use_linked_clones = false return error, use_linked_clones end if !disk_without_snapshots.empty? begin if self['config.template'] @item.MarkAsVirtualMachine( :pool => resource_pool, :host => self['runtime.host'] ) end rescue StandardError => e @item.MarkAsTemplate() error = 'Cannot mark the template as a VirtualMachine. '\ 'Not using linked clones. '\ "Reason: #{e.}/#{e.backtrace}" use_linked_clones = false return error, use_linked_clones end begin spec = {} spec[:deviceChange] = [] disk_without_snapshots.each do |disk| remove_disk_spec = { :operation => :remove, :device => disk } spec[:deviceChange] << remove_disk_spec add_disk_spec = { :operation => :add, :fileOperation => :create, :device => disk.dup.tap do |x| x.backing = x.backing.dup x.backing.fileName = "[#{disk.backing.datastore.name}]" x.backing.parent = disk.backing end } spec[:deviceChange] << add_disk_spec end @item .ReconfigVM_Task( :spec => spec ).wait_for_completion unless spec[:deviceChange].empty? rescue StandardError => e error = 'Cannot create the delta disks on top '\ "of the template. Reason: #{e.}." if VCenterDriver::CONFIG[:debug_information] error += "\n\n#{e.backtrace}" end use_linked_clones = false return error, use_linked_clones end begin @item.MarkAsTemplate() rescue StandardError => e error = 'Cannot mark the VirtualMachine as '\ 'a template. Not using linked clones.' \ " Reason: #{e.}." if VCenterDriver::CONFIG[:debug_information] error += "\n\n#{e.backtrace}" end use_linked_clones = false return error, use_linked_clones end end error = nil use_linked_clones = true [error, use_linked_clones] end |
#create_network_for_import(opts) ⇒ Object
Creates an OpenNebula Virtual Network as part of the VM Template import process. This only need to happen if no VNET in OpenNebula is present that refers to the network where the NIC of the VM Template is hooked to.
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 |
# File 'lib/vm_template.rb', line 907 def create_network_for_import( opts ) nic = opts[:nic] ccr_ref = opts[:ccr_ref] ccr_name = opts[:ccr_name] vc_uuid = opts[:vc_uuid] vcenter_instance_name = opts[:vcenter_instance_name] dc_name = opts[:dc_name] template_ref = opts[:template_ref] dc_ref = opts[:dc_ref] vm_id = opts[:vm_id] hpool = opts[:hpool] vi_client = opts[:vi_client] config = {} config[:refs] = nic[:refs] # Let's get the OpenNebula hosts ids # associated to the clusters references config[:one_ids] = nic[:refs].map do |ref| VCenterDriver::VIHelper .find_by_ref( OpenNebula::HostPool, 'TEMPLATE/VCENTER_CCR_REF', ref, vc_uuid, hpool )['CLUSTER_ID'] rescue -1 end if vm? unmanaged = 'wild' else unmanaged = 'template' end net = VCenterDriver::Network .new_from_ref( nic[:net_ref], vi_client ) if net vid = VCenterDriver::Network.retrieve_vlanid(net.item) end case nic[:pg_type] # Distributed PortGroups when VCenterDriver::Network::NETWORK_TYPE_DPG config[:sw_name] = nic[:network] .config .distributedVirtualSwitch .name # For DistributedVirtualPortgroups # there is networks and uplinks config[:uplink] = false # NSX-V PortGroups when VCenterDriver::Network::NETWORK_TYPE_NSXV config[:sw_name] = nic[:network] .config .distributedVirtualSwitch .name # For NSX-V ( is the same as # DistributedVirtualPortgroups ) # there is networks and uplinks config[:uplink] = false host_id = vi_client.instance_variable_get '@host_id' begin nsx_client = NSXDriver::NSXClient.new_from_id(host_id) rescue StandardError nsx_client = nil end if !nsx_client.nil? nsx_net = NSXDriver::VirtualWire .new_from_name(nsx_client, nic[:net_name]) config[:nsx_id] = nsx_net.ls_id config[:nsx_vni] = nsx_net.ls_vni config[:nsx_tz_id] = nsx_net.tz_id end # Standard PortGroups when VCenterDriver::Network::NETWORK_TYPE_PG # There is no uplinks for standard portgroups, # so all Standard # PortGroups are networks and no uplinks config[:uplink] = false config[:sw_name] = VCenterDriver::Network .virtual_switch(nic[:network]) # NSX-T PortGroups when VCenterDriver::Network::NETWORK_TYPE_NSXT config[:sw_name] = \ nic[:network].summary.opaqueNetworkType # There is no uplinks for NSX-T networks, # so all NSX-T networks # are networks and no uplinks config[:uplink] = false host_id = vi_client.instance_variable_get '@host_id' begin nsx_client = NSXDriver::NSXClient.new_from_id(host_id) rescue StandardError nsx_client = nil end if !nsx_client.nil? nsx_net = NSXDriver::OpaqueNetwork .new_from_name(nsx_client, nic[:net_name]) config[:nsx_id] = nsx_net.ls_id config[:nsx_vni] = nsx_net.ls_vni config[:nsx_tz_id] = nsx_net.tz_id end else raise "Unknown network type: #{nic[:pg_type]}" end import_opts = { :network_name=> nic[:net_name], :sw_name=> config[:sw_name], :network_ref=> nic[:net_ref], :network_type=> nic[:pg_type], :ccr_ref=> ccr_ref, :ccr_name=> ccr_name, :vcenter_uuid=> vc_uuid, :vcenter_instance_name=> vcenter_instance_name, :dc_name=> dc_name, :unmanaged=> unmanaged, :template_ref=> template_ref, :dc_ref=> dc_ref, :template_id=> vm_id } if nic[:pg_type] == VCenterDriver::Network::NETWORK_TYPE_NSXV || nic[:pg_type] == VCenterDriver::Network::NETWORK_TYPE_NSXT import_opts[:nsx_id] = config[:nsx_id] import_opts[:nsx_vni] = config[:nsx_vni] import_opts[:nsx_tz_id] = config[:nsx_tz_id] end if vid vlanid = VCenterDriver::Network.vlanid(vid) # we have vlan id if /\A\d+\z/.match(vlanid) import_opts[:vlanid] = vlanid end end # Prepare the Virtual Network template one_vnet = VCenterDriver::Network.to_one_template(import_opts) # always has to be created because of # templates when they are instantiated ar_tmp = '' ar_tmp << "AR=[\n" ar_tmp << "TYPE=\"ETHER\",\n" ar_tmp << "SIZE=255\n" ar_tmp << "]\n" if vm? ar_tmp << create_ar(nic, false, nic[:ipv4]) if nic[:ipv4] if nic[:ipv6] ar_tmp << create_ar(nic, false, nil, nic[:ipv6]) end ar_tmp << create_ar(nic, true) if !nic[:ipv4] && !nic[:ipv6] end one_vnet[:one] << ar_tmp config[:one_object] = one_vnet[:one] _cluster_id = VCenterDriver::VIHelper .get_cluster_id(config[:one_ids]) one_vn = VCenterDriver::Network.create_one_network(config) VCenterDriver::VIHelper.clean_ref_hash one_vn.info # Wait until the virtual network is in ready state t_start = Time.now error = false timeout = 30 while Time.now - t_start < timeout begin if one_vn.short_state_str == 'rdy' error = false break end rescue StandardError error = true end sleep 1 one_vn.info end if error error_msg = "VNET #{one_vn.id} in state " error_msg += "#{one_vn.short_state_str}, aborting import" raise error_msg end one_vn end |
#create_template_copy(template_name) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/vm_template.rb', line 183 def create_template_copy(template_name) error = nil template_ref = nil template_name = "one-#{self['name']}" if template_name.empty? relocate_spec_params = {} relocate_spec_params[:pool] = resource_pool relocate_spec = RbVmomi::VIM .VirtualMachineRelocateSpec( relocate_spec_params ) clone_spec = RbVmomi::VIM .VirtualMachineCloneSpec( { :location => relocate_spec, :powerOn => false, :template => false } ) begin template = @item .CloneVM_Task( :folder => @item.parent, :name => template_name, :spec => clone_spec ).wait_for_completion template_ref = template._ref rescue StandardError => e if !e..start_with?('DuplicateName') error = 'Could not create the template'\ " clone. Reason: #{e.}" return error, nil end dc = datacenter vm_folder = dc.vm_folder vm_folder.fetch! vm = vm_folder.items .select {|_k, v| v.item.name == template_name } .values.first.item rescue nil if vm begin vm.Destroy_Task.wait_for_completion template = @item .CloneVM_Task( :folder => @item.parent, :name => template_name, :spec => clone_spec ).wait_for_completion template_ref = template._ref rescue StandardError error = 'Could not delete the existing '\ 'template, please remove it manually'\ " from vCenter. Reason: #{e.}" end else error = 'Could not create the template '\ "clone. Reason: #{e.}" end end [error, template_ref] end |
#datacenter ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/vm_template.rb', line 68 def datacenter item = @item trace = [] while item && !item.instance_of?(RbVmomi::VIM::Datacenter) rp = item.resourcePool rescue nil if rp && rp.instance_of?(RbVmomi::VIM::VirtualApp) trace << 'rp:' + item.to_s item = rp.parent rescue nil else trace << item.to_s item = item.parent rescue nil end end if item.nil? trace = '[' + trace.join(', ') + ']' raise "Could not find the parent Datacenter. Trace: #{trace}" end Datacenter.new(item) end |
#delete_template ⇒ Object
91 92 93 |
# File 'lib/vm_template.rb', line 91 def delete_template @item.Destroy_Task.wait_for_completion end |
#disk?(device) ⇒ Boolean
Checks if a RbVmomi::VIM::VirtualDevice is a disk
1565 1566 1567 |
# File 'lib/vm_template.rb', line 1565 def disk?(device) !device.class.ancestors.index(RbVmomi::VIM::VirtualDisk).nil? end |
#disk_or_cdrom?(device) ⇒ Boolean
Checks if a RbVmomi::VIM::VirtualDevice is a disk or a cdrom
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 |
# File 'lib/vm_template.rb', line 1536 def disk_or_cdrom?(device) is_disk = !device .class .ancestors .index(RbVmomi::VIM::VirtualDisk).nil? is_cdrom = !device .class .ancestors .index(RbVmomi::VIM::VirtualCdrom).nil? is_disk || is_cdrom end |
#disk_or_iso?(device) ⇒ Boolean
Checks if a RbVmomi::VIM::VirtualDevice is a disk or an iso file
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 |
# File 'lib/vm_template.rb', line 1551 def disk_or_iso?(device) is_disk = !device .class .ancestors .index(RbVmomi::VIM::VirtualDisk).nil? is_iso = device .backing .is_a? RbVmomi::VIM::VirtualCdromIsoBackingInfo is_disk || is_iso end |
#esx_name ⇒ Object
1578 1579 1580 |
# File 'lib/vm_template.rb', line 1578 def esx_name self['runtime.host.name'] end |
#fill_nic(ip_addresses, nic) ⇒ Object
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 |
# File 'lib/vm_template.rb', line 1466 def fill_nic(ip_addresses, nic) (0...ip_addresses.length).each do |i| ip = ip_addresses[i].ipAddress if ip =~ Resolv::IPv4::Regex if nic[:ipv4] if nic[:ipv4_additionals] nic[:ipv4_additionals] += ',' + ip else nic[:ipv4_additionals] = ip end else nic[:ipv4] = ip end elsif ip_addresses[i].ipAddress =~ Resolv::IPv6::Regex if get_ipv6_prefix(ip, 10) == 'fe80' # we not process this address elsif get_ipv6_prefix(ip, 7) == 'fc00' nic[:ipv6_ula] = ip else if nic[:ipv6] if nic[:ipv6_additionals] nic[:ipv6_additionals] += ',' + ip else nic[:ipv6_additionals] = ip end else nic[:ipv6] = ip end end end end end |
#find_alias_ips_in_network(network, vm_object, alias_ipv4 = nil, alias_ipv6 = nil) ⇒ Object
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
# File 'lib/vm_template.rb', line 593 def find_alias_ips_in_network( network, vm_object, alias_ipv4 = nil, alias_ipv6 = nil ) ipv4 = ipv6 = '' return unless vm_object.is_a?(VCenterDriver::VirtualMachine) ip = nil network.info unless alias_ipv4.nil? ip = IPAddr.new(alias_ipv4) end unless alias_ipv6.nil? ip = IPAddr.new(alias_ipv6) end if ip.nil? return [ipv4, ipv6] end ar_array = network.to_hash['VNET']['AR_POOL']['AR'] ar_array = [ar_array] if ar_array.is_a?(Hash) ipv4, ipv6 = find_ip_in_ar(ip, ar_array) if ar_array [ipv4, ipv6] end |
#find_ip_in_ar(ip, ar_array) ⇒ Object
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
# File 'lib/vm_template.rb', line 693 def find_ip_in_ar(ip, ar_array) ipv4 = ipv6 = '' ar_id = -1 ar_array.each do |ar| first_condition = ar.key?('IP') && ar.key?('IP_END') second_condition = ar.key?('IP6') && ar.key?('IP6_END') next unless first_condition || second_condition start_ip = IPAddr.new(ar['IP']) unless ar['IP'].nil? end_ip = IPAddr.new(ar['IP_END']) unless ar['IP_END'].nil? start_ip = IPAddr.new(ar['IP6']) unless ar['IP6'].nil? end_ip = IPAddr.new(ar['IP6_END']) unless ar['IP6_END'].nil? next unless ip.family == start_ip.family && ip.family == end_ip.family next unless ip >= start_ip && ip <= end_ip ipv4 = ip.to_s if ip.ipv4? ipv6 = ip.to_s if ip.ipv6? ar_id = ar['ID'] end [ipv4, ipv6, ar_id] end |
#find_ips_in_network(network, vm_object, nic, force = false, first_ip = false) ⇒ Object
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 |
# File 'lib/vm_template.rb', line 625 def find_ips_in_network( network, vm_object, nic, force = false, first_ip = false ) ipv4 = ipv6 = '' ar_id = -1 return unless vm_object.is_a?(VCenterDriver::VirtualMachine) network.info if nic[:ipv4] || nic[:ipv6] ar_array = network.to_hash['VNET']['AR_POOL']['AR'] ar_array = [ar_array] if ar_array.is_a?(Hash) ipv4, _ipv6, _arid = find_ip_in_ar( IPAddr.new(nic[:ipv4]), ar_array ) if ar_array && nic[:ipv4] _ipv4, ipv6, _arid = find_ip_in_ar( IPAddr.new(nic[:ipv6]), ar_array ) if ar_array && nic[:ipv6] return [ipv4, ipv6] end # Iterate over Retrieve vCenter VM NICs unless vm_object.item.guest.net.empty? vm_object.item.guest.net.each do |net| mac = net.macAddress next unless nic[:mac] == mac next unless net.ipConfig next if net.ipConfig.ipAddress.empty? net.ipConfig.ipAddress.each do |ip_config| ip = IPAddr.new(ip_config.ipAddress) if ip.ipv6? && get_ipv6_prefix(ip.to_s, 10) == 'fe80' next end if force ipv4 = ip.to_s if ip.ipv4? ipv6 = ip.to_s if ip.ipv6? return [ipv4, ipv6] end ar_array = network.to_hash['VNET']['AR_POOL']['AR'] ar_array = [ar_array] if ar_array.is_a?(Hash) ipv4, ipv6, ar_id = find_ip_in_ar(ip, ar_array) if ar_array if first_ip return [ipv4, ipv6, ar_id] end break if (ipv4 !='') || (ipv6 != '') end break end end [ipv4, ipv6, ar_id] end |
#get_ipv6_prefix(ipv6, prefix_length) ⇒ Object
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 |
# File 'lib/vm_template.rb', line 1499 def get_ipv6_prefix(ipv6, prefix_length) ip_slice = ipv6 .split(':') .map {|elem| elem.hex } .map do |elem| int, dec = elem.divmod(1) bin = int.to_s(2).to_s while dec > 0 int, dec = (dec * 2).divmod(1) bin << int.to_s end bin end.map {|elem| elem.rjust(16, '0') } # rubocop:disable Style/MultilineBlockChain ip_chain = ip_slice.join prefix = ip_chain[0, prefix_length] cont = 0 limit = prefix.length index = 0 slices = [] while cont < limit slices[index] = prefix.slice(cont, 4) slices[index] = slices[index].ljust(4, '0') index +=1 cont+=4 end slices.map {|elem| format('%0x', elem.to_i(2)) } .join.ljust(4, '0') end |
#get_vcenter_disk_key(unit_number, controller_key) ⇒ Object
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 |
# File 'lib/vm_template.rb', line 1247 def get_vcenter_disk_key(unit_number, controller_key) key = nil @item['config.hardware.device'].each do |device| disk = {} next unless disk_or_iso?(device) disk[:device] = device next unless device.controllerKey == controller_key && device.unitNumber == unit_number key = device.key break end key end |
#identify_network(identifier, network) ⇒ Object
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 |
# File 'lib/vm_template.rb', line 1335 def identify_network(identifier, network) if network.class == RbVmomi::VIM::DistributedVirtualPortgroup return network if identifier == network.key return end if network.class == RbVmomi::VIM::Network return network if identifier == network return end return unless network.class == RbVmomi::VIM::OpaqueNetwork if identifier == network.summary.opaqueNetworkId network else nil end end |
#import_vcenter_disks(vc_uuid, dpool, ipool, type) ⇒ Object
Import vcenter disks identifier(:id)
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/vm_template.rb', line 360 def import_vcenter_disks(vc_uuid, dpool, ipool, type) disk_info = '' error = '' images = [] begin # Lock import operation, to avoid concurrent creation of images lock dc = datacenter dc_ref = dc.item._ref # Get disks and info required vc_disks = vcenter_disks_get vc_disks.sort_by! {|d| d[:device].unitNumber } # Track allocated images allocated_images = [] vc_disks.each do |disk| ds_ref = nil begin ds_ref = disk[:datastore]._ref rescue StandardError raise "The ISO #{disk[:path_wo_ds].name} cannot "\ 'be found because the datastore was '\ 'removed or deleted' end datastore_found = VCenterDriver::Storage .get_one_image_ds_by_ref_and_dc( ds_ref, dc_ref, vc_uuid, dpool ) if datastore_found.nil? error = "\n ERROR: datastore "\ "#{disk[:datastore].name}: "\ 'has to be imported first as'\ " an image datastore!\n" # Rollback delete disk images allocated_images.each do |i| i.delete end break end # Read configuration for imported images, taking # into account if we are importing a VM Tempalte # or a Wild VM image_persistency = nil if vm? image_persistency = :wild_vm_persistent_images else image_persistency = :vm_template_persistent_images end image_persistency = VCenterDriver::CONFIG[image_persistency] params = { :disk => disk, :ipool => ipool, :_type => type, :ds_id => datastore_found['ID'], :opts => { :persistent => image_persistency ? 'YES':'NO' }, :images => images } image_import, image_name = VCenterDriver::Datastore .get_image_import_template( params ) # Image is already in the datastore if image_import[:one] # This is the disk info disk_tmp = '' disk_tmp << "DISK=[\n" disk_tmp << "IMAGE_ID=\"#{image_import[:one]['ID']}\",\n" disk_tmp << "OPENNEBULA_MANAGED=\"NO\"\n" disk_tmp << "]\n" disk_info << disk_tmp elsif !image_import[:template].empty? # Then the image is created as it's not in the datastore one_i = VCenterDriver::VIHelper .new_one_item( OpenNebula::Image ) allocated_images << one_i rc = one_i.allocate(image_import[:template], datastore_found['ID'].to_i, false) if OpenNebula.is_error?(rc) error = ' Error creating disk from '\ "template: #{rc.}\n" break end # Monitor image, we need READY state one_i.info start_time = Time.now while (one_i.state_str != 'READY') && (Time.now - start_time < 300) sleep 1 one_i.info end # Add info for One template one_i.info disk_info << "DISK=[\n" disk_info << "IMAGE_ID=\"#{one_i['ID']}\",\n" disk_info << "OPENNEBULA_MANAGED=\"NO\"\n" disk_info << "]\n" images.push(image_name) end end rescue StandardError => e error = "\n There was an error trying to create an "\ 'image for disk in vcenter template. '\ "Reason: #{e.}" if VCenterDriver::CONFIG[:debug_information] error += "\n\n#{e.backtrace}" end ensure unlock if !error.empty? && allocated_images # Rollback delete disk images allocated_images.each do |i| i.delete end end end [error, disk_info, allocated_images] end |
#import_vcenter_nics(opts, vm_id = nil, dc_name = nil) ⇒ Object
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 |
# File 'lib/vm_template.rb', line 1120 def import_vcenter_nics( opts, vm_id = nil, dc_name = nil ) vi_client = opts[:vi_client] vc_uuid = opts[:vc_uuid] npool = opts[:npool] hpool = opts[:hpool] vcenter_instance_name = opts[:vcenter] template_ref = opts[:template_moref] vm_object = opts[:vm_object] nic_info = '' error = '' ar_ids = {} begin # Lock import operation, to avoid # concurrent creation of networks lock if !dc_name dc = datacenter dc_name = dc.item.name dc_ref = dc.item._ref end ccr_ref = self['runtime.host.parent._ref'] ccr_name = self['runtime.host.parent.name'] # Get nics and info required vc_nics = vcenter_nics_hash # Track allocated networks for rollback allocated_networks = [] nic_index = 0 vc_nics.each do |nic| [:ipv4, :ipv6].each do |type| if nic[type] opts[type].shift if opts[type] next end begin ip = opts[type].shift if opts[type] # Check if it is a valid IP IPAddr.new(ip) nic[type] = ip rescue StandardError end end # Check if the network already exists network_found = VCenterDriver::VIHelper .find_by_ref( OpenNebula::VirtualNetworkPool, 'TEMPLATE/VCENTER_NET_REF', nic[:net_ref], vc_uuid, npool ) # Network is already in OpenNebula if network_found nic_info << nic_from_network_found(network_found, vm_object, nic, ar_ids, nic_index.to_s) # Network not found else opts = { :nic => nic, :ccr_ref => ccr_ref, :ccr_name => ccr_name, :vc_uuid => vc_uuid, :vcenter_instance_name => vcenter_instance_name, :dc_name => dc_name, :template_ref => template_ref, :dc_ref => dc_ref, :vm_id => vm_id, :hpool => hpool, :vi_client => vi_client } one_vn = create_network_for_import( opts ) allocated_networks << one_vn nic_info << nic_from_network_created( one_vn, nic, nic_index.to_s, vm_object, ar_ids ) # Refresh npool npool.info_all end nic_index += 1 end rescue StandardError => e error = "\n There was an error trying to create \ a virtual network to repesent a \ vCenter network for a VM or VM Template. \ Reason: #{e.}" ensure unlock # Rollback, delete virtual networks if !error.empty? && allocated_networks allocated_networks.each do |n| n.delete end end end [error, nic_info, ar_ids, allocated_networks] end |
#lock ⇒ Object
Locking function. Similar to flock
40 41 42 43 44 45 |
# File 'lib/vm_template.rb', line 40 def lock return unless @locking @locking_file = File.open('/var/tmp/vcenter-importer-lock', 'w') @locking_file.flock(File::LOCK_EX) end |
#nic_alias_from_nic(id, nic, nic_index, network_found, vm_object) ⇒ Object
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 |
# File 'lib/vm_template.rb', line 719 def nic_alias_from_nic(id, nic, nic_index, network_found, vm_object) nic_tmp = '' nic_alias_index = 0 if nic[:ipv4_additionals] nic[:ipv4_additionals].split(',').each do |ipv4_additional| ipv4, ipv6 = find_alias_ips_in_network( network_found, vm_object, ipv4_additional ) if ipv4.empty? && ipv6.empty? ar_tmp = create_ar( nic, false, ipv4_additional ) network_found.add_ar(ar_tmp) end network_found.info nic_tmp << "NIC_ALIAS=[\n" nic_tmp << "NETWORK_ID=\"#{id}\",\n" nic_tmp << "IP=\"#{ipv4_additional}\",\n" nic_tmp << "NAME=\"VC_NIC#{nic_index}_ALIAS#{nic_alias_index}\",\n" nic_tmp << "PARENT=\"VC_NIC#{nic_index}\"\n" nic_tmp << "]\n" nic_alias_index += 1 end end if nic[:ipv6_additionals] nic[:ipv6_additionals].split(',').each do |ipv6_additional| ipv4, ipv6 = find_alias_ips_in_network( network_found, vm_object, ipv6_additional ) if ipv4.empty? && ipv6.empty? ar_tmp = create_ar( nic, false, nil, ipv6_additional ) network_found.add_ar(ar_tmp) end network_found.info nic_tmp << "NIC_ALIAS=[\n" nic_tmp << "NETWORK_ID=\"#{id}\",\n" nic_tmp << "IP6=\"#{ipv6_additional}\",\n" nic_tmp << "NAME=\"VC_NIC#{nic_index}_ALIAS#{nic_alias_index}\",\n" nic_tmp << "PARENT=\"VC_NIC#{nic_index}\"\n" nic_tmp << "]\n" nic_alias_index += 1 end end nic_tmp end |
#nic_from_network_created(one_vn, nic, nic_index, vm_object, _ar_ids) ⇒ Object
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 |
# File 'lib/vm_template.rb', line 783 def nic_from_network_created(one_vn, nic, nic_index, vm_object, _ar_ids) nic_tmp = "NIC=[\n" nic_tmp << "NETWORK_ID=\"#{one_vn.id}\",\n" nic_tmp << "NAME =\"VC_NIC#{nic_index}\",\n" nic_tmp << "IP = \"#{nic[:ipv4]}\",\n" if nic[:ipv4] if vm? if nic[:mac] && !nic[:ipv4] nic_tmp << "MAC=\"#{nic[:mac]}\",\n" end if nic[:ipv4_additionals] nic_tmp << 'VCENTER_ADDITIONALS_IP4'\ "=\"#{nic[:ipv4_additionals]}\",\n" end if nic[:ipv6] nic_tmp << "VCENTER_IP6=\"#{nic[:ipv6]}\",\n" end if nic[:ipv6_global] nic_tmp << "IP6_GLOBAL=\"#{nic[:ipv6_global]}\",\n" end if nic[:ipv6_ula] nic_tmp << "IP6_ULA=\"#{nic[:ipv6_ula]}\",\n" end if nic[:ipv6_additionals] nic_tmp << 'VCENTER_ADDITIONALS_IP6'\ "=\"#{nic[:ipv6_additionals]}\",\n" end end nic_tmp << "OPENNEBULA_MANAGED=\"NO\"\n" nic_tmp << "]\n" if vm? nic_tmp << nic_alias_from_nic(one_vn.id, nic, nic_index, one_vn, vm_object) end nic_tmp end |
#nic_from_network_found(network_found, vm_object, nic, _ar_ids, nic_index) ⇒ Object
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 |
# File 'lib/vm_template.rb', line 828 def nic_from_network_found( network_found, vm_object, nic, _ar_ids, nic_index ) nic_tmp = "NIC=[\n" nic_tmp << "NETWORK_ID=\"#{network_found['ID']}\",\n" nic_tmp << "NAME =\"VC_NIC#{nic_index}\",\n" if vm? ipv4, ipv6 = find_ips_in_network(network_found, vm_object, nic, false, true) if ipv4.empty? && ipv6.empty? ar_tmp = create_ar(nic) network_found.add_ar(ar_tmp) end ipv4, ipv6 = find_ips_in_network(network_found, vm_object, nic, true) network_found.info if nic[:mac] nic_tmp << "MAC_IMPORTED=\"#{nic[:mac]}\",\n" end # This is the existing nic info if nic[:mac] && ipv4.empty? && ipv6.empty? nic_tmp << "MAC=\"#{nic[:mac]}\",\n" end nic_tmp << "IP=\"#{ipv4}\"," unless ipv4.empty? nic_tmp << "IP6=\"#{ipv6}\"," unless ipv6.empty? if nic[:ipv4_additionals] nic_tmp << 'VCENTER_ADDITIONALS_IP4'\ "=\"#{nic[:ipv4_additionals]}\",\n" end if nic[:ipv6] nic_tmp << "VCENTER_IP6=\"#{nic[:ipv6]}\",\n" end if nic[:ipv6_global] nic_tmp << "IP6_GLOBAL=\"#{nic[:ipv6_global]}\",\n" end if nic[:ipv6_ula] nic_tmp << "IP6_ULA=\"#{nic[:ipv6_ula]}\",\n" end if nic[:ipv6_additionals] nic_tmp << 'VCENTER_ADDITIONALS_IP6'\ "=\"#{nic[:ipv6_additionals]}\",\n" end end nic_tmp << "OPENNEBULA_MANAGED=\"NO\"\n" nic_tmp << "]\n" if vm? nic_tmp << nic_alias_from_nic( network_found['ID'], nic, nic_index, network_found, vm_object ) end nic_tmp end |
#online? ⇒ Boolean
62 63 64 65 66 |
# File 'lib/vm_template.rb', line 62 def online? raise 'vcenter item not found!' unless @item !@item['guest.net'].empty? end |
#resource_pool ⇒ Object
Returns RbVmomi::VIM::ResourcePool, first resource pool in cluster.
1574 1575 1576 |
# File 'lib/vm_template.rb', line 1574 def resource_pool self['runtime.host.parent.resourcePool'] end |
#retrieve_from_device(device) ⇒ Object
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 |
# File 'lib/vm_template.rb', line 1357 def retrieve_from_device(device) device_network = nil device_network_id = nil # First search network corresponding this device # Distributed Networks and NSX-V Networks if !device.backing[:port].nil? device_network_id = device.backing.port.portgroupKey # Standard Networks elsif !device.backing[:network].nil? device_network_id = device.backing[:network] # NSX-T Opaque Networks elsif !device.backing[:opaqueNetworkId].nil? device_network_id = device.backing[:opaqueNetworkId] end # Check if networkId exists if device_network_id.nil? raise "Invalid or not supported network #{device.backing}" end # Matching between device and network objects @item.network.each do |net| device_network = identify_network(device_network_id, net) break unless device_network.nil? end # Check network matching if device_network.nil? raise "\"#{device.deviceInfo.label}\" \ not match any known network" end res = {} res[:refs] = device_network.host.map do |h| h.parent._ref if h.parent end res[:net_name] = device_network.name res[:net_ref] = device_network._ref res[:pg_type] = VCenterDriver::Network .get_network_type( device_network, res[:net_name] ) res[:network] = device_network res end |
#save_ar_ids(network_found, nic, ar_ids) ⇒ Object
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
# File 'lib/vm_template.rb', line 577 def save_ar_ids(network_found, nic, ar_ids) value = [] ars_new = network_found.to_hash['VNET']['AR_POOL']['AR'] ars_new = [ars_new] if ars_new.class.to_s.eql? 'Hash' last_id = ars_new.last['AR_ID'] if ar_ids.key?(nic[:net_ref]) ref = nic[:net_ref] value = ar_ids[ref.to_s] end value.insert(value.length, last_id) ar_ids.store(nic[:net_ref], value) last_id end |
#save_as_linked_clones(name) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/vm_template.rb', line 99 def save_as_linked_clones(name) error = nil disks = @item.config.hardware.device.grep( RbVmomi::VIM::VirtualMachine ) disks.select {|x| x.backing.parent.nil? }.each do |disk| spec = { :deviceChange => [ { :operation => :remove, :device => disk }, { :operation => :add, :fileOperation => :create, :device => disk.dup.tap do |x| x.backing = x.backing.dup x.backing.fileName = "[#{disk.backing.datastore.name}]" x.backing.parent = disk.backing end } ] } @item.ReconfigVM_Task( :spec => spec ).wait_for_completion end relocateSpec = RbVmomi::VIM.VirtualMachineRelocateSpec( :diskMoveType => :moveChildMostDiskBacking ) spec = RbVmomi::VIM.VirtualMachineCloneSpec( :location => relocateSpec, :powerOn => false, :template => true ) new_template = @item.CloneVM_Task( :folder => @item.parent, :name => name, :spec => spec ).wait_for_completion new_vm_template_ref = new_template._ref one_client = OpenNebula::Client.new importer = VCenterDriver::VmImporter.new( one_client, @vi_client ) importer.retrieve_resources({}) importer.get_indexes(new_vm_template_ref) importer.process_import( new_vm_template_ref, { new_vm_template_ref.to_s => { :type => 'default', :linked_clone => '1', :copy => '0', :name => '', :folder => '' } } ) begin importer.output[:success][0][:id][0] rescue StandardError => e error = 'Creating linked clone VM Template' \ " failed due to \"#{e.}\".\n" if VCenterDriver::CONFIG[:debug_information] error += " #{e.backtrace}\n" end end [error, new_vm_template_ref] end |
#unlock ⇒ Object
Unlock driver execution mutex
48 49 50 51 52 53 54 55 56 |
# File 'lib/vm_template.rb', line 48 def unlock return unless @locking @locking_file.close return unless File.exist?('/var/tmp/vcenter-importer-lock') File.delete('/var/tmp/vcenter-importer-lock') end |
#vcenter_disks_get ⇒ Object
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 |
# File 'lib/vm_template.rb', line 1266 def vcenter_disks_get disks = [] ide_controlled = [] sata_controlled = [] scsi_controlled = [] controller = {} @item['config.hardware.device'].each do |device| disk = {} if device.is_a? RbVmomi::VIM::VirtualIDEController ide_controlled.concat(device.device) controller[device.key] = "ide#{device.busNumber}" end if device.is_a? RbVmomi::VIM::VirtualSATAController sata_controlled.concat(device.device) controller[device.key] = "sata#{device.busNumber}" end if device.is_a? RbVmomi::VIM::VirtualSCSIController scsi_controlled.concat(device.device) controller[device.key] = "scsi#{device.busNumber}" end next unless disk_or_iso?(device) disk[:device] = device unless device.backing.datastore raise "datastore not found for VM's device" end disk[:datastore] = device.backing.datastore disk[:path] = device.backing.fileName disk[:path_wo_ds]= disk[:path].sub(/^\[(.*?)\] /, '') disk?(device) ? disk[:type] = 'OS' : disk[:type] = 'CDROM' disk[:key] = device.key if ide_controlled.include?(device.key) disk[:prefix] = 'hd' end if scsi_controlled.include?(device.key) disk[:prefix] = 'sd' end if sata_controlled.include?(device.key) disk[:prefix] = 'sd' end disk[:tag] = "#{controller[device.controllerKey]}:#{device.unitNumber}" disks << disk end disks end |
#vcenter_instance_uuid ⇒ Object
95 96 97 |
# File 'lib/vm_template.rb', line 95 def vcenter_instance_uuid @vi_client.vim.serviceContent.about.instanceUuid rescue nil end |
#vcenter_nics_hash ⇒ Object
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 1457 1458 1459 1460 1461 1462 1463 1464 |
# File 'lib/vm_template.rb', line 1411 def vcenter_nics_hash parse_live = lambda {|inets_raw| h = nil begin h = inets_raw.to_h rescue NoMethodError h = {} inets_raw.each do |nic_dev| h[nic_dev[0]] = nic_dev[1] end end return h } nics = [] inets_raw = nil inets = {} @item['config.hardware.device'].each do |device| next unless VCenterDriver::Network.nic?(device) nic = retrieve_from_device(device) nic[:mac] = device.macAddress rescue nil if vm? && online? inets_raw ||= @item['guest.net'] .map .with_index {|x, _| [x.macAddress, x] } inets = parse_live.call(inets_raw) if inets.empty? if !inets[nic[:mac]].nil? ip_addresses = inets[nic[:mac]] .ipConfig .ipAddress rescue nil end if !ip_addresses.nil? && !ip_addresses.empty? nic[:ipv4], nic[:ipv4_additionals] = nil nic[:ipv6], nic[:ipv6_ula], nic[:ipv6_global], nic[:ipv6_additionals] = nil fill_nic(ip_addresses, nic) end end nics << nic end nics end |
#vcenter_nics_list ⇒ Object
1326 1327 1328 1329 1330 1331 1332 1333 |
# File 'lib/vm_template.rb', line 1326 def vcenter_nics_list nics = [] @item.config.hardware.device.each do |device| nics << device if VCenterDriver::Network.nic?(device) end nics end |
#vm? ⇒ Boolean
58 59 60 |
# File 'lib/vm_template.rb', line 58 def vm? self.class == VCenterDriver::VirtualMachine end |
#vm_template_ds_ref ⇒ Object
Gets MOREF from Datastore used by the VM. It validates the selected DS is not only used to host swap.
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 |
# File 'lib/vm_template.rb', line 1667 def vm_template_ds_ref begin ds_ref = nil if @vm_info['datastore'].length > 1 swap_path = '' @vm_info['config.extraConfig'].each do |element| if element.key == 'sched.swap.derivedName' swap_path = element.value end end @vm_info['datastore'].each do |datastore| path = datastore.summary.url.sub(%r{ds:///*}, '') if !swap_path.include?(path) && !datastore._ref.nil? ds_ref = datastore._ref break end end elsif @vm_info['datastore'].length == 1 if !@vm_info['datastore'].first._ref.nil? ds_ref = @vm_info['datastore'].first._ref end end ds_ref rescue StandardError => e "Could not find DATASTORE for this VM. Reason: #{e.}" end end |
#vm_to_one(vm_name) ⇒ Object
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 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 |
# File 'lib/vm_template.rb', line 1582 def vm_to_one(vm_name) str = "NAME = \"#{vm_name}\"\n"\ "CPU = \"#{@vm_info['config.hardware.numCPU']}\"\n"\ "vCPU = \"#{@vm_info['config.hardware.numCPU']}\"\n"\ "MEMORY = \"#{@vm_info['config.hardware.memoryMB']}\"\n"\ "HYPERVISOR = \"vcenter\"\n"\ "CONTEXT = [\n"\ " NETWORK = \"YES\",\n"\ " SSH_PUBLIC_KEY = \"$USER[SSH_PUBLIC_KEY]\"\n"\ "]\n"\ "VCENTER_INSTANCE_ID =\"#{@vm_info[:vc_uuid]}\"\n"\ "VCENTER_CCR_REF =\"#{@vm_info[:cluster_ref]}\"\n" str << "DEPLOY_ID =\"#{self['_ref']}\"\n" @state = 'POWEROFF' if @state == 'd' str << "IMPORT_STATE =\"#{@state}\"\n" # Get DS information if !@vm_info['datastore'].nil? !@vm_info['datastore'].last.nil? && !@vm_info['datastore'].last._ref.nil? ds_ref = vm_template_ds_ref str << "VCENTER_DS_REF = \"#{ds_ref}\"\n" end vnc_port = nil keymap = VCenterDriver::VIHelper .get_default( 'VM/TEMPLATE/GRAPHICS/KEYMAP' ) @vm_info['config.extraConfig'].select do |xtra| if xtra[:key].downcase=='remotedisplay.vnc.port' vnc_port = xtra[:value] end if xtra[:key].downcase=='remotedisplay.vnc.keymap' keymap = xtra[:value] end end if !@vm_info['config.extraConfig'].empty? str << "GRAPHICS = [\n"\ " TYPE =\"vnc\",\n" str << " PORT =\"#{vnc_port}\",\n" if vnc_port str << " KEYMAP =\"#{keymap}\",\n" if keymap str << " LISTEN =\"0.0.0.0\"\n" str << "]\n" end if !@vm_info['config.annotation'] || @vm_info['config.annotation'] .empty? str << 'DESCRIPTION = "vCenter Template \ imported by OpenNebula' \ " from Cluster #{@vm_info['cluster_name']}\"\n" else notes = @vm_info['config.annotation'] .gsub('\\', '\\\\') .gsub('"', '\\"') str << "DESCRIPTION = \"#{notes}\"\n" end case @vm_info['guest.guestFullName'] when /CentOS/i str << "LOGO=images/logos/centos.png\n" when /Debian/i str << "LOGO=images/logos/debian.png\n" when /Red Hat/i str << "LOGO=images/logos/redhat.png\n" when /Ubuntu/i str << "LOGO=images/logos/ubuntu.png\n" when /Windows XP/i str << "LOGO=images/logos/windowsxp.png\n" when /Windows/i str << "LOGO=images/logos/windows8.png\n" when /Linux/i str << "LOGO=images/logos/linux.png\n" end str end |