1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-08 20:58:17 +03:00

feature #4186: Add export functionality from vCenter DS to Marketplace

This commit is contained in:
Tino Vazquez 2016-02-11 18:28:45 +01:00
parent 006ba41c62
commit 8c07d9d7b9
8 changed files with 197 additions and 27 deletions

View File

@ -35,6 +35,19 @@ class RbVmomi::VIM::Datastore
fail "download failed" unless $?.success?
end
# Download a file from this datastore.
# @param remote_path [String] Source path on the datastore.
# @param local_path [String] Destination path on the local machine.
# @return [void]
def download_to_stdout remote_path
url = "http#{_connection.http.use_ssl? ? 's' : ''}://#{_connection.http.address}:#{_connection.http.port}#{mkuripath(remote_path)}"
pid = spawn CURLBIN, "-k", '--noproxy', '*', '-f',
"-b", _connection.cookie,
url
Process.waitpid(pid, 0)
fail "download failed" unless $?.success?
end
# Upload a file to this datastore.
# @param remote_path [String] Destination path on the datastore.
# @param local_path [String] Source path on the local machine.

View File

@ -116,9 +116,9 @@ else
fi
cat <<EOT
<MD5>\$CHECKSUM</MD5>
<SIZE>$SIZE</SIZE>
<FORMAT>${FORMAT}</FORMAT>
<MD5><![CDATA[\$CHECKSUM]]></MD5>
<SIZE><![CDATA[$SIZE]]></SIZE>
<FORMAT><![CDATA[${FORMAT}]]></FORMAT>
EOT
EOF
)
@ -133,7 +133,7 @@ fi
cat <<EOF
<IMPORT_INFO>
<IMPORT_SOURCE>$IMPORT_SOURCE</IMPORT_SOURCE>
<IMPORT_SOURCE><![CDATA[$IMPORT_SOURCE]]></IMPORT_SOURCE>
$INFO
<DISPOSE>NO</DISPOSE>
</IMPORT_INFO>"

View File

@ -18,8 +18,10 @@
if [ -z "${ONE_LOCATION}" ]; then
LIB_LOCATION=/usr/lib/one
VAR_LOCATION=/var/lib/one
else
LIB_LOCATION=$ONE_LOCATION/lib
VAR_LOCATION=$ONE_LOCATION/var
fi
. $LIB_LOCATION/sh/scripts_common.sh
@ -295,6 +297,9 @@ s3://*)
rbd://*)
command="$(get_rbd_cmd $FROM)"
;;
vcenter://*)
command="$VAR_LOCATION/remotes/datastore/vcenter_downloader.rb \"$FROM\""
;;
*)
if [ ! -r $FROM ]; then
echo "Cannot read from $FROM" >&2

View File

@ -61,9 +61,9 @@ CHECKSUM=\$(${MD5SUM} ${SRC} | cut -f1 -d' ')
SIZE=\$(${DU} -Lm ${SRC} | cut -f1)
FORMAT=\$(${QEMU_IMG} info ${SRC} 2>/dev/null | grep -Po '(?<=file format: )\w+')
echo "<MD5>\$CHECKSUM</MD5>"
echo "<SIZE>\$SIZE</SIZE>"
echo "<FORMAT>\${FORMAT-"unknown"}</FORMAT>"
echo "<MD5><![CDATA[\$CHECKSUM]]></MD5>"
echo "<SIZE><![CDATA[\$SIZE]]></SIZE>"
echo "<FORMAT><![CDATA[\${FORMAT-"unknown"}]]></FORMAT>"
EOF
)
@ -83,7 +83,7 @@ if [ "$INFO_STATUS" != "0" ]; then
exit $INFO_STATUS
fi
echo "<IMPORT_INFO><IMPORT_SOURCE>$SRC</IMPORT_SOURCE> \
echo "<IMPORT_INFO><IMPORT_SOURCE><![CDATA[$SRC]]></IMPORT_SOURCE> \
$INFO \
<DISPOSE>NO</DISPOSE></IMPORT_INFO>"

View File

