From 9655a98c41b68a52697cea90b79e0930794045fa Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sat, 24 Feb 2024 17:02:30 +0100 Subject: [PATCH] fix: [compose-core] add ``build`` support again, after the cache introduced a regression --- bin/compose-core | 75 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 12 deletions(-) diff --git a/bin/compose-core b/bin/compose-core index 25385bb..8433490 100755 --- a/bin/compose-core +++ b/bin/compose-core @@ -1523,11 +1523,55 @@ cache:image:produce() { ;; build) local service_build="$src" + build_opts=() + if [ "$COMPOSE_ACTION" == "build" ]; then + while read-0 arg; do + case "$arg" in + -t|--tag) + ## XXXvlab: doesn't seem to be actually a valid option + if [ -n "$COMPOSE_PUSH_TO_REGISTRY" ]; then + err "You can't use -t|--tag option when pushing to a registry." + exit 1 + fi + has_named_image=true + read-0 val ## should always be okay because already checked + build_opts+=("$arg" "$val") + ;; + --help|-h) + docker-compose "$action" --help | + filter_docker_compose_help_message >&2 + exit 0 + ;; + --*|-*) + if str_pattern_matches "$arg" $DC_MATCH_MULTI; then + read-0 value + build_opts+=("$arg" "$value") + shift + elif str_pattern_matches "$arg" $DC_MATCH_SINGLE; then + build_opts+=("$arg") + else + err "Unexpected error while parsing a second time the build arguments." + fi + ;; + *) + ## Already parsed + build_opts+=("$arg") + ;; + esac + done < <(cla.normalize "${action_opts[@]}") + fi + if [ -z "$has_named_image" ]; then + build_opts+=(-t "${charm_image}") + fi + Wrap -v -d "Building ${DARKPINK}$charm${NORMAL}:$hash image" -- \ - docker build "$service_build" -t "${charm_image}" >&2 || { + docker build "$service_build" -t "${charm_image}" "${build_opts[@]}" >&2 || { err "Failed to build image '${charm_image}' for ${DARKYELLOW}$service${NORMAL}." return 1 } + if [ -n "$has_named_image" ]; then + exit 0 + fi ;; *) err "Unknown type '$type'." @@ -1621,7 +1665,7 @@ service_ensure_image_ready() { .services.[\"${service_quoted}\"].image = \"${charm_image_name}:${hash}\"" \ "$_CURRENT_DOCKER_COMPOSE" || return 1 - if docker_has_image "${charm_image_name}:${hash}"; then + if [ "$COMPOSE_ACTION" != "build" ] && docker_has_image "${charm_image_name}:${hash}"; then if [ -n "$DEBUG" ]; then Elt "using ${DARKPINK}$charm${NORMAL}'s image from local cache" >&2 print_status noop >&2 @@ -1634,7 +1678,7 @@ service_ensure_image_ready() { fi ## Can we pull it ? Let's check on $COMPOSE_DOCKER_REGISTRY - if [ -n "$COMPOSE_DOCKER_REGISTRY" ]; then + if [ "$COMPOSE_ACTION" != "build" ] && [ -n "$COMPOSE_DOCKER_REGISTRY" ]; then img=$(cache:image:registry:get "$charm" "$hash" "$service") || { err "Failed to get image '$charm_image_name:$hash' from registry for ${DARKYELLOW}$service${NORMAL}." return 1 @@ -4750,6 +4794,8 @@ while read-0 arg; do shift done < <(cla.normalize "$@") +[ -n "$CACHEDIR" ] || die "No cache directory defined." +[ -d "$CACHEDIR" ] || die "Cache directory '$CACHEDIR' doesn't exists." case "$action" in cache) case "${remainder_args[0]}" in @@ -4758,8 +4804,6 @@ case "$action" in exit 0 ;; clear) - [ -n "$CACHEDIR" ] || die "No cache directory defined." - [ -d "$CACHEDIR" ] || die "Cache directory '$CACHEDIR' doesn't exists." Wrap "${wrap_opts[@]}" -v -d "clear cache directory" -- rm -rf "$CACHEDIR/"* ## clear all docker caches @@ -4911,6 +4955,9 @@ get_docker_compose "${services_args[@]}" >/dev/null || { ## precalculate variab full_init= case "$action" in + build) + full_init=true ## will actually stop after build + ;; up|run) full_init=true post_hook=true @@ -4936,19 +4983,21 @@ case "$action" in ;; esac - if [ -n "$full_init" ]; then - ## init in order - if [[ -z "$no_init" && -z "$no_hooks" ]]; then - Section setup host resources - setup_host_resources "${services_args[@]}" || exit 1 - Section "acquire charm's images" + if [[ "$action" == "build" ]] || [[ -z "$no_init" && -z "$no_hooks" ]]; then + [[ "$action" == "build" ]] || Section "acquire charm's images" run_service_acquire_images "${services_args[@]}" || exit 1 Feed + [ "$action" == "build" ] && { + exit 0 + } + + Section setup host resources + setup_host_resources "${services_args[@]}" || exit 1 + ## init in order Section initialisation run_service_hook init "${services_args[@]}" || exit 1 fi - ## Get relations if [[ -z "$no_relations" && -z "$no_hooks" ]]; then if [ "${#rebuild_relations_to_service[@]}" != 0 ]; then @@ -4975,6 +5024,8 @@ fi | log if [ "${PIPESTATUS[0]}" != 0 ]; then exit 1 fi +[ "$action" == "build" ] && exit 0 + if [ "$action" == "run" ] && [ "${#services_args}" != 0 ]; then charm=$(get_service_charm "${services_args[0]}") || exit 1