Advertisement
gusibsd

dynamic haproxy

Jul 14th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. : ${HAPROXY_PATCHED:=/etc/haproxy/haproxy.cfg.auto-scaling}
  4. : ${HAPROXY_TEMPLATE:=/etc/haproxy/haproxy.cfg.auto-scaling.template}
  5. : ${MARFEEL_AUTOSCALING_GROUP:=marfeelcandidate4-auto-scaling-group}
  6.  
  7. : ${HAPROXY:=/usr/sbin/haproxy}
  8.  
  9. yell() { echo -e "$0: $*" >&2; }
  10. die() { yell "$*"; exit 1; }
  11. die_and_yell() { yell "$1"; exit $2; }
  12. try() { "$@" || die "cannot $*"; }
  13.  
  14. if (( $# != 1 )); then
  15. die "Wrong number of arguments: $0 + final haproxy.cfg file\n\n - You can also set some environment variables, like MARFEEL_AUTOSCALING_GROUP, check the man page"
  16. fi
  17.  
  18. HAPROXY_CFG=$1;
  19. HAPROXY_CFG_DIR=`dirname ${HAPROXY_CFG}`
  20.  
  21. if [ -e ${HAPROXY_CFG_DIR} ]; then
  22. cp ${HAPROXY_TEMPLATE} ${HAPROXY_PATCHED}
  23. #for i in `aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name marfeelcandidate4-auto-scaling-group | grep INSTANCES | awk '{ print $4}'`; do aws ec2 describe-instances --instance-ids $i | grep ASSOCIATION | awk '{ print $4 }' | head -1; done | xargs -L 1 -I IP sed -i "0,/autoscale_ip/s//IP/" ${HAPROXY_PATCHED}
  24. for i in `aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name marfeelcandidate4-auto-scaling-group | grep INSTANCES | awk '{ print $4}'`; do aws ec2 describe-instances --instance-ids $i | grep ASSOCIATION | awk '{ print $4 }' | head -1; done | xargs -L 1 -I IP echo " server marfeel-candidate-4-webserver IP:80/ check" >> ${HAPROXY_PATCHED}
  25. if [[ $? == 0 ]]; then
  26. ${HAPROXY} -c -f ${HAPROXY_PATCHED} -q
  27. if [[ $? == 0 ]]; then
  28. cp ${HAPROXY_PATCHED} ${HAPROXY_CFG}
  29. /etc/init.d/haproxy restart
  30. if [[ $? != 0 ]]; then
  31. die "Panic, check what happened"
  32. fi
  33. echo "haproxy restarted"
  34. # some housekeeping, remove the temporal patched file
  35. rm ${HAPROXY_PATCHED}
  36. else
  37. echo "HOLA"
  38. die "Wrong ${HAPROXY_PATCHED} syntax, please check it"
  39. fi
  40. exit 0
  41. else
  42. die "Problem patching the ${HAPROXY_TEMPLATE} file."
  43. fi
  44. fi
  45.  
  46. die "Something went wrong"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement