1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-04-02 10:50:07 +03:00

M #: Defaults values for Service template (#4364)

This commit is contained in:
Christian González 2020-03-17 11:01:43 +01:00 committed by GitHub
parent f004510f10
commit 473b838ddf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 337 additions and 214 deletions

View File

@ -1696,4 +1696,195 @@ EOT
"-"
end
end
def OpenNebulaHelper.parse_user_inputs(inputs, get_defaults = false)
unless get_defaults
puts 'There are some parameters that require user input. ' \
'Use the string <<EDITOR>> to launch an editor ' \
'(e.g. for multi-line inputs)'
end
answers = {}
inputs.each do |key, val|
input_cfg = val.split('|', -1)
if input_cfg.length < 3
STDERR.puts 'Malformed user input. It should have at least 3 '\
"parts separated by '|':"
STDERR.puts " #{key}: #{val}"
exit(-1)
end
mandatory, type, description, params, initial = input_cfg
optional = mandatory.strip == 'O'
type.strip!
description.strip!
if input_cfg.length > 3
if input_cfg.length != 5
STDERR.puts 'Malformed user input. It should have 5 parts'\
" separated by '|':"
STDERR.puts " #{key}: #{val}"
exit(-1)
end
params.strip!
initial.strip!
end
if get_defaults
answers[key]= initial unless mandatory == 'M'
next
end
puts " * (#{key}) #{description}"
header = ' '
if !initial.nil? && initial != ''
header += "Press enter for default (#{initial}). "
end
case type
when 'text', 'text64'
print header
answer = STDIN.readline.chop
if answer == '<<EDITOR>>'
answer = OpenNebulaHelper.editor_input
end
# use default in case it's empty
answer = initial if answer.empty?
if type == 'text64'
answer = Base64.encode64(answer).strip.delete("\n")
end
when 'boolean'
print header
answer = STDIN.readline.chop
# use default in case it's empty
answer = initial if answer.empty?
unless %w[YES NO].include?(answer)
STDERR.puts "Invalid boolean '#{answer}'"
STDERR.puts 'Boolean has to be YES or NO'
exit(-1)
end
when 'password'
print header
answer = OpenNebulaHelper::OneHelper.get_password
# use default in case it's empty
answer = initial if answer.empty?
when 'number', 'number-float'
if type == 'number'
header += 'Integer: '
exp = INT_EXP
else
header += 'Float: '
exp = FLOAT_EXP
end
begin
print header
answer = STDIN.readline.chop
answer = initial if answer == ''
noanswer = ((answer == '') && optional)
end while !noanswer && (answer =~ exp) == nil
if noanswer
next
end
when 'range', 'range-float'
min, max = params.split('..')
if min.nil? || max.nil?
STDERR.puts 'Malformed user input. '\
"Parameters should be 'min..max':"
STDERR.puts " #{key}: #{val}"
exit(-1)
end
if type == 'range'
exp = INT_EXP
min = min.to_i
max = max.to_i
header += "Integer in the range [#{min}..#{max}]: "
else
exp = FLOAT_EXP
min = min.to_f
max = max.to_f
header += "Float in the range [#{min}..#{max}]: "
end
begin
print header
answer = STDIN.readline.chop
answer = initial if answer == ''
noanswer = (answer == '') && optional
end while !noanswer && ((answer =~ exp) == nil ||
answer.to_f < min || answer.to_f > max)
if noanswer
next
end
when 'list'
options = params.split(',')
options.each_with_index do |opt, i|
puts " #{i} #{opt}"
end
puts
header += 'Please type the selection number: '
begin
print header
answer = STDIN.readline.chop
if answer == ''
answer = initial
else
answer = options[answer.to_i]
end
noanswer = ((answer == '') && optional)
end while !noanswer && !options.include?(answer)
if noanswer
next
end
when 'fixed'
puts " Fixed value of (#{initial}). Cannot be changed"
answer = initial
else
STDERR.puts 'Wrong type for user input:'
STDERR.puts " #{key}: #{val}"
exit(-1)
end
answers[key] = answer
end
answers
end
end

View File

