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.

171 lines
4.6 KiB

  1. #!/bin/bash
  2. ##
  3. ## Get domain in option of relation "publish-dir"
  4. ##
  5. ## XXXvlab: there is a tiny lapse of time where database is not yet
  6. ## installed, and admin password is the default value.
  7. ## XXXvlab: can't get real config here
  8. if ! read-0 publish_dir_ts _ _ < <(get_service_relation "$SERVICE_NAME" "publish-dir"); then
  9. err "Couldn't find relation ${DARKCYAN}publish-dir${NORMAL}."
  10. exit 1
  11. fi
  12. publish_dir_relation_dir=$(get_relation_data_dir "$SERVICE_NAME" "$publish_dir_ts" "publish-dir") || {
  13. err "Failed to find relation file"
  14. exit 1
  15. }
  16. publish_dir_relation_config=$(cat "$publish_dir_relation_dir/data") || exit 2
  17. domain=$(e "$publish_dir_relation_config" | shyaml get-value domain) || {
  18. err "Couldn't get domain information in ${DARKCYAN}publish-dir${NORMAL} relation's data."
  19. exit 1
  20. }
  21. url=$(e "$publish_dir_relation_config" | shyaml get-value url) || {
  22. err "Couldn't get domain information in ${DARKCYAN}publish-dir${NORMAL} relation's data."
  23. exit 1
  24. }
  25. ##
  26. ## Local charm config
  27. ##
  28. admin_password=$(options-get admin-password 2>/dev/null ) || exit 1
  29. admin_email=$(options-get admin-email 2>/dev/null ) || true
  30. admin_email=${admin_email:-"admin@$domain"}
  31. CONTROL="$SERVICE_DATASTORE/.control"
  32. ## Was it already properly propagated to database ?
  33. control=$(H "${admin_password}" "${admin_email}")
  34. if [ -e "$CONTROL" ] && [ "$control" == "$(cat "$CONTROL")" ]; then
  35. exit 0
  36. fi
  37. ## XXXvlab: can't get real config here
  38. if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "mysql-database"); then
  39. err "Couldn't find relation ${DARKCYAN}mysql-database${NORMAL}."
  40. exit 1
  41. fi
  42. ##
  43. ## We need mysql relation config also
  44. ##
  45. relation_file=$(get_relation_data_file "$SERVICE_NAME" "$ts" "mysql-database") || {
  46. err "Failed to find relation file"
  47. exit 1
  48. }
  49. mysql_config=$(cat "$relation_file") || exit 2
  50. DBNAME="$(e "$mysql_config" | shyaml get-value dbname)" || {
  51. err "Couldn't retrieve information of ${DARKCYAN}mysql-database${NORMAL}'s relation."
  52. exit 1
  53. }
  54. CONFIG="$SERVICE_DATASTORE/opt/apps/piwigo/local/config/database.inc.php"
  55. if [ -f "$CONFIG" ]; then
  56. debug "Database already configured."
  57. export SERVICE_NAME="$ts"
  58. export SERVICE_DATASTORE="$DATASTORE/$SERVICE_NAME"
  59. DOCKER_BASE_IMAGE=$(service_base_docker_image "$SERVICE_NAME")
  60. export DOCKER_BASE_IMAGE
  61. target_charm=$(get_service_charm "$ts") || exit 1
  62. target_charm_path=$(charm.get_dir "$target_charm") || exit 1
  63. set +e
  64. . "$target_charm_path/lib/common"
  65. set -e
  66. ensure_db_docker_running
  67. if ! out=$(ddb "$DBNAME" < <(e "UPDATE users SET password=md5('$admin_password'), mail_address='$admin_email' WHERE id=1")); then
  68. debug "Failed to set password for admin users."
  69. exit 1
  70. fi
  71. e "$control" > "$CONTROL"
  72. exit 0
  73. fi
  74. ##
  75. ## We are in post_deploy, so our service is up, we need to get
  76. ## the ``network`` and ``container``'s id to communicate with
  77. ## him.
  78. ##
  79. container_id=$(
  80. for container_id in $(get_running_containers_for_service "$MASTER_BASE_SERVICE_NAME"); do
  81. e "$container_id"
  82. break
  83. done
  84. )
  85. read-0 network container_ip < <(get_container_network_ip "$container_id")
  86. ##
  87. ## We'll need password from mysql database config
  88. ##
  89. PASSWORD="$(e "$mysql_config" | shyaml get-value password)" &&
  90. USER="$(e "$mysql_config" | shyaml get-value user)" || {
  91. err "Couldn't retrieve information of ${DARKCYAN}mysql-database${NORMAL}'s relation."
  92. exit 1
  93. }
  94. default_lang=$(options-get default-lang) || exit 1
  95. ## We deliberately are not setting these: (they will be considered off)
  96. # newsletter_subscribe "on"
  97. # send_password_by_mail "on"
  98. debug "Running install.php"
  99. out=$(docker run --network "$network" docker.0k.io/curl -s -X POST -H "Host: $domain" -L \
  100. -F language="$default_lang" \
  101. -F dbhost="$ts" \
  102. -F dbuser="$USER" \
  103. -F dbpasswd="$PASSWORD" \
  104. -F dbname="$DBNAME" \
  105. -F prefix="" \
  106. -F admin_name="admin" \
  107. -F admin_pass1="$admin_password" \
  108. -F admin_pass2="$admin_password" \
  109. -F admin_mail="$admin_email" \
  110. -F install="Start+installation" \
  111. "$url/install.php") || {
  112. err "Failed to run install.php script"
  113. exit 1
  114. }
  115. #e "$out" > "$SERVICE_DATASTORE/out.html"
  116. # debug "written $SERVICE_DATASTORE/out.html"
  117. had_error=
  118. while read-0 error_msg; do
  119. had_error=1
  120. err "Installation failed with these errors:"
  121. echo "- ${error_msg}" >&2
  122. done < <(e "$out" | xpath "//div[@class='errors']/ul/li/text()")
  123. if [ "$had_error" ]; then
  124. exit 1
  125. fi
  126. debug "No error catched on \`\`install.php\`\` result."
  127. e "$control" > "$CONTROL"
  128. exit 0