Class: VCenterDriver::VmImporter

Inherits:
VcImporter show all
Defined in:
lib/vm_template.rb

Overview

Class VmImporter

Instance Attribute Summary

Attributes inherited from VcImporter

#list

Instance Method Summary collapse

Methods inherited from VcImporter

#get_indexes, import_clusters, new_child, #one_str, #output, #process_import, register_hooks, #retrieve_resources, sanitize, #stdout

Constructor Details

#initialize(one_client, vi_client) ⇒ VmImporter

Returns a new instance of VmImporter.



1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
# File 'lib/vm_template.rb', line 1754

def initialize(one_client, vi_client)
    super(one_client, vi_client)
    @one_class = OpenNebula::Template

    @defaults = {
        :linked_clone => '0',
        :copy => '0',
        :name => '',
        :folder => '',
        :resourcepool => [],
        :type => ''
    }
end

Instance Method Details

#attrObject



1957
1958
1959
# File 'lib/vm_template.rb', line 1957

def attr
    'TEMPLATE/VCENTER_TEMPLATE_REF'
end

#get_list(_args = {}) ⇒ Object



1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
# File 'lib/vm_template.rb', line 1768

def get_list(_args = {})
    dc_folder = VCenterDriver::DatacenterFolder.new(@vi_client)

    # Get OpenNebula's templates pool
    tpool =
        VCenterDriver::VIHelper
        .one_pool(
            OpenNebula::TemplatePool,
            false
        )
    if tpool.respond_to?(:message)
        raise "Could not get OpenNebula TemplatePool: #{tpool.message}"
    end

    @list = dc_folder.get_unimported_templates(@vi_client, tpool)
end

#import(selected) ⇒ Object



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
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
# File 'lib/vm_template.rb', line 1805

def import(selected)
    opts = @info[selected[:ref]][:opts]
    working_template = selected

    vcenter = selected[:vcenter]
    vc_uuid = selected[:vcenter_instance_uuid]
    dc      = selected[:dc_name]

    linked_clone     = opts[:linked_clone] == '1'
    copy             = opts[:copy] == '1'
    deploy_in_folder = !opts[:folder].empty?

    res = { :id => [], :name => selected[:name] }
    dpool, ipool, npool, hpool = create_pools

    template =
        VCenterDriver::Template
        .new_from_ref(
            selected[:vcenter_ref],
            @vi_client
        )
    # Linked clones and copy preparation
    if linked_clone
        # reached this point we need to delete
        # the template if something go wrong
        if copy
            error, template_copy_ref =
                selected[:template]
                .create_template_copy(
                    opts[:name]
                )
            unless template_copy_ref
                raise 'There is a problem creating creating' \
                      "your copy: #{error}"
            end

            template =
                VCenterDriver::Template
                .new_from_ref(
                    template_copy_ref,
                    @vi_client
                )
            @rollback <<
                Raction
                .new(
                    template,
                    :delete_template
                )

            one_template =
                VCenterDriver::Template
                .get_xml_template(
                    template,
                    vc_uuid,
                    @vi_client,
                    dc
                )
            unless one_template
                raise 'There is a problem obtaining info '\
                      "from your template's copy"
            end

            working_template = one_template
        end

        lc_error, use_lc = template.create_delta_disks
        if lc_error
            raise 'Something was wront with create \
            delta disk operation'
        end

        if use_lc
            working_template[:one] <<
                "\nVCENTER_LINKED_CLONES=\"YES\"\n"
        end
    end

    if deploy_in_folder
        working_template[:one] <<
            "VCENTER_VM_FOLDER=\"#{opts[:folder]}\"\n"
    end

    working_template[:one] <<
        "VCENTER_TEMPLATE_NAME=\"#{selected[:name]}\"\n"

    create(working_template[:one]) do |one_object, id|
        res[:id] << id

        type = { :object => 'template', :id => id }
        error, template_disks, allocated_images =
            template
            .import_vcenter_disks(
                vc_uuid,
                dpool,
                ipool,
                type
            )

        if allocated_images
            # rollback stack
            allocated_images.reverse.each do |i|
                @rollback.unshift(Raction.new(i, :delete))
            end
        end
        raise error unless error.empty?

        working_template[:one] << template_disks

        if template_copy_ref
            template_moref = template_copy_ref
        else
            template_moref = selected[:vcenter_ref]
        end

        opts_nics = {
            :vi_client => @vi_client,
            :vc_uuid => vc_uuid,
            :npool => npool,
            :hpool => hpool,
            :vcenter => vcenter,
            :template_moref => template_moref,
            :vm_object => nil
        }

        error, template_nics, _ar_ids, allocated_nets =
            template
            .import_vcenter_nics(
                opts_nics,
                id,
                dc
            )

        if allocated_nets
            # rollback stack
            allocated_nets.reverse.each do |n|
                @rollback.unshift(Raction.new(n, :delete))
            end
        end
        raise error unless error.empty?

        working_template[:one] << template_nics
        working_template[:one] << rp_opts(
            opts[:type],
            opts[:resourcepool]
        )

        one_object.update(working_template[:one])
    end

    res
end

#rp_opts(type, rps) ⇒ Object



1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
# File 'lib/vm_template.rb', line 1785

def rp_opts(type, rps)
    str = ''

    return str if (type == 'default') || rps.empty?

    if type == 'fixed'
        str << "VCENTER_RESOURCE_POOL=\"#{rps}\"\n"
    else
        default = rps.first
        rps_str = rps.join(',')

        str << 'USER_INPUTS=['
        str << "VCENTER_RESOURCE_POOL=\"M|list|resource \
        pool list|#{rps_str}|#{default}\""
        str << ']'
    end

    str
end