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.
 
 

65 lines
1.7 KiB

#!/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``.
. lib/common
set -e
DBNAME=$(relation-get dbname) || {
DBNAME=$(service:map:get_one "dbname") || exit 1
DBNAME="${DBNAME:-$BASE_SERVICE_NAME}"
relation-set dbname "$DBNAME"
}
USER=$(relation-get user) || {
USER=$(service:map:get_one "user") || exit 1
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"
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