From dbba0b1d89839832d9bf9db7f92ae0de41bdb734 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Tue, 19 Apr 2016 21:28:41 +0800 Subject: [PATCH] new: ``host-resources`` implementation. --- bin/compose | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/bin/compose b/bin/compose index 24927ae..b3cd05b 100755 --- a/bin/compose +++ b/bin/compose @@ -299,6 +299,10 @@ export -f image_exposed_ports_0 ## Generic ## +fn.exists() { + declare -F "$1" >/dev/null +} + str_matches() { local str="$1" shift @@ -1104,6 +1108,95 @@ EOF } +host_resource_get() { + local location="$1" cfg="$2" + type=$(echo "$cfg" | shyaml get-value type 2>/dev/null) || { + err "Missing ${WHITE}type$NORMAL option in ${WHITE}get$NORMAL config for location '$location'" + return 1 + } + if fn.exists host_resource_get_$type; then + host_resource_get_$type "$location" "$cfg" + else + err "Source ${WHITE}source$NORMAL type '$type' unknown for" \ + "${WHITE}host-resource$NORMAL '$location' defined in" \ + "$DARKYELLOW$subservice$NORMAL config." + return 1 + fi +} +export -f host_resource_get + + +host_resource_get_git() { + local location="$1" cfg="$2" branch parent url + branch=$(echo "$cfg" | shyaml get-value branch 2>/dev/null) + branch=${branch:-master} + + url=$(echo "$cfg" | shyaml get-value url 2>/dev/null) + parent="$(dirname "$location")" + ( + mkdir -p "$parent" && cd "$parent" && + git clone -b "$branch" "$url" "$(basename "$location")" + ) || return 1 +} +export -f host_resource_get_git + + +host_resource_get_git-sub() { + local location="$1" cfg="$2" branch parent url + branch=$(echo "$cfg" | shyaml get-value branch 2>/dev/null) + branch=${branch:-master} + + url=$(echo "$cfg" | shyaml get-value url 2>/dev/null) + parent="$(dirname "$location")" + ( + mkdir -p "$parent" && cd "$parent" && + git sub clone -b "$branch" "$url" "$(basename "$location")" + ) || return 1 +} +export -f host_resource_get_git-sub + + +setup_host_resources () { + local services="$1" action="$2" loaded location cfg + + declare -A loaded + for service in $services; do + for subservice in $(get_ordered_service_dependencies "$service"); do + if [ "${loaded[$subservice]}" ]; then + ## Prevent double inclusion of same service if this + ## service is deps of two or more of your + ## requirements. + continue + fi + + service_def=$(get_compose_service_def "$subservice") || return 1 + while read-0 location cfg; do + ## XXXvlab: will it be a git resources always ? + if [ -d "$location" -a ! -d "$location/.git" ]; then + err "Hum, location '$location' does not seem to be a git directory." + return 1 + fi + if [ -d "$location" ]; then + info "host resource '$location' already set up." + continue + fi + get=$(echo "$cfg" | shyaml get-value get 2>/dev/null) + + if [ -z "$get" ]; then + err "No host directory '$location' found, and no ${WHITE}source$NORMAL" \ + "specified for $DARKYELLOW$subservice$NORMAL." + return 1 + fi + host_resource_get "$location" "$get" || return 1 + done < <(echo "$service_def" | shyaml key-values-0 host-resources 2>/dev/null) + + loaded[$subservice]=1 + done + done + return 0 +} + + relation-get () { local key="$1" cat "$RELATION_DATA_FILE" | shyaml get-value "$key" 2>/dev/null @@ -2240,6 +2333,8 @@ esac if [ "$full_init" ]; then ## init in order if [ -z "$no_init" ]; then + Section setup host resources + setup_host_resources "$services" || exit 1 Section initialisation run_service_hook "$services" init || exit 1 fi