@ -160,47 +160,147 @@ class OneFlowTemplateHelper < OpenNebulaHelper::OneHelper
#
# @return [Hash] Custom attributes values
def custom_attrs(custom_attrs)
# rubocop:disable Layout/LineLength
return unless custom_attrs
puts 'There are some custom attrs which need a value'
ret = {}
ret['custom_attrs_values'] = OpenNebulaHelper.parse_user_inputs(custom_attrs)
# rubocop:enable Layout/LineLength
ret
end
def networks(vnets)
return unless vnets
ret = {}
ret['custom_attrs_values'] = {}
custom_attrs.each do |key, value|
split_value = value.split('|')
if split_value.size < 2
STDERR.puts 'Custom attribute malformed'
STDERR.puts 'M/O|description|<optional_value>'
exit(-1)
end
type, desc, initial = split_value
if %w[M O].include?(type)
puts "Introduce (#{type}) value for `#{key}` (#{desc}):"
else
STDERR.puts "Incorrect type: #{type}"
STDERR.puts 'only M (mandatory) O (optional) supported'
exit(-1)
end
answer = STDIN.readline.chop
if answer.empty? && type == 'M'
while answer.empty?
STDERR.puts 'Mandatory value can\'t be empty'
answer = STDIN.readline.chop
end
elsif answer.empty?
answer = initial
end
ret['custom_attrs_values'][key] = answer
end
ret['networks_values'] = parse_networks(vnets)
ret
end
def parse_networks(vnets, get_defaults = false)
unless get_defaults
puts 'There are some networks that require user input. ' \
'Use the string <<EDITOR>> to launch an editor ' \
'(e.g. for multi-line inputs)'
end
answers = []
vnets.each do |key, val|
input_cfg = val.split('|', -1)
if input_cfg.length != 5
STDERR.puts 'Malformed user input. It should have 5'\
"parts separated by '|':"
STDERR.puts " #{key}: #{val}"
exit(-1)
end
mandatory, _type, description, _params, initial = input_cfg
vnet = {}
if initial && !initial.empty?
type, resource_id, extra = initial.split(':', -1)
end
if (type.nil? || resource_id.nil?) &&
(initial && !initial.empty?)
STDERR.puts 'Wrong type for user input default value:'
STDERR.puts " #{key}: #{val}"
exit(-1)
end
vnet[key] = {}
if get_defaults
vnet[key][type] = resource_id
vnet[key]['extra'] = extra
answers << vnet unless mandatory == 'M'
next
end
puts " * (#{key}) #{description}"
#######################################
# Asks for type
#######################################
header = ' '
header += 'TYPE Existing(1), Create(2), Reserve(3). '
if !type.nil? && type != ''
header += 'Press enter for default. '
end
print header
answer = STDIN.readline.chop
type_a = type
case answer.to_i
when 1
type_a = 'id'
when 2
type_a = 'template_id'
when 3
type_a = 'reserve_from'
end
#######################################
# Asks for resource id
#######################################
header = ' '
if type_a == 'template_id'
header += 'VN Template ID. '
else
header += 'VN ID. '
end
if !resource_id.nil? && resource_id != ''
header += "Press enter for default (#{resource_id}). "
end
print header
resource_id_a = STDIN.readline.chop
resource_id_a = resource_id if resource_id_a.empty?
#######################################
# Asks for extra
#######################################
if type_a != 'id'
header = ' '
header += 'EXTRA (Type EMPTY for leaving empty). '
if !extra.nil? && extra != ''
header += " Press enter for default (#{extra}). "
end
print header
extra_a = STDIN.readline.chop
if extra_a.empty?
vnet[key]['extra'] = extra
else
vnet[key]['extra'] = extra_a
end
end
vnet[key][type_a] = resource_id_a
answers << vnet
end
answers
end
end

View File

