diff --git a/tools/configure_server_settings.rb b/tools/configure_server_settings.rb index 6151196d8d9..799d62f1a91 100755 --- a/tools/configure_server_settings.rb +++ b/tools/configure_server_settings.rb @@ -2,14 +2,22 @@ require File.expand_path("../config/environment", __dir__) require 'trollop' +TYPES = %w(string integer boolean symbol float).freeze + opts = Trollop.options(ARGV) do banner "USAGE: #{__FILE__} -s -p -v \n" \ - "Example: #{__FILE__} -d -s 1 -p reporting/history/keep_reports -v 42" + "Example (String): #{__FILE__} -s 1 -p reporting/history/keep_reports -v 3.months\n" \ + "Example (Integer): #{__FILE__} -s 1 -p workers/worker_base/queue_worker_base/ems_metrics_collector_worker/defaults/count -v 1 -t integer\n" \ + "Example (Boolean): #{__FILE__} -s 1 -p ui/mark_translated_strings -v true -t boolean\n" \ + "Example (Symbol): #{__FILE__} -s 1 -p workers/worker_base/queue_worker_base/ems_metrics_collector_worker/defaults/poll_method -v escalate -t symbol\n" \ + "Example (Float): #{__FILE__} -s 1 -p capacity/profile/1/vcpu_commitment_ratio -v 1.5 -t float" - opt :dry_run, "Dry Run", :short => "d" - opt :serverid, "Server Id", :short => "s", :default => 0 - opt :path, "Path within advanced settings hash", :short => "p", :default => "" - opt :value, "New value for setting", :short => "v", :default => "" + opt :dry_run, "Dry Run", :short => "d" + opt :serverid, "Server Id", :short => "s", :type => :integer, :required => true + opt :path, "Path within advanced settings hash", :short => "p", :type => :string, :required => true + opt :value, "New Value for setting", :short => "v", :type => :string, :required => true + opt :force, "Force change value regardless of type", :short => "f", :type => :boolean, :default => false + opt :type, "Type of value provided, #{TYPES.inspect}", :short => "t", :type => :string, :default => "string" end puts opts.inspect @@ -17,6 +25,26 @@ Trollop.die :serverid, "is required" unless opts[:serverid_given] Trollop.die :path, "is required" unless opts[:path_given] Trollop.die :value, "is required" unless opts[:value_given] +Trollop.die :type, "must be one of #{TYPES.inspect}" unless TYPES.include?(opts[:type]) + +# Grab the value that we have set and translate to appropriate var class +newval = + case opts[:type] + when "string" + opts[:value] + when "integer" + opts[:value].to_i + when "boolean" + if opts[:value].downcase == "true" + true + elsif opts[:value].downcase == "false" + false + end + when "symbol" + opts[:value].to_sym + when "float" + opts[:value].to_f + end server = MiqServer.where(:id => opts[:serverid]).take unless server @@ -31,8 +59,17 @@ key = keys.pop.to_sym keys.each { |p| path = path[p.to_sym] } +# allow user to escape if the new value's class is not the same as the original, +# such as setting a String where it was previously an Integer +if opts[:force] + puts "Change [#{opts[:path]}], old class: [#{path[key].class}], new class: [#{newval.class}]" +elsif path[key] && path[key].class != newval.class + STDERR.puts "The new value's class #{newval.class} does not match the prior one's #{path[key].class}. Use -t to specify the type for the provided value. Use -f to force changing this value. Note, -f may break things! See -h for examples." + exit 1 +end + puts "Setting [#{opts[:path]}], old value: [#{path[key]}], new value: [#{opts[:value]}]" -path[key] = opts[:value] +path[key] = newval valid, errors = VMDB::Config::Validator.new(settings).validate unless valid