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.
111 lines
2.7 KiB
111 lines
2.7 KiB
#!/bin/bash
|
|
## compose: no-hooks
|
|
|
|
## Load action gets a first argument a FILE/DIRECTORY/URL holding the necessary files.
|
|
##
|
|
##
|
|
|
|
if [ -z "$SERVICE_DATASTORE" ]; then
|
|
echo "This script is meant to be run through 'compose' to work properly." >&2
|
|
exit 1
|
|
fi
|
|
|
|
version=0.1
|
|
usage="$exname [-h|--help] DBNAME"
|
|
help="
|
|
USAGE:
|
|
|
|
$usage
|
|
|
|
DESCRIPTION:
|
|
|
|
Read stdin and content to related postgres service in the database
|
|
DBNAME. If DBNAME is not provided, it'll take the default database
|
|
from the ${DARKCYAN}postgres-database${NORMAL} relation of current
|
|
service.
|
|
|
|
EXAMPLES:
|
|
|
|
$exname < foo.sql
|
|
$exname mydb2 < foo.sql
|
|
|
|
"
|
|
|
|
|
|
dbname=
|
|
output=
|
|
while [ "$1" ]; do
|
|
case "$1" in
|
|
"--help"|"-h")
|
|
print_help >&2
|
|
exit 0
|
|
;;
|
|
"--force"|"-f")
|
|
force=yes
|
|
;;
|
|
--*|-*)
|
|
err "Unexpected optional argument '$1'"
|
|
print_usage >&2
|
|
exit 1
|
|
;;
|
|
*)
|
|
[ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
|
|
err "Unexpected positional argument '$1'"
|
|
print_usage >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
|
|
if [ -z "$dbname" ]; then
|
|
|
|
##
|
|
## Fetch default dbname in relation to postgres-database
|
|
##
|
|
|
|
## XXXvlab: can't get real config here
|
|
if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "postgres-database"); then
|
|
err "Couldn't find relation ${DARKCYAN}postgres-database${NORMAL}."
|
|
exit 1
|
|
fi
|
|
|
|
relation_dir=$(get_relation_data_dir "$SERVICE_NAME" "$ts" "postgres-database") || {
|
|
err "Failed to find relation dir"
|
|
exit 1
|
|
}
|
|
postgres_config=$(cat "$relation_dir"/data) || exit 2
|
|
|
|
dbname="$(e "$postgres_config" | shyaml get-value dbname)" || {
|
|
err "Couldn't retrieve information of ${DARKCYAN}postgres-database${NORMAL}'s relation."
|
|
exit 1
|
|
}
|
|
dbuser="$(e "$postgres_config" | shyaml get-value user)" || {
|
|
err "Couldn't retrieve information of ${DARKCYAN}postgres-database${NORMAL}'s relation."
|
|
exit 1
|
|
}
|
|
password="$(e "$postgres_config" | shyaml get-value password)" || {
|
|
err "Couldn't retrieve information of ${DARKCYAN}postgres-database${NORMAL}'s relation."
|
|
exit 1
|
|
}
|
|
|
|
fi
|
|
|
|
set -e
|
|
|
|
containers="$(get_running_containers_for_service "$RELATION_TARGET_SERVICE")"
|
|
|
|
if [ -z "$containers" ]; then
|
|
err "No containers running for service $DARKYELLOW$service$NORMAL."
|
|
exit 1
|
|
fi
|
|
|
|
|
|
|
|
## XXXvlab: taking first container is probably not a good idea
|
|
container_id="$(echo "$containers" | head -n 1)"
|
|
docker exec -i -u 0 \
|
|
-e PGUSER="$dbuser" \
|
|
-e PGPASSWORD="$password" \
|
|
"${container_id}" psql -qAt "$dbname"
|