Skip to content

Commit

Permalink
Merge pull request #11204 from gtanzillo/settings-tool
Browse files Browse the repository at this point in the history
Rails scripts for setting a server's zone and configuration settings from a command line
  • Loading branch information
jrafanie authored Jun 13, 2017
2 parents 7b50f75 + a116a75 commit 9e39485
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tools/change_server_zone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env ruby
require File.expand_path("../config/environment", __dir__)

if ARGV.empty?
puts "USAGE: #{__FILE__} serverid zone_name"
exit 0
end

server_id, zone_name = ARGV

server = MiqServer.where(:id => server_id).take
unless server
puts "Unable to find server with id [#{server_id}]"
exit 1
end

zone = Zone.where(:name => zone_name).take
unless zone
puts "Unable to find zone with name [#{zone_name}]"
exit 1
end

server.zone = zone
server.save!

settings = server.get_config("vmdb")
settings.config[:server][:zone] = zone.name
server.set_config(settings)

server.save!

puts "Configured server [#{server.id}] to be in zone [#{zone.name}]"
51 changes: 51 additions & 0 deletions tools/configure_server_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env ruby
require File.expand_path("../config/environment", __dir__)
require 'trollop'

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"

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 => ""
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]

server = MiqServer.where(:id => opts[:serverid]).take
unless server
puts "Unable to find server with id [#{opts[:serverid]}]"
exit 1
end

settings = server.get_config("vmdb")

path = settings.config
keys = opts[:path].split("/")
key = keys.pop.to_sym
keys.each { |p| path = path[p.to_sym] }

puts "Setting [#{opts[:path]}], old value: [#{path[key]}], new value: [#{opts[:value]}]"
path[key] = opts[:value]

valid, errors = VMDB::Config::Validator.new(settings).validate
unless valid
puts "ERROR: Configuration is invalid:"
errors.each { |k, v| puts "\t#{k}: #{v}" }
exit 1
end

if opts[:dry_run]
puts "Dry run, no updates have been made"
else
server.set_config(settings)
server.save!

puts "Done"
end

0 comments on commit 9e39485

Please sign in to comment.