Compare commits

..

2 commits

View file

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