Skip to content

Commit

Permalink
Updated scripts for the new base image
Browse files Browse the repository at this point in the history
  • Loading branch information
drivera-armedia committed Aug 26, 2024
1 parent 7df7d93 commit d78d931
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 95 deletions.
26 changes: 26 additions & 0 deletions dependencies.new
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
enabled: true
mode: all
template:
# Any of the required ports are enough to succeed
mode: any
initialDelay: 10
delay: 10
timeout: 10
attempts: 120
dependencies:
acme:
url: "@env:ACME_URL"
app-artifacts:
url: "@env:DEPL_URL"
content:
url: "@env:ARKCASE_CONTENT_URL"
ldap:
url: "@env:ARKCASE_LDAP_URL"
messaging:
url: "@env:ARKCASE_MQ_AMQP_URL"
rdbms:
host: "@env:ARKCASE_JDBC_HOST"
port: "@env:ARKCASE_JDBC_PORT"
search:
url: "@env:ARKCASE_SOLR_URL"

60 changes: 9 additions & 51 deletions only-once
Original file line number Diff line number Diff line change
@@ -1,77 +1,35 @@
#!/bin/bash

set -euo pipefail

timestamp()
{
date -Ins -u
}

say()
{
echo -e "$(timestamp): ${@}"
}

err()
{
say "${@}" 1>&2
}

fail()
{
err "${@}"
exit ${EXIT_CODE:-1}
}

quit()
{
say "${@}"
exit ${EXIT_CODE:-0}
}

to_boolean()
{
case "${1,,}" in
true | t | 1 | yes | on | enabled | enable ) echo "true" ;;
* ) echo "false" ;;
esac
}
. /.functions

[ -v WRAPPED_STATE ] || fail "Must set the WRAPPED_STATE environment variable to point to the location where to track if the initializer script has already been run"
[ -e "${WRAPPED_STATE}" ] || fail "The wrapped script [${WRAPPED_STATE}] does not exist"
[ -d "${WRAPPED_STATE}" ] || fail "The path [${WRAPPED_STATE}] is not a directory"
[ -r "${WRAPPED_STATE}" ] || fail "The wrapped script [${WRAPPED_STATE}] is not readable"
[ -w "${WRAPPED_STATE}" ] || fail "The wrapped script [${WRAPPED_STATE}] is not writable"
[ -x "${WRAPPED_STATE}" ] || fail "The wrapped script [${WRAPPED_STATE}] is not executable"

require_dir_readwrite "${WRAPPED_STATE}"

[ -v WRAPPED_SCRIPT ] || fail "Must set the WRAPPED_SCRIPT environment variable to point to the script to be wrapped"
[ -e "${WRAPPED_SCRIPT}" ] || fail "The wrapped script [${WRAPPED_SCRIPT}] does not exist"
[ -f "${WRAPPED_SCRIPT}" ] || fail "The path [${WRAPPED_SCRIPT}] is not a regular file"
[ -r "${WRAPPED_SCRIPT}" ] || fail "The wrapped script [${WRAPPED_SCRIPT}] is not readable"
[ -x "${WRAPPED_SCRIPT}" ] || fail "The wrapped script [${WRAPPED_SCRIPT}] is not executable"
require_file_executable "${WRAPPED_SCRIPT}"

# In case the arguments haven't been defined
[ -v WRAPPED_SCRIPT_ARGS ] || WRAPPED_SCRIPT_ARGS=""

# If we're not given an explicit name to track state with, we deduce it from the script's
# filename and add a dot in front to hide the file
[ -v WRAPPED_STATE_NAME ] || WRAPPED_STATE_NAME=".${WRAPPED_SCRIPT##*/}"
set_or_default WRAPPED_STATE_NAME ".${WRAPPED_SCRIPT##*/}"

MARKER="${WRAPPED_STATE}/${WRAPPED_STATE_NAME}"
[ -e "${MARKER}" ] && quit "The state has already been initialized"

[ -v SSL_DIR ] || SSL_DIR="/.ssl"
acme-init
init_ssl

say "🚀 Calling the wrapped script"
running "Calling the wrapped script"
(
# This should help mitigate exploitation
eval CMD=( "${WRAPPED_SCRIPT@Q}" ${WRAPPED_SCRIPT_ARGS} ) || fail "Failed to define the wrapped command using script [${WRAPPED_SCRIPT}] and args [${WRAPPED_SCRIPT_ARGS}]"
say "🚀 Launching the wrapped script: ${CMD[@]@Q}"
exec "${CMD[@]}"
execute "${CMD[@]}"
) || fail "Failed to execute the wrapped script (rc=${?})"

