Revision 593600f7 src/onedb/vcenter_one54_pre.rb

View differences:

src/onedb/vcenter_one54_pre.rb
32 32
$: << RUBY_LIB_LOCATION+"/cli"
33 33
$: << REMOTES_LOCATION+"vmm/vcenter/"
34 34

  
35
require 'fileutils'
36

  
35 37
require 'command_parser'
36 38
require 'one_helper/onehost_helper'
37 39
require 'one_helper/onecluster_helper'
38 40
require 'vcenter_driver'
39 41
require 'opennebula'
40 42

  
41
TEMP_DIR="/tmp"
43
TEMP_DIR="/var/tmp/vcenter_one54"
44
FileUtils.mkdir_p TEMP_DIR
42 45

  
43 46
def banner(msg, header=false, extended=nil)
44 47
    STDOUT.puts
......
65 68
    STDOUT.puts "="*80
66 69
end
67 70

  
68

  
69

  
70

  
71 71
################################################################################
72 72
# Monkey patch XMLElement with retrieve_xmlelements
73 73
################################################################################
......
89 89
end
90 90

  
91 91
def get_image_size(ds, img_str)
92

  
93 92
    ds_name = ds.name
93

  
94 94
    img_path = File.dirname img_str
95 95
    img_name = File.basename img_str
96 96

  
......
109 109
                                                :fileType     => true,
110 110
                                                :modification => true)
111 111

  
112

  
113 112
    spec.matchPattern = img_name.nil? ? [] : [img_name]
114 113

  
115 114
    datastore_path = "[#{ds_name}]"
......
483 482
                else
484 483
                    template_disk = template_xml.xpath("VMTEMPLATE/TEMPLATE/DISK")[unmanaged_disk_index] rescue nil
485 484
                    raise "Cannot find unmanaged disk inside template" if !template_disk
485

  
486 486
                    image_id = template_disk.xpath("IMAGE_ID").text
487 487
                    raise "Cannot find image id for unmanaged disk" if image_id.empty?
488

  
488 489
                    one_image = OpenNebula::Image.new_with_id(image_id, one_client)
489
                    rc   = one_image.info
490
                    raise "\n    ERROR! Could not get image info for unmanged disk. Reason #{rc.message}" if OpenNebula.is_error?(rc)
490
                    rc = one_image.info
491
                    raise "\n    ERROR! Could not get image info for unmanaged disk. image_id '#{image_id}'. Reason #{rc.message}" if OpenNebula.is_error?(rc)
492

  
493
                    ds_id = one_image['DATASTORE_ID']
494
                    ds = OpenNebula::Datastore.new_with_id(ds_id, one_client)
495
                    rc = ds.info
496
                    raise "\n    ERROR! Could not get ds info. Reason #{rc.message}" if OpenNebula.is_error?(rc)
497

  
498
                    ds_ref = ds["TEMPLATE/VCENTER_DS_REF"]
499
                    ds_name = ds["NAME"]
500

  
491 501
                    STDOUT.puts "--- Image #{one_image["NAME"]} with ID #{one_image["ID"]} already exists"
492 502
                end
493 503

  
......
1198 1208
    end
1199 1209

  
1200 1210
    loop do
1201
        STDOUT.print("\nFrom the list above, please \e[95mpick up one number\e[39m in order to specify the cluster: ")
1211
        STDOUT.print("\nFrom the list above, please \e[95mpick a number\e[39m in order to specify the cluster: ")
1202 1212
        cluster_index = STDIN.gets.strip.to_i
1203 1213
        next if cluster_index == 0 || cluster_index - 1 < 0 || cluster_index - 1 > ccr_refs.size
1204 1214
        ccr_ref  = ccr_refs[cluster_index-1] rescue nil
......
1286 1296
    # Get all hosts from pool with VN_MAD="vcenter"
1287 1297
    hosts = hpool.retrieve_xmlelements("HOST[VM_MAD=\"vcenter\"]")
1288 1298

  
1289

  
1290 1299
    hosts.each do |host|
1291 1300
        begin
1292 1301

  
......
1322 1331
                    # We inform that the Cluster has been created
1323 1332
                    STDOUT.puts "OpenNebula Cluster named #{ccr_name} \e[92mhas been created.\e[39m"
1324 1333
                    STDOUT.puts
1334

  
1335
                    # Fetch the cluster info
1336
                    rc = one_cluster.info
1337
                    if OpenNebula.is_error?(rc)
1338
                        STDOUT.puts "    Error Getting information from cluster '#{ccr_name}'. Reason: #{rc.message}\n"
1339
                        next
1340
                    end
1325 1341
                else
1326 1342
                    STDOUT.puts "OpenNebula Cluster #{ccr_name} \e[92malready exists.\e[39m"
1327 1343
                    STDOUT.puts
......
1403 1419
            one_ds.delete
1404 1420
        end
1405 1421
    end
1422

  
1406 1423
    STDOUT.puts
1407 1424

  
1408 1425
    # Refresh dspool and retrieve datastores again
......
1420 1437
            raise rc.message if OpenNebula.is_error?(rc)
1421 1438
            ds_name = one_ds["NAME"]
1422 1439
            ccr_name = one_ds["TEMPLATE/VCENTER_CLUSTER"]
1423
            next if !ccr_name #If VCENTER_CLUSTER doesn't exist it's not usable
1440
            next if !ccr_name # If VCENTER_CLUSTER doesn't exist it's not usable
1424 1441

  
1425 1442
            # Get cluster's host from its name stored in VCENTER_CLUSTER
1426 1443
            hosts = hpool.retrieve_xmlelements("HOST[NAME=\"#{ccr_name}\"]")
......
1460 1477
            # otherwise the administrator should select one from the list
1461 1478
            # We need to extract the datacenter ref and name as they are now
1462 1479
            # required attributes.
1463

  
1464 1480
            if datastores_with_name.size == 1
1465 1481
                vc_datastores[vcenter_uuid].each do |ref, ds|
1466 1482
                    if ds["name"] == ds_name
......
1525 1541

  
1526 1542
                # Loop until the admin user chooses the right datastore
1527 1543
                loop do
1528
                    STDOUT.print("\nFrom the list above, please \e[95mpick up one number\e[39m in order to specify the datastore: ")
1544
                    STDOUT.print("\nFrom the list above, please \e[95mpick one number\e[39m in order to specify the datastore: ")
1529 1545
                    ds_index = STDIN.gets.strip.to_i
1530 1546
                    next if ds_index == 0 || ds_index - 1 < 0 || ds_index - 1 > ds_info.size
1531 1547
                    ds_ref  = ds_info[ds_index-1][:ref] rescue nil
......
1563 1579
            # Inform what attributes have been added
1564 1580
            STDOUT.puts "Datastore \e[96m#{ds_name}\e[39m got new attributes:\n"
1565 1581
            STDOUT.puts
1566
            STDOUT.puts "--- VCENTER_DS_REF=#{ds_ref}\n"
1567
            STDOUT.puts "--- VCENTER_DS_NAME=#{ds_name}\n"
1568
            STDOUT.puts "--- VCENTER_DC_REF=#{dc_ref}\n"
1569
            STDOUT.puts "--- VCENTER_DC_NAME=#{dc_name}\n"
1570
            STDOUT.puts "--- VCENTER_HOST=#{vcenter_host}\"\n"
1571
            STDOUT.puts "--- VCENTER_USER=#{vcenter_user}\"\n"
1572
            STDOUT.puts "--- VCENTER_PASSWORD=#{vcenter_pass}\"\n"
1573
            STDOUT.puts "--- VCENTER_INSTANCE_ID=#{vcenter_uuid}\n"
1582
            STDOUT.puts "--- VCENTER_DS_REF=\"#{ds_ref}\"\n"
1583
            STDOUT.puts "--- VCENTER_DS_NAME=\"#{ds_name}\"\n"
1584
            STDOUT.puts "--- VCENTER_DC_REF=\"#{dc_ref}\"\n"
1585
            STDOUT.puts "--- VCENTER_DC_NAME=\"#{dc_name}\"\n"
1586
            STDOUT.puts "--- VCENTER_HOST=\"#{vcenter_host}\"\n"
1587
            STDOUT.puts "--- VCENTER_USER=\"#{vcenter_user}\"\n"
1588
            STDOUT.puts "--- VCENTER_PASSWORD=\"#{vcenter_pass}\"\n"
1589
            STDOUT.puts "--- VCENTER_INSTANCE_ID=\"#{vcenter_uuid}\"\n"
1574 1590

  
1575 1591
            STDOUT.puts
1576 1592

  
......
1690 1706
                STDOUT.puts("#{ccr_names.size+1}: None of the above.")
1691 1707

  
1692 1708
                loop do
1693
                    STDOUT.print("\nFrom the list above, please pick up one number in order to specify the cluster: ")
1709
                    STDOUT.print("\nFrom the list above, please pick one number in order to specify the cluster: ")
1694 1710
                    cluster_index = STDIN.gets.strip.to_i
1695 1711
                    next if cluster_index == 0 || cluster_index - 1 < 0 || cluster_index - 1 > ccr_names.size+1
1696 1712
                    ccr_name  = ccr_names[cluster_index-1] rescue nil
......
1758 1774

  
1759 1775
                # Loop until the administrator selects a vnet
1760 1776
                loop do
1761
                    STDOUT.print("\nFrom the list above, please \e[95mpick up one number\e[39m in order to specify the vnet: ")
1777
                    STDOUT.print("\nFrom the list above, please \e[95mpick one number\e[39m in order to specify the vnet: ")
1762 1778
                    vnet_index = STDIN.gets.strip.to_i
1763 1779
                    next if vnet_index == 0 || vnet_index - 1 < 0 || vnet_index - 1 > vnet_refs.size
1764 1780
                    vnet_ref  = vnet_refs[vnet_index-1] rescue nil
......
1854 1870
    imported_images.each do |image|
1855 1871
        one_image  = OpenNebula::Image.new_with_id(image["ID"], one_client)
1856 1872
        one_image.delete
1873

  
1874
        loop do
1875
            rc = one_image.info
1876
            break if OpenNebula.is_error?(rc)
1877
        end
1857 1878
    end
1879

  
1858 1880
    STDOUT.puts
1859 1881

  
1860 1882
    # Refresh pool
......
1962 1984

  
1963 1985
################################################################################
1964 1986
def inspect_templates(vc_templates, vc_clusters, one_clusters, tpool, ipool, vnpool, dspool, hpool, one_client, vcenter_ids)
1965

  
1966

  
1967 1987
    # Retrieve all OpenNebula templates associated with PUBLIC_CLOUD=vcenter
1968 1988
    templates = tpool.retrieve_xmlelements("VMTEMPLATE[TEMPLATE/PUBLIC_CLOUD/TYPE=\"vcenter\"]")
1969 1989

  
1970 1990
    templates.each do |template|
1971

  
1972 1991
        begin
1973 1992
            # Refresh pools
1974 1993
            rc = vnpool.info_all
......
1979 1998
            raise "\n    ERROR! Could not update dspool. Reason #{rc.message}" if OpenNebula.is_error?(rc)
1980 1999

  
1981 2000
            # Get some variables
1982
            ccr_ref = nil
2001
            ccr_ref      = nil
1983 2002
            vcenter_uuid = nil
1984
            host_id = nil
2003
            host_id      = nil
2004

  
1985 2005
            template_name    = template["NAME"]
1986 2006
            template_uuid    = template["TEMPLATE/PUBLIC_CLOUD/VM_TEMPLATE"]
1987 2007
            template_ref     = template["TEMPLATE/PUBLIC_CLOUD/VCENTER_REF"]
......
2001 2021
                    ccr_ref      = hosts.first["TEMPLATE/VCENTER_CCR_REF"]
2002 2022
                    vcenter_uuid = hosts.first["TEMPLATE/VCENTER_INSTANCE_ID"]
2003 2023
                    host_id      = hosts.first["ID"]
2024
                    vcenter_user = hosts.first["TEMPLATE/VCENTER_USER"]
2025
                    vcenter_pass = hosts.first["TEMPLATE/VCENTER_PASSWORD"]
2026
                    vcenter_host = hosts.first["TEMPLATE/VCENTER_HOST"]
2027

  
2004 2028
                    template_cluster = nil if !ccr_ref || !vcenter_uuid
2005 2029
                end
2006 2030
            end
......
2008 2032
            # As we don't know which vCenter cluster is associated with the template
2009 2033
            # The administrator must select one from a list
2010 2034
            if !template_cluster
2011
                STDOUT.puts("\n\e[93mWARNING: Manual intervention required!\e[39m")
2012
                STDOUT.puts("\nWhich vCenter cluster is associated with OpenNebula \e[96mtemplate #{template_name}?\n\e[39m")
2013
                STDOUT.puts
2035
                hpool_vcenter = hpool.select{|h| h["VM_MAD"] == "vcenter"}
2014 2036

  
2015
                ccr_names = []
2016
                hpool.each_with_index do |host, index|
2017
                    STDOUT.puts("#{index+1}: #{host["NAME"]} in #{host["TEMPLATE/VCENTER_HOST"]}")
2018
                    ccr_names << host["NAME"]
2019
                end
2037
                if hpool_vcenter.count == 1
2038
                    template_cluster = hpool_vcenter.first["NAME"]
2039
                else
2040
                    STDOUT.puts("\n\e[93mWARNING: Manual intervention required!\e[39m")
2041
                    STDOUT.puts("\nWhich vCenter cluster is associated with OpenNebula \e[96mtemplate #{template_name}?\n\e[39m")
2042
                    STDOUT.puts
2020 2043

  
2021
                STDOUT.puts("#{ccr_names.size+1}: None of the above.")
2044
                    ccr_names = []
2045
                    hpool_vcenter.each_with_index do |host, index|
2046
                        STDOUT.puts("#{index+1}: #{host["NAME"]} in #{host["TEMPLATE/VCENTER_HOST"]}")
2047
                        ccr_names << host["NAME"]
2048
                    end
2022 2049

  
2023
                loop do
2024
                    STDOUT.print("\nFrom the list above, please \e[95mpick up one number\e[39m in order to specify the cluster: ")
2025
                    cluster_index = STDIN.gets.strip.to_i
2026
                    next if cluster_index == 0 || cluster_index - 1 < 0 || cluster_index > ccr_names.size+1
2027
                    template_cluster  = ccr_names[cluster_index-1] rescue nil
2028
                    break
2029
                end
2050
                    STDOUT.puts("#{ccr_names.size+1}: None of the above.")
2030 2051

  
2031
                STDOUT.puts
2032
                STDOUT.puts "-" * 80
2033
                STDOUT.puts
2052
                    loop do
2053
                        STDOUT.print("\nFrom the list above, please \e[95mpick one number\e[39m in order to specify the cluster: ")
2054
                        cluster_index = STDIN.gets.strip.to_i
2055
                        next if cluster_index == 0 || cluster_index - 1 < 0 || cluster_index > ccr_names.size+1
2056
                        template_cluster  = ccr_names[cluster_index-1] rescue nil
2057
                        break
2058
                    end
2059

  
2060
                    STDOUT.puts
2061
                    STDOUT.puts "-" * 80
2062
                    STDOUT.puts
2063
                end
2034 2064

  
2035 2065
                if !template_cluster
2036 2066
                    raise "We could not find the host name associated to template #{template_name}\n"\
......
2040 2070
                # Get host attributes from the name of the cluster associated
2041 2071
                # to the template
2042 2072
                hosts = hpool.retrieve_xmlelements("HOST[NAME=\"#{template_cluster}\"]")
2073

  
2043 2074
                ccr_ref      = hosts.first["TEMPLATE/VCENTER_CCR_REF"]
2044 2075
                vcenter_uuid = hosts.first["TEMPLATE/VCENTER_INSTANCE_ID"]
2045 2076
                host_id      = hosts.first["ID"]
2046 2077
                vcenter_user = hosts.first["TEMPLATE/VCENTER_USER"]
2047 2078
                vcenter_pass = hosts.first["TEMPLATE/VCENTER_PASSWORD"]
