Revision 1427e3e9

View differences:

src/tm_mad/vcenter/mvds
1
../common/dummy.sh
src/tm_mad/vcenter/mvds
1
#!/usr/bin/env ruby
2

  
3
# ---------------------------------------------------------------------------- #
4
# Copyright 2002-2016, 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
# mvds host:remote_system_ds/disk.i fe:SOURCE 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_driver2'
39

  
40
################################################################################
41

  
42
def check_valid(parameter, label)
43
    if parameter.nil? || parameter.empty?
44
        STDERR.puts "Not enough information to delete the image. " +
45
                    "Missing '#{label}'."
46
        exit -1
47
    end
48
end
49

  
50
################################################################################
51

  
52
path = ARGV[0]
53
vmid = ARGV[2]
54

  
55
check_valid path, "path"
56
check_valid vmid, "vmid"
57

  
58
hostname, img_path = path.split(":")
59

  
60
# Get host ID
61
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
62
host_id = host['ID']
63

  
64
# Get VM
65
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vmid)
66
vm_ref = one_vm['DEPLOY_ID']
67

  
68
# Get image path
69
disk_id = img_path.split(".")[-1]
70

  
71
disk = one_vm.retrieve_xmlelements("TEMPLATE/DISK[DISK_ID=#{disk_id}]").first
72
img_path = VCenterDriver::FileHelper.get_img_name(disk, vmid)
73

  
74
begin
75
    vi_client = VCenterDriver::VIClient.new_from_host(host_id)
76
    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
77

  
78
    vm.detach_disk(disk)
79
rescue Exception => e
80
    STDERR.puts "Error detaching virtual disk #{img_path} from vm #{vmid}."\
81
                " Reason: #{e.message}\n#{e.backtrace}"
82
    exit -1
83
ensure
84
    vi_client.close_connection
85
end

Also available in: Unified diff