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

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