Created
March 20, 2010 00:14
-
-
Save Aslan/338341 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/ruby | |
# == Synopsis | |
# This is a tool to help poolpartyrb and chef set haproxy.conf file. | |
# It also can list clouds and add the new clouds to the chef server i | |
# security group in order to let the chef clients connect to the chef server | |
# TODO: list all the ips of a given cloud and list the passenger process(run | |
# passenger-status remotly on each server). | |
# Note: At the moment the script would work only if there is a cloud.rb in | |
# your cwd. | |
# | |
# == Examples | |
# This command does blah blah blah. | |
# | |
# | |
# Other examples: | |
# ruby_cl_skeleton -q bar.doc | |
# ruby_cl_skeleton --verbose foo.html | |
# | |
# == Usage | |
# pparty [options] | |
# | |
# For help use: ruby_cl_skeleton -h | |
# | |
# == Options | |
# -h, --help Displays help message | |
# -v, --version Display the version, then exit | |
# -q, --quiet Output as little as possible, overrides verbose | |
# -V, --verbose Verbose output | |
# TO DO - add additional options | |
# | |
# == Author | |
# Aslan N | |
# | |
# == Copyright | |
# Copyright (c) 2010 Aslan. Licensed under the MIT License: | |
# http://www.opensource.org/licenses/mit-license.php | |
require 'optparse' | |
require 'ostruct' | |
require 'rdoc/usage' | |
require 'date' | |
require 'poolparty.rb' | |
require 'clouds.rb' | |
class Pptoolkit | |
VERSION = '0.0.1' | |
attr_reader :options | |
def initialize(arguments,stdin) | |
@arguments = arguments | |
@stdin = stdin | |
@options = OpenStruct.new | |
@options.haproxy_path_provided = false | |
@options.ips = [] | |
@options.security_group_name = "default" | |
@options.security_group_port = "4000-4001" | |
end | |
def run | |
#puts "Start at #{DateTime.now}\n\n" if @options.verbose | |
#output_options | |
if parsed_options? && arguments_valid? | |
puts "Start at #{DateTime.now}\n\n" if @options.verbose | |
output_options if @options.verbose | |
process_arguments | |
process_command | |
else | |
output_usage | |
end | |
end | |
protected | |
def parsed_options? | |
opts = OptionParser.new | |
opts.on('-v', '--version'){output_version; exit 0} | |
opts.on('-h', '--help'){output_help} | |
opts.on('-V', '--verbose'){@options.verbose=true} | |
opts.on('-a', '--add') do |o| | |
@options.add = o | |
end | |
opts.on('-r', '--rm') do |o| | |
@options.rm = o | |
end | |
opts.on('-n', '--cloud CLOUD_NAME',String, "Name of the cloud for the given action.") do |o| | |
@options.cloud = o | |
end | |
opts.on('-i', '--ip IPS',Array, "List of ip's separated by comma") do |o| | |
@options.ips = o | |
end | |
opts.on('-l', '--list-process') do |o| | |
# TODO | |
puts "This is going to check the passenger process on each server listed in the given cloud using passenger status" | |
end | |
#opts.on('-s', '--add-security-group GROUP', String, "The name of the security group that has to be added to default(chef-server) group") do |o| | |
opts.on('-s', '--add-security-group', "The name of the security group that has to be added to default(chef-server) group") do |o| | |
@options.security_group = o | |
end | |
opts.on('-f', '--haproxy-path-file PATH', String, "The path to haproxy attributes file") do |o| | |
@options.haproxy_path = o | |
@options.haproxy_path_provided = true | |
end | |
opts.on('-k', '--haproxy-key PATH', String, "The attribute key on which the ip list is added to set up haproxy.") do |o| | |
@options.haproxy_key = o | |
end | |
opts.on('-g', '--security-group-name NAME', String, "The name of the security group that the we want to add authorization to") do |o| | |
@options.security_group_name = o | |
end | |
opts.on('-m', '--monirot-passenger') do |o| | |
@options.monitor_passenger = o | |
end | |
opts.on('-B', '--backend-lb', "To configure the backend-app part of the lb") do |o| | |
@options.set_lb_block = o | |
@options.haproxy_path = "/srv/chef/site-cookbooks/haproxy/attributes/backend-block.rb" | |
@options.lb_block = 'backend' | |
end | |
opts.on('-F', '--frontend-lb', "To configure the frontend-app part of the lb") do |o| | |
@options.set_lb_block = o | |
@options.haproxy_path = "/srv/chef/site-cookbooks/haproxy/attributes/frontend-block.rb" | |
@options.lb_block = 'frontend' | |
end | |
opts.on('-M', '--add-db-master-ip', "The ip address of db master is added to database yaml") do |o| | |
@options.set_db_host = o | |
@options.cloud ||= 'database-master' | |
@options.db_server = 'master' | |
@options.db_attribute_file = "/srv/chef/site-cookbooks/configure-site/attributes/db_host_master.rb" | |
end | |
opts.on('-S', '--add-db-slave-ip', "The ip address of db slave is added to the database yaml") do |o| | |
@options.set_db_host = o | |
@options.cloud ||= 'database-slave' | |
@options.db_server = 'slave' | |
@options.db_attribute_file = "/srv/chef/site-cookbooks/configure-site/attributes/db_host_slave.rb" | |
end | |
opts.parse!(@arguments) rescue return false | |
process_options | |
true | |
end | |
def process_options | |
#At the moment nothing to do here. | |
end | |
def output_options | |
puts "Options: \n" | |
@options.marshal_dump.each do |name, val| | |
puts " #{name} = #{val}" | |
end | |
end | |
def arguments_valid? | |
if @options.set_lb_block | |
unless File.exist?(@options.haproxy_path) | |
puts "File #{@options.haproxy_path} not found." | |
return false | |
end | |
end | |
if [email protected]? | |
@options.ips.each do |x| | |
unless x =~ /10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b/ | |
puts "wrong ip address : #{x}" | |
return false | |
end | |
end | |
end | |
if(@options.add or @options.rm) | |
unless [email protected]? or @options.cloud | |
puts "You must pass ip or cloud when using -a or -r arguments" | |
return false | |
end | |
end | |
if(@options.add and @options.rm) | |
puts "-a and -r can not be used at the same time" | |
return false | |
end | |
#if(@options.haproxy_key) | |
# unless(@options.haproxy_path_provided) | |
# puts "For -k option -f must also be provided" | |
# return false | |
# end | |
#end | |
true if (1==1) | |
end | |
# Setup the arguments | |
def process_arguments | |
# TO DO - place in local vars, etc | |
end | |
def output_help | |
output_version | |
RDoc::usage() #exits app | |
end | |
def output_usage | |
RDoc::usage('usage') # gets usage from comments above | |
end | |
def output_version | |
puts "#{File.basename(__FILE__)} version #{VERSION}" | |
end | |
def process_command | |
haproxy_add if(@options.add) | |
haproxy_rm if(@options.rm) | |
monitor_passenger if(@options.monitor_passenger) | |
add_security_group if(@options.security_group) | |
set_db_host_ip if(@options.set_db_host) | |
end | |
def process_standard_input | |
input = @stdin.read | |
# TO DO - process input | |
# [Optional] | |
# @stdin.each do |line| | |
# # TO DO - process each line | |
#end | |
end | |
def haproxy_add | |
a = [] | |
a = current_ips_in_haproxy | |
f = @options.cloud ? @options.ips + get_cloud_ips : @options.ips | |
update_haproxy(a + f) | |
end | |
def haproxy_rm | |
a = [] | |
f = [] | |
a = current_ips_in_haproxy | |
f = @options.cloud ? @options.ips + get_cloud_ips : @options.ips | |
puts f | |
update_haproxy(a - f) | |
end | |
def get_cloud_ips | |
f = [] | |
cmd = "cloud list -n #{@options.cloud}" | |
pp_output = `#{cmd}` | |
lines = pp_output.split("\n") | |
lines.each do |line| | |
if( line =~ /running\t\S+\t(10.\S+)/ ) | |
f << $1 | |
end | |
end | |
f | |
end | |
def set_db_host_ip | |
ip_list = get_cloud_ips | |
conf_file = File.new(@options.db_attribute_file,'w') | |
str = "set[:database][:host][:#{@options.db_server}]=" | |
ip_list = ip_list.map{|ip| "'#{ip}'"} | |
str = "#{str}[#{ip_list.uniq.join(",")}]" #just one more check to make sure the ip list is unique | |
conf_file.puts str | |
puts "The following has been written to the #{@options.db_attribute_file} file:\n\t#{str}" | |
end | |
def current_ips_in_haproxy | |
f = [] | |
conf_file = File.new(@options.haproxy_path,'r') | |
lines = conf_file.read.split("\n") | |
lines.each do |line| | |
line.split(/\[|\]|,|'/).each do |x| | |
f << x.strip if x =~ /10./ | |
end | |
end | |
f | |
end | |
def add_security_group | |
if group = get_cloud_security_group | |
cmd = "ec2-authorize #{@options.security_group_name} -P tcp -p #{@options.security_group_port} -u 435397005020 -o #{group}" | |
puts `#{cmd}` | |
end | |
end | |
def get_cloud_security_group | |
cmd = "cloud show -n #{@options.cloud}" | |
output = `#{cmd}` | |
output.split("\n").each do |line| | |
return $1 if line =~ /Security group: +(\S+)/ | |
end | |
false | |
end | |
def update_haproxy(ip_list) | |
conf_file = File.new(@options.haproxy_path,'w') | |
str = "set[:haproxy][:#{@options.lb_block}_servers]=" | |
ip_list = ip_list.map{|ip| "'#{ip}'"} | |
str = "#{str}[#{ip_list.uniq.join(",")}]" #just one more check to make sure the ip list is unique | |
conf_file.puts str | |
puts "The following has been written to the #{@options.haproxy_path} file:\n\t#{str}" | |
end | |
def monitor_passenger | |
pool.clouds[@options.cloud].nodes.each {|c| c.ssh "passenger-status" } | |
end | |
end | |
pptk = Pptoolkit.new(ARGV, STDIN) | |
pptk.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment