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.

141 lines
4.0 KiB

  1. #!/bin/bash
  2. . lib/common
  3. set -e
  4. ## If no admin-password set, then don't try to pre-initialize database
  5. admin_password=$(options-get admin-password 2>/dev/null) || exit 0
  6. admin_email=$(options-get admin-email 2>/dev/null ) || true
  7. ## XXXvlab: can't get real config here
  8. if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "mongo-database"); then
  9. err "Couldn't find relation ${DARKCYAN}mongo-database${NORMAL}."
  10. exit 1
  11. fi
  12. ##
  13. ## We need mongo relation config also
  14. ##
  15. relation_file=$(get_relation_data_file "$SERVICE_NAME" "$ts" "mongo-database") || {
  16. err "Failed to find relation file"
  17. exit 1
  18. }
  19. mongo_config=$(cat "$relation_file") || exit 2
  20. DBNAME="$(e "$mongo_config" | shyaml get-value dbname)" || {
  21. err "Couldn't retrieve information of ${DARKCYAN}mongo-database${NORMAL}'s relation."
  22. exit 1
  23. }
  24. CONTROL="$SERVICE_DATASTORE/.control"
  25. ## Was it already properly propagated to database ?
  26. control=$(H "${admin_password}" "${admin_email}")
  27. if [ -e "$CONTROL" ]; then
  28. if [ "$control" == "$(cat "$CONTROL")" ]; then
  29. exit 0
  30. else
  31. err "Changing admin password in compose file not yet supported"
  32. exit 1
  33. fi
  34. fi
  35. ##
  36. ## Get domain in option of relation "publish-dir"
  37. ##
  38. ## XXXvlab: there is a tiny lapse of time where database is not yet
  39. ## installed, and admin password is the default value.
  40. ## XXXvlab: can't get real config here
  41. if ! read-0 publish_dir_ts _ _ < <(get_service_relation "$SERVICE_NAME" "publish-dir"); then
  42. err "Couldn't find relation ${DARKCYAN}publish-dir${NORMAL}."
  43. exit 1
  44. fi
  45. publish_dir_relation_dir=$(get_relation_data_dir "$SERVICE_NAME" "$publish_dir_ts" "publish-dir") || {
  46. err "Failed to find relation file"
  47. exit 1
  48. }
  49. publish_dir_relation_config=$(cat "$publish_dir_relation_dir/data") || exit 2
  50. domain=$(e "$publish_dir_relation_config" | shyaml get-value domain) || {
  51. err "Couldn't get domain information in ${DARKCYAN}publish-dir${NORMAL} relation's data."
  52. exit 1
  53. }
  54. url=$(e "$publish_dir_relation_config" | shyaml get-value url) || {
  55. err "Couldn't get domain information in ${DARKCYAN}publish-dir${NORMAL} relation's data."
  56. exit 1
  57. }
  58. protocol="${url%%://*}"
  59. ##
  60. ## We are in post_deploy, so our service is up, we need to get
  61. ## the ``network`` and ``container``'s id to communicate with
  62. ## him.
  63. ##
  64. container_id=$(
  65. for container_id in $(get_running_containers_for_service "$MASTER_BASE_SERVICE_NAME"); do
  66. e "$container_id"
  67. break
  68. done
  69. )
  70. admin_email=${admin_email:-"admin@$domain"}
  71. read-0 network container_ip < <(get_container_network_ip "$container_id")
  72. debug "Running index.php/project/initialize"
  73. echo docker run --network "$network" docker.0k.io/curl -k -s -X POST -H "Host: $domain" -L \
  74. -F sonata_user_registration[username]="admin" \
  75. -F sonata_user_registration[email]="$admin_email" \
  76. -F sonata_user_registration[plainPassword][first]="$admin_password" \
  77. -F sonata_user_registration[plainPassword][second]="$admin_password" \
  78. "$protocol://$container_ip/index.php/project/initialize"
  79. out=$(docker run --network "$network" docker.0k.io/curl -k -s -X POST -H "Host: $domain" -L \
  80. -F sonata_user_registration[username]="admin" \
  81. -F sonata_user_registration[email]="$admin_email" \
  82. -F sonata_user_registration[plainPassword][first]="$admin_password" \
  83. -F sonata_user_registration[plainPassword][second]="$admin_password" \
  84. "$protocol://$container_ip/index.php/project/initialize") || {
  85. err "Failed to run project/initialize script"
  86. echo "$out"
  87. exit 1
  88. }
  89. # #e "$out" > "$SERVICE_DATASTORE/out.html"
  90. # # debug "written $SERVICE_DATASTORE/out.html"
  91. # had_error=
  92. # while read-0 error_msg; do
  93. # had_error=1
  94. # err "Installation failed with these errors:"
  95. # echo "- ${error_msg}" >&2
  96. # done < <(e "$out" | xpath "//div[@class='errors']/ul/li/text()")
  97. # if [ "$had_error" ]; then
  98. # exit 1
  99. # fi
  100. # debug "No error catched on \`\`install.php\`\` result."
  101. # e "$control" > "$CONTROL"
  102. exit 0