Skip to content

Commit 891d162

Browse files
committed
SG-4240: Update how commands are built for execution.
1 parent c3a5509 commit 891d162

1 file changed

Lines changed: 59 additions & 46 deletions

File tree

main.sh

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,8 @@ spinner() { #{{{
703703
tail -n0 -f "${log_file}" --pid "${spinner_pid}"
704704
fi
705705
wait "${spinner_pid}"
706-
local exit_code=$?
706+
local exit_code
707+
exit_code=$?
707708
printf " \b\b\b\b\b\r"
708709
debug "$msg (exit code):" "$exit_code"
709710
if [[ ! "${LOG_DEBUG}" =~ true|True ]]; then
@@ -1138,33 +1139,36 @@ configure_fluentbit() { #{{{
11381139
local exists
11391140
local image
11401141

1141-
image="$($CONTAINER_ORCHESTRATOR images -q -f reference="$FLUENTBIT_IMAGE")"
1142+
image=$($CONTAINER_ORCHESTRATOR images -q -f reference="$FLUENTBIT_IMAGE")
11421143
if [[ -z "$image" ]]; then
11431144
info "Fluentbit image:" "$FLUENTBIT_IMAGE"
11441145
$CONTAINER_ORCHESTRATOR pull "$FLUENTBIT_IMAGE" >> "$LOG_FILE" 2>&1 &
11451146
spinner "$!" "Pulling image"
11461147
fi
11471148

11481149
spinner_wait "Configuring fluentbit agent for workflow log collection.."
1149-
# TODO: Identify --network host use-case
1150-
docker_run_command="$CONTAINER_ORCHESTRATOR run -d \
1151-
--name fluentbit-agent \
1152-
--restart=always \
1153-
-p 24224:24224 \
1154-
-p 2020:2020 \
1155-
--network bridge \
1156-
-v /var/lib/docker/containers:/var/lib/docker/containers:ro \
1157-
-v $(pwd)/volumes/db-state/:/var/log/ \
1158-
-v $(pwd)/fluent-bit.conf:/fluent-bit/etc/fluentbit.conf \
1159-
-v /var/log/registration:/var/log/registration \
1160-
--log-driver=fluentd \
1161-
--log-opt fluentd-async=true \
1162-
--log-opt tag=fluentbit
1163-
"
1150+
11641151
running=$($CONTAINER_ORCHESTRATOR ps -q --filter "name=fluentbit-agent")
11651152
exists=$($CONTAINER_ORCHESTRATOR ps -aq --filter "name=fluentbit-agent")
11661153

11671154
if [[ -z "${exists}" ]]; then
1155+
# Build docker command as array to prevent word splitting issues
1156+
local -a docker_cmd=(
1157+
"$CONTAINER_ORCHESTRATOR" run -d
1158+
--name fluentbit-agent
1159+
--restart=always
1160+
-p 24224:24224
1161+
-p 2020:2020
1162+
--network bridge
1163+
-v /var/lib/docker/containers:/var/lib/docker/containers:ro
1164+
-v "$(pwd)/volumes/db-state/:/var/log/"
1165+
-v "$(pwd)/fluent-bit.conf:/fluent-bit/etc/fluentbit.conf"
1166+
-v /var/log/registration:/var/log/registration
1167+
--log-driver=fluentd
1168+
--log-opt fluentd-async=true
1169+
--log-opt tag=fluentbit
1170+
)
1171+
11681172
if [[ "${STORAGE_BACKEND_TYPE}" == "aws_s3" && -n "${S3_AWS_ACCESS_KEY_ID}" && -n "${S3_AWS_SECRET_ACCESS_KEY}" && -n "${S3_AWS_REGION}" ]]; then
11691173
# Create AWS credentials file (cleaned up during deregistration via clean_local_setup)
11701174
mkdir -p "$(pwd)/volumes/aws"
@@ -1177,16 +1181,14 @@ region = ${S3_AWS_REGION}
11771181
EOF
11781182
chmod 600 "$(pwd)/volumes/aws/credentials"
11791183

1180-
extra_options="-v $(pwd)/volumes/aws/credentials:/root/.aws/credentials:ro \
1181-
-e AWS_REGION=${S3_AWS_REGION} \
1182-
$FLUENTBIT_IMAGE \
1183-
/fluent-bit/bin/fluent-bit -c /fluent-bit/etc/fluentbit.conf"
1184-
$docker_run_command $extra_options >> "$LOG_FILE" 2>&1
1185-
elif [[ "${STORAGE_BACKEND_TYPE}" == "azure_blob_storage" || "${STORAGE_BACKEND_TYPE}" == "aws_s3" ]]; then
1186-
extra_options="$FLUENTBIT_IMAGE \
1187-
/fluent-bit/bin/fluent-bit -c /fluent-bit/etc/fluentbit.conf"
1188-
$docker_run_command $extra_options >> "$LOG_FILE" 2>&1
1184+
docker_cmd+=(-v "$(pwd)/volumes/aws/credentials:/root/.aws/credentials:ro")
1185+
docker_cmd+=(-e "AWS_REGION=${S3_AWS_REGION}")
11891186
fi
1187+
1188+
docker_cmd+=("$FLUENTBIT_IMAGE")
1189+
docker_cmd+=(/fluent-bit/bin/fluent-bit -c /fluent-bit/etc/fluentbit.conf)
1190+
1191+
"${docker_cmd[@]}" >> "$LOG_FILE" 2>&1
11901192
else
11911193
if [[ -z "${running}" ]]; then
11921194
$CONTAINER_ORCHESTRATOR start fluentbit-agent >&/dev/null
@@ -1387,20 +1389,35 @@ deregister_instance() { #{{{
13871389
}
13881390
#}}}: deregister_instance
13891391

1392+
#######################################
1393+
# Update diagnostic JSON file with key-value pair
1394+
# Globals:
1395+
# SG_DIAGNOSTIC_FILE
1396+
# SG_DIAGNOSTIC_TMP_FILE
1397+
# Arguments:
1398+
# $1 - JSON key path (e.g., "system.last_check")
1399+
# $2 - Value to set
1400+
#######################################
1401+
update_diagnostic() { #{{{
1402+
local key="$1"
1403+
local value="$2"
1404+
jq ".$key = \"$value\"" "$SG_DIAGNOSTIC_FILE" > "$SG_DIAGNOSTIC_TMP_FILE"
1405+
mv "$SG_DIAGNOSTIC_TMP_FILE" "$SG_DIAGNOSTIC_FILE"
1406+
}
1407+
#}}}: update_diagnostic
1408+
13901409
doctor() { #{{{
13911410
echo
13921411

1393-
jq ".system.last_check = \"$(date)\"" "$SG_DIAGNOSTIC_FILE" >> "$SG_DIAGNOSTIC_TMP_FILE"
1394-
mv "$SG_DIAGNOSTIC_TMP_FILE" "$SG_DIAGNOSTIC_FILE"
1412+
update_diagnostic "system.last_check" "$(date)"
13951413

13961414
local status_list=""
13971415
local service_status
13981416
local service_list=( "ecs" "docker" )
13991417

14001418
for service in "${service_list[@]}"; do
14011419
service_status="$(systemctl is-active "${service}")"
1402-
jq ".health.service.${service} = \"$service_status\"" $SG_DIAGNOSTIC_FILE > $SG_DIAGNOSTIC_TMP_FILE
1403-
mv $SG_DIAGNOSTIC_TMP_FILE $SG_DIAGNOSTIC_FILE
1420+
update_diagnostic "health.service.${service}" "$service_status"
14041421
if [[ -n ${service_status} && ${service_status} == "active" ]]; then
14051422
status_list="$(printf "%s\n%s" \
14061423
"${status_list}" \
@@ -1416,8 +1433,7 @@ doctor() { #{{{
14161433
echo
14171434
service_status="$(systemctl is-active "$CONTAINER_ORCHESTRATOR")"
14181435
if [[ "${service_status}" != "active" ]]; then
1419-
jq ".health.service.$CONTAINER_ORCHESTRATOR = \"$service_status\"" "$SG_DIAGNOSTIC_FILE" > "$SG_DIAGNOSTIC_TMP_FILE"
1420-
mv "$SG_DIAGNOSTIC_TMP_FILE" "$SG_DIAGNOSTIC_FILE"
1436+
update_diagnostic "health.service.$CONTAINER_ORCHESTRATOR" "$service_status"
14211437
printf " + Container Status (${C_BOLD}$CONTAINER_ORCHESTRATOR ${C_RESET}service: ${C_RED}%s${C_RESET})\n\n" "${service_status}"
14221438
return
14231439
fi
@@ -1432,14 +1448,12 @@ doctor() { #{{{
14321448
--format '{{.Status}}'\
14331449
)"
14341450
if [[ -z ${container_status} ]]; then
1435-
jq ".health.container.$container = \"Not Running\"" $SG_DIAGNOSTIC_FILE > $SG_DIAGNOSTIC_TMP_FILE
1436-
mv $SG_DIAGNOSTIC_TMP_FILE $SG_DIAGNOSTIC_FILE
1451+
update_diagnostic "health.container.$container" "Not Running"
14371452
status_list="$(printf "%s\n%s" \
14381453
"${status_list}" \
14391454
"$(printf " | * ${C_BOLD}%s${C_RESET} agent: ${C_RED}Not Running${C_RESET}\n" "${container}")")"
14401455
else
1441-
jq ".health.container.$container = \"$container_status\"" $SG_DIAGNOSTIC_FILE > $SG_DIAGNOSTIC_TMP_FILE
1442-
mv $SG_DIAGNOSTIC_TMP_FILE $SG_DIAGNOSTIC_FILE
1456+
update_diagnostic "health.container.$container" "$container_status"
14431457
status_list="$(printf "%s\n%s" \
14441458
"${status_list}" \
14451459
"$(printf " | * ${C_BOLD}%s${C_RESET} agent: ${C_GREEN}%s${C_RESET}\n" "${container}" "${container_status}")")"
@@ -1452,25 +1466,24 @@ doctor() { #{{{
14521466

14531467
prune() { #{{{
14541468
local reclaimed
1455-
prune_filter="until=4h"
1469+
local prune_filter="until=4h"
1470+
local curr_time
14561471
curr_time=$(date)
14571472

14581473
spinner_wait "Cleaning up system.."
1474+
local reclaimed_containers_images
14591475
reclaimed_containers_images=$($CONTAINER_ORCHESTRATOR system prune -f \
1460-
--filter $prune_filter \
1476+
--filter "$prune_filter" \
14611477
| cut -d: -f2 | tr -d ' ')
14621478

1463-
jq ".system.docker.last_prune = \"$curr_time\"" "$SG_DIAGNOSTIC_FILE" >> "$SG_DIAGNOSTIC_TMP_FILE"
1464-
mv "$SG_DIAGNOSTIC_TMP_FILE" "$SG_DIAGNOSTIC_FILE"
1465-
jq ".system.docker.reclaimed_containers_images = \"$reclaimed_containers_images\"" "$SG_DIAGNOSTIC_FILE" >> "$SG_DIAGNOSTIC_TMP_FILE"
1466-
mv "$SG_DIAGNOSTIC_TMP_FILE" "$SG_DIAGNOSTIC_FILE"
1467-
jq ".system.docker.prune_filter = \"$prune_filter\"" "$SG_DIAGNOSTIC_FILE" >> "$SG_DIAGNOSTIC_TMP_FILE"
1468-
mv "$SG_DIAGNOSTIC_TMP_FILE" "$SG_DIAGNOSTIC_FILE"
1479+
update_diagnostic "system.docker.last_prune" "$curr_time"
1480+
update_diagnostic "system.docker.reclaimed_containers_images" "$reclaimed_containers_images"
1481+
update_diagnostic "system.docker.prune_filter" "$prune_filter"
14691482

1483+
local reclaimed_volumes
14701484
reclaimed_volumes=$($CONTAINER_ORCHESTRATOR system prune --volumes -f \
14711485
| cut -d: -f2 | tr -d ' ')
1472-
jq ".system.docker.reclaimed_volumes = \"$reclaimed_volumes\"" "$SG_DIAGNOSTIC_FILE" >> "$SG_DIAGNOSTIC_TMP_FILE"
1473-
mv "$SG_DIAGNOSTIC_TMP_FILE" "$SG_DIAGNOSTIC_FILE"
1486+
update_diagnostic "system.docker.reclaimed_volumes" "$reclaimed_volumes"
14741487

14751488
# # Already taken care by ECS agent: Remove all unused images not just dangling, older than 10 days, check if the image created date is used.
14761489
# reclaimed=$($CONTAINER_ORCHESTRATOR system prune -a \

0 commit comments

Comments
 (0)