Browse Source

new: [compose-core] add relation and service traversal utilities

Signed-off-by: Valentin Lab <valentin.lab@kalysto.org>
cache-relation
Valentin Lab 3 years ago
parent
commit
d486bf50bd
  1. 68
      bin/compose-core

68
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

Loading…
Cancel
Save