Skip to content

Instantly share code, notes, and snippets.

Created February 1, 2013 11:26
Show Gist options
  • Save anonymous/4690764 to your computer and use it in GitHub Desktop.
Save anonymous/4690764 to your computer and use it in GitHub Desktop.
#!/bin/env ruby
#
# Copyright (c) 2007-2012 RightScale, Inc, All Rights Reserved Worldwide.
#
# Contacts and configures an HAProxy server for user with a generic apache appli cation
# LB_APPLISTENER_NAME -- specifies which HAProxy server pool to use
# LB_BACKEND_NAME -- A unique name for each back end e.g. (AMI_INSTANCE_ID)
# MAX_CONN_PER_SERVER -- Maximum number of connections per server
# HEALTH_CHECK_URI --
require 'yaml'
require 'resolv'
#Temporary, require. These will be automatically passed in soon.
#require '/var/spool/ec2/meta-data.rb'
os_info=`cat /etc/redhat-release`
if os_info =~ /ubuntu/
ENV['RS_DISTRO']="ubuntu"
apache="apache"
ubuntu = true
centos = false
elsif os_info =~ /centos/
ENV['RS_DISTRO']="centos"
apache="httpd"
centos = true
ubuntu = false
end
#Escape the 4 problematic shell characters: ", $, `, and \ to get through the ss h command correctly
def shell_escape(string)
return string.gsub(/\\/,"\\\\\\").gsub(/\"/,"\\\"").gsub(/\$/,"\\\$").gsub(/\` /,"\\\\\`")
end
# Connect server machine to load balancer to start receiving traffic
applistener = ENV['mysql']
backend_name = ENV['`hostname`']
# Set the default port for the application server.
port="3306"
# Internal IP
this_backend = ENV['EC2_LOCAL_IPV4']
# Use cookies?
sess_sticky = "#{ENV['OPT_SESSION_STICKINESS']}".downcase
if(sess_sticky && sess_sticky.match(/^(true|yes|on)$/))
cookie_options = "-c #{ENV['EC2_INSTANCE_ID']}"
end
# How many conns do we tell the LB to bring to the AJP port?
max_conn_per_svr = ENV['MAX_CONN_PER_SERVER']
puts ">>>>>>>Attaching app to host $(hostname) <<<<<<<<<<<<<<"
# Using the default config file...no cookie persistence...and health checks
# /opt/rightscale/lb/bin/haproxy_config_server.rb -a add -w -l $LB_APPLISTENER -s
# $LB_BACKEND_NAME -t $BACKEND_TARGET -k on -e "inter 3000 rise 2 fall 3 maxconn 3"
sshcmd = "ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no root@192. 168.7.100"
cfg_cmd="/root/lb/bin/haproxy_config_server.rb"
server_ip=`hostname -I`
server_name=`hostname`
service="mysql"
server_name = server_name.strip
server_ip = server_ip.strip
args= "-a add -w -l #{service} -s #{server_name} -t \"#{server_ip}\":#{port} -e \"inter 3000 rise 2 fall 3 maxconn 1\" "
res=`#{sshcmd} #{cfg_cmd} #{shell_escape(args)}`
puts res #for debugging...
if $? != 0
puts "Error performing action: #{res}"
exit(-1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment