From 7c91082e33cea534d384df4a792b3c7fb71fb64a Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Mon, 23 Oct 2023 04:19:42 -0600 Subject: [PATCH] F OpenNebula/one#6242: prioritize file read (#2779) --- src/cli/one_helper/onebackupjob_helper.rb | 2 + src/cli/one_helper/onevm_helper.rb | 2 + src/cli/onebackupjob | 6 ++ src/cli/onedatastore | 7 ++- src/cli/oneflow-template | 68 ++++++++++++----------- src/cli/onehook | 9 +++ src/cli/onemarket | 7 ++- src/cli/onesecgroup | 9 ++- src/cli/onevm | 7 ++- src/cli/onevmgroup | 16 +++++- src/cli/onevnet | 9 ++- src/cli/onevntemplate | 8 +++ src/cli/onevrouter | 7 ++- src/cli/onezone | 9 ++- 14 files changed, 119 insertions(+), 47 deletions(-) diff --git a/src/cli/one_helper/onebackupjob_helper.rb b/src/cli/one_helper/onebackupjob_helper.rb index 101b5b8bab..7c8ee987c7 100644 --- a/src/cli/one_helper/onebackupjob_helper.rb +++ b/src/cli/one_helper/onebackupjob_helper.rb @@ -155,6 +155,8 @@ class OneBackupJobHelper < OpenNebulaHelper::OneHelper # Get user information if file str = File.read(file) + elsif STDIN.wait_readable(0) + str = STDIN.read else str = OpenNebulaHelper.update_template(id, bj, nil, xpath) end diff --git a/src/cli/one_helper/onevm_helper.rb b/src/cli/one_helper/onevm_helper.rb index 92f0ff5b4f..c5be71fe54 100644 --- a/src/cli/one_helper/onevm_helper.rb +++ b/src/cli/one_helper/onevm_helper.rb @@ -397,6 +397,8 @@ class OneVMHelper < OpenNebulaHelper::OneHelper # Get user information if file str = File.read(file) + elsif STDIN.wait_readable(0) + str = STDIN.read else str = OpenNebulaHelper.update_template(vm_id, vm, nil, xpath) end diff --git a/src/cli/onebackupjob b/src/cli/onebackupjob index 7c5269d386..1b65012a2d 100755 --- a/src/cli/onebackupjob +++ b/src/cli/onebackupjob @@ -130,6 +130,10 @@ CommandParser::CmdParser.new(ARGV) do - using a template description file: onebackupjob create weekly_backup.tmpl + + - using a template description via stdin: + + cat $bj_template | onebackupjob create EOT command :create, @@ -143,6 +147,8 @@ CommandParser::CmdParser.new(ARGV) do begin if args[0] template = File.read(args[0]) + elsif STDIN.wait_readable?(0) + template = STDIN.read else template = OneBackupJobHelper.create_backupjob_template(options) end diff --git a/src/cli/onedatastore b/src/cli/onedatastore index 8913810886..575f8a0546 100755 --- a/src/cli/onedatastore +++ b/src/cli/onedatastore @@ -113,8 +113,11 @@ CommandParser::CmdParser.new(ARGV) do helper.create_resource(options) do |datastore| begin - template = File.read(args[0]) if args[0] - template = STDIN.read if STDIN.wait_readable(0) + if args[0] + template = File.read(args[0]) + elsif STDIN.wait_readable(0) + template = STDIN.read + end datastore.allocate(template, cid) rescue StandardError => e STDERR.puts e.message diff --git a/src/cli/oneflow-template b/src/cli/oneflow-template index 2a357c5514..01b11b5ee4 100755 --- a/src/cli/oneflow-template +++ b/src/cli/oneflow-template @@ -186,11 +186,13 @@ CommandParser::CmdParser.new(ARGV) do command :create, create_desc, [:file, nil], :options => Service::JSON_FORMAT do client = helper.client(options) - template = nil - template = File.read(args[0]) if args[0] - template = STDIN.read if STDIN.wait_readable(0) + if args[0] + template = File.read(args[0]) + elsif STDIN.wait_readable(0) + template = STDIN.read + end - if template.nil? + if !template STDERR.puts 'A template must be provided' exit(-1) end @@ -257,38 +259,40 @@ CommandParser::CmdParser.new(ARGV) do rc = 0 client = helper.client(options) - number.times do - template = nil - template = File.read(args[1]) if args[1] - template = STDIN.read if STDIN.wait_readable(0) + if args[1] + template = File.read(args[1]) + elsif STDIN.wait_readable(0) + template = STDIN.read + end + params['merge_template'] = JSON.parse(template) if template - params['merge_template'] = JSON.parse(template) if template + unless params['merge_template'] + response = client.get("#{RESOURCE_PATH}/#{args[0]}") - unless params['merge_template'] - response = client.get("#{RESOURCE_PATH}/#{args[0]}") - - if CloudClient.is_error?(response) - rc = [response.code.to_i, response.to_s] - break - end - - params['merge_template'] = {} - body = JSON.parse(response.body)['DOCUMENT']['TEMPLATE']['BODY'] - - # Check global custom attributes - custom_attrs = helper.custom_attrs(body['custom_attrs']) - params['merge_template'].merge!(custom_attrs) unless custom_attrs.nil? - - # Check role level custom attributes - custom_role_attrs = helper.custom_role_attrs(body['roles']) - params['merge_template'].merge!(custom_role_attrs) unless custom_role_attrs.nil? - - # Check vnets attributes - vnets = helper.networks(body['networks']) - params['merge_template'].merge!(vnets) unless vnets.nil? + if CloudClient.is_error?(response) + rc = [response.code.to_i, response.to_s] + break end - json = Service.build_json_action('instantiate', params) + params['merge_template'] = {} + body = JSON.parse(response.body)['DOCUMENT']['TEMPLATE']['BODY'] + + # Check global custom attributes + custom_attrs = helper.custom_attrs(body['custom_attrs']) + params['merge_template'].merge!(custom_attrs) unless custom_attrs.nil? + + # Check role level custom attributes + custom_role_attrs = helper.custom_role_attrs(body['roles']) + params['merge_template'].merge!(custom_role_attrs) unless custom_role_attrs.nil? + + # Check vnets attributes + vnets = helper.networks(body['networks']) + params['merge_template'].merge!(vnets) unless vnets.nil? + end + + json = Service.build_json_action('instantiate', params) + + number.times do response = client.post("#{RESOURCE_PATH}/#{args[0]}/action", json) if CloudClient.is_error?(response) diff --git a/src/cli/onehook b/src/cli/onehook index 15e546fe62..fb6bfbfd03 100755 --- a/src/cli/onehook +++ b/src/cli/onehook @@ -164,6 +164,10 @@ CommandParser::CmdParser.new(ARGV) do onehook create hook.tmpl + - using a Hook description file via stdin: + + cat $hook_template | onehook create + EOT command :create, create_desc, :file, :options => @@ -172,6 +176,11 @@ CommandParser::CmdParser.new(ARGV) do begin if args[0] template = File.read(args[0]) + elsif STDIN.wait_readable(0) + template = STDIN.read + else + STDERR.puts 'No hook template provided' + exit(-1) end if options[:dry] diff --git a/src/cli/onemarket b/src/cli/onemarket index 82ac5f8ebd..87c81e139d 100755 --- a/src/cli/onemarket +++ b/src/cli/onemarket @@ -105,8 +105,11 @@ CommandParser::CmdParser.new(ARGV) do command :create, create_desc, [:file, nil] do helper.create_resource(options) do |marketplace| begin - template = File.read(args[0]) if args[0] - template = STDIN.read if STDIN.wait_readable(0) + if args[0] + template = File.read(args[0]) + elsif STDIN.wait_readable(0) + template = STDIN.read + end marketplace.allocate(template) rescue StandardError => e STDERR.puts e.message diff --git a/src/cli/onesecgroup b/src/cli/onesecgroup index b690887f8d..6e280ffe11 100755 --- a/src/cli/onesecgroup +++ b/src/cli/onesecgroup @@ -110,7 +110,14 @@ CommandParser::CmdParser.new(ARGV) do command :create, create_desc, :file do helper.create_resource(options) do |obj| begin - template = File.read(args[0]) + if args[0] + template = File.read(args[0]) + elsif STDIN.wait_readable(0) + template = STDIN.read + else + STDERR.puts 'No Security Group description provided' + exit(-1) + end obj.allocate(template) rescue StandardError => e diff --git a/src/cli/onevm b/src/cli/onevm index 9443f10239..1e4e36b094 100755 --- a/src/cli/onevm +++ b/src/cli/onevm @@ -1352,8 +1352,11 @@ CommandParser::CmdParser.new(ARGV) do template = '' begin - template = File.read(args[1]) if args[1] - template = STDIN.read if STDIN.wait_readable(0) + if args[1] + template = File.read(args[1]) + elsif STDIN.wait_readable(0) + template = STDIN.read + end rescue StandardError => e STDERR.puts "Error reading template: #{e.message}." exit(-1) diff --git a/src/cli/onevmgroup b/src/cli/onevmgroup index 03c07cf402..cb1c09df6d 100755 --- a/src/cli/onevmgroup +++ b/src/cli/onevmgroup @@ -136,7 +136,14 @@ CommandParser::CmdParser.new(ARGV) do command :create, create_desc, :file do helper.create_resource(options) do |obj| begin - template = File.read(args[0]) + if args[0] + template = File.read(args[0]) + elsif STDIN.wait_readable(0) + template = STDIN.read + else + STDERR.puts 'No VM Group description provided' + exit(-1) + end obj.allocate(template) rescue StandardError => e @@ -283,8 +290,11 @@ CommandParser::CmdParser.new(ARGV) do command :"role-add", role_add_desc, :vmgroupid, [:file, nil] do begin - template = File.read(args[1]) if args[1] - template = STDIN.read if STDIN.wait_readable(0) + if args[1] + template = File.read(args[1]) + elsif STDIN.wait_readable(0) + template = STDIN.read + end rescue StandardError => e STDERR.puts "Error reading template: #{e.message}." exit(-1) diff --git a/src/cli/onevnet b/src/cli/onevnet index 347958cb13..e3290cfdc2 100755 --- a/src/cli/onevnet +++ b/src/cli/onevnet @@ -171,8 +171,11 @@ CommandParser::CmdParser.new(ARGV) do helper.create_resource(options) do |vn| begin - template = File.read(args[0]) if args[0] - template = STDIN.read if STDIN.wait_readable(0) + if args[0] + template = File.read(args[0]) + elsif STDIN.wait_readable(0) + template = STDIN.read + end vn.allocate(template, cid) rescue StandardError => e STDERR.puts "Error creating network: #{e.message}" @@ -200,6 +203,8 @@ CommandParser::CmdParser.new(ARGV) do helper.perform_action(args[0], options, 'address range added') do |vn| if args[1] ar = File.read(args[1]) + elsif STDIN.wait_readable(0) + ar = STDIN.read else ar = OpenNebulaHelper.create_ar(options) end diff --git a/src/cli/onevntemplate b/src/cli/onevntemplate index 963593e5c7..0aa74f6d22 100755 --- a/src/cli/onevntemplate +++ b/src/cli/onevntemplate @@ -149,6 +149,10 @@ CommandParser::CmdParser.new(ARGV) do onevntemplate create vn_description.tmpl + - using a Virtual Network Template description file via stdin: + + cat $vn_template | onevntemplate create + EOT command :create, create_desc, [:file, nil], :options => @@ -157,6 +161,8 @@ CommandParser::CmdParser.new(ARGV) do begin if args[0] template = File.read(args[0]) + elsif STDIN.wait_readable(0) + template = STDIN.read end if options[:dry] @@ -250,6 +256,8 @@ CommandParser::CmdParser.new(ARGV) do if args[1] extra_template = File.read(args[1]) + elsif STDIN.wait_readable(0) + extra_template = STDIN.read else res = OpenNebulaHelper.create_template(options, t) diff --git a/src/cli/onevrouter b/src/cli/onevrouter index f5ab98d61f..ac27d06c4e 100755 --- a/src/cli/onevrouter +++ b/src/cli/onevrouter @@ -140,8 +140,11 @@ CommandParser::CmdParser.new(ARGV) do command :create, create_desc, [:file, nil] do helper.create_resource(options) do |obj| begin - template = File.read(args[0]) if args[0] - template = STDIN.read if STDIN.wait_readable(0) + if args[0] + template = File.read(args[0]) + elsif STDIN.wait_readable(0) + template = STDIN.read + end obj.allocate(template) rescue StandardError => e STDERR.puts e.message diff --git a/src/cli/onezone b/src/cli/onezone index 95f30126e4..b9e421eb2c 100755 --- a/src/cli/onezone +++ b/src/cli/onezone @@ -109,7 +109,14 @@ CommandParser::CmdParser.new(ARGV) do command :create, create_desc, :file do helper.create_resource(options) do |zone| begin - template = File.read(args[0]) + if args[0] + template = File.read(args[0]) + elsif STDIN.wait_readable(0) + template = STDIN.read + else + STDERR.puts 'No zone template provided' + exit(-1) + end zone.allocate(template) rescue StandardError => e STDERR.puts e.message