Statistics
| Branch: | Tag: | Revision:

one / src / tm_mad / vcenter / clone @ 95fc9267

History | View | Annotate | Download (6.26 KB)

1
#!/usr/bin/env ruby
2

    
3
# ---------------------------------------------------------------------------- #
4
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                  #
5
#                                                                              #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may      #
7
# not use this file except in compliance with the License. You may obtain      #
8
# a copy of the License at                                                     #
9
#                                                                              #
10
# http://www.apache.org/licenses/LICENSE-2.0                                   #
11
#                                                                              #
12
# Unless required by applicable law or agreed to in writing, software          #
13
# distributed under the License is distributed on an "AS IS" BASIS,            #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     #
15
# See the License for the specific language governing permissions and          #
16
# limitations under the License.                                               #
17
# ---------------------------------------------------------------------------- #
18

    
19
# clone fe:SOURCE host:remote_system_ds/disk.i vmid dsid
20
#   - fe is the front-end hostname
21
#   - SOURCE is the path of the disk image in the form DS_BASE_PATH/disk
22
#   - host is the target host to deploy the VM
23
#   - remote_system_ds is the path for the system datastore in the host
24
#   - vmid is the id of the VM
25
#   - dsid is the target datastore (0 is the system datastore)
26

    
27
ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION)
28

    
29
if !ONE_LOCATION
30
    RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION)
31
else
32
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION)
33
end
34

    
35
$: << RUBY_LIB_LOCATION
36
$: << File.dirname(__FILE__)
37

    
38
require 'vcenter_driver'
39

    
40
src          = ARGV[0]
41
dst          = ARGV[1]
42
vm_id        = ARGV[2]
43
source_ds_id = ARGV[3]
44

    
45
check_valid src, "src"
46
check_valid dst, "dst"
47
check_valid vm_id, "vm_id"
48
check_valid source_ds_id, "source_ds_id"
49

    
50
target_ds_id = dst.split("/")[-3]
51
disk_id = dst.split(".")[-1]
52

    
53
src_path = src.split(":")[-1]
54
hostname = dst.split(":").first
55

    
56
# Get host ID
57
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
58
host_id = host['ID']
59

    
60
# Get datastores refs
61
source_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, source_ds_id)
62
source_ds_ref = source_ds['TEMPLATE/VCENTER_DS_REF']
63

    
64
target_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, target_ds_id)
65
target_ds_ref = target_ds['TEMPLATE/VCENTER_DS_REF']
66

    
67
check_valid source_ds_ref, "source_ds"
68
check_valid target_ds_ref, "target_ds"
69

    
70
# Get VM info
71
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id)
72

    
73
# calculate target path
74
target_path = VCenterDriver::FileHelper.get_img_name_from_path(src_path,
75
                                                               vm_id,
76
                                                              disk_id)
77
begin
78
    vi_client     = VCenterDriver::VIClient.new_from_host(host_id)
79

    
80
    # Find disk info
81
    disk = one_vm.retrieve_xmlelements("TEMPLATE/DISK[SOURCE=\"#{src_path}\"]").first rescue nil
82
    raise "Cannot find disk element in vm template" if !disk
83

    
84
    new_size = nil
85
    # Check if resize is needed
86
    if disk["ORIGINAL_SIZE"]
87
        original_size = disk["ORIGINAL_SIZE"].to_i
88
        new_size      = disk["SIZE"].to_i
89

    
90
        # Shrink not supported (nil). Size is in KB
91
        new_size = new_size > original_size ? new_size * 1024 : nil
92
    end
93

    
94
    unmanaged_disk = false
95

    
96
    # Unmanaged disks are those with OPENNEBULA_MANAGED=NO or volatile disks
97
    # that are created in StorageDRS clusters
98
    if (target_ds_ref.start_with?('group-') && !!disk["TYPE"] && disk["TYPE"].downcase == "fs")
99
        unmanaged_disk = true
100
    else
101
        if (disk['OPENNEBULA_MANAGED'] && disk['OPENNEBULA_MANAGED'].downcase == "no")
102
            # Let's check if the disk is really unmanaged
103

    
104
            if one_vm["TEMPLATE/CLONING_TEMPLATE_ID"]
105
                # In this case we're not cloning the disk, althought the disk
106
                # is unmanaged, the disk is treated as persistent until the
107
                # vm is terminated. That way the disk added to the new vcenter
108
                # template is the one that OpenNebula created when the OpenNebula
109
                # VM template was created
110
               unmanaged_disk = true
111
            else
112
                # What's the moref of the template linked to this VM?
113
                template_ref = one_vm["USER_TEMPLATE/VCENTER_TEMPLATE_REF"]
114

    
115
                # What's the ID of the image and the source
116
                image_id     = disk["IMAGE_ID"]
117
                one_image    = VCenterDriver::VIHelper.one_item(OpenNebula::Image, image_id)
118
                one_source   = one_image["SOURCE"]
119

    
120
                # Let's inspect the disks inside the template
121
                # if we found that the image source of the disk matches one of
122
                # the template disks backing paths, then it's really an unmanaged
123
                vc_template    = VCenterDriver::Template.new_from_ref(template_ref, vi_client)
124
                template_disks = vc_template.get_vcenter_disks
125
                found_disks    = template_disks.select { |d| d[:path_wo_ds] == one_source } rescue []
126
                unmanaged_disk = !found_disks.empty?
127
            end
128
        end
129
    end
130

    
131
    if !unmanaged_disk
132
        source_ds_vc = VCenterDriver::Datastore.new_from_ref(source_ds_ref, vi_client)
133

    
134
        if source_ds_ref == target_ds_ref
135
            target_ds_vc = source_ds_vc
136
        else
137
            target_ds_vc = VCenterDriver::Storage.new_from_ref(target_ds_ref, vi_client)
138
        end
139

    
140
        target_ds_name_vc = target_ds_vc['name']
141

    
142
        if target_ds_ref.start_with?('group-')
143
            raise "Non persistent images management is not supported for StorageDRS datastores"
144
        end
145

    
146
        source_ds_vc.copy_virtual_disk(src_path, target_ds_vc, target_path, new_size)
147
    end
148

    
149
rescue Exception => e
150
    message = "Error clone virtual disk #{src_path} in "\
151
              "datastore #{target_ds_name_vc}. "\
152
              "Reason: #{e.message}\n#{e.backtrace}"
153
    STDERR.puts error_message(message)
154
    exit -1
155
ensure
156
    vi_client.close_connection if vi_client
157
end