From e0c6b69e4904b32fe55823368206d0abfee1adb1 Mon Sep 17 00:00:00 2001 From: Sebastian Wollner Date: Wed, 28 Aug 2024 11:04:32 +0200 Subject: [PATCH 1/5] deploy.sh added modes for ci/cd --- .../resources/archetype-resources/deploy.sh | 302 +++++++++++------- 1 file changed, 178 insertions(+), 124 deletions(-) diff --git a/archetypes/helm/src/main/resources/archetype-resources/deploy.sh b/archetypes/helm/src/main/resources/archetype-resources/deploy.sh index 845d014e..7ac4cef5 100755 --- a/archetypes/helm/src/main/resources/archetype-resources/deploy.sh +++ b/archetypes/helm/src/main/resources/archetype-resources/deploy.sh @@ -4,17 +4,40 @@ set -o pipefail if [[ -z $1 ]] ; then echo "" - echo "deploy.sh [-s skip checks and deploy directly] []" + echo "deploy.sh [options] []" + echo " options:" + echo " -s # skip checks and deploy directly" + echo " -d # run diff" + echo " -t # run try run" + echo " -e # run execute deployment" echo "" exit 0 fi SKIP=false -while getopts s: opt; do +WIZARD=true +DIFF=false +TRY_RUN=false +DEPLOY=false +while getopts sdte opt; do case $opt in - s) SKIP=true + s) + SKIP=true + ;; + d) + WIZARD=false + DIFF=true + ;; + t) + WIZARD=false + TRY_RUN=true + ;; + e) + WIZARD=false + DEPLOY=true + ;; + *);; esac - shift done RELEASE=${1?"release required"} @@ -92,127 +115,158 @@ fi popd >/dev/null || exit -if [ "$SKIP" = false ] ; then -echo "" -echo "--------------------------------------------------------------------------------" -echo "" -echo "cluster: ${CONTEXT}" -echo "namespace: ${NAMESPACE}" -echo "" -echo "--------------------------------------------------------------------------------" -echo "" -read -p "---> kubectl context ok [y/N] " answer -echo "" -echo "--------------------------------------------------------------------------------" -echo "" -case ${answer:0:1} in - y | Y) - echo "Request existing releases, please wait ..." - echo "" - helm repo update >/dev/null - helm ls -a - ;; - *) - exit 0 - ;; -esac - -echo "" -echo "--------------------------------------------------------------------------------" -echo "" -echo "release: ${RELEASE}" -echo "" -echo "--------------------------------------------------------------------------------" -echo "" -read -p "---> helm release ok [y/N] " answer -echo "" -echo "--------------------------------------------------------------------------------" -echo "" - -case ${answer:0:1} in - y | Y) - echo "Request existing revisions, please wait ..." - echo "" - helm history ${RELEASE} 2>/dev/null || echo "skipped ..." - ;; - *) - exit 0 - ;; -esac - -echo "" -echo "--------------------------------------------------------------------------------" -echo "" -echo "chart: ${CHART}" -echo "version: ${VERSION}" -echo "options: ${ARGS[@]}" -echo "" -echo "--------------------------------------------------------------------------------" -echo "" -read -p "---> helm parameter ok [y/N] " answer -echo "" -echo "--------------------------------------------------------------------------------" -echo "" - -if [[ ! -f ${CHART} ]] ; then - - ARGS+=("--version") - ARGS+=("${VERSION}") +diff() { + echo "Request diff from last revision, please wait ..." + echo "" + helm diff upgrade --install "${RELEASE}" "${CHART}" "${ARGS[@]}" +} + +tryRun(){ + echo "Dry-run, please wait ..." + echo "" + helm upgrade --install "${RELEASE}" "${CHART}" "${ARGS[@]}" --dry-run --debug +} + +execute(){ + echo "Execute, please wait ..." + echo "" + helm upgrade --install "${RELEASE}" "${CHART}" "${ARGS[@]}" --timeout=30m +} + + +runAsWizard() { + if [ "$SKIP" = false ] ; then + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + echo "cluster: ${CONTEXT}" + echo "namespace: ${NAMESPACE}" + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + read -p "---> kubectl context ok [y/N] " answer + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + case ${answer:0:1} in + y | Y) + echo "Request existing releases, please wait ..." + echo "" + helm repo update >/dev/null + helm ls -a + ;; + *) + exit 0 + ;; + esac + + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + echo "release: ${RELEASE}" + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + read -p "---> helm release ok [y/N] " answer + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + + case ${answer:0:1} in + y | Y) + echo "Request existing revisions, please wait ..." + echo "" + helm history ${RELEASE} 2>/dev/null || echo "skipped ..." + ;; + *) + exit 0 + ;; + esac + + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + echo "chart: ${CHART}" + echo "version: ${VERSION}" + echo "options: ${ARGS[*]}" + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + read -p "---> helm parameter ok [y/N] " answer + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + + if [[ ! -f ${CHART} ]] ; then + + ARGS+=("--version") + ARGS+=("${VERSION}") + + fi + + case ${answer:0:1} in + y | Y) + diff + ;; + *) + exit 0 + ;; + esac + + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + read -p "---> perform test [y/N] " answer + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + + case ${answer:0:1} in + y | Y) + tryRun + ;; + *) + echo "skipped ..." + ;; + esac + + fi + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + read -p "---> perform deployment [y/N] " answer + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + + case ${answer:0:1} in + y | Y) + execute + ;; + *) + echo "skipped ..." + ;; + esac + + echo "" + echo "--------------------------------------------------------------------------------" + echo "" + echo "done" + echo "" +} -fi -case ${answer:0:1} in - y | Y) - echo "Request diff from last revision, please wait ..." - echo "" - helm diff upgrade --install "${RELEASE}" "${CHART}" "${ARGS[@]}" - ;; - *) - exit 0 - ;; -esac - -echo "" -echo "--------------------------------------------------------------------------------" -echo "" -read -p "---> perform test [y/N] " answer -echo "" -echo "--------------------------------------------------------------------------------" -echo "" - -case ${answer:0:1} in - y | Y) - echo "Dry-run, please wait ..." - echo "" - helm upgrade --install "${RELEASE}" "${CHART}" "${ARGS[@]}" --dry-run --debug - ;; - *) - echo "skipped ..." - ;; -esac +if [[ $WIZARD == "true" ]] ; then + runAsWizard +else + [[ $DIFF == "true" ]] && { + diff + } + [[ $TRY_RUN == "true" ]] && { + tryRun + } + [[ $DEPLOY == "true" ]] && { + execute + } fi -echo "" -echo "--------------------------------------------------------------------------------" -echo "" -read -p "---> perform deployment [y/N] " answer -echo "" -echo "--------------------------------------------------------------------------------" -echo "" - -case ${answer:0:1} in - y | Y) - echo "Run, please wait ..." - echo "" - helm upgrade --install "${RELEASE}" "${CHART}" "${ARGS[@]}" --timeout=30m - ;; - *) - echo "skipped ..." - ;; -esac - -echo "" -echo "--------------------------------------------------------------------------------" -echo "" -echo "done" -echo "" From 16b35fb554f4bcb4177e51e27b4b692cdbbbd0fc Mon Sep 17 00:00:00 2001 From: Sebastian Wollner Date: Wed, 28 Aug 2024 12:06:38 +0200 Subject: [PATCH 2/5] fix: options not cleared --- .../helm/src/main/resources/archetype-resources/deploy.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/archetypes/helm/src/main/resources/archetype-resources/deploy.sh b/archetypes/helm/src/main/resources/archetype-resources/deploy.sh index 7ac4cef5..e3cb6bc9 100755 --- a/archetypes/helm/src/main/resources/archetype-resources/deploy.sh +++ b/archetypes/helm/src/main/resources/archetype-resources/deploy.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -e +set -ex set -o pipefail if [[ -z $1 ]] ; then @@ -36,10 +36,13 @@ while getopts sdte opt; do WIZARD=false DEPLOY=true ;; - *);; + *) + ;; esac done +shift $(( "$OPTIND" - 1)) + RELEASE=${1?"release required"} CHART=${2?"chart required"} VERSION=${3:-"":->=0.0.0-0} From e5f9906c0db416623bf03321d2826b0cc446a50e Mon Sep 17 00:00:00 2001 From: Sebastian Wollner Date: Wed, 28 Aug 2024 12:07:00 +0200 Subject: [PATCH 3/5] fix: remove debugging option --- .../helm/src/main/resources/archetype-resources/deploy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/archetypes/helm/src/main/resources/archetype-resources/deploy.sh b/archetypes/helm/src/main/resources/archetype-resources/deploy.sh index e3cb6bc9..a348a4fb 100755 --- a/archetypes/helm/src/main/resources/archetype-resources/deploy.sh +++ b/archetypes/helm/src/main/resources/archetype-resources/deploy.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -ex +set -e set -o pipefail if [[ -z $1 ]] ; then From 430e78d3c265ac3b1570b7fb567f1205c27886d8 Mon Sep 17 00:00:00 2001 From: Sebastian Wollner Date: Fri, 30 Aug 2024 09:21:26 +0200 Subject: [PATCH 4/5] fix: shell exit code --- .../resources/archetype-resources/deploy.sh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/archetypes/helm/src/main/resources/archetype-resources/deploy.sh b/archetypes/helm/src/main/resources/archetype-resources/deploy.sh index a348a4fb..e4f72ae3 100755 --- a/archetypes/helm/src/main/resources/archetype-resources/deploy.sh +++ b/archetypes/helm/src/main/resources/archetype-resources/deploy.sh @@ -121,7 +121,10 @@ popd >/dev/null || exit diff() { echo "Request diff from last revision, please wait ..." echo "" + # temporarily disable the script from exiting on a non-zero status code + set +e helm diff upgrade --install "${RELEASE}" "${CHART}" "${ARGS[@]}" + set -e } tryRun(){ @@ -262,14 +265,16 @@ runAsWizard() { if [[ $WIZARD == "true" ]] ; then runAsWizard else - [[ $DIFF == "true" ]] && { + if [[ $DIFF == "true" ]] ; then diff - } - [[ $TRY_RUN == "true" ]] && { + fi + + if [[ $TRY_RUN == "true" ]] ; then tryRun - } + fi - [[ $DEPLOY == "true" ]] && { + if [[ $DEPLOY == "true" ]] ; then execute - } + fi + fi From 096c70907efc63bc9e39d838cb18a5ac3312d9ae Mon Sep 17 00:00:00 2001 From: Sebastian Wollner Date: Tue, 10 Sep 2024 10:10:22 +0200 Subject: [PATCH 5/5] fix: missing mongodb backup for compose --- .../docker/compose/src/main/scripts/utils.sh | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/archetypes/project/src/main/resources/archetype-resources/deploy/docker/compose/src/main/scripts/utils.sh b/archetypes/project/src/main/resources/archetype-resources/deploy/docker/compose/src/main/scripts/utils.sh index a74ce6de..420ea4da 100755 --- a/archetypes/project/src/main/resources/archetype-resources/deploy/docker/compose/src/main/scripts/utils.sh +++ b/archetypes/project/src/main/resources/archetype-resources/deploy/docker/compose/src/main/scripts/utils.sh @@ -335,8 +335,25 @@ backup() { if [[ -n $repodb ]] ; then - echo "backup postgres" + if [[ "$($COMPOSE_EXEC ps repository-mongo -a)" != "no such service: repository-mongo" ]]; then + echo "backup mongo" + if [[ -n $compressed ]] ; then + $COMPOSE_EXEC exec -t repository-mongo sh -c "mongodump --archive --gzip -u ${REPOSITORY_MONGO_ROOT_USER:-root} -p ${REPOSITORY_MONGO_ROOT_PASS:-root}" >"$backupDir/repository-mongo.gz" || { + rm -rf "$backupDir" + echo "ERROR on creating mongodb dump" + exit 1 + } + else + $COMPOSE_EXEC exec -t repository-mongo sh -c "mongodump --archive -u ${REPOSITORY_MONGO_ROOT_USER:-root} -p ${REPOSITORY_MONGO_ROOT_PASS:-root}" >"$backupDir/repository-mongo.dump" || { + rm -rf "$backupDir" + echo "ERROR on creating mongodb dump" + exit 1 + } + fi + fi + + echo "backup postgres" if [[ -n $compressed ]] ; then $COMPOSE_EXEC exec -t repository-database sh -c "export PGPASSWORD=${REPOSITORY_DATABASE_PASS:-repository}; pg_dump --username ${REPOSITORY_DATABASE_USER:-repository} --format custom --no-owner --no-privileges ${REPOSITORY_DATABASE_NAME:-repository} | gzip" >"$backupDir/repository-db.gz" || { rm -rf "$backupDir"