Created
February 26, 2014 20:43
-
-
Save brokenthumbs/9238130 to your computer and use it in GitHub Desktop.
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
require 'typhoeus' | |
require 'json' | |
require 'awesome_print' | |
def display_server_status(response) | |
t = {} | |
t["disabled"] = response["properties"]["basic"]["disabled"] | |
t["draining"] = response["properties"]["basic"]["draining"] | |
t["active"] = response["properties"]["basic"]["nodes"] | |
ap t | |
end | |
if ARGV[0].nil? || ARGV[1].nil? || ARGV[2].nil? || ARGV[3].nil? | |
puts "Usage: drain <https://base_api:port> <user:password> <pool_name> <node>" | |
exit 1 | |
end | |
base_api = ARGV[0] | |
auth = ARGV[1] | |
pool_name = ARGV[2] | |
node = ARGV[3] | |
url = base_api + '/api/tm/1.0/config/active/pools/' + pool_name | |
response = Typhoeus::Request.get("#{url}", | |
:userpwd => "#{auth}", | |
:ssl_verifypeer => false ) | |
r = JSON.parse(response.body) | |
log_file = "tmp/#{Time.now.to_i}.json" | |
File.open(log_file, 'w+') {|f| f.write(response.body) } | |
puts "Backing up #{pool_name} config to #{log_file}" | |
puts | |
display_server_status(r) | |
if (r['properties']['basic']['nodes'].include? node) | |
if (r['properties']['basic']['draining'].include? node) | |
puts | |
puts "Removing #{node} from draining list" | |
r['properties']['basic']['draining'].delete(node) | |
response = Typhoeus::Request.put("#{url}", | |
:headers => {"Content-Type" => "application/json", "Cache-Control" => "no-cache"}, | |
:userpwd => "#{auth}", | |
:ssl_verifypeer => false, | |
:body => JSON.generate(r) ) | |
r = JSON.parse(response.body) | |
puts | |
display_server_status(r) | |
else | |
puts | |
puts "#{pool_name} does not contain #{node} in draining list" | |
exit 2 | |
end | |
else | |
puts | |
puts "#{pool_name} does not contain #{node} in active list" | |
exit 1 | |
end | |
exit 0 |
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
require 'typhoeus' | |
require 'json' | |
require 'awesome_print' | |
def display_server_status(response) | |
t = {} | |
t["disabled"] = response["properties"]["basic"]["disabled"] | |
t["draining"] = response["properties"]["basic"]["draining"] | |
t["active"] = response["properties"]["basic"]["nodes"] | |
ap t | |
end | |
if ARGV[0].nil? || ARGV[1].nil? || ARGV[2].nil? || ARGV[3].nil? | |
puts "Usage: drain <https://base_api:port> <user:password> <pool_name> <node>" | |
exit 1 | |
end | |
base_api = ARGV[0] | |
auth = ARGV[1] | |
pool_name = ARGV[2] | |
node = ARGV[3] | |
url = base_api + '/api/tm/1.0/config/active/pools/' + pool_name | |
response = Typhoeus::Request.get("#{url}", | |
:userpwd => "#{auth}", | |
:ssl_verifypeer => false ) | |
r = JSON.parse(response.body) | |
log_file = "tmp/#{Time.now.to_i}.json" | |
File.open(log_file, 'w+') {|f| f.write(response.body) } | |
puts "Backing up #{pool_name} config to #{log_file}" | |
puts | |
display_server_status(r) | |
if (r['properties']['basic']['nodes'].include? node) | |
if (!r['properties']['basic']['draining'].include? node) | |
puts | |
puts "Adding #{node} to draining list" | |
r['properties']['basic']['draining'] << node | |
response = Typhoeus::Request.put("#{url}", | |
:headers => {"Content-Type" => "application/json", "Cache-Control" => "no-cache"}, | |
:userpwd => "#{auth}", | |
:ssl_verifypeer => false, | |
:body => JSON.generate(r) ) | |
r = JSON.parse(response.body) | |
puts | |
display_server_status(r) | |
else | |
puts | |
puts "#{pool_name} already contains #{node} in draining list" | |
exit 2 | |
end | |
else | |
puts | |
puts "#{pool_name} does not contain #{node} in active list" | |
exit 1 | |
end | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment