diff --git a/share/vendor/ruby/gems/rbvmomi/lib/rbvmomi/vim/Datastore.rb b/share/vendor/ruby/gems/rbvmomi/lib/rbvmomi/vim/Datastore.rb index c5ebeda328..e7ceebd9fd 100644 --- a/share/vendor/ruby/gems/rbvmomi/lib/rbvmomi/vim/Datastore.rb +++ b/share/vendor/ruby/gems/rbvmomi/lib/rbvmomi/vim/Datastore.rb @@ -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. diff --git a/src/datastore_mad/remotes/ceph/export b/src/datastore_mad/remotes/ceph/export index 6466b0c52f..4db1d722b6 100755 --- a/src/datastore_mad/remotes/ceph/export +++ b/src/datastore_mad/remotes/ceph/export @@ -116,9 +116,9 @@ else fi cat <\$CHECKSUM -$SIZE -${FORMAT} + + + EOT EOF ) @@ -133,7 +133,7 @@ fi cat < - $IMPORT_SOURCE + $INFO NO " diff --git a/src/datastore_mad/remotes/downloader.sh b/src/datastore_mad/remotes/downloader.sh index 33a52e50ff..18024df0d3 100755 --- a/src/datastore_mad/remotes/downloader.sh +++ b/src/datastore_mad/remotes/downloader.sh @@ -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 diff --git a/src/datastore_mad/remotes/fs/export b/src/datastore_mad/remotes/fs/export index 4660f33311..4c4e13f69e 100755 --- a/src/datastore_mad/remotes/fs/export +++ b/src/datastore_mad/remotes/fs/export @@ -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 "\$CHECKSUM" -echo "\$SIZE" -echo "\${FORMAT-"unknown"}" +echo "" +echo "" +echo "" EOF ) @@ -83,7 +83,7 @@ if [ "$INFO_STATUS" != "0" ]; then exit $INFO_STATUS fi -echo "$SRC \ +echo " \ $INFO \ NO" diff --git a/src/datastore_mad/remotes/vcenter/export b/src/datastore_mad/remotes/vcenter/export new file mode 100755 index 0000000000..e79fe89f82 --- /dev/null +++ b/src/datastore_mad/remotes/vcenter/export @@ -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 " + + + + VMDK + NO +" diff --git a/src/datastore_mad/remotes/vcenter_downloader.rb b/src/datastore_mad/remotes/vcenter_downloader.rb new file mode 100755 index 0000000000..770993f5ee --- /dev/null +++ b/src/datastore_mad/remotes/vcenter_downloader.rb @@ -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 diff --git a/src/datastore_mad/remotes/vcenter_uploader.rb b/src/datastore_mad/remotes/vcenter_uploader.rb index 17c49200fe..75d330ce08 100755 --- a/src/datastore_mad/remotes/vcenter_uploader.rb +++ b/src/datastore_mad/remotes/vcenter_uploader.rb @@ -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 diff --git a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb index d53ef495cf..77ba10a558 100644 --- a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb +++ b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb @@ -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 ################################################################################