From d486bf50bd3a10dcdd3d0a5da2787ffde65d1ee8 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Tue, 15 Mar 2022 11:51:21 +0100 Subject: [PATCH] new: [compose-core] add relation and service traversal utilities Signed-off-by: Valentin Lab --- bin/compose-core | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/bin/compose-core b/bin/compose-core index 5f89edc..4f42012 100755 --- a/bin/compose-core +++ b/bin/compose-core @@ -2117,6 +2117,74 @@ get_service_relation() { export -f get_service_relation +export TRAVERSE_SEPARATOR=: +## Traverse on first service satisfying relation +service:traverse() { + local service_path="$1" + { + SEPARATOR=: + read -d "$TRAVERSE_SEPARATOR" service + while read -d "$TRAVERSE_SEPARATOR" relation; do + ## XXXvlab: Take only first service + if ! read-0 ts _ _ < <(get_service_relation "${service}" "${relation}"); then + err "Couldn't find relation ${DARKCYAN}${relation}${NORMAL}" \ + "from ${DARKYELLOW}$service${NORMAL}." + return 1 + fi + service="$ts" + done + echo "$service" + } < <(e "${service_path}${TRAVERSE_SEPARATOR}") +} +export -f service:traverse + + +service:relation-file() { + local service_path="$1" relation service relation_file + if ! [[ "$service_path" == *"$TRAVERSE_SEPARATOR"* ]]; then + err "Invalid argument '$service_path'." \ + "Must provide a service path (no '${TRAVERSE_SEPARATOR}' found)." + return 1 + fi + relation="${service_path##*${TRAVERSE_SEPARATOR}}" + service=$(service:traverse "${service_path%${TRAVERSE_SEPARATOR}*}") || return 1 + if ! read-0 ts _ _ < <(get_service_relation "${service}" "${relation}"); then + err "Couldn't find relation ${DARKCYAN}${relation}${NORMAL}" \ + "from ${DARKYELLOW}$service${NORMAL}." + return 1 + fi + relation_dir=$(get_relation_data_dir "$service" "$ts" "$relation") || { + err "Failed to find relation file" + return 1 + } + echo "$relation_dir/data" +} +export -f service:relation-file + + +service:relation-options() { + local service_path="$1" relation_file + relation_file=$(service:relation-file "$service_path") || { + err "Failed to find relation file" + return 1 + } + cat "$relation_file" +} +export -f service:relation-options + + +relation:get() { + local service_path="$1" query="$2" relation_file + relation_file=$(service:relation-file "$service_path") || { + err "Failed to find relation file" + return 1 + } + cfg-get-value "$query" < "$relation_file" +} +export -f relation:get + + + _get_charm_metadata_uses() { local metadata="$1" cache_file="$CACHEDIR/$FUNCNAME.cache.$(printf "%s\0" "$@" | md5_compat)" if [ -e "$cache_file" ]; then