-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11204 from gtanzillo/settings-tool
Rails scripts for setting a server's zone and configuration settings from a command line
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |