Revision 95fc9267 src/tm_mad/vcenter/delete

View differences:

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}"

Also available in: Unified diff