fork 0k-charms
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.

66 lines
1.8 KiB

  1. #!/bin/bash
  2. ## Init is run on host
  3. ## For now it is run every time the script is launched, but
  4. ## it should be launched only once after build.
  5. ## Accessible variables are:
  6. ## - SERVICE_NAME Name of current service
  7. ## - DOCKER_BASE_IMAGE Base image from which this service might be built if any
  8. ## - SERVICE_DATASTORE Location on host of the DATASTORE of this service
  9. ## - SERVICE_CONFIGSTORE Location on host of the CONFIGSTORE of this service
  10. . lib/common
  11. set -e
  12. if [ "${HOST_DATASTORE+x}" ]; then
  13. export HOST_DB_PASSFILE="$HOST_DATASTORE/${SERVICE_NAME}$DB_DATADIR/my.cnf"
  14. else
  15. export HOST_DB_PASSFILE="$CLIENT_DB_PASSFILE"
  16. fi
  17. if ! [ -d "$HOST_DATASTORE/${SERVICE_NAME}$DB_DATADIR" ]; then
  18. MYSQL_ROOT_PASSWORD="$(gen_password)"
  19. mkdir -p "${HOST_DB_PASSFILE%/*}"
  20. ## XXXvlab: this won't help support multiple project running on the
  21. ## same host
  22. cat <<EOF > "$HOST_DB_PASSFILE"
  23. [client]
  24. password=$MYSQL_ROOT_PASSWORD
  25. EOF
  26. chmod 600 "$HOST_DB_PASSFILE"
  27. ## deactivating final connection check
  28. ddb () { true; }
  29. export -f ddb
  30. ensure_db_docker_running || exit 1
  31. docker exec -i "$_DB_NAME" mysql <<EOF
  32. USE mysql;
  33. GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD' WITH GRANT OPTION;
  34. GRANT ALL ON *.* TO 'root'@'localhost' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD' WITH GRANT OPTION;
  35. SET PASSWORD FOR 'root'@'localhost'=PASSWORD('${MYSQL_ROOT_PASSWORD}');
  36. FLUSH PRIVILEGES;
  37. EOF
  38. . lib/common
  39. err=$(echo "$check_command" | ddb 2>&1 >/dev/null) || {
  40. err "Docker run probably failed to do it's job."
  41. echo "$err" | prefix " " >&2
  42. exit 1
  43. }
  44. info "New root password for mysql. "
  45. fi
  46. init-config-add "\
  47. $MASTER_BASE_SERVICE_NAME:
  48. volumes:
  49. - '$HOST_DB_PASSFILE:/root/.my.cnf'
  50. "