keepalived-eipgw-notify.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env bash
  2. prog="$0"
  3. statefile=/var/run/yunion-sdnagent-eipgw.state
  4. pidfile=/var/run/yunion-sdnagent-eipgw.pid
  5. __errmsg() {
  6. logger -t "$prog" "$*"
  7. }
  8. _has_eipgw() {
  9. local comm
  10. comm="$(pgrep --list-name --pidfile "$pidfile" | cut -d' ' -f2)"
  11. [ -n "$comm" -a "$comm" = sdnagent ]
  12. }
  13. _start() {
  14. /opt/yunion/bin/sdnagent --config /etc/yunion/sdnagent.conf 2>&1 | logger -t sdnagent-eipgw &
  15. __errmsg "sdnagent eipgw process group id $$"
  16. }
  17. _stop() {
  18. pkill --pidfile "$pidfile"
  19. ovs-vsctl --if-exists del-br breip
  20. ovs-vsctl list-ports brvpc \
  21. | grep '^ev-' \
  22. | while read port; do \
  23. ovs-vsctl --if-exists del-port brvpc "$port"; \
  24. done
  25. }
  26. notify() {
  27. local state="$1"; shift
  28. __errmsg "notify: got state $state"
  29. set -x
  30. echo "$state" >"$statefile"
  31. case "$state" in
  32. MASTER)
  33. _start
  34. ;;
  35. *)
  36. _stop
  37. ;;
  38. esac
  39. }
  40. monitor() {
  41. local state
  42. if [ -s "$statefile" ]; then
  43. state="$(<"$statefile")"
  44. __errmsg "monitor: hey state $state"
  45. case "$state" in
  46. MASTER)
  47. if ! _has_eipgw; then
  48. _start
  49. fi
  50. ;;
  51. *)
  52. if _has_eipgw; then
  53. _stop
  54. fi
  55. ;;
  56. esac
  57. fi
  58. return 0
  59. }
  60. "$@"