#!/bin/bash ## When writing relation script, remember: ## - they should be idempotents ## - they can be launched while the dockers is already up ## - they are launched from the host ## - the target of the link is launched first, and get a chance to ``relation-set`` ## - both side of the scripts get to use ``relation-get``. DBNAME=$(relation-get dbname) || { DBNAME="$BASE_SERVICE_NAME" relation-set dbname "$DBNAME" } USER=$(relation-get user) || { USER="$BASE_SERVICE_NAME" relation-set user "$USER" } HOST=$(options-get host) || true if [ -z "$HOST" ]; then err "No host specified. Please specify ${WHITE}host${NORMAL}" \ "in ${DARKYELLOW}$SERVICE_NAME${NORMAL}'s option." exit 1 fi if [[ "$HOST" =~ :[0-9]+$ ]]; then PORT="${HOST##*:}" HOST="${HOST%:*}" fi if ! [[ "$HOST" =~ ^[0-9a-zA-Z.-]+$ ]]; then err "Invalid host specified: '$HOST'. Please provide a valid" \ "network target as ${WHITE}host${NORMAL} value" \ "in ${DARKYELLOW}$SERVICE_NAME${NORMAL}'s option." exit 1 fi PORT=${PORT:-5432} relation-set host "$HOST" relation-set port "$PORT" . lib/common set -e if ! PASSWORD="$(relation-get password 2>/dev/null)"; then err "Please provide a password" exit 1 fi echo "SELECT 1;" | ddb "$DBNAME" || { err "Connection failed to $DBNAME on $HOST:$PORT with user $USER and given password" exit 1 } array_read-0 extensions < <(relation-get extensions 2>/dev/null | shyaml get-values-0) if [ "${#extensions[@]}" -gt 0 ]; then db_install_extensions "$DBNAME" "${extensions[@]}" || exit 1 fi