Revision 013979a6 src/datastore_mad/remotes/vcenter/clone

View differences:

src/datastore_mad/remotes/vcenter/clone
1 1
#!/usr/bin/env ruby
2 2

  
3 3
# ---------------------------------------------------------------------------- #
4
# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems                  #
4
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                  #
5 5
#                                                                              #
6 6
# Licensed under the Apache License, Version 2.0 (the "License"); you may      #
7 7
# not use this file except in compliance with the License. You may obtain      #
......
16 16
# limitations under the License.                                               #
17 17
# ---------------------------------------------------------------------------- #
18 18

  
19
###############################################################################
20
# This script is used retrieve the file size of a disk
21
###############################################################################
22

  
23 19
ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION)
24 20

  
25 21
if !ONE_LOCATION
......
31 27
$: << RUBY_LIB_LOCATION
32 28
$: << File.dirname(__FILE__)
33 29

  
34
require 'opennebula'
35
require 'vcenter_driver'
30
require 'vcenter_driver2'
36 31
require 'digest'
37 32

  
38
drv_action_enc = ARGV[0]
39
id             = ARGV[1]
40

  
41
drv_action = OpenNebula::XMLElement.new
42
drv_action.initialize_xml(Base64.decode64(drv_action_enc), 'DS_DRIVER_ACTION_DATA')
43

  
44
target_ds_name = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/NAME"]
45
hostname       = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_CLUSTER"]
46
img_path       = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/PATH"]
47
src_img_id     = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/CLONING_ID"]
33
################################################################################
48 34

  
49
if target_ds_name.nil? || hostname.nil? || img_path.nil?
50
    STDERR.puts "Not enough information to clone the image, missing datastore"\
51
                " name or vcenter cluster name or image path."
52
    exit -1
35
def check_valid(parameter, label)
36
    if parameter.nil? || parameter.empty?
37
        STDERR.puts "Not enough information to clone the image. " +
38
                    "Missing '#{label}'."
39
        exit -1
40
    end
53 41
end
54 42

  
55
# Get source image
56
client = OpenNebula::Client.new
43
################################################################################
57 44

  
58
src_img = OpenNebula::Image.new_with_id(src_img_id, client)
59

  
60
rc = src_img.info
61
if OpenNebula.is_error?(rc)
62
    STDERR.puts rc.message
63
    exit -1
64
end
45
drv_action_enc = ARGV[0]
46
id             = ARGV[1]
65 47

  
66
src_ds_id = src_img['DATASTORE_ID']
48
drv_action = OpenNebula::XMLElement.new
49
drv_action.initialize_xml(Base64.decode64(drv_action_enc), 'DS_DRIVER_ACTION_DATA')
67 50

  
68
# Get the source datastore
69
src_ds = OpenNebula::Datastore.new_with_id(src_ds_id, client)
51
target_ds_ref = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_DS_REF"]
52
host_id       = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_CLUSTER"]
53
src_path      = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/PATH"]
54
src_img_id    = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/CLONING_ID"]
70 55

  
71
rc = src_ds.info
72
if OpenNebula.is_error?(rc)
73
    STDERR.puts rc.message
74
    exit -1
75
end
56
check_valid src_img_id, "cloning id"
57
check_valid host_id, "vcenter cluster"
58
check_valid src_path, "image path"
59
check_valid target_ds_ref, "target ds ref"
76 60

  
77
src_ds_name = src_ds["TEMPLATE/VCENTER_NAME"]
61
# Get source ds_ref
62
source_img = VCenterDriver::VIHelper.one_item(OpenNebula::Image, src_img_id)
63
source_ds_id = source_img['DATASTORE_ID']
64
source_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, source_ds_id)
65
source_ds_ref = source_ds['TEMPLATE/VCENTER_DS_REF']
78 66

  
79 67
# Generate target path
80
str_for_target_path = Time.now.to_s + id.to_s
81
target_path = Digest::MD5.hexdigest(str_for_target_path) + ".vmdk"
68
target_path = Digest::MD5.hexdigest(Time.now.to_s + id.to_s) + ".vmdk"
82 69

  
83 70
begin
84
    host_id      = VCenterDriver::VIClient.translate_hostname(hostname)
85
    vi_client    = VCenterDriver::VIClient.new host_id
71
    vi_client = VCenterDriver::VIClient.new_from_host(host_id)
72
    ds = VCenterDriver::Datastore.new_from_ref(source_ds_ref, vi_client)
86 73

  
87
    puts vi_client.copy_virtual_disk(img_path, src_ds_name, target_path, target_ds_name)
74
    target_ds = VCenterDriver::Datastore.new_from_ref(target_ds_ref, vi_client)
75
    target_ds_name = target_ds['name']
76

  
77
    puts ds.copy_virtual_disk(src_path, target_ds_name, target_path)
88 78
rescue Exception => e
89
    STDERR.puts "Error cloning img #{img_path} size. Reason: #{e.message}"
79
    STDERR.puts "Error cloning img #{src_path}. Reason: #{e.message}"
90 80
    exit -1
91
end
81
end

Also available in: Unified diff