From c64104513040218512a8bffe512160314996dc73 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Fri, 23 Oct 2020 15:44:40 +0200 Subject: [PATCH] new: [gogocarto] set admin password on first install Signed-off-by: Valentin Lab --- gogocarto/hooks/post_deploy | 141 ++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100755 gogocarto/hooks/post_deploy diff --git a/gogocarto/hooks/post_deploy b/gogocarto/hooks/post_deploy new file mode 100755 index 0000000..e746e6e --- /dev/null +++ b/gogocarto/hooks/post_deploy @@ -0,0 +1,141 @@ +#!/bin/bash + +. lib/common + + +set -e + + +## If no admin-password set, then don't try to pre-initialize database +admin_password=$(options-get admin-password 2>/dev/null) || exit 0 +admin_email=$(options-get admin-email 2>/dev/null ) || true + + +## XXXvlab: can't get real config here +if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "mongo-database"); then + err "Couldn't find relation ${DARKCYAN}mongo-database${NORMAL}." + exit 1 +fi + + +## +## We need mongo relation config also +## + +relation_file=$(get_relation_data_file "$SERVICE_NAME" "$ts" "mongo-database") || { + err "Failed to find relation file" + exit 1 +} + +mongo_config=$(cat "$relation_file") || exit 2 + +DBNAME="$(e "$mongo_config" | shyaml get-value dbname)" || { + err "Couldn't retrieve information of ${DARKCYAN}mongo-database${NORMAL}'s relation." + exit 1 +} + + + +CONTROL="$SERVICE_DATASTORE/.control" +## Was it already properly propagated to database ? +control=$(H "${admin_password}" "${admin_email}") +if [ -e "$CONTROL" ]; then + if [ "$control" == "$(cat "$CONTROL")" ]; then + exit 0 + else + err "Changing admin password in compose file not yet supported" + exit 1 + fi +fi + + +## +## Get domain in option of relation "publish-dir" +## + +## XXXvlab: there is a tiny lapse of time where database is not yet +## installed, and admin password is the default value. + +## XXXvlab: can't get real config here +if ! read-0 publish_dir_ts _ _ < <(get_service_relation "$SERVICE_NAME" "publish-dir"); then + err "Couldn't find relation ${DARKCYAN}publish-dir${NORMAL}." + exit 1 +fi + +publish_dir_relation_dir=$(get_relation_data_dir "$SERVICE_NAME" "$publish_dir_ts" "publish-dir") || { + err "Failed to find relation file" + exit 1 +} + +publish_dir_relation_config=$(cat "$publish_dir_relation_dir/data") || exit 2 + +domain=$(e "$publish_dir_relation_config" | shyaml get-value domain) || { + err "Couldn't get domain information in ${DARKCYAN}publish-dir${NORMAL} relation's data." + exit 1 +} + +url=$(e "$publish_dir_relation_config" | shyaml get-value url) || { + err "Couldn't get domain information in ${DARKCYAN}publish-dir${NORMAL} relation's data." + exit 1 +} + +protocol="${url%%://*}" + + +## +## We are in post_deploy, so our service is up, we need to get +## the ``network`` and ``container``'s id to communicate with +## him. +## + +container_id=$( + for container_id in $(get_running_containers_for_service "$MASTER_BASE_SERVICE_NAME"); do + e "$container_id" + break + done +) + + +admin_email=${admin_email:-"admin@$domain"} + + +read-0 network container_ip < <(get_container_network_ip "$container_id") + + +debug "Running index.php/project/initialize" +echo docker run --network "$network" docker.0k.io/curl -k -s -X POST -H "Host: $domain" -L \ + -F sonata_user_registration[username]="admin" \ + -F sonata_user_registration[email]="$admin_email" \ + -F sonata_user_registration[plainPassword][first]="$admin_password" \ + -F sonata_user_registration[plainPassword][second]="$admin_password" \ + "$protocol://$container_ip/index.php/project/initialize" +out=$(docker run --network "$network" docker.0k.io/curl -k -s -X POST -H "Host: $domain" -L \ + -F sonata_user_registration[username]="admin" \ + -F sonata_user_registration[email]="$admin_email" \ + -F sonata_user_registration[plainPassword][first]="$admin_password" \ + -F sonata_user_registration[plainPassword][second]="$admin_password" \ + "$protocol://$container_ip/index.php/project/initialize") || { + err "Failed to run project/initialize script" + echo "$out" + exit 1 +} + + +# #e "$out" > "$SERVICE_DATASTORE/out.html" +# # debug "written $SERVICE_DATASTORE/out.html" +# had_error= +# while read-0 error_msg; do +# had_error=1 +# err "Installation failed with these errors:" +# echo "- ${error_msg}" >&2 +# done < <(e "$out" | xpath "//div[@class='errors']/ul/li/text()") + +# if [ "$had_error" ]; then +# exit 1 +# fi + +# debug "No error catched on \`\`install.php\`\` result." +# e "$control" > "$CONTROL" + + +exit 0