2048 2079
                vcenter_host = hosts.first["TEMPLATE/VCENTER_HOST"]
2049

  
2050
                if ccr_ref.nil? || vcenter_uuid.nil?
2051
                    raise "Template #{template_name} could not be updated, cannot find cluster's MOREF or vcenter uuid"
2052
                end
2053 2080
            end
2054 2081

  
2055
            if !ccr_ref
2056
                raise "Template #{template_name} could not be updated, cannot find cluster's MOREF"
2082
            if ccr_ref.nil? || vcenter_uuid.nil?
2083
                raise "Template #{template_name} could not be updated, cannot find cluster's MOREF: '#{ccr_ref}'" \
2084
                      ", or vcenter uuid: '#{vcenter_uuid}'. "
2057 2085
            end
2058 2086

  
2059 2087
            # Create Rbvmomi connection
......
2126 2154
                STDOUT.puts("#{template_refs.size+1}: None of the above.")
2127 2155

  
2128 2156
                loop do
2129
                    STDOUT.print("\nFrom the list above, please \e[95mpick up one number\e[39m in order to specify the template: ")
2157
                    STDOUT.print("\nFrom the list above, please \e[95mpick a number\e[39m in order to specify the template: ")
2130 2158
                    template_index = STDIN.gets.strip.to_i
2131 2159
                    next if template_index == 0 || template_index - 1 < 0 || template_index > template_refs.size + 1
2132 2160
                    template_ref  = template_refs[template_index-1] rescue nil
......
2341 2369
        next if !vm["DEPLOY_ID"] # Ignore undeployed vms
2342 2370

  
2343 2371
        begin
2344

  
2345 2372
            # Refresh pools
2346 2373
            rc = vnpool.info_all
2347 2374
            raise "\n    ERROR! Could not update vnpool. Reason #{rc.message}" if OpenNebula.is_error?(rc)
......
2542 2569
            dc_ref  = dc._ref
2543 2570

  
2544 2571
            # Get xml template from tmp with unmanaged disks and nics and new attributes
2545
            template_id       = vm["TEMPLATE/TEMPLATE_ID"]
2546
            template_xml      = nil
2572
            template_id  = vm["TEMPLATE/TEMPLATE_ID"]
2573
            template_xml = nil
2574

  
2547 2575
            if !vm_wild
2548 2576
                template_filename = "#{TEMP_DIR}/one_migrate_template_#{template_id}"
2549 2577
                if File.exist?("#{template_filename}")
......
2665 2693

  
2666 2694
            hpool = OpenNebula::HostPool.new(one_client)
2667 2695
            rc = hpool.info
2696

  
2668 2697
            raise "Error contacting OpenNebula #{rc.message}" if OpenNebula.is_error?(rc)
2669 2698

  
2670 2699
            cpool = OpenNebula::ClusterPool.new(one_client)
2671 2700
            rc = cpool.info
2672 2701
            raise "Error contacting OpenNebula #{rc.message}" if OpenNebula.is_error?(rc)
2673 2702

  
2674
            vc_clusters = {}
2703
            vc_clusters   = {}
2675 2704
            vc_datastores = {}
2676
            vc_networks = {}
2677
            vc_vmachines = {}
2678
            vc_templates = {}
2705
            vc_networks   = {}
2706
            vc_vmachines  = {}
2707
            vc_templates  = {}
2679 2708

  
2680 2709
            banner " PHASE 0 - Before running the script please read the following notes", true
2681 2710

  
......
2685 2714
                        "  restarted your OpenNebula services to apply the new configuration before\n"\
2686 2715
                        "  launching the script.")
2687 2716
            STDOUT.puts
