Skip to content

Commit

Permalink
Merge pull request #637 from IBM/mgk-improve-clean-up
Browse files Browse the repository at this point in the history
add --clean-up option
  • Loading branch information
fketelaars committed Feb 12, 2024
2 parents bba292a + fee80b3 commit 73384be
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions cp-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ command_usage() {
echo " --air-gapped Only for environment subcommand; if specified the deployer is considered to run in an air-gapped environment (\$CPD_AIRGAP)"
echo " --skip-mirror-images Pertains to env apply and env download. When specified, the mirroring of images to the private registry is skipped (\$CPD_SKIP_MIRROR)"
echo " --skip-portable-registry Pertains to env download. When specified, no portable registry is used to transport the images (\$CPD_SKIP_PORTABLE_REGISTRY)"
echo " --clean-up Remove the container after the run is completed (\$CPD_CLEANUP)"
echo " -v Show standard ansible output (\$ANSIBLE_STANDARD_OUTPUT)"
echo " -vv, -vvv, -vvvv, ... Show verbose ansible output, verbose option used is (number of v)-1 (\$ANSIBLE_VERBOSE)"
echo
Expand All @@ -75,15 +76,25 @@ command_usage() {
# Show the logs of the currently running env process
run_env_logs() {
if [[ "${ACTIVE_CONTAINER_ID}" != "" ]];then
while ${CPD_CONTAINER_ENGINE} ps --no-trunc 2>/dev/null | grep -q ${ACTIVE_CONTAINER_ID};do
while ${CPD_CONTAINER_ENGINE} ps --no-trunc 2>/dev/null | grep -q ${ACTIVE_CONTAINER_ID} 2>/dev/null; do
${CPD_CONTAINER_ENGINE} logs -f --tail 10 ${ACTIVE_CONTAINER_ID}
if [ $? -ne 0 ];then
break
fi
sleep 0.5
done
else
${CPD_CONTAINER_ENGINE} logs ${CURRENT_CONTAINER_ID}
# If $CPD_CLEANUP was used for the previous run, the $CURRENT_CONTAINER_ID
# will not exist so we fall back to the previous log file if there is one.
if [ $( ${CPD_CONTAINER_ENGINE} ps -a --no-trunc 2>/dev/null | grep ${CURRENT_CONTAINER_ID} 2>/dev/null | wc -l ) -gt 0 ]; then
${CPD_CONTAINER_ENGINE} logs ${CURRENT_CONTAINER_ID}
else
if [ -f ${STATUS_DIR}/log/cloud-pak-deployer.log ]; then
cat ${STATUS_DIR}/log/cloud-pak-deployer.log
else
echo "No previous log found"
fi
fi
fi
}

Expand All @@ -99,6 +110,7 @@ if [ "${CHECK_ONLY}" == "" ];then CHECK_ONLY=false;fi
if [ "${CPD_AIRGAP}" == "" ];then CPD_AIRGAP=false;fi
if [ "${CPD_SKIP_MIRROR}" == "" ];then CPD_SKIP_MIRROR=false;fi
if [ "${CPD_SKIP_PORTABLE_REGISTRY}" == "" ];then CPD_SKIP_PORTABLE_REGISTRY=false;fi
if [ "${CPD_CLEANUP}" == "" ];then CPD_CLEANUP=false;fi
if [ "${CPD_DEVELOP}" == "" ];then CPD_DEVELOP=false;fi
if [ "${CPD_TEST_CARTRIDGES}" == "" ];then CPD_TEST_CARTRIDGES=false;fi
if [ "${CPD_ACCEPT_LICENSES}" == "" ];then CPD_ACCEPT_LICENSES=false;fi
Expand Down Expand Up @@ -556,6 +568,14 @@ while (( "$#" )); do
fi
export CPD_SKIP_PORTABLE_REGISTRY=true
shift 1
;;
--clean-up)
if [[ "${ACTION}" != "apply" && "${ACTION}" != "destroy" && "${ACTION}" != "download" && "${SUBCOMMAND}" != "vault" ]];then
echo "Error: --clean-up is only valid for environment subcommand with apply/destroy or download or the vault subcommand."
command_usage 2
fi
export CPD_CLEANUP=true
shift 1
;;
-vv*)
export ANSIBLE_VERBOSE=$(echo -${1:2})
Expand Down Expand Up @@ -854,7 +874,7 @@ if ! $INSIDE_CONTAINER;then
ACTIVE_CONTAINER_ID=${CURRENT_CONTAINER_ID}
# If container ID was found, check if it is currently running
if [ "${ACTIVE_CONTAINER_ID}" != "" ];then
if ! ${CPD_CONTAINER_ENGINE} ps --no-trunc | grep -q ${ACTIVE_CONTAINER_ID};then
if ! ${CPD_CONTAINER_ENGINE} ps --no-trunc | grep -q ${ACTIVE_CONTAINER_ID} 2>/dev/null;then
ACTIVE_CONTAINER_ID=""
fi
fi
Expand Down Expand Up @@ -1029,6 +1049,8 @@ if ! $INSIDE_CONTAINER;then
run_cmd+=" -e EXTRA_PARMS=\"${arrExtraKey[*]}\""
fi

if "$CPD_CLEANUP"; then run_cmd+=" --rm"; fi

if [[ "$SUBCOMMAND" == "environment" && "${ACTION}" == "command" ]];then
run_cmd+=" -ti --entrypoint /cloud-pak-deployer/docker-scripts/env-command.sh"
elif [[ "$SUBCOMMAND" == "environment" && "${ACTION}" == "wizard" ]];then
Expand All @@ -1039,7 +1061,7 @@ if ! $INSIDE_CONTAINER;then
fi
run_cmd+=" cloud-pak-deployer:${CPD_IMAGE_TAG}"

# echo $run_cmd
# echo $run_cmd

# If running "environment" subcommand with apply/destroy, follow log
if [ "$SUBCOMMAND" == "environment" ] && [[ "${ACTION}" == "apply" || "${ACTION}" == "destroy" || "${ACTION}" == "wizard" || "${ACTION}" == "download" ]];then
Expand All @@ -1049,7 +1071,12 @@ if ! $INSIDE_CONTAINER;then
echo "${CURRENT_CONTAINER_ID}" > ${STATUS_DIR}/pid/container.id
fi
run_env_logs
PODMAN_EXIT_CODE=$(${CPD_CONTAINER_ENGINE} inspect ${CURRENT_CONTAINER_ID} --format='{{.State.ExitCode}}')
if "$CPD_CLEANUP"; then
# We have no container to get an exit code from as it was automatically deleted with --rm
PODMAN_EXIT_CODE=$?
else
PODMAN_EXIT_CODE=$(${CPD_CONTAINER_ENGINE} inspect ${CURRENT_CONTAINER_ID} --format='{{.State.ExitCode}}')
fi
else
eval $run_cmd
PODMAN_EXIT_CODE=$?
Expand Down

0 comments on commit 73384be

Please sign in to comment.