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

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