No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mass_deploy.sh 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/bin/bash
  2. #
  3. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  4. #
  5. # Copyright (C) 2015-2017 Cléo UMS-3287
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published
  9. # by the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. conffile="[@]LODEL2_CONFDIR[@]/mass_deploy.cfg"
  21. badconf() {
  22. echo -e "Either the file $conffile cannot be found or it didn't contains expected informations\n\nThe conffile is expected to define MONGODB_ADMIN_USER and MONGODB_ADMIN_PASSWORD variables" >&2
  23. exit 1
  24. }
  25. mongodb_connfail() {
  26. echo -e "Credential from $conffile seems to be incorrect. Unable to connect on admin db" >&2
  27. exit 2
  28. }
  29. usage() {
  30. echo -e "Usage : $0 (N|purgedb|-h|--help) [FIXED_NAME]
  31. \tWith :
  32. \t\t- N the number of instances to create
  33. \t\t- purgedb is to remove all created mongodb db\n"
  34. }
  35. slim_fails() {
  36. if [ -z "$1" ]
  37. then
  38. echo "SLIM fails for some reason. Abording..." >&2
  39. else
  40. echo "SLIM fails when $1. Abording...." >&2
  41. fi
  42. echo "It can be a good idea to run '$0 purgedb' and 'slim -p' now that mass_deploy just crashed. Those commands should be able to clean the mess ;)"
  43. exit 1
  44. }
  45. fixed_name="$2"
  46. test -f $conffile || badconf
  47. #Load conffile
  48. . $conffile
  49. test -z "$MONGODB_ADMIN_USER" && badconf
  50. test -z "$MONGODB_ADMIN_PASSWORD" && badconf
  51. $MONGODB_HOST=${MONGODB_HOST:="127.0.0.1"}
  52. if [ -z "$fixed_name" ]
  53. then
  54. #Check for the presence of /usr/share/dict/words to generate random names
  55. if [ -f '/usr/share/dict/words' ]
  56. then
  57. echo "/usr/share/dict/words found. Using this file as source for random names"
  58. random_name=$(sed -nE 's/^([A-Za-z0-9]+)$/\1/p' /usr/share/dict/words | iconv -f utf8 -t ascii//TRANSLIT |shuf|head -n1)
  59. else
  60. echo -e "\n\n\tWarning... /usr/share/dict/words not found using \$RANDOM for random names generation"
  61. random_name=$RANDOM
  62. fi
  63. else
  64. random_name="$fixed_name"
  65. fi
  66. #Check for the presence of mongo and its conf
  67. if hash mongo 2>/dev/null
  68. then
  69. echo "Mongo found"
  70. else
  71. echo "You need mongo on this host to do a mass deploy !" >&2
  72. exit
  73. fi
  74. if [ -f "/etc/mongodb.conf" ]
  75. then
  76. if cat /etc/mongodb.conf |grep -E '^ *auth *= *true *$' >/dev/null
  77. then
  78. echo "OK, auth enabled"
  79. else
  80. echo "WARNING : auth seems disabled on mongod !" >&2
  81. fi
  82. else
  83. echo "/etc/mongodb.conf not found. Unable to check if auth is on"
  84. fi
  85. echo "exit" | mongo $MONGODB_HOST --quiet -u "$MONGODB_ADMIN_USER" -p "$MONGODB_ADMIN_PASSWORD" --authenticationDatabase admin &>/dev/null || mongodb_connfail
  86. if [ "$1" == 'purgedb' ]
  87. then
  88. if [ -z "$MONGODB_DB_PREFIX" ]
  89. then
  90. echo -e "MONGODB_DB_PREFIX not indicated in $conffile. Using purgedb without prefix is really a bad idea... It will result in a deletion of ALL db.\nexiting."
  91. exit 3
  92. fi
  93. echo -n "Are you sure you want to delete all mongo database with name begining with '$MONGODB_DB_PREFIX' ? Y/n "
  94. read rep
  95. if [ "$rep" = "Y" ]
  96. then
  97. for dbname in $(echo "show dbs" | mongo $MONGODB_HOST --quiet -u "$MONGODB_ADMIN_USER" -p "$MONGODB_ADMIN_PASSWORD" --authenticationDatabase admin |grep "^$MONGODB_DB_PREFIX"|cut -f1)
  98. do
  99. mongo --host $MONGODB_HOST -u "$MONGODB_ADMIN_USER" -p "$MONGODB_ADMIN_PASSWORD" --quiet --eval 'db.dropDatabase()' --authenticationDatabase admin $dbname && echo "$dbname succesfully deleted" || echo "$dbname deletion fails" >&2
  100. done
  101. echo "Done."
  102. else
  103. echo "You didn't answer 'Y' (case matters), exiting"
  104. fi
  105. exit
  106. elif echo $1 | grep -E "[A-Za-z]" &>/dev/null
  107. then
  108. usage
  109. exit 0
  110. fi
  111. #Check for the presence of pwgen for password generation
  112. if hash pwgen 2>/dev/null
  113. then
  114. echo "Using pwgen to generate passwords"
  115. rnd_pass_cmd='pwgen 25 1'
  116. else
  117. echo -e "\n\n\tpwgen not found... Using \$RANDOM to generate passwords\n\n"
  118. rnd_pass_cmd='echo $RANDOM'
  119. fi
  120. ninstance=$1
  121. instance=${ninstance:=50}
  122. echo -n "You are going to create $ninstance lodel2 instances. Are you sure ? Y/n "
  123. read rep
  124. if [ "$rep" = "Y" ]
  125. then
  126. echo "GO ! (you have 3 secs to cancel)"
  127. sleep 3
  128. else
  129. echo "You didn't answer 'Y' (case matters), exiting"
  130. exit
  131. fi
  132. echo "Creating instances"
  133. for i in $(seq $ninstance)
  134. do
  135. iname="${random_name}_$(printf "%05d" $i)"
  136. slim -n $iname -c || slim_fails "creating instance"
  137. slim -n $iname -s --interface web --static-url 'http://127.0.0.1/static' --uwsgi-workers 2 || slim_fails "configuring instancee"
  138. slim -n $iname -m || slim_fails "running make in instance"
  139. #Mongo db database creation
  140. dbname="${MONGODB_DB_PREFIX}_$iname"
  141. dbuser="lodel2_$iname"
  142. dbpass=$($rnd_pass_cmd)
  143. mongo $MONGODB_HOST -u "$MONGODB_ADMIN_USER" -p "$MONGODB_ADMIN_PASSWORD" --authenticationDatabase admin <<EOF
  144. use $dbname
  145. db.addUser({user:"$dbuser", pwd:"$dbpass", roles:["readWrite", "dbAdmin"]})
  146. exit
  147. EOF
  148. #Append created db to instance conf
  149. slim -n $iname -s --datasource_connectors mongodb --host $MONGODB_HOST --user $dbuser --password $dbpass --db_name $dbname || slim_fails "configuring the instance's datasource"
  150. done