TS="$(timestamp)"
say "Wrapped script succeeded, marking the state to [${MARKER}] (timestamp = ${TS})"
ok "Wrapped script succeeded, marking the state to [${MARKER}] (timestamp = ${TS})"
echo "${TS}" > "${MARKER}" || fail "Failed to create the state tracking marker"
exit 0
55 changes: 11 additions & 44 deletions run-from-env
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

set -euo pipefail
. /.functions

RE_DIGITS="[1-9][0-9]*"
RE_NUM="^${RE_DIGITS}$"
RE_ZERO="^(0|${RE_DIGITS})$"
Expand All @@ -17,49 +20,12 @@ DEBUG="false"
RE_DISABLE="${RE_BOOLEAN}"
RE_DEBUG="${RE_BOOLEAN}"

[ -v TIMEOUT ] || TIMEOUT=""
[ -n "${TIMEOUT}" ] || TIMEOUT=""
[[ "${TIMEOUT}" =~ ${RE_ZERO} ]] || TIMEOUT="${DEFAULT_TIMEOUT}"

[ -v RETRY_COUNT ] || RETRY_COUNT=""
[ -n "${RETRY_COUNT}" ] || RETRY_COUNT=""
set_or_default TIMEOUT "${DEFAULT_TIMEOUT}"
set_or_default RETRY_COUNT "${DEFAULT_RETRY_COUNT}"
set_or_default RETRY_WAIT "${DEFAULT_RETRY_WAIT}"
[[ "${RETRY_COUNT}" =~ ${RE_RETRY_COUNT} ]] || RETRY_COUNT="${DEFAULT_RETRY_COUNT}"

[ -v RETRY_WAIT ] || RETRY_WAIT=""
[ -n "${RETRY_WAIT}" ] || RETRY_WAIT=""
[[ "${RETRY_WAIT}" =~ ${RE_RETRY_WAIT} ]] || RETRY_WAIT="${DEFAULT_RETRY_WAIT}"

timestamp()
{
/usr/bin/date -Ins
}

say()
{
echo -e "$(timestamp): ${@}"
}

ok()
{
say "${@}"
}

warn()
{
say "⚠️ ${@}"
}

err()
{
say "${@}" 1>&2
}

fail()
{
err "${@}"
exit ${EXIT_CODE:-1}
}

usage()
{
echo -e "usage: ${BASH_SOURCE:-${0}} var1 [var2 var3 ... varN]" 1>&2
Expand Down Expand Up @@ -101,6 +67,7 @@ for VAR in "${@}" ; do
RC=0
(
set -euo pipefail
. /.functions

#
# We can control timeouts, attempts, and even
Expand Down Expand Up @@ -129,12 +96,12 @@ for VAR in "${@}" ; do
for (( A=1 ; A <= RETRY_COUNT ; A++ )) ; do

if [ ${A} -gt 1 ] ; then
say "Applying a wait of ${RETRY_WAIT} before the next attempt"
waiting "Applying a wait of ${RETRY_WAIT} before the next attempt"
/usr/bin/sleep ${RETRY_WAIT} || fail "Retry wait aborted"
fi

ATTEMPT="(${A}/${RETRY_COUNT})"
say "👉 Running the check ${ATTEMPT}"
doing "Running the check ${ATTEMPT}"

RC=0
say "########################################"
Expand All @@ -143,8 +110,8 @@ for VAR in "${@}" ; do
[ ${RC} -eq 0 ] && ok "Check succeeded ${ATTEMPT}" && exit 0

case "${RC}" in
124 ) say "Check timed out ${ATTEMPT}" ;;
* ) say "Check failed with status ${RC} ${ATTEMPT}" ;;
124 ) waiting "Check timed out ${ATTEMPT}" ;;
* ) err "Check failed with status ${RC} ${ATTEMPT}" ;;
esac
done
exit 1
Expand Down
9 changes: 9 additions & 0 deletions testenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export ACME_URL="https://acme:9000"
export DEPL_URL="https://deploy"
export ARKCASE_CONTENT_URL="https://content-server:333"
export ARKCASE_LDAP_URL="https://content-server:222"
export ARKCASE_SOLR_URL="https://content-server:111"
export ARKCASE_JDBC_HOST="some-host-name"
export ARKCASE_JDBC_PORT="gopher"
export ARKCASE_JDBC_PORT="gopher"
export ARKCASE_MQ_AMQP_URL="ssl://messaging:61616"

0 comments on commit d78d931

Please sign in to comment.