Skip to content

Commit 545b93a

Browse files
committed
Merge branch 'tdt-update-device-info' of https://github.com/TDT-AG/openwisp-config into TDT-AG-tdt-update-device-info
2 parents f00ecf9 + 8c68a52 commit 545b93a

1 file changed

Lines changed: 60 additions & 11 deletions

File tree

openwisp-config/files/openwisp.agent

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ CONFIGURATION_CHECKSUM="$WORKING_DIR/checksum"
7676
CONFIGURATION_BACKUP="$WORKING_DIR/backup.tar.gz"
7777
REGISTRATION_PARAMETERS="$WORKING_DIR/registration_parameters"
7878
TEST_CHECKSUM="$WORKING_DIR/test_checksum"
79+
UPDATE_INFO="$WORKING_DIR/update_info"
7980
STATUS_REPORT="$WORKING_DIR/status_report"
8081
APPLYING_CONF="$WORKING_DIR/applying_conf"
8182
BOOTUP="$WORKING_DIR/bootup"
@@ -335,6 +336,49 @@ apply_configuration() {
335336
post_reload_hook
336337
}
337338

339+
# send up to date device info to the controller
340+
update_info() {
341+
# get device model, OS identifier and SOC info
342+
local model=$(cat /tmp/sysinfo/model)
343+
local os=$(ubus call system board | grep description | awk -F\" '{ print $4 }')
344+
local system=$(ubus call system board | grep system | awk -F\" '{ print $4 }')
345+
# retry several times
346+
for i in $(seq 1 10); do
347+
$FETCH_COMMAND -i \
348+
--data "key=$KEY" \
349+
--data-urlencode model="$model" \
350+
--data-urlencode os="$os" \
351+
--data-urlencode system="$system" \
352+
"$UPDATE_INFO_URL" > $UPDATE_INFO
353+
local exit_code=$?
354+
if [ "$exit_code" = "0" ]; then
355+
logger "Device info updated on the controller" \
356+
-t openwisp \
357+
-p daemon.info
358+
break
359+
else
360+
sleep 2
361+
fi
362+
done
363+
364+
if [ "$exit_code" != "0" ]; then
365+
logger -s "Failed to connect to controller during update-info: curl exit code $exit_code" \
366+
-t openwisp \
367+
-p daemon.err
368+
return 2
369+
fi
370+
371+
if [ "$(head -n 1 "$UPDATE_INFO" | grep -c "200 OK")" -lt 1 ]; then
372+
local status=$(head -n 1 $UPDATE_INFO)
373+
logger -s "Failed to update device info: $status" \
374+
-t openwisp \
375+
-p daemon.err
376+
return 3
377+
fi
378+
check_header $UPDATE_INFO
379+
rm $UPDATE_INFO
380+
}
381+
338382
# report configuration status: "running" or "error"
339383
report_status() {
340384
# retry several times
@@ -568,26 +612,31 @@ if [ -z "$UUID" ] || [ -z "$KEY" ]; then
568612
sleep $(expr $INTERVAL / 4)
569613
done
570614
touch "$BOOTUP"
571-
else
572-
# add delay only after bootup, not after registration or restart
573-
if [ "$BOOTUP_DELAY" != "0" ] && [ ! -f "$BOOTUP" ]; then
574-
# random bootup delay between 0..300 seconds
575-
DELAY="$(($RANDOM%$BOOTUP_DELAY))"
576-
logger "Delaying startup for $DELAY seconds..." \
577-
-t openwisp \
578-
-p daemon.info
579-
sleep $DELAY
580-
touch "$BOOTUP"
581-
fi
582615
fi
583616

584617
# these variables are evaluated here because "register()" might set UUID and KEY
585618
BASE_CONFIGURATION_URL="$BASEURL/download-config/$UUID/?key=$KEY"
586619
BASE_CHECKSUM_URL="$BASEURL/checksum/$UUID/?key=$KEY"
587620
CONFIGURATION_URL=$BASE_CONFIGURATION_URL
588621
CHECKSUM_URL=$BASE_CHECKSUM_URL
622+
UPDATE_INFO_URL="$BASEURL/update-info/$UUID/"
589623
REPORT_URL="$BASEURL/report-status/$UUID/"
590624

625+
if [ ! -f "$BOOTUP" ]; then
626+
# actions to do only after agent start, not after registration or agent restart
627+
if [ "$BOOTUP_DELAY" != "0" ]; then
628+
# random bootup delay between 0..300 seconds
629+
DELAY="$(($RANDOM%$BOOTUP_DELAY))"
630+
logger "Delaying startup for $DELAY seconds..." \
631+
-t openwisp \
632+
-p daemon.info
633+
sleep $DELAY
634+
fi
635+
# send up to date device info
636+
update_info
637+
touch "$BOOTUP"
638+
fi
639+
591640
while true
592641
do
593642
# check management interface at each iteration

0 commit comments

Comments
 (0)