diff --git a/scripts/easy-docker/lib/app/wizard/common/apps/metadata.sh b/scripts/easy-docker/lib/app/wizard/common/apps/metadata.sh index 9dbbb498..3dba35b4 100755 --- a/scripts/easy-docker/lib/app/wizard/common/apps/metadata.sh +++ b/scripts/easy-docker/lib/app/wizard/common/apps/metadata.sh @@ -253,126 +253,3 @@ persist_stack_apps_json_from_metadata_apps() { return 0 } - -persist_stack_metadata_top_level_object() { - local stack_dir="${1}" - local object_key="${2}" - local object_json="${3}" - local insert_before_key="${4:-}" - local metadata_path="" - local metadata_tmp_path="" - - metadata_path="${stack_dir}/metadata.json" - metadata_tmp_path="${metadata_path}.tmp" - if [ ! -f "${metadata_path}" ]; then - return 1 - fi - - if [ -z "${object_json}" ]; then - return 1 - fi - - if ! awk -v object_key="${object_key}" -v object_json="${object_json}" -v insert_before_key="${insert_before_key}" ' - BEGIN { - target_regex = "^ \"" object_key "\"[[:space:]]*:" - before_regex = "" - if (insert_before_key != "") { - before_regex = "^ \"" insert_before_key "\"[[:space:]]*:" - } - in_target = 0 - target_depth = 0 - inserted = 0 - prev = "" - } - function flush_prev() { - if (prev != "") { - print prev - prev = "" - } - } - { - if (!in_target && $0 ~ target_regex) { - flush_prev() - if (object_key == "wizard") { - print " \"" object_key "\": " object_json - } else { - print " \"" object_key "\": " object_json "," - } - in_target = 1 - inserted = 1 - if ($0 ~ /{/) { - target_depth += gsub(/{/, "{", $0) - target_depth -= gsub(/}/, "}", $0) - } else { - target_depth = 0 - } - if (target_depth <= 0) { - in_target = 0 - } - next - } - - if (in_target) { - target_depth += gsub(/{/, "{", $0) - target_depth -= gsub(/}/, "}", $0) - if (target_depth <= 0) { - in_target = 0 - } - next - } - - if (!inserted && before_regex != "" && $0 ~ before_regex) { - flush_prev() - print " \"" object_key "\": " object_json "," - inserted = 1 - } - - if (!inserted && $0 ~ /^}/) { - if (prev != "") { - if (prev !~ /,[[:space:]]*$/) { - prev = prev "," - } - print prev - prev = "" - } - print " \"" object_key "\": " object_json - inserted = 1 - print $0 - next - } - - flush_prev() - prev = $0 - } - END { - flush_prev() - if (!inserted) { - exit 2 - } - } - ' "${metadata_path}" >"${metadata_tmp_path}"; then - rm -f -- "${metadata_tmp_path}" >/dev/null 2>&1 || true - return 1 - fi - - if ! mv -- "${metadata_tmp_path}" "${metadata_path}"; then - rm -f -- "${metadata_tmp_path}" >/dev/null 2>&1 || true - return 1 - fi - - return 0 -} - -persist_stack_metadata_apps_object() { - local stack_dir="${1}" - local apps_json_object="${2}" - - persist_stack_metadata_top_level_object "${stack_dir}" "apps" "${apps_json_object}" "wizard" -} - -persist_stack_metadata_wizard_object() { - local stack_dir="${1}" - local wizard_json_object="${2}" - - persist_stack_metadata_top_level_object "${stack_dir}" "wizard" "${wizard_json_object}" -} diff --git a/scripts/easy-docker/lib/app/wizard/common/apps/persistence.sh b/scripts/easy-docker/lib/app/wizard/common/apps/persistence.sh index e581f3cc..4a1dead2 100755 --- a/scripts/easy-docker/lib/app/wizard/common/apps/persistence.sh +++ b/scripts/easy-docker/lib/app/wizard/common/apps/persistence.sh @@ -1,8 +1,10 @@ #!/usr/bin/env bash -persist_stack_metadata_apps_object() { +persist_stack_metadata_top_level_object() { local stack_dir="${1}" - local apps_json_object="${2}" + local object_key="${2}" + local object_json="${3}" + local insert_before_key="${4:-}" local metadata_path="" local metadata_tmp_path="" @@ -12,14 +14,19 @@ persist_stack_metadata_apps_object() { return 1 fi - if [ -z "${apps_json_object}" ]; then + if [ -z "${object_json}" ]; then return 1 fi - if ! awk -v apps_object="${apps_json_object}" ' + if ! awk -v object_key="${object_key}" -v object_json="${object_json}" -v insert_before_key="${insert_before_key}" ' BEGIN { - in_top_level_apps = 0 - apps_depth = 0 + target_regex = "^ \"" object_key "\"[[:space:]]*:" + before_regex = "" + if (insert_before_key != "") { + before_regex = "^ \"" insert_before_key "\"[[:space:]]*:" + } + in_target = 0 + target_depth = 0 inserted = 0 prev = "" } @@ -30,35 +37,39 @@ persist_stack_metadata_apps_object() { } } { - if (!in_top_level_apps && $0 ~ /^ "apps"[[:space:]]*:/) { + if (!in_target && $0 ~ target_regex) { flush_prev() - print " \"apps\": " apps_object "," - in_top_level_apps = 1 + if (object_key == "wizard") { + print " \"" object_key "\": " object_json + } else { + print " \"" object_key "\": " object_json "," + } + in_target = 1 inserted = 1 if ($0 ~ /{/) { - apps_depth += gsub(/{/, "{", $0) - apps_depth -= gsub(/}/, "}", $0) + target_depth += gsub(/{/, "{", $0) + target_depth -= gsub(/}/, "}", $0) } else { - apps_depth = 0 + target_depth = 0 } - if (apps_depth <= 0) { - in_top_level_apps = 0 + if (target_depth <= 0) { + in_target = 0 } next } - if (in_top_level_apps) { - apps_depth += gsub(/{/, "{", $0) - apps_depth -= gsub(/}/, "}", $0) - if (apps_depth <= 0) { - in_top_level_apps = 0 + if (in_target) { + target_depth += gsub(/{/, "{", $0) + target_depth -= gsub(/}/, "}", $0) + if (target_depth <= 0) { + in_target = 0 } next } - if (!inserted && $0 ~ /^ "wizard"[[:space:]]*:/) { + if (!inserted && before_regex != "" && $0 ~ before_regex) { flush_prev() - print " \"apps\": " apps_object "," + print " \"" object_key "\": " object_json "," inserted = 1 } @@ -70,7 +81,7 @@ persist_stack_metadata_apps_object() { print prev prev = "" } - print " \"apps\": " apps_object + print " \"" object_key "\": " object_json inserted = 1 print $0 next @@ -98,94 +109,16 @@ persist_stack_metadata_apps_object() { return 0 } +persist_stack_metadata_apps_object() { + local stack_dir="${1}" + local apps_json_object="${2}" + + persist_stack_metadata_top_level_object "${stack_dir}" "apps" "${apps_json_object}" "wizard" +} + persist_stack_metadata_wizard_object() { local stack_dir="${1}" local wizard_json_object="${2}" - local metadata_path="" - local metadata_tmp_path="" - metadata_path="${stack_dir}/metadata.json" - metadata_tmp_path="${metadata_path}.tmp" - if [ ! -f "${metadata_path}" ]; then - return 1 - fi - - if [ -z "${wizard_json_object}" ]; then - return 1 - fi - - if ! awk -v wizard_object="${wizard_json_object}" ' - BEGIN { - in_top_level_wizard = 0 - wizard_depth = 0 - inserted = 0 - prev = "" - } - function flush_prev() { - if (prev != "") { - print prev - prev = "" - } - } - { - if (!in_top_level_wizard && $0 ~ /^ "wizard"[[:space:]]*:/) { - flush_prev() - print " \"wizard\": " wizard_object - in_top_level_wizard = 1 - inserted = 1 - if ($0 ~ /{/) { - wizard_depth += gsub(/{/, "{", $0) - wizard_depth -= gsub(/}/, "}", $0) - } else { - wizard_depth = 0 - } - if (wizard_depth <= 0) { - in_top_level_wizard = 0 - } - next - } - - if (in_top_level_wizard) { - wizard_depth += gsub(/{/, "{", $0) - wizard_depth -= gsub(/}/, "}", $0) - if (wizard_depth <= 0) { - in_top_level_wizard = 0 - } - next - } - - if (!inserted && $0 ~ /^}/) { - if (prev != "") { - if (prev !~ /,[[:space:]]*$/) { - prev = prev "," - } - print prev - prev = "" - } - print " \"wizard\": " wizard_object - inserted = 1 - print $0 - next - } - - flush_prev() - prev = $0 - } - END { - flush_prev() - if (!inserted) { - exit 2 - } - } - ' "${metadata_path}" >"${metadata_tmp_path}"; then - rm -f -- "${metadata_tmp_path}" >/dev/null 2>&1 || true - return 1 - fi - - if ! mv -- "${metadata_tmp_path}" "${metadata_path}"; then - rm -f -- "${metadata_tmp_path}" >/dev/null 2>&1 || true - return 1 - fi - - return 0 + persist_stack_metadata_top_level_object "${stack_dir}" "wizard" "${wizard_json_object}" } diff --git a/scripts/easy-docker/lib/app/wizard/env/apps.sh b/scripts/easy-docker/lib/app/wizard/env/apps.sh index ea357578..4a0bbf47 100755 --- a/scripts/easy-docker/lib/app/wizard/env/apps.sh +++ b/scripts/easy-docker/lib/app/wizard/env/apps.sh @@ -84,12 +84,17 @@ build_predefined_apps_metadata_json_object() { local result_var="${1}" local predefined_csv="${2}" local branch_lines="${3}" + local custom_apps_lines="${4:-}" local app_id="" local app_branch="" + local custom_repo="" + local custom_branch="" local predefined_json_entries="" local branch_json_entries="" + local custom_json_entries="" local escaped_app_id="" local escaped_branch="" + local escaped_repo="" local entry_json="" local line="" local -a predefined_ids=() @@ -134,7 +139,30 @@ build_predefined_apps_metadata_json_object() { ${branch_lines} EOF - printf -v "${result_var}" '{\n "predefined": [\n%s\n ],\n "predefined_branches": {\n%s\n },\n "custom": [\n ]\n }' "${predefined_json_entries}" "${branch_json_entries}" + while IFS= read -r line; do + if [ -z "${line}" ]; then + continue + fi + + custom_repo="${line%%|*}" + custom_branch="${line#*|}" + if [ -z "${custom_repo}" ] || [ -z "${custom_branch}" ]; then + continue + fi + + escaped_repo="$(json_escape_string "${custom_repo}")" + escaped_branch="$(json_escape_string "${custom_branch}")" + entry_json="$(printf ' {\n "repo": "%s",\n "branch": "%s"\n }' "${escaped_repo}" "${escaped_branch}")" + if [ -z "${custom_json_entries}" ]; then + custom_json_entries="${entry_json}" + else + custom_json_entries="${custom_json_entries}"$',\n'"${entry_json}" + fi + done < ${EASY_DOCKER_BUILD_ERROR_DETAIL}" 6 - ;; - *) - show_warning_and_wait "Custom image build failed (${build_image_status})." 4 - ;; - esac - - return "${build_image_status}" -}