1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

B #-: Fix stdin hanlding in cli (#2909)

Don't realy solely on STDIN.wait_readable. When
running CLI from script, STDIN could be easily
inherited and already processed. Also Jenkins in
`sh` function always passes STDIN.

Improve error message when attributes should be
merged.

(cherry picked from commit 954c545605e87dc0613af8bd195a383566388697)
This commit is contained in:
Jan Orel 2024-01-30 16:45:25 +01:00 committed by Ruben S. Montero
parent 6c388414e2
commit af0c109c1b
No known key found for this signature in database
GPG Key ID: A0CEA6FA880A1D87
22 changed files with 125 additions and 90 deletions

View File

@ -1712,8 +1712,8 @@ Bash symbols must be escaped on STDIN passing'
def self.update_template_helper(append, _id, resource, path, xpath, update = true)
if path
File.read(path)
elsif STDIN.wait_readable(0)
STDIN.read
elsif !(stdin = self.read_stdin).empty?
stdin
elsif append
editor_input
else
@ -2095,16 +2095,17 @@ Bash symbols must be escaped on STDIN passing'
ar << ']'
end
def self.create_template_options_used?(options)
def self.create_template_options_used?(options, conflicting_opts)
# Get the template options names as symbols. options hash
# uses symbols
template_options=OpenNebulaHelper::TEMPLATE_OPTIONS.map do |o|
o[:name].to_sym
end
# Check if one at least one of the template options is
# in options hash
(template_options-options.keys)!=template_options
# Check if at least one of the template options is in options hash
conflicting_opts.replace(options.keys & template_options)
!conflicting_opts.empty?
end
def self.sunstone_url
@ -2660,4 +2661,11 @@ Bash symbols must be escaped on STDIN passing'
end
end
def self.read_stdin
if STDIN.wait_readable(0)
STDIN.read()
else
''
end
end
end

View File

@ -155,8 +155,8 @@ class OneBackupJobHelper < OpenNebulaHelper::OneHelper
# Get user information
if file
str = File.read(file)
elsif STDIN.wait_readable(0)
str = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
str = stdin
else
str = OpenNebulaHelper.update_template(id, bj, nil, xpath)
end

View File

@ -518,6 +518,18 @@ class OneImageHelper < OpenNebulaHelper::OneHelper
[0, template]
end
def create_template_options_used?(options, conflicting_opts)
# Get the template options names as symbols, options hash uses symbols
template_options=TEMPLATE_OPTIONS.map do |o|
o[:name].to_sym
end
# Check if one at least one of the template options is in options hash
conflicting_opts.replace(options.keys & template_options)
!conflicting_opts.empty?
end
end
end

View File

@ -129,16 +129,17 @@ class OneMarketPlaceAppHelper < OpenNebulaHelper::OneHelper
end
end
def self.create_template_options_used?(options)
def self.create_template_options_used?(options, conflicting_opts)
# Get the template options names as symbols. options hash
# uses symbols
template_options=self::TEMPLATE_OPTIONS.map do |o|
o[:name].to_sym
end
# Check if one at least one of the template options is
# in options hash
(template_options-options.keys)!=template_options
# Check if at least one of the template options is in options hash
conflicting_opts.replace(options.keys & template_options)
!conflicting_opts.empty?
end
# Import object into marketplace

View File

@ -397,8 +397,8 @@ class OneVMHelper < OpenNebulaHelper::OneHelper
# Get user information
if file
str = File.read(file)
elsif STDIN.wait_readable(0)
str = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
str = stdin
else
str = OpenNebulaHelper.update_template(vm_id, vm, nil, xpath)
end

View File

