Skip to content

Commit

Permalink
Implemented unmanaged configurations #16
Browse files Browse the repository at this point in the history
Replacing merge_default
  • Loading branch information
nemesifier committed Mar 4, 2016
1 parent 999af06 commit 6376e99
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 44 deletions.
59 changes: 22 additions & 37 deletions openwisp-config/files/openwisp.agent
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ while [ -n "$1" ]; do
--uuid) export UUID="$2"; shift;;
--key) export KEY="$2"; shift;;
--shared-secret) export SHARED_SECRET="$2"; shift;;
--merge-default) export MERGE_DEFAULT="$2"; shift;;
--unmanaged) export UNMANAGED="$2"; shift;;
--test-config) export TEST_CONFIG="$2"; shift;;
--test-script) export TEST_SCRIPT="$2"; shift;;
-*)
Expand Down Expand Up @@ -48,18 +48,19 @@ TEST_CHECKSUM="$WORKING_DIR/test_checksum"
STATUS_REPORT="$WORKING_DIR/status_report"
APPLYING_CONF="$WORKING_DIR/applying_conf"
REGISTRATION_URL="$URL/controller/register/"
DEFAULT_DIR="/etc/openwisp-config-defaults"
# DEFAULT_DIR="/etc/openwisp-config-defaults"
UNMANAGED_DIR="$WORKING_DIR/unmanaged"
FETCH_COMMAND="curl -s"
mkdir -p $WORKING_DIR
mkdir -p $DEFAULT_DIR
mkdir -p $UNMANAGED_DIR

if [ "$VERIFY_SSL" != "1" ]; then
FETCH_COMMAND="$FETCH_COMMAND -k"
fi

if [ -n "$MERGE_DEFAULT" ]; then
if [ -n "$UNMANAGED" ]; then
# replace commas with spaces
MERGE_DEFAULT=$(echo $MERGE_DEFAULT | tr ',' ' ')
UNMANAGED=$(echo $UNMANAGED | tr ',' ' ')
fi

# ensures we are dealing with the right web server
Expand Down Expand Up @@ -170,8 +171,8 @@ configuration_changed() {
# applies a specified configuration archive
apply_configuration() {
local sleep_time=${2-2}
# store default config (if enabled)
store_default
# store unmanaged config (if enabled)
store_unmanaged
# -r restores the specified configuration archive
sysupgrade -r $1
local exit_code=$?
Expand All @@ -183,7 +184,7 @@ apply_configuration() {
return 1
fi
# merge default config (if enabled)
merge_default
merge_unmanaged
# apply changes and wait 2 seconds
/usr/sbin/apply_config
sleep $sleep_time
Expand Down Expand Up @@ -254,42 +255,26 @@ test_configuration() {
return $ret
}

# stores default configuration files that will be merged with the
# configuration downloaded from the controller
store_default() {
if [ -z "$MERGE_DEFAULT" ]; then
# stores unmanaged configuration sections that will be merged
# with the configuration downloaded from the controller
store_unmanaged() {
if [ -z "$UNMANAGED" ]; then
return 0
fi

for config in $MERGE_DEFAULT; do
# store default config only if not already done previously
if [ ! -f "$DEFAULT_DIR/$config" ] && [ -f "/etc/config/$config" ]; then
# system is a special case:
# we need to remove hostname and timezone because these
# are automatically generated by the controller
if [ "$config" == "system" ]; then
uci delete system.@system[0]
uci commit system
fi
cp "/etc/config/$config" "$DEFAULT_DIR/$config"
logger -s "Stored $DEFAULT_DIR/$config" \
-t openwisp \
-p daemon.info
fi
done
/usr/sbin/store_unmanaged -o="$UNMANAGED"
}

# merges default configuration files stored by store_default()
merge_default() {
if [ -z "$MERGE_DEFAULT" ]; then
# merges unmanaged configuration files stored by store_unmanaged()
merge_unmanaged() {
if [ -z "$UNMANAGED" ]; then
return 0
fi

for config in $MERGE_DEFAULT; do
if [ -f "$DEFAULT_DIR/$config" ]; then
uci import -m -f $DEFAULT_DIR/$config $config
uci commit $config
fi
for path in $UNMANAGED_DIR/*
do
local config=$(basename $path)
uci import -m -f $path $config
uci commit $config
done
}

Expand Down
10 changes: 8 additions & 2 deletions openwisp-config/files/openwisp.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ config controller 'http'
option uuid ''
option key ''
option shared_secret ''
list merge_default 'system'
list merge_default 'network'
option test_config '1'
#option test_script '/usr/sbin/mytest'
list unmanaged 'system.ntp'
list unmanaged 'system.@led'
list unmanaged 'network.loopback'
list unmanaged 'network.globals'
list unmanaged 'network.lan'
list unmanaged 'network.wan'
list unmanaged 'network.@switch'
list unmanaged 'network.@switch_vlan'
10 changes: 5 additions & 5 deletions openwisp-config/files/openwisp.init
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ start_service() {
uuid=$(config_get http uuid)
key=$(config_get http key)
shared_secret=$(config_get http shared_secret)
merge_default=$(config_get http merge_default)
unmanaged=$(config_get http unmanaged)
test_config=$(config_get http test_config)
test_script=$(config_get http test_script)
if [ $url ]; then url="--url $url"; fi
Expand All @@ -23,11 +23,11 @@ start_service() {
if [ $uuid ]; then uuid="--uuid $uuid"; fi
if [ $key ]; then key="--key $key"; fi
if [ $shared_secret ]; then shared_secret="--shared-secret $shared_secret"; fi
if [ -n "$merge_default" ]; then
if [ -n "$unmanaged" ]; then
# replace spaces with commas to avoid problems when
# passing this arg to procd_set_param command
merge_default=$(echo $merge_default | tr ' ' ',')
merge_default="--merge-default $merge_default";
unmanaged=$(echo $unmanaged | tr ' ' ',')
unmanaged="--unmanaged $unmanaged";
fi
if [ $test_config ]; then test_config="--test-config $test_config"; fi
if [ $test_script ]; then test_script="--test-script $test_script"; fi
Expand All @@ -48,7 +48,7 @@ start_service() {

procd_open_instance
procd_set_param command $PROG $url $interval $verify_ssl $uuid $key \
$shared_secret $merge_default $test_config $test_script
$shared_secret $unmanaged $test_config $test_script
procd_set_param respawn
procd_close_instance
logger -s "$PROG_NAME started" \
Expand Down

0 comments on commit 6376e99

Please sign in to comment.