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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/bin/bash
  2. conffile="/etc/lodel2/mass_deploy.cfg"
  3. badconf() {
  4. 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
  5. exit 1
  6. }
  7. mongodb_connfail() {
  8. echo -e "Credential from $conffile seems to be incorrect. Unable to connect on admin db" >&2
  9. exit 2
  10. }
  11. usage() {
  12. echo -e "Usage : $0 (N|purgedb|-h|--help) [FIXED_NAME]
  13. \tWith :
  14. \t\t- N the number of instances to create
  15. \t\t- purgedb is to remove all created mongodb db\n"
  16. }
  17. slim_fails() {
  18. if [ -z "$1" ]
  19. then
  20. echo "SLIM fails for some reason. Abording..." >&2
  21. else
  22. echo "SLIM fails when $1. Abording...." >&2
  23. fi
  24. 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 ;)"
  25. exit 1
  26. }
  27. fixed_name="$2"
  28. test -f $conffile || badconf
  29. #Load conffile
  30. . $conffile
  31. test -z "$MONGODB_ADMIN_USER" && badconf
  32. test -z "$MONGODB_ADMIN_PASSWORD" && badconf
  33. $MONGODB_HOST=${MONGODB_HOST:="127.0.0.1"}
  34. if [ -z "$fixed_name" ]
  35. then
  36. #Check for the presence of /usr/share/dict/words to generate random names
  37. if [ -f '/usr/share/dict/words' ]
  38. then
  39. echo "/usr/share/dict/words found. Using this file as source for random names"
  40. random_name=$(sed -nE 's/^([A-Za-z0-9]+)$/\1/p' /usr/share/dict/words |shuf|head -n1)
  41. else
  42. echo -e "\n\n\tWarning... /usr/share/dict/words not found using \$RANDOM for random names generation"
  43. random_name=$RANDOM
  44. fi
  45. else
  46. random_name="$fixed_name"
  47. fi
  48. #Check for the presence of mongo and its conf
  49. if hash mongo 2>/dev/null
  50. then
  51. echo "Mongo found"
  52. else
  53. echo "You need mongo on this host to do a mass deploy !" >&2
  54. exit
  55. fi
  56. if [ -f "/etc/mongodb.conf" ]
  57. then
  58. if cat /etc/mongodb.conf |grep -E '^ *auth *= *true *$' >/dev/null
  59. then
  60. echo "OK, auth enabled"
  61. else
  62. echo "WARNING : auth seems disabled on mongod !" >&2
  63. fi
  64. else
  65. echo "/etc/mongodb.conf not found. Unable to check if auth is on"
  66. fi
  67. echo "exit" | mongo $MONGODB_HOST --quiet -u "$MONGODB_ADMIN_USER" -p "$MONGODB_ADMIN_PASSWORD" --authenticationDatabase admin &>/dev/null || mongodb_connfail
  68. if [ "$1" == 'purgedb' ]
  69. then
  70. if [ -z "$MONGODB_DB_PREFIX" ]
  71. then
  72. 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."
  73. exit 3
  74. fi
  75. echo -n "Are you sure you want to delete all mongo database with name begining with '$MONGODB_DB_PREFIX' ? Y/n "
  76. read rep
  77. if [ "$rep" = "Y" ]
  78. then
  79. 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)
  80. do
  81. 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
  82. done
  83. echo "Done."
  84. else
  85. echo "You didn't answer 'Y' (case matters), exiting"
  86. fi
  87. exit
  88. elif echo $1 | grep -E "[A-Za-z]" &>/dev/null
  89. then
  90. usage
  91. exit 0
  92. fi
  93. #Check for the presence of pwgen for password generation
  94. if hash pwgen 2>/dev/null
  95. then
  96. echo "Using pwgen to generate passwords"
  97. rnd_pass_cmd='pwgen 25 1'
  98. else
  99. echo -e "\n\n\tpwgen not found... Using \$RANDOM to generate passwords\n\n"
  100. rnd_pass_cmd='echo $RANDOM'
  101. fi
  102. ninstance=$1
  103. instance=${ninstance:=50}
  104. echo -n "You are going to create $ninstance lodel2 instances. Are you sure ? Y/n "
  105. read rep
  106. if [ "$rep" = "Y" ]
  107. then
  108. echo "GO ! (you have 3 secs to cancel)"
  109. sleep 3
  110. else
  111. echo "You didn't answer 'Y' (case matters), exiting"
  112. exit
  113. fi
  114. echo "Creating instances"
  115. for i in $(seq $ninstance)
  116. do
  117. iname="${random_name}_$(printf "%05d" $i)"
  118. slim -n $iname -c || slim_fails "creating instance"
  119. slim -n $iname -s --interface web --static-url 'http://127.0.0.1/static' --uwsgi-workers 2 || slim_fails "configuring instancee"
  120. slim -n $iname -m || slim_fails "running make in instance"
  121. #Mongo db database creation
  122. dbname="${MONGODB_DB_PREFIX}_$iname"
  123. dbuser="lodel2_$iname"
  124. dbpass=$($rnd_pass_cmd)
  125. mongo $MONGODB_HOST -u "$MONGODB_ADMIN_USER" -p "$MONGODB_ADMIN_PASSWORD" --authenticationDatabase admin <<EOF
  126. use $dbname
  127. db.addUser({user:"$dbuser", pwd:"$dbpass", roles:["readWrite", "dbAdmin"]})
  128. exit
  129. EOF
  130. #Append created db to instance conf
  131. slim -n $iname -s --datasource_connectors mongodb --host $MONGODB_HOST --user $dbuser --password $dbpass --db_name $dbname || slim_fails "configuring the instance's datasource"
  132. done