Revision 95fc9267

View differences:

src/tm_mad/vcenter/clone
91 91
        new_size = new_size > original_size ? new_size * 1024 : nil
92 92
    end
93 93

  
94
    unmanaged_disk = false
95

  
94 96
    # Unmanaged disks are those with OPENNEBULA_MANAGED=NO or volatile disks
95 97
    # that are created in StorageDRS clusters
96
    unmanaged_disk = (!!disk['OPENNEBULA_MANAGED'] && disk['OPENNEBULA_MANAGED'].downcase == "no") ||
97
                     (target_ds_ref.start_with?('group-') && !!disk["TYPE"] && disk["TYPE"].downcase == "fs")
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
98 130

  
99 131
    if !unmanaged_disk
100 132
        source_ds_vc = VCenterDriver::Datastore.new_from_ref(source_ds_ref, vi_client)
src/tm_mad/vcenter/delete
94 94

  
95 95
    begin
96 96
        if !vm.has_snapshots?
97
            # TODO: if the deploy has failed, the disks may exist, but the vm may
98
            # not exist...
97

  
99 98
            vm.one_item = one_vm
100 99

  
101
            # detach the disk or cdrom
102
            ds_ref, img_path = vm.detach_disk(disk)
100
            # Don't do a detach if unmanaged disks and state is terminate (EPILOG)
101
            unless one_vm["LCM_STATE"].to_i == 11 && disk["OPENNEBULA_MANAGED"] && disk["OPENNEBULA_MANAGED"].upcase == "NO"
102

  
103
                # Detach disk
104
                ds_ref, img_path = vm.detach_disk(disk)
103 105

  
104
            # If disk was already detached we have no way to remove it
105
            if ds_ref && img_path && !img_path.empty?
106
                ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client)
106
                # If disk was already detached we have no way to remove it
107
                if ds_ref && img_path && !img_path.empty?
108
                    ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client)
107 109

  
108
                # delete the disk if it's not a CDROM (CLONE=NO)
109
                if disk["CLONE"].nil? || disk["CLONE"] == "YES"
110
                    # delete the disk if it's not a CDROM (CLONE=NO)
111
                    if disk["CLONE"].nil? || disk["CLONE"] == "YES"
110 112

  
111
                    search_params = ds.get_search_params(ds['name'],
112
                                                         File.dirname(img_path),
113
                                                         File.basename(img_path))
113
                        search_params = ds.get_search_params(ds['name'],
114
                                                            File.dirname(img_path),
115
                                                            File.basename(img_path))
114 116

  
115
                    # Perform search task and return results
116
                    begin
117
                        search_task = ds['browser'].SearchDatastoreSubFolders_Task(search_params)
118
                        search_task.wait_for_completion
117
                        # Perform search task and return results
118
                        begin
119
                            search_task = ds['browser'].SearchDatastoreSubFolders_Task(search_params)
120
                            search_task.wait_for_completion
119 121

  
120
                        ds.delete_virtual_disk(img_path)
121
                        img_dir = File.dirname(img_path)
122
                        ds.rm_directory(img_dir) if ds.dir_empty?(img_dir)
122
                            ds.delete_virtual_disk(img_path)
123
                            img_dir = File.dirname(img_path)
124
                            ds.rm_directory(img_dir) if ds.dir_empty?(img_dir)
123 125

  
124
                    rescue Exception => e
125
                        if !e.message.start_with?('FileNotFound')
126
                            raise e.message # Ignore FileNotFound
126
                        rescue Exception => e
127
                            if !e.message.start_with?('FileNotFound')
128
                                raise e.message # Ignore FileNotFound
129
                            end
127 130
                        end
128 131
                    end
129 132
                end
......
142 145
    # Remove the VM
143 146
    begin
144 147

  
145
        # All OpenNebula managed disks have been detached. The VM may have still
146
        # disks that belong to the template (OPENNEBULA_MANAGED disks). These disks
147
        # will be deleted with the destroy operation. If the user wants to
148
        # save them to a VM, it can be done using the disk-saveas operation.
148
        # All OpenNebula managed disks have been detached unless it has snapshots.
149
        # The VM may have still unmanaged disks that belong to the template
150
        # (OPENNEBULA_MANAGED disks). These disks will be deleted with the destroy
151
        # operation. If the user wants to save them, it can be done using the
152
        # disk-saveas operation.
149 153

  
150 154
        vm.poweroff_hard if vm.is_powered_on?
151 155

  
......
153 157
        # will try to detach persistent disks once we have removed all snapshots
154 158
        # that way they won't be removed. If the vm has been marked as template
155 159
        # persistent disks shouldn't be detached
156
        if vm.has_snapshots? && !vm.instantiated_as_persistent?
157
            vm.remove_all_snapshots
158
            disks = one_vm.retrieve_xmlelements("TEMPLATE/DISK[PERSISTENT=\"YES\"]")
160
        vm.remove_all_snapshots if vm.has_snapshots?
161

  
162
        disks = one_vm.retrieve_xmlelements("TEMPLATE/DISK")
163
        if vm.has_snapshots? || vm.instantiated_as_persistent?
159 164
            disks.each do |d|
160
                vm.detach_disk(d)
165
                if d["PERSISTENT"] == "YES"
166
                    vm.detach_disk(d)
167
                else
168
                    # Remove any disk that is not unmanaged from the VM if
169
                    # instantiate as persistent was set
170
                    if vm.instantiated_as_persistent? &&
171
                    (!d["OPENNEBULA_MANAGED"] || d["OPENNEBULA_MANAGED"].upcase != "NO")
172
                        vm.detach_disk(d)
