diff --git a/share/linters/.rubocop.yml b/share/linters/.rubocop.yml index b74ea3ac9d..b2fa6b93da 100644 --- a/share/linters/.rubocop.yml +++ b/share/linters/.rubocop.yml @@ -21,7 +21,6 @@ AllCops: Exclude: - - src/datastore_mad/remotes/vcenter/cp - src/sunstone/public/node_modules/**/* - src/tm_mad - share/onegate/onegate diff --git a/src/datastore_mad/remotes/vcenter/cp b/src/datastore_mad/remotes/vcenter/cp old mode 100755 new mode 100644 index 7da72f8224..f979bde3b8 --- a/src/datastore_mad/remotes/vcenter/cp +++ b/src/datastore_mad/remotes/vcenter/cp @@ -65,6 +65,65 @@ def there_is_not_system_error?(rc) !rc end +def find_image_type_in_directory(target_path) + img_files=Dir.glob("#{target_path}/*") + if img_files.empty? + raise StandardError, 'No files in img_path folder' + end + + img_files.each do |f| + Open3.popen3("qemu-img info #{f}") do |_sdtin, stdout, stderr, wait_thr| + unless wait_thr.value.success? + raise StandardError, + "'qemu-img info' failed, " \ + "stderr: #{stderr.read}" + end + tmp_file_type=stdout.read.split("\n")[1].split(':')[1].lstrip + if tmp_file_type.nil? + raise StandardError, + "'qemu-img info' output unexpected format" + end + file_types[f] = tmp_file_type + wait_thr.join + end + end + + if file_types.values.include? 'vmdk' + STDERR.puts 'image should already be in vmdk format' + return target_path, 'vmdk' + end + + if file_types.empty? + raise StandardError, + "No valid images found in #{target_path}" + end + + if file_types.length > 1 + raise StandardError, + "More than 1 image file found in #{target_path}" + end + + file_types[0] +end + +def find_image_type(target_file) + target_file_type=nil + Open3.popen3("qemu-img info #{target_file}") do + |_stdin, stdout, stderr, wait_thr| + unless wait_thr.value.success? + raise StandardError, + "'qemu-img info' failed, stderr: #{stderr.read}" + end + target_file_type=stdout.read.split("\n")[1].split(':')[1].lstrip + if target_file_type.nil? + raise StandardError, + "'qemu-img info' output unexpected format" + end + wait_thr.join + end + target_file_type +end + drv_action_enc = ARGV[0] id = ARGV[1] @@ -135,78 +194,36 @@ if VCenterDriver::FileHelper.remote_or_needs_unpack?(img_path) # Convert from current format to VMDK unless VCenterDriver::FileHelper.from_s3?(img_path) - file_type='' - file_types={} + file_type=nil + temp_file_folder=nil begin if File.directory?(temp_file) - img_files=Dir.glob("#{temp_file}/*") - if img_files.empty? - raise StandardError.new("No files in img_path folder") - end - img_files.each do |f| - Open3.popen3("qemu-img info #{f}") do - |_sdtin, stdout, stderr, wait_thr| - unless wait_thr.value.success? - raise StandardError.new("'qemu-img info' failed, stderr: #{stderr.read}") - end - tmp_file_type=stdout.read.split("\n")[1].split(':')[1].lstrip - if tmp_file_type.nil? - raise StandardError.new("'qemu-img info' output unexpected format") - end - file_types[f] = tmp_file_type - wait_thr.join - end - end - if file_types.values.include? 'vmdk' - file_types={} - file_type='vmdk' - STDERR.puts "image(s) should already be in vmdk format" - raise StandardError.new("0") - end + temp_file_folder = temp_file + temp_file, file_type = find_image_type_in_directory(temp_file) else - Open3.popen3("qemu-img info #{temp_file}") do - |_stdin, stdout, stderr, wait_thr| - unless wait_thr.value.success? - raise StandardError.new("'qemu-img info' failed, stderr: #{stderr.read}") - end - file_type=stdout.read.split("\n")[1].split(':')[1].lstrip - if file_type.nil? - raise StandardError.new("'qemu-img info' output unexpected format") - end - wait_thr.join - end + file_type = find_image_type(temp_file) end rescue StandardError => e - unless e.message == "0" - STDERR.puts "Failed to determine image type, error: #{e}" - STDERR.puts "Removing #{temp_file}..." - FileUtils.rm_rf(temp_file) - exit(-1) + STDERR.puts "Failed to determine image type, error: #{e}" + STDERR.puts "Removing #{temp_file}..." + FileUtils.rm_rf(temp_file) + unless temp_file_folder.nil? + STDERR.puts "Removing #{temp_file_folder}." + FileUtils.rm_rf(temp_file_folder) end + + exit(-1) end - # If we have file_types, that means we're converting a directory + begin - if !file_types.empty? - out_dir="#{temp_file}.tmp" - Dir.mkdir(out_dir) - file_types.each do |path,type| - next if type == 'vmdk' - out_file="#{out_dir}/#{File.basename(path, '.*')}.vmdk" - convert=system("qemu-img convert -f #{type}"\ - " -O vmdk #{path} #{out_file}") - if there_is_not_system_error?(convert) - raise StandardError - end - end - FileUtils.rm_rf(temp_file) - FileUtils.mv(out_dir, temp_file) - elsif file_type != 'vmdk' + if file_type != 'vmdk' out_file = File.basename(temp_file, '.*')+'.tmp' convert=system("qemu-img convert -f #{file_type}"\ " -O vmdk #{temp_file} #{out_file}") if there_is_not_system_error?(convert) raise StandardError end + FileUtils.mv(out_file, temp_file) end rescue StandardError @@ -214,6 +231,11 @@ if VCenterDriver::FileHelper.remote_or_needs_unpack?(img_path) STDERR.puts "Removing #{temp_file} and #{out_file}." FileUtils.rm_rf(out_file) FileUtils.rm_rf(temp_file) + unless temp_file_folder.nil? + STDERR.puts "Removing #{temp_file_folder}." + FileUtils.rm_rf(temp_file_folder) + end + exit(-1) end end