Skip to content

Commit e39625f

Browse files
[change] Remove options and sections coming from remote configuration
1 parent 10f0740 commit e39625f

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

openwisp-config/files/sbin/openwisp-update-config.lua

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,6 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
112112
end
113113
end
114114
end
115-
-- remove entire section if empty
116-
local result = standard:get_all(file, section['.name'])
117-
if result and utils.is_uci_empty(result) then
118-
standard:delete(file, section['.name'])
119-
end
120115
end
121116
end
122117
standard:commit(file)
@@ -148,8 +143,32 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
148143
-- ensure we are acting on a file
149144
if lfs.attributes(remote_path, 'mode') == 'file' then
150145
-- if there's no backup of the file yet, create one
151-
if (not utils.file_exists(stored_path) and not utils.file_exists(remote_path)) then
146+
if (not utils.file_exists(stored_path)) then
152147
os.execute('cp '..standard_path..' '..stored_path)
148+
if (utils.file_exists(remote_path)) then
149+
for key, section in pairs(stored:get_all(file)) do
150+
-- check if section is in remote configuration
151+
local section_check = check:get(file, section['.name'])
152+
if section_check ~= nil then
153+
-- check if options is in remote configuration
154+
for option, value in pairs(section) do
155+
if not utils.starts_with_dot(option) then
156+
local option_check = check:get(file, section['.name'], option)
157+
if option_check ~= nil then
158+
-- if option is in remote configuration, remove it
159+
stored:delete(file, section['.name'], option)
160+
end
161+
end
162+
end
163+
end
164+
end
165+
stored:commit(file)
166+
-- remove uci file if empty
167+
local uci_file = stored:get_all(file)
168+
if uci_file and utils.is_table_empty(uci_file) then
169+
os.remove(stored_path)
170+
end
171+
end
153172
end
154173
-- MERGE mode
155174
if MERGE then

openwisp-config/tests/test_update_config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function TestUpdateConfig.test_update()
126126
-- ensure network and configuration file is not backed up as it is overwritten by remote
127127
local storedNetworkFile = io.open(openwisp_dir .. '/etc/config/network')
128128
luaunit.assertNil(storedNetworkFile)
129-
local storedSystemFile = io.open(stored_dir .. '/etc/config/system')
129+
local storedSystemFile = io.open(openwisp_dir .. '/etc/config/system')
130130
luaunit.assertNil(storedSystemFile)
131131
end
132132

0 commit comments

Comments
 (0)