2688
            STDOUT.puts("- Don't forget to edit the file \e[92m/var/lib/one/remotes/datastore/vcenter/rm\e[39m\n"\
2689
                        "  and replace the following line:\n\n"\
2690
                        "  \e[96mvi_client.delete_virtual_disk(img_src,ds_name)\e[39m \n\n"\
2717
            STDOUT.puts("- Edit the file \e[92m/var/lib/one/remotes/datastore/vcenter/rm\e[39m and replace the\n"\
2718
                        "  following lines:\n\n"\
2719
                        "  \e[96mvi_client.delete_virtual_disk(img_src,\n"\
2720
                        "                                ds_name)\e[39m \n\n"\
2691 2721
                        "  with the following lines:\n\n"\
2692 2722
                        "  \e[96mif drv_action[\"/DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/VCENTER_IMPORTED\"] != \"YES\"\n"\
2693 2723
                        "       vi_client.delete_virtual_disk(img_src,ds_name) \n"\
......
2711 2741
            STDOUT.puts("- This script can be executed as many times as you wish. It will update previous\n"\
2712 2742
                        "  results and XML template will be always overwritten.")
2713 2743
            STDOUT.puts
2714

  
2744
            STDOUT.puts("\e[93mDon't forget to restart OpenNebula if you have made changes!\e[39m")
2715 2745

  
2716 2746
            STDOUT.print("\nDo you want to continue? ([y]/n): ")
2747

  
2717 2748
            exit! if STDIN.gets.strip.downcase == 'n'
2718 2749

  
2719 2750
            banner " PHASE 1 - Retrieve objects from vCenter instances", true
......
2736 2767
                    next
2737 2768
                end
2738 2769
                vcenter_instances << vcenter_uuid
2770

  
2739 2771
                # Retrieve vCenter Managed Objects
2740 2772
                vc_clusters[vcenter_uuid]   = retrieve_vcenter_clusters(vi_client)
2741 2773
                vc_datastores[vcenter_uuid] = retrieve_vcenter_datastores(vi_client)
2742 2774
                vc_networks[vcenter_uuid]   = retrieve_vcenter_networks(vi_client)
2743
                vc_vmachines[vcenter_uuid], vc_templates[vcenter_uuid]   = retrieve_vcenter_vms(vi_client)
2775
                vc_vmachines[vcenter_uuid], vc_templates[vcenter_uuid]  = retrieve_vcenter_vms(vi_client)
2776

  
2744 2777
                STDOUT.puts "--- #{host["TEMPLATE/VCENTER_HOST"]} \e[92mobjects have been retrieved\e[39m"
2745 2778
            end
2746 2779

  
......
2759 2792

  
2760 2793
            banner " PHASE 3 - Create OpenNebula clusters if needed", true
2761 2794
            STDOUT.puts
2795

  
2762 2796
            one_clusters = create_new_clusters(vc_clusters, hpool, cpool, one_client)
2763 2797

  
2764 2798
            dspool = OpenNebula::DatastorePool.new(one_client)
2765 2799
            rc = dspool.info
2766 2800
            raise "Error contacting OpenNebula #{rc.message}" if OpenNebula.is_error?(rc)
2767 2801

  
2768
            rc = hpool.info #Update host pool to get new attributes
2802
            rc = hpool.info # Update host pool to get new attributes
2769 2803
            raise "Error contacting OpenNebula #{rc.message}" if OpenNebula.is_error?(rc)
2770 2804

  
2771
            rc = cpool.info #Update cluster pool to get new clusters
2805
            rc = cpool.info # Update cluster pool to get new clusters
2772 2806
            raise "Error contacting OpenNebula #{rc.message}" if OpenNebula.is_error?(rc)
2773 2807

  
2774 2808
            extended_message = " - Add new attributes to datastores\n"\
2775 2809
                               " - Create SYSTEM datastores if needed\n"\
2776 2810
                               " - Assign datastores to OpenNebula Clusters"
2811

  
2777 2812
            banner " PHASE 4 - Inspect existing datatores ", true, extended_message
2813

  
2778 2814
            inspect_datastores(vc_datastores, vc_clusters, one_clusters, dspool, hpool, one_client, vcenter_ids)
2779 2815

  
2780
            rc = dspool.info #Refresh datastore pool
2816
            rc = dspool.info # Refresh datastore pool
2781 2817
            raise "Error contacting OpenNebula #{rc.message}" if OpenNebula.is_error?(rc)
2782 2818

  
2783 2819
            vnpool = OpenNebula::VirtualNetworkPool.new(one_client)

Also available in: Unified diff