Revision 50ab7527

View differences:

src/tm_mad/vcenter/cpds
55 55
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
56 56
host_id = host['ID']
57 57

  
58
# Get OpenNebula VM
58
# Get OpenNebula VM (state, disks and deploy_id)
59 59
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vmid)
60 60
disks = one_vm.retrieve_xmlelements("TEMPLATE/DISK[DISK_ID=#{disk_id}]")
61 61
src_path = VCenterDriver::FileHelper.get_img_name(disks.first, vmid)
62
vm_ref = one_vm['DEPLOY_ID']
63

  
62 64
if one_vm['LCM_STATE'] == 26 #ACTIVE / HOTPLUG_SAVEAS
63 65
    STDERR.puts "'disk-saveas' operation is not supported for running VMs."
64 66
    exit 1
......
74 76
begin
75 77
    vi_client = VCenterDriver::VIClient.new_from_host(host_id)
76 78

  
79
    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
80

  
81
    if vm.has_snapshots?
82
        STDERR.puts "'disk-saveas' operation is not supported for VMs with system snapshots."
83
        exit 1
84
    end
85

  
77 86
    source_ds_vc = VCenterDriver::Datastore.new_from_ref(source_ds_ref, vi_client)
78 87

  
79 88
    target_ds_vc = VCenterDriver::Datastore.new_from_ref(target_ds_ref, vi_client)
src/tm_mad/vcenter/delete
79 79
    img_path = VCenterDriver::FileHelper.get_img_name(disk, vmid)
80 80

  
81 81
    begin
82
        # TODO: if the deploy has failed, the disks may exist, but the vm may
83
        # not exist...
84
        vm.one_item = one_vm
82
        if !vm.has_snapshots?
83
            # TODO: if the deploy has failed, the disks may exist, but the vm may
84
            # not exist...
85
            vm.one_item = one_vm
85 86

  
86
        # detach the disk
87
        vm.detach_disk(disk)
87
            # detach the disk
88
            vm.detach_disk(disk)
89

  
90
            # delete the disk
91
            ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client)
92
            ds.delete_virtual_disk(img_path)
93
        end
88 94

  
89
        # delete the disk
90
        ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client)
91
        ds.delete_virtual_disk(img_path)
92 95
    rescue Exception => e
93 96
        STDERR.puts "Error delete virtual disk #{img_path} in datastore #{dsid}."\
94 97
                    " Reason: #{e.message}\n#{e.backtrace}"
......
100 103
    # Remove the VM
101 104
    begin
102 105
        # All OpenNebula managed disks have been detached. The VM may have still
103
        # disks that belong to the template (VCENTER_MANAGED disks). These disks
106
        # disks that belong to the template (OPENNEBULA_MANAGED disks). These disks
104 107
        # will be deleted with the destroy operation. If the user wants to
105 108
        # save them to a VM, it can be done using the disk-saveas operation.
106 109
        vm.destroy
src/tm_mad/vcenter/mvds
65 65

  
66 66
    vm.one_item = one_vm
67 67

  
68
    vm.detach_disk(disk)
68
    vm.detach_disk(disk) if !vm.has_snapshots?
69 69
rescue Exception => e
70 70
    STDERR.puts "Error detaching virtual disk #{img_path} from vm #{vmid}."\
71 71
                " Reason: #{e.message}\n#{e.backtrace}"
src/vmm_mad/remotes/vcenter/detach_disk
1
../common/dummy.sh
src/vmm_mad/remotes/vcenter/detach_disk
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
ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION)
20

  
21
if !ONE_LOCATION
22
    RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION)
23
else
24
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION)
25
end
26

  
27
$: << RUBY_LIB_LOCATION
28
$: << File.dirname(__FILE__)
29

  
30
require 'vcenter_driver'
31

  
32
vm_ref           = ARGV[0]
33
vc_cluster_name  = ARGV[-1]
34

  
35
drv_action = OpenNebula::XMLElement.new
36
drv_action.initialize_xml(Base64.decode64(STDIN.read), 'VMM_DRIVER_ACTION_DATA')
37

  
38
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name)
39
host_id = host['ID']
40

  
41
begin
42
    vi_client = VCenterDriver::VIClient.new_from_host(host_id)
43

  
44
    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
45

  
46
    raise "vCenter doesn't allow to remove a virtual disk if it's part of a "\
47
          "snapshot of the virtual machine." if vm.has_snapshots?
48

  
49
rescue Exception => e
50
    STDERR.puts "Detach DISK for VM #{vm_ref} on vCenter cluster #{vc_cluster_name} "\
51
                "failed due to \"#{e.message}\"\n#{e.backtrace}"
52
    exit -1
53
ensure
54
    vi_client.close_connection
55
end
src/vmm_mad/remotes/vcenter/snapshot_create
46 46

  
47 47
    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
48 48

  
49
    vm.create_snapshot(snap_id, snap_name)
49
    one_vm = drv_action.retrieve_xmlelements("VM").first
50

  
51
    vm.one_item = one_vm
52

  
53
    persistent_disks = one_vm.retrieve_xmlelements("TEMPLATE/DISK[PERSISTENT=YES]")
54

  
55
    if !persistent_disks.empty?
56
        STDERR.puts "Snapshot of VM #{vm_ref} on vCenter cluster "\
57
                    "#{vc_cluster_name} cannot be performed if "\
58
                    "it contains persistent disks"
59
        exit 1
60
    end
61

  
62
    puts vm.create_snapshot(snap_id, snap_name)
50 63

  
51 64
rescue Exception => e
52 65
    STDERR.puts "Snapshot of VM #{vm_ref} on vCenter cluster "\
53 66
                "#{vc_cluster_name} failed due to "\
54 67
                "\"#{e.message}\"\n#{e.backtrace}"
55
    exit(-1)
68
    exit -1
56 69
ensure
57 70
    vi_client.close_connection
58
end
71
end

Also available in: Unified diff