Skip to content

Commit 360a376

Browse files
committed
v0.0.1
1 parent fc04e62 commit 360a376

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.DS_Store
22
openwrt*
3+
.build/

openwisp-config/files/openwisp.agent

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ PRE_RELOAD_HOOK=${PRE_RELOAD_HOOK:-/etc/openwisp/pre-reload-hook}
7070
POST_RELOAD_HOOK=${POST_RELOAD_HOOK:-/etc/openwisp/post-reload-hook}
7171
POST_REGISTRATION_HOOK=${POST_REGISTRATION_HOOK:-/etc/openwisp/post-registration-hook}
7272
WORKING_DIR="/tmp/openwisp"
73-
BASEURL="$URL/controller"
73+
BASEURL="$URL/controller/device"
7474
CONFIGURATION_ARCHIVE="$WORKING_DIR/configuration.tar.gz"
7575
CONFIGURATION_CHECKSUM="$WORKING_DIR/checksum"
7676
CONFIGURATION_BACKUP="$WORKING_DIR/backup.tar.gz"
77+
ACTIVE_CONFIGURATION_BACKUP="$WORKING_DIR/active_config.tar.gz"
78+
REMOTE_KNOWN_CONFIG_CHECKSUM="$WORKING_DIR/remote_config_checksum"
79+
UPLOAD_CONFIG_RESPONSE="$WORKING_DIR/upload_config_response"
7780
REGISTRATION_PARAMETERS="$WORKING_DIR/registration_parameters"
7881
TEST_CHECKSUM="$WORKING_DIR/test_checksum"
7982
UPDATE_INFO="$WORKING_DIR/update_info"
@@ -237,7 +240,7 @@ register() {
237240

238241
# gets checksum from controller
239242
get_checksum() {
240-
$($FETCH_COMMAND -i $CHECKSUM_URL > $1)
243+
$($FETCH_COMMAND -i $2 > $1)
241244
local exit_code=$?
242245

243246
if [ "$exit_code" != "0" ]; then
@@ -260,7 +263,7 @@ get_checksum() {
260263
# returns 1 if configuration in controller has changed
261264
configuration_changed() {
262265
local CURRENT_CHECKSUM=$(tail -n 1 $CONFIGURATION_CHECKSUM 2> /dev/null)
263-
get_checksum $CONFIGURATION_CHECKSUM
266+
get_checksum $CONFIGURATION_CHECKSUM $CHECKSUM_URL
264267
local exit_code=$?
265268

266269
if [ "$exit_code" != "0" ]; then
@@ -626,6 +629,11 @@ CHECKSUM_URL=$BASE_CHECKSUM_URL
626629
UPDATE_INFO_URL="$BASEURL/update-info/$UUID/"
627630
REPORT_URL="$BASEURL/report-status/$UUID/"
628631

632+
BASE_CONFIG_BACKUP_CHECKSUM_URL="$BASEURL/known-active-config/checksum/$UUID/?key=$KEY"
633+
CONFIG_BACKUP_CHECKSUM_URL=$BASE_CONFIG_BACKUP_CHECKSUM_URL
634+
BASE_CONFIG_BACKUP_UPLOAD_URL="$BASEURL/upload-config/$UUID/?key=$KEY"
635+
CONFIG_BACKUP_UPLOAD_URL=$BASE_CONFIG_BACKUP_UPLOAD_URL
636+
629637
if [ ! -f "$BOOTUP" ]; then
630638
# actions to do only after agent start, not after registration or agent restart
631639
if [ "$BOOTUP_DELAY" != "0" ]; then
@@ -641,18 +649,79 @@ if [ ! -f "$BOOTUP" ]; then
641649
touch "$BOOTUP"
642650
fi
643651

652+
create_backup() {
653+
$(sysupgrade -q -b $ACTIVE_CONFIGURATION_BACKUP)
654+
if [ ! -f $ACTIVE_CONFIGURATION_BACKUP ]; then
655+
logger -s "Failed to create a configuration backup" \
656+
-t openwisp \
657+
-p daemon.err
658+
return 2
659+
fi
660+
}
661+
662+
remote_configuration_changed() {
663+
create_backup
664+
# Backup process failed
665+
local exit_code=$?
666+
if [ "$exit_code" != "0" ]; then
667+
return 2
668+
fi
669+
# Compare checksum
670+
local local_md5hash=$(md5sum $ACTIVE_CONFIGURATION_BACKUP | awk '{print $1}')
671+
get_checksum $REMOTE_KNOWN_CONFIG_CHECKSUM $CONFIG_BACKUP_CHECKSUM_URL
672+
local remote_md5hash=$(tail -n 1 $REMOTE_KNOWN_CONFIG_CHECKSUM 2> /dev/null)
673+
# Getting remote md hash failed
674+
local exit_code=$?
675+
if [ "$exit_code" != "0" ]; then
676+
return 2
677+
fi
678+
if [ "$local_md5hash" != "$remote_md5hash" ]; then
679+
logger "Remote configuration outdated" \
680+
-t openwisp \
681+
-p daemon.info
682+
return 1
683+
fi
684+
return 0
685+
}
686+
687+
remote_update_configuration() {
688+
$($FETCH_COMMAND -i -F config=@$ACTIVE_CONFIGURATION_BACKUP $CONFIG_BACKUP_UPLOAD_URL > $UPLOAD_CONFIG_RESPONSE)
689+
690+
if [ "$?" != "0" ]; then
691+
logger -s "Failed to connect to controller while getting uploading configurations: curl exit code $exit_code" \
692+
-t openwisp \
693+
-p daemon.err
694+
return 2
695+
fi
696+
697+
check_header $UPLOAD_CONFIG_RESPONSE
698+
local response_message=$(tail -n 1 $UPLOAD_CONFIG_RESPONSE)
699+
if [ $(echo $response_message | grep -c "config-upload: success") -lt 1 ]; then
700+
logger -s "Local config upload failed: $response_message, will retry in $INTERVAL seconds." \
701+
-t openwisp \
702+
-p daemon.err
703+
return 2
704+
fi
705+
rm $UPLOAD_CONFIG_RESPONSE
706+
}
707+
644708
while true
645709
do
646710
# check management interface at each iteration
647711
# (because the address may change due to
648712
# configuration updates or manual reconfigurations)
649713
discover_management_ip
650-
714+
# Check if configurations changed on the server
651715
configuration_changed
652-
653716
if [ "$?" == "1" ]; then
654717
update_configuration
655718
fi
719+
# Check if local configurations are different from
720+
# openwisp configurations
721+
remote_configuration_changed
722+
if [ "$?" == "1" ]; then
723+
remote_update_configuration
724+
fi
656725

657726
sleep $INTERVAL
658727
done

runbuild

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
# ci build script
33
# runs tests and then builds the OpenWRT package
44
# requires the following env vars:
5-
# - $BUILD_DIR
6-
# - $DOWNLOADS_DIR
5+
# - $BUILD_DIR (defaults to .build/ in root of repository)
6+
# - $DOWNLOADS_DIR (defaults to .build/downloads in root of repository)
77
# - $CORES (defaults to 1)
88
set -e
99

1010
./runtests
1111

1212
START_TIME=$(date +"%Y-%m-%d-%H%M%S")
13+
# TODO: Ask if these defaults are welcomed
14+
BUILD_DIR=${BUILD_DIR:-$(pwd)/.build}
15+
DOWNLOADS_DIR=${DOWNLOADS_DIR:-$BUILD_DIR/downloads}
1316
VERSIONED_DIR="$DOWNLOADS_DIR/$START_TIME"
1417
LATEST_LINK="$DOWNLOADS_DIR/latest"
1518
CORES=${CORES:-1}
@@ -27,7 +30,10 @@ git checkout openwrt-18.06
2730
git pull origin openwrt-18.06
2831

2932
# configure feeds
30-
echo "src-git openwisp https://github.com/openwisp/openwisp-config.git" > feeds.conf
33+
# TODO: Revert change
34+
# echo "src-git openwisp https://github.com/openwisp/openwisp-config.git" > feeds.conf
35+
echo "src-git openwisp https://github.com/atb00ker/openwisp-config.git;gsoc2020" > feeds.conf
36+
3137
cat feeds.conf.default >> feeds.conf
3238
# remove unneeded feeds
3339
sed -i '/telephony/d' feeds.conf

0 commit comments

Comments
 (0)