Skip to content

Commit 8c68a52

Browse files
committed
[agent] Update general device info on the controller after bootup #58
Fixes #58
1 parent e64e244 commit 8c68a52

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"
@@ -328,6 +329,49 @@ apply_configuration() {
328329
post_reload_hook
329330
}
330331

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

577610
# these variables are evaluated here because "register()" might set UUID and KEY
578611
BASE_CONFIGURATION_URL="$BASEURL/download-config/$UUID/?key=$KEY"
579612
BASE_CHECKSUM_URL="$BASEURL/checksum/$UUID/?key=$KEY"
580613
CONFIGURATION_URL=$BASE_CONFIGURATION_URL
581614
CHECKSUM_URL=$BASE_CHECKSUM_URL
615+
UPDATE_INFO_URL="$BASEURL/update-info/$UUID/"
582616
REPORT_URL="$BASEURL/report-status/$UUID/"
583617

618+
if [ ! -f "$BOOTUP" ]; then
619+
# actions to do only after agent start, not after registration or agent restart
620+
if [ "$BOOTUP_DELAY" != "0" ]; then
621+
# random bootup delay between 0..300 seconds
622+
DELAY="$(($RANDOM%$BOOTUP_DELAY))"
623+
logger "Delaying startup for $DELAY seconds..." \
624+
-t openwisp \
625+
-p daemon.info
626+
sleep $DELAY
627+
fi
628+
# send up to date device info
629+
update_info
630+
touch "$BOOTUP"
631+
fi
632+
584633
while true
585634
do
586635
# check management interface at each iteration

0 commit comments

Comments
 (0)