Revision 05ed37c9

View differences:

src/oca/ruby/opennebula/virtual_machine.rb
702 702
            self['DEPLOY_ID']
703 703
        end
704 704

  
705
        # Returns the deploy_id of the VirtualMachine (numeric value)
706
        def keep_disks?
707
            !self['USER_TEMPLATE/KEEP_DISKS_ON_DONE'].nil? &&
708
                self['USER_TEMPLATE/KEEP_DISKS_ON_DONE'].downcase=="yes"
709
        end
710

  
705 711
        # Clones the VM's source Template, replacing the disks with live snapshots
706 712
        # of the current disks. The VM capacity and NICs are also preserved
707 713
        #
src/vmm_mad/remotes/vcenter/cancel
36 36
vm = OpenNebula::VirtualMachine.new_with_id(vm_id, OpenNebula::Client.new)
37 37
vm.info
38 38

  
39
lcm_state = vm.lcm_state_str
39
lcm_state   = vm.lcm_state_str
40
keep_disks  = vm.keep_disks?
40 41

  
41 42
begin
42
    VCenterDriver::VCenterVm.cancel(deploy_id, host, lcm_state)
43
    VCenterDriver::VCenterVm.cancel(deploy_id, host, lcm_state, keep_disks)
43 44
rescue Exception => e
44 45
    STDERR.puts "Cancel of VM #{deploy_id} on host #{host} failed " +
45 46
                "due to \"#{e.message}\""
src/vmm_mad/remotes/vcenter/shutdown
37 37
vm = OpenNebula::VirtualMachine.new_with_id(vm_id, OpenNebula::Client.new)
38 38
vm.info
39 39

  
40
lcm_state     = vm.lcm_state_str
40
lcm_state   = vm.lcm_state_str
41
keep_disks  = vm.keep_disks?
41 42

  
42 43
begin
43
    VCenterDriver::VCenterVm.shutdown(deploy_id, host, lcm_state)
44
    VCenterDriver::VCenterVm.shutdown(deploy_id, host, lcm_state, keep_disks)
44 45
rescue Exception => e
45 46
    STDERR.puts "Shutdown of VM #{deploy_id} on host #{host} failed " +
46 47
                "due to \"#{e.message}\""
src/vmm_mad/remotes/vcenter/vcenter_driver.rb
774 774
    #  @param deploy_id vcenter identifier of the VM
775 775
    #  @param hostname name of the host (equals the vCenter cluster)
776 776
    ############################################################################
777
    def self.cancel(deploy_id, hostname, lcm_state)
777
    def self.cancel(deploy_id, hostname, lcm_state, keep_disks)
778 778
        case lcm_state
779 779
            when "SHUTDOWN_POWEROFF", "SHUTDOWN_UNDEPLOY"
780 780
                shutdown(deploy_id, hostname, lcm_state)
......
789 789
                    end
790 790
                rescue
791 791
                end
792
                detach_all_disks(vm) if keep_disks
792 793
                vm.Destroy_Task.wait_for_completion
793 794
        end
794 795
    end
......
857 858
    #  @param deploy_id vcenter identifier of the VM
858 859
    #  @param hostname name of the host (equals the vCenter cluster)
859 860
    ############################################################################
860
    def self.shutdown(deploy_id, hostname, lcm_state)
861
    def self.shutdown(deploy_id, hostname, lcm_state, keep_disks)
861 862
        hid         = VIClient::translate_hostname(hostname)
862 863
        connection  = VIClient.new(hid)
863 864

  
......
870 871
                rescue
871 872
                end
872 873
                vm.PowerOffVM_Task.wait_for_completion
874
                detach_all_disks(vm) if keep_disks
873 875
                vm.Destroy_Task.wait_for_completion
874 876
            when "SHUTDOWN_POWEROFF", "SHUTDOWN_UNDEPLOY"
875 877
                begin
......
1203 1205
    end
1204 1206

  
1205 1207
    ########################################################################
1208
    #  Checks if a RbVmomi::VIM::VirtualDevice is a disk
1209
    ########################################################################
1210
    def self.is_disk?(device)
1211
        !device.class.ancestors.index(RbVmomi::VIM::VirtualDisk).nil?
1212
    end
1213

  
1214
    ########################################################################
1206 1215
    # Returns the spec to reconfig a VM and add a NIC
1207 1216
    ########################################################################
1208 1217
    def self.calculate_addnic_spec(vm, mac, bridge, model)
......
1411 1420

  
1412 1421
        return vm_uuid
1413 1422
    end
1423

  
1424
    ############################################################################
1425
    # Detach all disks from a VM
1426
    ############################################################################
1427
    def self.detach_all_disks(vm)
1428
        disks  = vm.config.hardware.device.select { |d| is_disk?(d) }
1429

  
1430
        return if disks.nil?
1431

  
1432
        spec = { :deviceChange => [] }
1433

  
1434
        disks.each{|disk|
1435
            spec[:deviceChange] <<  {
1436
                :operation => :remove,
1437
                :device => disk
1438
            }
1439
        }
1440

  
1441
        vm.ReconfigVM_Task(:spec => spec).wait_for_completion
1442
    end
1414 1443
end
1415 1444
end

Also available in: Unified diff