173
                    end
174
                end
161 175
            end
162 176
        end
163 177

  
164 178
        # If the VM was instantiated to persistent keep the VM
165 179
        if vm.instantiated_as_persistent?
166 180

  
167
            #Convert VM to template in vCenter
181
            # Remove the MAC addresses so they cannot be in conflict
182
            # with OpenNebula assigned mac addresses.
183

  
184
            # We detach all nics from the VM
185
            vm.detach_all_nics
186

  
187
            # We attach new NICs where the MAC address is assigned by vCenter
188
            nic_specs = []
189
            nics = one_vm.retrieve_xmlelements("TEMPLATE/NIC")
190
            nics.each do |nic|
191
                if (nic["OPENNEBULA_MANAGED"] && nic["OPENNEBULA_MANAGED"].upcase == "NO")
192
                    nic_specs << vm.calculate_add_nic_spec_autogenerate_mac(nic)
193
                end
194
            end
195

  
196
            # Reconfigure VM to add unmanaged nics
197
            spec_hash = {}
198
            spec_hash[:deviceChange] = nic_specs
199
            spec = RbVmomi::VIM.VirtualMachineConfigSpec(spec_hash)
200
            vm.item.ReconfigVM_Task(:spec => spec).wait_for_completion
201

  
202
            # Convert VM to template in vCenter
168 203
            vm.mark_as_template
169 204

  
170
            # Create new Opennebula template and set VCENTER_TEMPLATE_REF
205
            # Edit the Opennebula template
171 206
            one_client = OpenNebula::Client.new
172 207
            template_id = vm.one_item['TEMPLATE/TEMPLATE_ID']
173 208
            new_template = OpenNebula::Template.new_with_id(template_id, one_client)
174 209
            new_template.info
210

  
211
            # Update the template reference
175 212
            new_template.update("VCENTER_TEMPLATE_REF= #{vm.item._ref}", true)
213

  
214
        else
215
            # Destroy the VM and remaining disks
216
            vm.destroy
176 217
        end
177 218

  
178
        # Destroy the VM unless the instantiate as persistent is used
179
        vm.destroy if !vm.instantiated_as_persistent?
180 219
    rescue Exception => e
181 220
        message = "Error unregistering vm #{vmid} (#{vm_ref})."\
182 221
                  " Reason: #{e.message}\n#{e.backtrace}"
src/vmm_mad/remotes/lib/vcenter_driver/file_helper.rb
5 5

  
6 6
class FileHelper
7 7

  
8
    def self.get_img_name(disk, vm_id, vm_name)
8
    def self.get_img_name(disk, vm_id, vm_name, instantiate_as_persistent=false)
9 9
        if disk["PERSISTENT"] == "YES" || disk["TYPE"] == "CDROM"
10 10
            return disk["SOURCE"]
11 11
        else
12 12
            disk_id  = disk["DISK_ID"]
13 13
            if disk["SOURCE"]
14
                image_name = disk["SOURCE"].split(".").first
15
                return "#{image_name}-#{vm_id}-#{disk_id}.vmdk"
14
                if instantiate_as_persistent &&
15
                   disk["OPENNEBULA_MANAGED"] &&
16
                   disk["OPENNEBULA_MANAGED"].upcase == "NO"
17
                    return disk["SOURCE"] # Treat this disk as if was persistent
18
                else
19
                    image_name = disk["SOURCE"].split(".").first
20
                    return "#{image_name}-#{vm_id}-#{disk_id}.vmdk"
21
                end
16 22
            else
17 23
                ds_volatile_dir  = disk["VCENTER_DS_VOLATILE_DIR"] || "one-volatile"
18 24
                return "#{ds_volatile_dir}/#{vm_id}/one-#{vm_id}-#{disk_id}.vmdk"
src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb
1878 1878
                next
1879 1879
            end
1880 1880

  
1881
            img_name  = VCenterDriver::FileHelper.get_img_name(disk, one_item['ID'], self['name'])
1881
            img_name  = VCenterDriver::FileHelper.get_img_name(disk, one_item['ID'], self['name'], instantiated_as_persistent?)
1882 1882
            onevm_disks_vector << "#{img_name}"
1883 1883
        end
1884 1884

  
......
2120 2120
        return ds_ref, img_path
2121 2121
    end
2122 2122

  
2123
        # Get vcenter device representing DISK object (hotplug)
2123
    # Get vcenter device representing DISK object (hotplug)
2124 2124
    def disk_attached_to_vm(disk, unmanaged_keys, vc_disks)
2125 2125

  
2126 2126
        img_name = ""
......
2136 2136
            end
2137 2137

  
2138 2138
            # Alright let's see if we can find other devices only with the expected image name
2139
            img_name  = VCenterDriver::FileHelper.get_img_name(disk, one_item['ID'], self['name'])
2139
            img_name  = VCenterDriver::FileHelper.get_img_name(disk, one_item['ID'], self['name'], instantiated_as_persistent?)
2140 2140
            if d[:path_wo_ds] == "#{img_name}"
2141 2141
                device_found = d
2142 2142
                break
......
2147 2147
    end
2148 2148

  
2149 2149
    def calculate_add_disk_spec(disk, position=0)
2150
        img_name = VCenterDriver::FileHelper.get_img_name(disk, one_item['ID'], self['name'])
2150
        img_name = VCenterDriver::FileHelper.get_img_name(disk, one_item['ID'], self['name'],instantiated_as_persistent?)
2151 2151
        type     = disk["TYPE"]
2152 2152
        size_kb  = disk["SIZE"].to_i * 1024
2153 2153

  

Also available in: Unified diff