install-consul.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/bash
  2. set -e
  3. source /etc/terraform_environment
  4. SERVER_ARGS=""
  5. UI_DIR="null"
  6. HTTP_CLIENT_ADDR="127.0.0.1"
  7. echo "Installing Consul..."
  8. pushd /tmp
  9. wget https://dl.bintray.com/mitchellh/consul/0.4.1_linux_amd64.zip -O consul.zip
  10. unzip consul.zip >/dev/null
  11. chmod +x consul
  12. mv consul /usr/local/bin/consul
  13. mkdir -p /etc/consul.d
  14. mkdir -p /mnt/consul/data
  15. mkdir -p /etc/service
  16. rm /tmp/consul.zip
  17. popd
  18. if [[ "${ROLE}" == *consul-server* ]]; then
  19. echo "Configure as Consul Server..."
  20. SERVER_ARGS="-server -bootstrap-expect=3"
  21. else
  22. echo "Configure as Consul Client..."
  23. fi
  24. if [[ "${ROLE}" == *consul-ui* ]]; then
  25. echo "Installing Consul UI..."
  26. pushd /tmp
  27. wget https://dl.bintray.com/mitchellh/consul/0.4.1_web_ui.zip -O consul-ui.zip
  28. unzip consul-ui.zip >/dev/null
  29. mkdir -p /mnt/consul/ui
  30. mv dist/* /mnt/consul/ui/
  31. rm /tmp/consul-ui.zip
  32. popd
  33. HTTP_CLIENT_ADDR="0.0.0.0"
  34. UI_DIR="\"/mnt/consul/ui\""
  35. fi
  36. # Configuration file
  37. echo "Creating configuration..."
  38. cat >/etc/consul.d/config.json << EOF
  39. {
  40. "addresses" : {
  41. "http" : "${HTTP_CLIENT_ADDR}"
  42. },
  43. "ports" : {
  44. "dns" : 53
  45. },
  46. "recursor" : "10.0.0.2",
  47. "disable_anonymous_signature" : true,
  48. "disable_update_check" : true,
  49. "data_dir" : "/mnt/consul/data",
  50. "ui_dir" : $UI_DIR
  51. }
  52. EOF
  53. chmod 0644 /etc/consul.d/config.json
  54. # Setup the join address
  55. echo "Configure IPs..."
  56. cat >/etc/service/consul-join << EOF
  57. export CONSUL_JOIN="10.0.1.10 10.0.1.11 10.0.1.12"
  58. EOF
  59. chmod 0644 /etc/service/consul-join
  60. # Configure the server
  61. echo "Configure server..."
  62. cat >/etc/service/consul << EOF
  63. export CONSUL_FLAGS="${SERVER_ARGS}"
  64. EOF
  65. chmod 0644 /etc/service/consul
  66. # Add "first start" join service
  67. echo "Creating 'join' service..."
  68. cat >/etc/init/consul-join.conf <<"EOF"
  69. description "Join the consul cluster"
  70. start on started consul
  71. stop on stopped consul
  72. task
  73. script
  74. if [ -f "/etc/service/consul-join" ]; then
  75. . /etc/service/consul-join
  76. fi
  77. # Keep trying to join until it succeeds
  78. set +e
  79. while :; do
  80. logger -t "consul-join" "Attempting join: ${CONSUL_JOIN}"
  81. /usr/local/bin/consul join \
  82. ${CONSUL_JOIN} \
  83. >>/var/log/consul-join.log 2>&1
  84. [ $? -eq 0 ] && break
  85. sleep 5
  86. done
  87. logger -t "consul-join" "Join success!"
  88. end script
  89. EOF
  90. chmod 0644 /etc/init/consul-join.conf
  91. # Add actual service
  92. echo "Creating service..."
  93. cat >/etc/init/consul.conf <<"EOF"
  94. description "Consul agent"
  95. start on runlevel [2345]
  96. stop on runlevel [!2345]
  97. respawn
  98. script
  99. if [ -f "/etc/service/consul" ]; then
  100. . /etc/service/consul
  101. fi
  102. # Make sure to use all our CPUs, because Consul can block a scheduler thread
  103. export GOMAXPROCS=`nproc`
  104. exec /usr/local/bin/consul agent \
  105. -config-dir="/etc/consul.d" \
  106. ${CONSUL_FLAGS} \
  107. >>/var/log/consul.log 2>&1
  108. end script
  109. EOF
  110. chmod 0644 /etc/init/consul.conf
  111. # Start service
  112. echo "Starting service..."
  113. initctl start consul