Compare commits

..

2 commits

View file

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