diff --git a/cyclos/hooks/pre_deploy b/cyclos/hooks/pre_deploy new file mode 100755 index 00000000..067036a1 --- /dev/null +++ b/cyclos/hooks/pre_deploy @@ -0,0 +1,62 @@ +#!/bin/bash + +## +## Get domain in option of relation "web-proxy" +## + +## XXXvlab: there is a tiny lapse of time where database is not yet +## installed, and admin password is the default value. + + +. lib/common + +set -ex + + +admin_password=$(options-get admin-password 2>/dev/null ) || exit 1 + +CONTROL_PASSWORD_FILE="$SERVICE_DATASTORE/.control-pass" +## Was it already properly propagated to database ? +control_password=$(H "${admin_password}") +if ! [ -e "$CONTROL_PASSWORD_FILE" ] || [ "$control_password" != "$(cat "$CONTROL_PASSWORD_FILE")" ]; then + + hash="$(htpasswd -nbBC 10 USER "$admin_password" | cut -f 2- -d :)" || { + err "Couldn't generate hash for admin password." + exit 1 + } + + if ! sql < <(e " + UPDATE passwords SET value = '$hash' + WHERE user_id = 1 + AND status = 'ACTIVE' + AND password_type_id in ( + SELECT id FROM password_types + WHERE input_method = 'TEXT_BOX' + AND password_mode = 'MANUAL'); + "); then + debug "Failed to set password for admin users." + exit 1 + fi + e "$control_password" > "$CONTROL_PASSWORD_FILE" +fi + + + +url=$(named-relation-get "web-proxy" url) || exit 1 + +CONTROL_URL_FILE="$SERVICE_DATASTORE/.control-url" +## Was it already properly propagated to database ? +control_url=$(H "${url}") +if ! [ -e "$CONTROL_URL_FILE" ] || [ "$control_url" != "$(cat "$CONTROL_URL_FILE")" ]; then + ## In ``configurations`` table, columns login_url, logout_url, root_url + + if ! sql < <(e " + UPDATE configurations + SET + root_url = '$url' + "); then + debug "Failed to set password for admin users." + exit 1 + fi + e "$control_password" > "$CONTROL_URL_FILE" +fi diff --git a/cyclos/lib/common b/cyclos/lib/common index c127b1da..7ba451a5 100644 --- a/cyclos/lib/common +++ b/cyclos/lib/common @@ -1,4 +1,4 @@ -# -*- mode: bash -*- +# -*- mode: shell-script -*- CYCLOS_CONFIG="/usr/local/cyclos/WEB-INF/classes/cyclos.properties" @@ -12,3 +12,62 @@ cyclos:build() { docker cp "$container_id":"$CYCLOS_CONFIG" "$HOST_CYCLOS_CONFIG" && docker rm "$container_id" } + + +named-relation-get-target-service() { + local relation="$1" ts + if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "$relation"); then + err "Couldn't find relation ${DARKCYAN}$relation${NORMAL}." + return 1 + fi + e "$ts" +} + + +## XXXvlab: Consider for inclusion in compose-core +relation-get-config() { + local relation="$1" ts relation_dir + + ts=$(named-relation-get-target-service "$relation") || return 1 + relation_dir=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "$relation") || return 1 + cat "${relation_dir}/data" +} + + +named-relation-get() { + local relation="$1" key="$2" config + + config=$(relation-get-config "$relation") || return 1 + + e "$config" | shyaml get-value "$key" || { + err "Couldn't get ${WHITE}$key${NORMAL} value" \ + "in ${DARKCYAN}$relation${NORMAL} relation's data." + exit 1 + } +} + + +sql() { + ( + DBNAME="$(named-relation-get "postgres-database" dbname)" || exit 1 + ts=$(named-relation-get-target-service "postgres-database") || exit 1 + + export SERVICE_NAME="$ts" + export SERVICE_DATASTORE="$DATASTORE/$SERVICE_NAME" + DOCKER_BASE_IMAGE=$(service_base_docker_image "$SERVICE_NAME") + export DOCKER_BASE_IMAGE + + target_charm=$(get_service_charm "$ts") || exit 1 + target_charm_path=$(charm.get_dir "$target_charm") || exit 1 + + set +e + + . "$target_charm_path/lib/common" + + set -e + + ensure_db_docker_running + + ddb "$DBNAME" + ) +} \ No newline at end of file diff --git a/cyclos/metadata.yml b/cyclos/metadata.yml index 3e858dae..57f0efd8 100644 --- a/cyclos/metadata.yml +++ b/cyclos/metadata.yml @@ -24,6 +24,8 @@ uses: - earthdistance - postgis - unaccent + init-sql: !bash-stdout | + zcat "$BASE_CHARM_PATH/src/init.sql.gz" log-rotate: #constraint: required | recommended | optional #auto: pair | summon | none ## default: pair diff --git a/cyclos/src/init.sql.gz b/cyclos/src/init.sql.gz new file mode 100644 index 00000000..8bb0f32a Binary files /dev/null and b/cyclos/src/init.sql.gz differ