Compare commits

..

2 commits

View file

@ -93,26 +93,20 @@ function rebuild_status_file() {
}
function rebuild_status() {
local output_var=$1
[[ -n "$REBUILD_STATUS_FILE" ]] || rebuild_status_file
if [[ -z "$2" ]]; then
declare -g "$output_var=$( cat "$REBUILD_STATUS_FILE" )"
if [[ -z "$1" ]]; then
cat "$(rebuild_status_file)"
else
declare -g "$output_var=$(
jq -r --arg container "$1" '.[$container]' < "$REBUILD_STATUS_FILE"
)"
rebuild_status | jq -r --arg container "$1" '.[$container]'
fi
}
function update_rebuild_status() {
local data
if [[ "$1" == "-d" ]]; then
rebuild_status DATA
# shellcheck disable=SC2153
data=$(
jq \
rebuild_status | jq \
--arg container "$2" \
'del(.[$container])' <<< "$DATA"
'del(.[$container])'
)
else
local args=( --arg container "$1" ) arg name value expr=( )
@ -123,16 +117,16 @@ function update_rebuild_status() {
args+=( --arg "$name" "$value" )
expr+=( ".[\$container].$name=\$$name" )
done
rebuild_status DATA
data=$(
jq "${args[@]}" "$( implode ' | ' "${expr[@]}" )" <<< "$DATA"
rebuild_status | jq \
"${args[@]}" "$( implode ' | ' "${expr[@]}" )"
)
fi
cat <<< "$data" > "$REBUILD_STATUS_FILE"
cat <<< "$data" > "$(rebuild_status_file)"
}
function remove_rebuild_status() {
[[ -n "$REBUILD_STATUS_FILE" ]] || rebuild_status_file
rebuild_status_file > /dev/null
[[ -e "$REBUILD_STATUS_FILE" ]] || return 0
debug "Remove previous rebuild status file ($REBUILD_STATUS_FILE) and log files it contains"
for log in $( jq -r '.[] | .log' "$REBUILD_STATUS_FILE" ); do
@ -264,11 +258,9 @@ if [[ -n "$DOCKERCOMPOSE_FILE" ]]; then
fi
if [[ $REBUILD_CRON -eq 1 ]]; then
rebuild_status DATA
mapfile -t to_rebuild < <(
jq -r -c 'to_entries[] | select(
(.value.start_date|not) and (.value.error|not)
) | .key' <<< "$DATA"
rebuild_status | jq -r -c \
'to_entries[] | select((.value.start_date|not) and (.value.error|not)) | .key'
)
if [[ ${#to_rebuild[@]} -eq 0 ]]; then
debug "No container need to be rebuild"
@ -300,10 +292,9 @@ if [[ $REBUILD_CRON -eq 1 ]]; then
fi
if [[ $DEPLOY_CRON -eq 1 ]]; then
rebuild_status DATA
mapfile -t to_deploy < <(
jq -r -c \
'to_entries[] | select((.value.end_date) and (.value.error|not)) | .key' <<< "$DATA"
rebuild_status | jq -r -c \
'to_entries[] | select((.value.end_date) and (.value.error|not)) | .key'
)
if [[ ${#to_deploy[@]} -eq 0 ]]; then
debug "No container need to be deploy"
@ -478,8 +469,7 @@ esac
# Trigger container build (if need, enabled and docker compose file is provided)
if [[ $REBUILD -eq 1 ]]; then
[[ -n "$REBUILD_STATUS_FILE" ]] || rebuild_status_file
debug "Check if we have to trigger some rebuild (status file: $REBUILD_STATUS_FILE)"
debug "Check if we have to trigger some rebuild (status file: $(rebuild_status_file))"
if [ ${#UPGRADABLE_CONTAINERS[@]} -eq 0 ]; then
debug "No upgradable container to rebuild"
remove_rebuild_status
@ -493,14 +483,13 @@ if [[ $REBUILD -eq 1 ]]; then
REBUILT_CONTAINERS=()
for container in "${!UPGRADABLE_CONTAINERS[@]}"; do
need_rebuild=0
rebuild_status "$container" CONTAINER_DATA
# shellcheck disable=SC2153
if [[ "$CONTAINER_DATA" != "null" ]]; then
debug "$container: data=$CONTAINER_DATA"
trigger_date=$( jq -r .trigger_date <<< "$CONTAINER_DATA" )
start_date=$( jq -r .start_date <<< "$CONTAINER_DATA" )
end_date=$( jq -r .end_date <<< "$CONTAINER_DATA" )
log=$( jq -r .log <<< "$CONTAINER_DATA" )
container_data=$( rebuild_status "$container" )
if [[ "$container_data" != "null" ]]; then
debug "$container: data=$container_data"
trigger_date=$( jq -r .trigger_date <<< "$container_data" )
start_date=$( jq -r .start_date <<< "$container_data" )
end_date=$( jq -r .end_date <<< "$container_data" )
log=$( jq -r .log <<< "$container_data" )
if [[ "$start_date" == "null" ]]; then
debug "$container: build triggered but not yet started"
message "- $container: rebuild triggered on $trigger_date and not yet started"
@ -510,11 +499,11 @@ if [[ $REBUILD -eq 1 ]]; then
message "- $container: rebuild triggered on $trigger_date and started on" \
"$start_date, but not yet finish (log: $log)"
else
duration=$( jq -r .duration <<< "$CONTAINER_DATA" )
duration=$( jq -r .duration <<< "$container_data" )
debug "$container: rebuilt in $duration on $start_date (finish on $end_date)"
prev_status=$( jq -r .status <<< "$CONTAINER_DATA" )
prev_status=$( jq -r .status <<< "$container_data" )
if [[ "$prev_status" == "${UPGRADABLE_CONTAINERS[$container]}" ]]; then
error=$( jq -r .error <<< "$CONTAINER_DATA" )
error=$( jq -r .error <<< "$container_data" )
if [[ "$error" != "null" ]]; then
message "- $container: $error (log: $log)"
else