Skip to content

Commit

Permalink
[fix] Use stored dir for storing configuration and minor changes #153
Browse files Browse the repository at this point in the history
Store only when configuration is not in remote

Close #153
  • Loading branch information
devkapilbansal committed Nov 24, 2021
1 parent f08c7df commit 668de1d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions openwisp-config/files/sbin/openwisp-update-config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ local test_root_dir = working_dir .. '/update-test'
local remote_dir = openwisp_dir .. '/remote'
local remote_config_dir = remote_dir .. '/etc/config'
local stored_dir = openwisp_dir .. '/stored'
local stored_config_dir = openwisp_dir .. '/etc/config'
local stored_config_dir = stored_dir .. '/etc/config'
local added_file = openwisp_dir .. '/added.list'
local modified_file = openwisp_dir .. '/modified.list'
local get_standard = function() return uci.cursor(standard_config_dir) end
Expand Down Expand Up @@ -84,8 +84,8 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
if section_stored == nil then
utils.remove_uci_options(standard, file, section)
-- section is in the backup configuration -> restore
-- delete all options first
else
-- delete all options first
for option, value in pairs(section) do
if not utils.starts_with_dot(option) then
standard:delete(file, section['.name'], option)
Expand Down Expand Up @@ -148,7 +148,7 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
-- ensure we are acting on a file
if lfs.attributes(remote_path, 'mode') == 'file' then
-- if there's no backup of the file yet, create one
if not utils.file_exists(stored_path) then
if (not utils.file_exists(stored_path) and not utils.file_exists(remote_path)) then
os.execute('cp '..standard_path..' '..stored_path)
end
-- MERGE mode
Expand Down
9 changes: 5 additions & 4 deletions openwisp-config/tests/test_update_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local update_config = assert(loadfile("../files/sbin/openwisp-update-config.lua"
local write_dir = './update-test/'
local config_dir = write_dir .. 'etc/config/'
local openwisp_dir = './openwisp/'
local stored_dir = openwisp_dir .. '/stored/'
local remote_config_dir = openwisp_dir .. 'remote/etc/config'

local function string_count(base, pattern)
Expand Down Expand Up @@ -38,8 +39,8 @@ TestUpdateConfig = {
os.execute('echo restore-me > '..openwisp_dir..'/stored/etc/restore-me')
os.execute('echo /etc/restore-me > '..openwisp_dir..'/modified.list')
-- this file is stored in the backup
os.execute('mkdir -p ' .. openwisp_dir ..'etc/config/')
os.execute("cp ./update/stored_wireless " ..openwisp_dir.. '/etc/config/wireless')
os.execute('mkdir -p ' .. stored_dir ..'etc/config/')
os.execute("cp ./update/stored_wireless " ..stored_dir.. '/etc/config/wireless')
end,
tearDown = function()
os.execute('rm -rf ' .. write_dir)
Expand Down Expand Up @@ -123,12 +124,12 @@ function TestUpdateConfig.test_update()
luaunit.assertEquals(restoreFile:read('*all'), 'restore-me\n')
luaunit.assertNil(io.open(openwisp_dir..'/stored/etc/restore-me'))
-- ensure network configuration file is backed up
local storedNetworkFile = io.open(openwisp_dir .. '/etc/config/network')
local storedNetworkFile = io.open(stored_dir .. '/etc/config/network')
luaunit.assertNotNil(storedNetworkFile)
local initialNetworkFile = io.open('update/network')
luaunit.assertEquals(storedNetworkFile:read('*all'), initialNetworkFile:read('*all'))
-- ensure system configuration file is backed up
local storedSystemFile = io.open(openwisp_dir .. '/etc/config/system')
local storedSystemFile = io.open(stored_dir .. '/etc/config/system')
luaunit.assertNotNil(storedSystemFile)
local initialSystemFile = io.open('update/system')
luaunit.assertEquals(storedSystemFile:read('*all'), initialSystemFile:read('*all'))
Expand Down

0 comments on commit 668de1d

Please sign in to comment.