@ -131,192 +131,21 @@ EOT
def self.get_user_inputs(template, get_defaults = false)
user_inputs = template['VMTEMPLATE']['TEMPLATE']['USER_INPUTS']
return "" if !user_inputs
return '' unless user_inputs
answers = ""
unless get_defaults
puts 'There are some parameters that require user input. ' \
'Use the string <<EDITOR>> to launch an editor ' \
'(e.g. for multi-line inputs)'
end
user_inputs.each do |key, val|
input_cfg = val.split('|', -1)
if input_cfg.length < 3
STDERR.puts "Malformed user input. It should have at least 3 parts separated by '|':"
STDERR.puts " #{key}: #{val}"
exit(-1)
end
mandatory, type, description, params, initial = input_cfg
optional = mandatory.strip == "O"
type.strip!
description.strip!
if input_cfg.length > 3
if input_cfg.length != 5
STDERR.puts "Malformed user input. It should have 5 parts separated by '|':"
STDERR.puts " #{key}: #{val}"
exit(-1)
end
params.strip!
initial.strip!
end
if get_defaults
answers << "#{key}=\"#{initial}\"" unless mandatory == 'M'
next
end
puts " * (#{key}) #{description}"
header = " "
if initial != nil && initial != ""
header += "Press enter for default (#{initial}). "
end
case type
when 'text', 'text64'
print header
answer = STDIN.readline.chop
if answer == "<<EDITOR>>"
answer = OpenNebulaHelper.editor_input()
end
if type == 'text64'
answer = Base64::encode64(answer).strip.delete("\n")
end
when 'boolean'
print header
answer = STDIN.readline.chop
# use default in case it's empty
answer = initial if answer.empty?
unless %w[YES NO].include?(answer)
STDERR.puts "Invalid boolean '#{answer}'"
STDERR.puts 'Boolean has to be YES or NO'
exit(-1)
end
when 'password'
print header
answer = OpenNebulaHelper::OneHelper.get_password
when 'number', 'number-float'
if type == "number"
header += "Integer: "
exp = INT_EXP
else
header += "Float: "
exp = FLOAT_EXP
end
begin
print header
answer = STDIN.readline.chop
answer = initial if (answer == "")
noanswer = ((answer == "") && optional)
end while !noanswer && (answer =~ exp) == nil
if noanswer
next
end
when 'range', 'range-float'
min,max = params.split('..')
if min.nil? || max.nil?
STDERR.puts "Malformed user input. Parameters should be 'min..max':"
STDERR.puts " #{key}: #{val}"
exit(-1)
end
if type == "range"
exp = INT_EXP
min = min.to_i
max = max.to_i
header += "Integer in the range [#{min}..#{max}]: "
else
exp = FLOAT_EXP
min = min.to_f
max = max.to_f
header += "Float in the range [#{min}..#{max}]: "
end
begin
print header
answer = STDIN.readline.chop
answer = initial if (answer == "")
noanswer = ((answer == "") && optional)
end while !noanswer && ((answer =~ exp) == nil || answer.to_f < min || answer.to_f > max)
if noanswer
next
end
when 'list'
options = params.split(",")
options.each_with_index {|opt,i|
puts " #{i} #{opt}"
}
puts
header += "Please type the selection number: "
begin
print header
answer = STDIN.readline.chop
if (answer == "")
answer = initial
else
answer = options[answer.to_i]
end
noanswer = ((answer == "") && optional)
end while !noanswer && (!options.include?(answer))
if noanswer
next
end
when 'fixed'
puts " Fixed value of (#{initial}). Cannot be changed"
answer = initial
else
STDERR.puts "Wrong type for user input:"
STDERR.puts " #{key}: #{val}"
exit(-1)
end
answers = OpenNebulaHelper.parse_user_inputs(user_inputs, get_defaults)
answers_s = ''
answers.each do |key, val|
# Do not replace values that are equal to the ones already in the
# template. Useful for cpu, mem, vcpu
if answer != template['VMTEMPLATE']['TEMPLATE'][key]
answers << "#{key} = \""
answers << answer.gsub('"', "\\\"") << "\"\n"
if key != template['VMTEMPLATE']['TEMPLATE'][key]
answers_s << "#{key} = \""
answers_s << val.gsub('"', "\\\"") << "\"\n"
end
end
answers
answers_s
end
private

View File

@ -210,6 +210,9 @@ CommandParser::CmdParser.new(ARGV) do
params['merge_template'] = helper.custom_attrs(
body['custom_attrs']
)
vnets = helper.networks(body['networks'])
params['merge_template'].merge!(vnets) unless vnets.nil?
end
json = Service.build_json_action('instantiate', params)