@ -147,8 +147,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable?(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
else
template = OneBackupJobHelper.create_backupjob_template(options)
end

View File

@ -115,8 +115,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
end
datastore.allocate(template, cid)
rescue StandardError => e

View File

@ -189,8 +189,8 @@ CommandParser::CmdParser.new(ARGV) do
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
end
if !template
@ -262,8 +262,8 @@ CommandParser::CmdParser.new(ARGV) do
if args[1]
template = File.read(args[1])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
end
params['merge_template'] = JSON.parse(template) if template

View File

@ -176,8 +176,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
else
STDERR.puts 'No hook template provided'
exit(-1)

View File

@ -216,10 +216,13 @@ CommandParser::CmdParser.new(ARGV) do
check_capacity = true
end
if (args[0] || STDIN.wait_readable(0)) &&
OpenNebulaHelper.create_template_options_used?(options)
conflicting_opts = []
if (args[0] || !(stdin = OpenNebulaHelper.read_stdin).empty?) &&
OneImageHelper.create_template_options_used?(options, conflicting_opts)
STDERR.puts 'You cannot pass template on STDIN and use template creation options, ' <<
"conflicting options: #{conflicting_opts.join(', ')}."
STDERR.puts 'You cannot use both template and template creation options.'
next -1
end
@ -236,8 +239,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !stdin.empty?
template = stdin
else
res = OneImageHelper.create_image_template(options)

View File

@ -107,8 +107,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
end
marketplace.allocate(template)
rescue StandardError => e

View File

@ -191,10 +191,12 @@ CommandParser::CmdParser.new(ARGV) do
exit(-1)
end
if (args[0] || STDIN.wait_readable(0)) &&
OneMarketPlaceAppHelper.create_template_options_used?(options)
STDERR.puts 'You can not use both template file and template'\
' creation options.'
conflicting_opts = []
if (args[0] || !(stdin = OpenNebulaHelper.read_stdin).empty?) &&
OneMarketPlaceAppHelper.create_template_options_used?(options, conflicting_opts)
STDERR.puts 'You cannot pass template on STDIN and use template creation options, ' <<
"conflicting options: #{conflicting_opts.join(', ')}."
next -1
end
@ -202,8 +204,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !stdin.empty?
template = stdin
else
res = OneMarketPlaceAppHelper
.create_datastore_template(options)

View File

@ -112,8 +112,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
else
STDERR.puts 'No Security Group description provided'
exit(-1)

View File

@ -166,10 +166,13 @@ CommandParser::CmdParser.new(ARGV) do
command :create, create_desc, [:file, nil], :options =>
[OneTemplateHelper::VM_NAME] + OpenNebulaHelper::TEMPLATE_OPTIONS +
[OpenNebulaHelper::DRY] do
if (args[0] || STDIN.wait_readable(0)) &&
OpenNebulaHelper.create_template_options_used?(options)
conflicting_opts = []
if (args[0] || !(stdin = OpenNebulaHelper.read_stdin).empty?) &&
OpenNebulaHelper.create_template_options_used?(options, conflicting_opts)
STDERR.puts 'You cannot pass template on STDIN and use template creation options, ' <<
"conflicting options: #{conflicting_opts.join(', ')}."
STDERR.puts 'You can not use both template file and template creation options.'
next -1
end
@ -177,8 +180,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !stdin.empty?
template = stdin
else
res = OpenNebulaHelper.create_template(options)
@ -255,11 +258,13 @@ CommandParser::CmdParser.new(ARGV) do
command :instantiate, instantiate_desc, :templateid, [:file, nil],
:options => instantiate_options + OpenNebulaHelper::TEMPLATE_OPTIONS do
exit_code = 0
conflicting_opts = []
if (args[1] || !(stdin = OpenNebulaHelper.read_stdin).empty?) &&
OpenNebulaHelper.create_template_options_used?(options, conflicting_opts)
if (args[1] || STDIN.wait_readable(0)) &&
OpenNebulaHelper.create_template_options_used?(options)
STDERR.puts 'You cannot pass template on STDIN and use template creation options, ' <<
"conflicting options: #{conflicting_opts.join(', ')}."
STDERR.puts 'You cannot use both template and template creation options.'
next -1
end
@ -305,8 +310,8 @@ CommandParser::CmdParser.new(ARGV) do
if args[1]
extra_template = File.read(args[1])
elsif STDIN.wait_readable(0)
extra_template = STDIN.read
elsif !stdin.empty?
extra_template = stdin
else
res = OpenNebulaHelper.create_template(options, t)

View File

@ -349,19 +349,21 @@ CommandParser::CmdParser.new(ARGV) do
number = options[:multiple] || 1
exit_code = nil
if (args[0] || STDIN.wait_readable(0)) &&
OpenNebulaHelper.create_template_options_used?(options)
conflicting_opts = []
if (args[0] || !(stdin = OpenNebulaHelper.read_stdin).empty?) &&
OpenNebulaHelper.create_template_options_used?(options, conflicting_opts)
STDERR.puts 'You cannot pass template on STDIN and use template creation options, ' <<
"conflicting options: #{conflicting_opts.join(', ')}."
STDERR.puts 'You can not use both template file and template'\
' creation options.'
exit(-1)
end
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !stdin.empty?
template = stdin
else
res = OpenNebulaHelper.create_template(options)
@ -615,8 +617,8 @@ CommandParser::CmdParser.new(ARGV) do
if options[:file]
extra_template = File.read(options[:file])
elsif STDIN.wait_readable(0)
extra_template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
extra_template = stdin
end
helper.perform_actions(args[0], options, verbose) do |vm|
@ -774,8 +776,8 @@ CommandParser::CmdParser.new(ARGV) do
TARGET, CACHE, DISCARD, PREFIX] do
if options[:file]
template = File.read(options[:file])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
elsif options[:image]
image_id = options[:image]
target = options[:target]
@ -851,8 +853,8 @@ CommandParser::CmdParser.new(ARGV) do
] do
if options[:file]
template = File.read(options[:file])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
elsif options[:network]
network_id = options[:network]
ip = options[:ip]
@ -1276,8 +1278,8 @@ CommandParser::CmdParser.new(ARGV) do
:options => OpenNebulaHelper::CAPACITY_OPTIONS_VM + [ENFORCE, OpenNebulaHelper::FILE] do
if options[:file]
template = File.read(options[:file])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
else
template = ''
@ -1373,8 +1375,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[1]
template = File.read(args[1])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
end
rescue StandardError => e
STDERR.puts "Error reading template: #{e.message}."

