Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting var type with configure_server_settings.rb #17039

Merged
merged 11 commits into from
Feb 27, 2018
49 changes: 43 additions & 6 deletions tools/configure_server_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,49 @@
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 <server id> -p <settings path separated by a /> -v <new value>\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

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
Expand All @@ -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
Expand Down