Compare commits

..

2 commits

View file

@ -93,20 +93,26 @@ function rebuild_status_file() {
} }
function rebuild_status() { function rebuild_status() {
if [[ -z "$1" ]]; then local output_var=$1
cat "$(rebuild_status_file)" [[ -n "$REBUILD_STATUS_FILE" ]] || rebuild_status_file
if [[ -z "$2" ]]; then
declare -g "$output_var=$( cat "$REBUILD_STATUS_FILE" )"
else else
rebuild_status | jq -r --arg container "$1" '.[$container]' declare -g "$output_var=$(
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=$(
rebuild_status | jq \ jq \
--arg container "$2" \ --arg container "$2" \
'del(.[$container])' 'del(.[$container])' <<< "$DATA"
) )
else else
local args=( --arg container "$1" ) arg name value expr=( ) local args=( --arg container "$1" ) arg name value expr=( )
@ -117,16 +123,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=$(
rebuild_status | jq \ jq "${args[@]}" "$( implode ' | ' "${expr[@]}" )" <<< "$DATA"
"${args[@]}" "$( implode ' | ' "${expr[@]}" )"
) )
fi fi
cat <<< "$data" > "$(rebuild_status_file)" cat <<< "$data" > "$REBUILD_STATUS_FILE"
} }
function remove_rebuild_status() { function remove_rebuild_status() {
rebuild_status_file > /dev/null [[ -n "$REBUILD_STATUS_FILE" ]] || rebuild_status_file
[[ -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
@ -258,9 +264,11 @@ 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 < <(
rebuild_status | jq -r -c \ jq -r -c 'to_entries[] | select(
'to_entries[] | select((.value.start_date|not) and (.value.error|not)) | .key' (.value.start_date|not) and (.value.error|not)
) | .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"
@ -292,9 +300,10 @@ 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 < <(
rebuild_status | jq -r -c \ jq -r -c \
'to_entries[] | select((.value.end_date) and (.value.error|not)) | .key' 'to_entries[] | select((.value.end_date) and (.value.error|not)) | .key' <<< "$DATA"
) )
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"
@ -469,7 +478,8 @@ 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
debug "Check if we have to trigger some rebuild (status file: $(rebuild_status_file))" [[ -n "$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
@ -483,13 +493,14 @@ 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
container_data=$( rebuild_status "$container" ) rebuild_status "$container" CONTAINER_DATA
if [[ "$container_data" != "null" ]]; then # shellcheck disable=SC2153
debug "$container: data=$container_data" if [[ "$CONTAINER_DATA" != "null" ]]; then
trigger_date=$( jq -r .trigger_date <<< "$container_data" ) debug "$container: data=$CONTAINER_DATA"
start_date=$( jq -r .start_date <<< "$container_data" ) trigger_date=$( jq -r .trigger_date <<< "$CONTAINER_DATA" )
end_date=$( jq -r .end_date <<< "$container_data" ) start_date=$( jq -r .start_date <<< "$CONTAINER_DATA" )
log=$( jq -r .log <<< "$container_data" ) end_date=$( jq -r .end_date <<< "$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"
@ -499,11 +510,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