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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/bin/bash
  2. conffile="[@]LODEL2_CONFDIR[@]/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)
  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. test -f $conffile || badconf
  28. #Load conffile
  29. . $conffile
  30. test -z "$MONGODB_ADMIN_USER" && badconf
  31. test -z "$MONGODB_ADMIN_PASSWORD" && badconf
  32. #Check for the presence of /usr/share/dict/words to generate random names
  33. if [ -f '/usr/share/dict/words' ]
  34. then
  35. random_name=$(sed -nE 's/^([A-Za-z0-9]+)$/\1/p' /usr/share/dict/words |shuf|head -n1)
  36. else
  37. random_name=$RANDOM
  38. fi
  39. #Check for the presence of mongo and its conf
  40. if hash mongo 2>/dev/null
  41. then
  42. echo "Mongo found"
  43. else
  44. echo "You need mongo on this host to do a mass deploy !" >&2
  45. exit
  46. fi
  47. if [ -f "/etc/mongodb.conf" ]
  48. then
  49. if cat /etc/mongodb.conf |grep -E '^ *auth *= *true *$' >/dev/null
  50. then
  51. echo "OK, auth enabled"
  52. else
  53. echo "WARNING : auth seems disabled on mongod !" >&2
  54. fi
  55. else
  56. echo "/etc/mongodb.conf not found. Unable to check if auth is on"
  57. fi
  58. echo "exit" | mongo $MONGODB_HOST --quiet -u "$MONGODB_ADMIN_USER" -p "$MONGODB_ADMIN_PASSWORD" admin &>/dev/null || mongodb_connfail
  59. if [ "$1" == 'purgedb' ]
  60. then
  61. if [ -z "$MONGODB_DB_PREFIX" ]
  62. then
  63. 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."
  64. exit 3
  65. fi
  66. echo -n "Are you sure you want to delete all mongo database with name begining with '$MONGODB_DB_PREFIX' ? Y/n "
  67. read rep
  68. if [ "$rep" = "Y" ]
  69. then
  70. for dbname in $(echo "show dbs" | mongo -u admin -p pass admin |grep "^$MONGODB_DB_PREFIX"|cut -f1)
  71. do
  72. echo -e "use $dname\ndb.dropDatabase()\nexit\n" | mongo -u $MONGODB_ADMIN_USER -p $MONGODB_ADMIN_PASSWORD --quiet --authenticationDatabase admin $dbname && echo "$dbname succesfully deleted" || echo "$dbname deletion fails" >&2
  73. done
  74. echo "Done."
  75. else
  76. echo "You didn't answer 'Y' (case matters), exiting"
  77. fi
  78. exit
  79. elif echo $1 | grep -E "[A-Za-z]" &>/dev/null
  80. then
  81. usage
  82. exit 0
  83. fi
  84. #Check for the presence of pwgen for password generation
  85. if hash pwgen 2>/dev/null
  86. then
  87. echo "Using pwgen to generate passwords"
  88. rnd_pass_cmd='pwgen 25 1'
  89. else
  90. echo "pwgen not found !!! Using \$RANDOM to generate passwords"
  91. rnd_pass_cmd='echo $RANDOM'
  92. fi
  93. ninstance=$1
  94. instance=${ninstance:=50}
  95. echo -n "You are going to create $ninstance lodel2 instances. Are you sure ? Y/n "
  96. read rep
  97. if [ "$rep" = "Y" ]
  98. then
  99. echo "GO ! (you have 3 secs to cancel)"
  100. sleep 3
  101. else
  102. echo "You didn't answer 'Y' (case matters), exiting"
  103. exit
  104. fi
  105. echo "Creating instances"
  106. for i in $(seq $ninstance)
  107. do
  108. iname="${random_name}_$(printf "%05d" $i)"
  109. slim -n $iname -c || slim_fails "creating instance"
  110. slim -n $iname -s --interface web --static-url 'http://127.0.0.1/static' --uwsgi-workers 2 || slim_fails "configuring instancee"
  111. slim -n $iname -m || slim_fails "running make in instance"
  112. #Mongo db database creation
  113. dbname="${MONGODB_DB_PREFIX}_$iname"
  114. dbuser="lodel2_$iname"
  115. dbpass=$($rnd_pass_cmd)
  116. mongo $MONGODB_HOST -u "$MONGODB_ADMIN_USER" -p "$MONGODB_ADMIN_PASSWORD" admin <<EOF
  117. use $dbname
  118. db.addUser('$dbuser', '$dbpass', ['readWrite', '$dbname'])
  119. exit
  120. EOF
  121. #Append created db to instance conf
  122. slim -n $iname -s --datasource_connectors mongodb --host localhost --user $dbuser --password $dbpass --db_name $dbname || slim_fails "configuring the instance's datasource"
  123. done