View File

@ -138,8 +138,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
else
STDERR.puts 'No VM Group description provided'
exit(-1)
@ -292,8 +292,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[1]
template = File.read(args[1])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
end
rescue StandardError => e
STDERR.puts "Error reading template: #{e.message}."

View File

@ -173,8 +173,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
end
vn.allocate(template, cid)
rescue StandardError => e
@ -203,8 +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
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
ar = stdin
else
ar = OpenNebulaHelper.create_ar(options)
end

View File

@ -161,8 +161,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
end
if options[:dry]
@ -256,8 +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
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
extra_template = stdin
else
res = OpenNebulaHelper.create_template(options, t)

View File

@ -142,8 +142,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
end
obj.allocate(template)
rescue StandardError => e
@ -174,10 +174,12 @@ CommandParser::CmdParser.new(ARGV) do
:vrouterid, :templateid, [:file, nil],
:options => instantiate_options +
OpenNebulaHelper::TEMPLATE_OPTIONS do
if (args[2] || STDIN.wait_readable(0)) &&
OpenNebulaHelper.create_template_options_used?(options)
conflicting_opts = []
if (args[2] || !(stdin = OpenNebulaHelper.read_stdin).empty?) &&
OpenNebulaHelper.create_template_options_used?(options, conflicting_opts)
STDERR.puts 'You cannot use both template and template creation options.'
STDERR.puts 'You cannot pass template on STDIN and use template creation options, ' <<
"conflicting options: #{conflicting_opts.join(', ')}."
exit(-1)
end
@ -201,8 +203,8 @@ CommandParser::CmdParser.new(ARGV) do
if args[2]
extra_template = File.read(args[2])
elsif STDIN.wait_readable(0)
extra_template = STDIN.read
elsif !stdin.empty?
extra_template = stdin
else
res = OpenNebulaHelper.create_template(options, t)
@ -307,8 +309,8 @@ CommandParser::CmdParser.new(ARGV) do
OneVirtualRouterHelper::FLOAT] do
if options[:file]
template = File.read(options[:file])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
elsif options[:network]
network_id = options[:network]
ip = options[:ip]

View File

@ -111,8 +111,8 @@ CommandParser::CmdParser.new(ARGV) do
begin
if args[0]
template = File.read(args[0])
elsif STDIN.wait_readable(0)
template = STDIN.read
elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
template = stdin
else
STDERR.puts 'No zone template provided'
exit(-1)

View File

@ -32,7 +32,7 @@ type Pool struct {
// VMGroup represents an OpenNebula VM group
type VMGroup struct {
XMLName xml.Name `xml:"VM_GROUP"`
ID int `xml:"ID,omitemtpy"`
ID int `xml:"ID,omitempty"`
UID int `xml:"UID,omitempty"`
GID int `xml:"GID,omitempty"`
UName string `xml:"UNAME,omitempty"`

View File

@ -167,7 +167,7 @@ module OneDBFsck
if !name_seen[name.downcase]
name_seen[name.downcase] = [uid, name]
else
log_error("User id:#{uid} has conficting name #{name}, " \
log_error("User id:#{uid} has conflicting name #{name}, " \
"another user id:#{name_seen[name.downcase][0]} " \
"with name #{name_seen[name.downcase][1]} is present",
false)