Doc, tools for lokavaluto development
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.

162 lines
6.7 KiB

  1. #!/bin/sh
  2. ROOT_DIR=$(pwd)
  3. DATA_ONLY=0
  4. for i in "$@"
  5. do
  6. case $i in
  7. -e|--env)
  8. ENV="$2"
  9. shift #past argument
  10. shift #past value
  11. ;;
  12. -d|--data-only)
  13. DATA_ONLY=1
  14. shift
  15. ;;
  16. -h|--help)
  17. echo "Generates scripts to initialize docker services and containers with a dev/test dataset \n"
  18. echo "WARNING : This script should never be executed in a production context \n"
  19. echo "Usage: --env ENV [--data-only] \n"
  20. echo "Options: \r"
  21. echo " -e, --env Set environment variable. It can be either dev/test. \r"
  22. echo " -d, --data-only Delete then generate dataset only instead of all services and docker containers. \r"
  23. echo " -h, --help Show this prompt with a list of options. \r"
  24. exit 0
  25. ;;
  26. esac
  27. done
  28. if [ "$ENV" = "dev" -o "$ENV" = "test" ]; then
  29. #analyse de la commande
  30. #if config to generate
  31. if [ "$DATA_ONLY" = "0" ]; then
  32. if [ "$ENV" = "dev" ]; then
  33. OTHER_ENV="test"
  34. else
  35. OTHER_ENV="dev"
  36. fi
  37. if [ -f ./etc/cyclos/cyclos_constants_$ENV.yml ] || [ ! -f ./etc/cyclos/cyclos_constants_$OTHER_ENV.yml ]; then
  38. read -p "You are about to regenerate all the docker services. Are you sure? (y/n)" response
  39. if [ -z $response ]; then
  40. response="n"
  41. echo "To regenerate dataset only, use -d option"
  42. fi
  43. while [ $response != "y" ] && [ $response != "n" ]; do
  44. read -p "You are about to regenerate all the docker services. Are you sure? (y/n)" response
  45. done
  46. if [ $response = "n" ]; then
  47. exit 0
  48. fi
  49. echo "$(tput setaf 3) Delete cyclos data... $(tput sgr 0)"
  50. rm -rf data/cyclos
  51. rm -f etc/cyclos/cyclos_constants_$ENV.yml
  52. rm -f etc/cyclos/cyclos_constants_$OTHER_ENV.yml
  53. echo "$(tput setaf 2) Delete cyclos data... OK ! "
  54. sleep 2
  55. echo "$(tput setaf 3) Stop and remove containers, networks and volumes... $(tput sgr 0)"
  56. docker-compose down -v
  57. echo "$(tput setaf 2) Stop and remove containers, networks and volumes... OK !"
  58. sleep 2
  59. echo "$(tput setaf 3) (Re)create cyclos database container from cyclos-db service $(tput sgr 0)"
  60. docker-compose up -d cyclos-db
  61. echo "INFO : Wait for cyclos init process to finish initial dump. It should take up to 3mins \n"
  62. echo "HINT : In order to check progression, input sudo docker-compose logs -f cyclos-db in another terminal \n"
  63. docker logs cyclos-db 2>&1 | grep -Pzl '(?s)init process complete.*\n.*ready to accept connections'
  64. while [ $? -ne 0 ]; do
  65. sleep 5;
  66. #this must be the last line of the while loop
  67. docker logs cyclos-db 2>&1 | grep -Pzl '(?s)init process complete.*\n.*ready to accept connections'
  68. done
  69. echo "$(tput setaf 2) (Re)create cyclos database container from cyclos-db service... OK !"
  70. sleep 2
  71. docker-compose up -d cyclos-app
  72. sleep 2
  73. docker-compose up -d odoo
  74. echo "wait for odoo postgres database to finish process after startup"
  75. docker logs postgres_odoo 2>&1 | grep -Pzl '(?s)init process complete'
  76. while [ $? -ne 0 ]; do
  77. sleep 5;
  78. #this must be the last line of the while loop
  79. docker logs postgres_odoo 2>&1 | grep -Pzl '(?s)init process complete'
  80. done
  81. ## Update postgres password
  82. docker-compose exec -T postgres bash -c \
  83. 'PGUSER=postgres psql <<<"ALTER USER postgres WITH ENCRYPTED password "'\\\''$POSTGRES_ROOT_PASSWORD'\\\'
  84. ## Set pg_hba.conf
  85. docker-compose exec -T postgres bash -c '
  86. PG_HBA=/var/lib/postgresql/data/pg_hba.conf
  87. if ! grep -E "^host all all (0.0.0.0/0|all) md5\$" "$PG_HBA" >/dev/null 2>&1; then
  88. if grep -E "^host all all (0.0.0.0/0|all) trust\$" "$PG_HBA" >/dev/null 2>&1; then
  89. sed -ri '\''s%^host all all (0\.0\.0\.0/0|all) trust$%host all all \1 md5%g'\'' \
  90. "$PG_HBA"
  91. echo "Accepting connection from outside."
  92. else
  93. echo "Can'\''t ensure connection from outside." >&2
  94. exit 1
  95. fi
  96. fi
  97. '
  98. if [ "$?" != 0 ]; then
  99. echo "Error: can't update pg_hba.conf" >&2
  100. else
  101. docker-compose restart postgres
  102. fi
  103. sleep 5
  104. docker-compose exec -T postgres bash -c \
  105. 'PGUSER=postgres createdb $PG_DATABASE'
  106. docker-compose exec -T postgres bash -c \
  107. 'PGUSER=postgres psql $PG_DATABASE -c "CREATE EXTENSION IF NOT EXISTS unaccent;"'
  108. docker-compose exec -T postgres bash -c \
  109. 'PGUSER=postgres psql <<<"CREATE USER \"$PG_USER\" WITH PASSWORD '\''$PG_PASS'\'' CREATEDB NOCREATEROLE;"'
  110. docker-compose exec -T postgres bash -c \
  111. 'PGUSER=postgres prefix_pg_local_command=" " pgm chown $PG_USER $PG_DATABASE'
  112. fi
  113. echo "$(tput setaf 3) Generate cyclos configuration and initial data... $(tput sgr 0)"
  114. docker-compose exec -e ENV=$ENV cyclos-app sh /cyclos/setup_cyclos.sh
  115. echo "$(tput setaf 2) Generate cyclos configuration and initial data... OK !"
  116. else
  117. if [ -f ./etc/cyclos/cyclos_constants_$ENV.yml ]; then
  118. echo "$(tput setaf 3) Clean cyclos database from all users, payments and accounts data... $(tput sgr 0)"
  119. sleep 2
  120. docker-compose exec -T cyclos-db psql -v network="'%$ENV%'" -U cyclos cyclos < etc/cyclos/script_clean_database.sql
  121. echo "$(tput setaf 2) Clean cyclos database from all users, payments and accounts data... OK !"
  122. sleep 2
  123. echo "$(tput setaf 3) Regenerate cyclos init data : users, accounts credit and payments ... $(tput sgr 0)"
  124. sleep 2
  125. docker-compose exec -T -e ENV=$ENV cyclos-app python3 /cyclos/init_test_data.py http://cyclos-app:8080/ YWRtaW46YWRtaW4=
  126. echo "$(tput setaf 2) Regenerate cyclos init data : users, accounts credit and payments... OK !"
  127. else
  128. echo "Cyclos constants file not found for $ENV mode. The cyclos containers and configuration have not been settled. \n Remove -d option from the command"
  129. exit 0
  130. fi
  131. fi
  132. else
  133. echo "choose dev / test as a script variable"
  134. exit 1
  135. fi