From 9c71d5e2ee1d6be8ac31f0f484c5b1a9579d0a20 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Wed, 12 Jan 2022 18:49:38 +0100 Subject: [PATCH] new: [compose-core] provide ``get_healthy_container_ip_for_service`` and ``switch_to_relation_service`` to charms base library Signed-off-by: Valentin Lab --- bin/compose-core | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/bin/compose-core b/bin/compose-core index d115c7c..dc23774 100755 --- a/bin/compose-core +++ b/bin/compose-core @@ -3103,6 +3103,65 @@ get_running_compose_containers() { export -f get_running_compose_containers +get_healthy_container_ip_for_service () { + local service="$1" port="$2" timeout=${3:-60} + + local containers container container_network container_ip + + containers="$(get_running_containers_for_service "$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="$(echo "$containers" | head -n 1)" + + ## XXXvlab: taking first ip is probably not a good idea + read-0 container_network container_ip < <(get_container_network_ip "$container") + + if [ -z "$container_ip" ]; then + err "Can't get container's IP. You should check health of" \ + "${DARKYELLOW}$service${NORMAL}'s container." + exit 1 + fi + + wait_for_tcp_port "$container_network" "$container_ip:8069" 4 || { + err "TCP port of ${DARKYELLOW}$service${NORMAL}'s container doesn't seem open" + echo " Please check that container is healthy. Here are last logs:" >&2 + docker logs "$container" --tail=10 | prefix " ${GRAY}|${NORMAL} " >&2 + exit 1 + } + info "Host/Port ${container_ip}:${port} checked ${GREEN}open${NORMAL}." + echo "$container_network:$container_ip" +} +export -f get_healthy_container_ip_for_service + + +switch_to_relation_service() { + local relation="$1" + + ## XXXvlab: can't get real config here + if ! read-0 ts _ _ < <(get_service_relation "$SERVICE_NAME" "$relation"); then + err "Couldn't find relation ${DARKCYAN}$relation${NORMAL}." + exit 1 + fi + + export SERVICE_NAME="$ts" + export SERVICE_DATASTORE="$DATASTORE/$SERVICE_NAME" + DOCKER_BASE_IMAGE=$(service_base_docker_image "$SERVICE_NAME") + export DOCKER_BASE_IMAGE + + target_charm=$(get_service_charm "$ts") || exit 1 + target_charm_path=$(charm.get_dir "$target_charm") || exit 1 + + cd "$target_charm_path" + +} +export -f switch_to_relation_service + + get_volumes_for_container() { local container="$1"