@ -0,0 +1,75 @@
#!/usr/bin/env ruby
# ---------------------------------------------------------------------------- #
# Copyright 2002-2015, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# ---------------------------------------------------------------------------- #
###############################################################################
# This script is used retrieve the file size of a disk
###############################################################################
ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION)
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION)
else
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION)
end
$: << RUBY_LIB_LOCATION
$: << File.dirname(__FILE__)
require 'opennebula'
drv_action_enc = ARGV[0]
id = ARGV[1]
drv_action = OpenNebula::XMLElement.new
drv_action.initialize_xml(Base64.decode64(drv_action_enc), 'DS_DRIVER_ACTION_DATA')
img_source = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/SOURCE"]
img_size = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/SIZE"]
md5 = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/MD5"]
md5 = md5.nil? ? "-" : md5
hostname = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_CLUSTER"]
ds_name = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/NAME"]
if img_source.nil?
STDERR.puts "Not enough information to export the image,"\
" missing image source."
exit -1
end
# Create vcenter url with all needed information to extract the file from
# the vCenter datastore
vcenter_url_img = "vcenter://#{img_source}?"\
"param_dsname=#{ds_name}&"\
"param_host=#{hostname}"
# Create import info document for marketplace import script
puts "<IMPORT_INFO>
<IMPORT_SOURCE><![CDATA[#{vcenter_url_img}]]></IMPORT_SOURCE>
<MD5><![CDATA[#{md5}]]></MD5>
<SIZE><![CDATA[#{img_size}]]></SIZE>
<FORMAT>VMDK</FORMAT>
<DISPOSE>NO</DISPOSE>
</IMPORT_INFO>"

View File

@ -0,0 +1,57 @@
#!/usr/bin/env ruby
# ---------------------------------------------------------------------------- #
# Copyright 2002-2015, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# ---------------------------------------------------------------------------- #
ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION)
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION)
else
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION)
end
$: << RUBY_LIB_LOCATION
$: << File.dirname(__FILE__)
require 'vcenter_driver'
require 'uri'
require 'cgi'
vcenter_url = ARGV[0]
u = URI.parse(vcenter_url)
params = CGI.parse(u.query)
hostname = params["param_host"][0]
ds_name = params["param_dsname"][0]
img_src = u.host + u.path
begin
host_id = VCenterDriver::VIClient.translate_hostname(hostname)
vi_client = VCenterDriver::VIClient.new host_id
ds = vi_client.get_datastore(ds_name)
# Setting "." as the source will read from the stdin
VCenterDriver::VIClient.in_stderr_silence do
ds.download_to_stdout img_src
end
rescue Exception => e
STDERR.puts "Cannot download image #{u.path} from datastore #{ds_name} "\
"on #{hostname}. Reason: #{e.message}"
exit -1
end

View File

@ -33,24 +33,6 @@ hostname = ARGV[0]
ds_name = ARGV[1]
target_path = ARGV[2]
def in_silence
begin
orig_stderr = $stderr.clone
orig_stdout = $stdout.clone
$stderr.reopen File.new('/dev/null', 'w')
$stdout.reopen File.new('/dev/null', 'w')
retval = yield
rescue Exception => e
$stdout.reopen orig_stdout
$stderr.reopen orig_stderr
raise e
ensure
$stdout.reopen orig_stdout
$stderr.reopen orig_stderr
end
retval
end
begin
host_id = VCenterDriver::VIClient.translate_hostname(hostname)
vi_client = VCenterDriver::VIClient.new host_id
@ -58,7 +40,7 @@ begin
ds = vi_client.get_datastore(ds_name)
# Setting "." as the source will read from the stdin
in_silence do
VCenterDriver::VIClient.in_silence do
ds.upload(target_path, ".")
end

View File

@ -777,6 +777,44 @@ class VIClient
datacenter: @dc
).wait_for_completion
end
############################################################################
# Silences standard output and error
############################################################################
def self.in_silence
begin
orig_stderr = $stderr.clone
orig_stdout = $stdout.clone
$stderr.reopen File.new('/dev/null', 'w')
$stdout.reopen File.new('/dev/null', 'w')
retval = yield
rescue Exception => e
$stdout.reopen orig_stdout
$stderr.reopen orig_stderr
raise e
ensure
$stdout.reopen orig_stdout
$stderr.reopen orig_stderr
end
retval
end
############################################################################
# Silences standard output and error
############################################################################
def self.in_stderr_silence
begin
orig_stderr = $stderr.clone
$stderr.reopen File.new('/dev/null', 'w')
retval = yield
rescue Exception => e
$stderr.reopen orig_stderr
raise e
ensure
$stderr.reopen orig_stderr
end
retval
end
end
################################################################################