11#! /usr/bin/env lua
22-- openwisp-config configuration updater
33
4- require (' os' )
5- require (' lfs' )
6- require (' uci' )
4+ local os = require (' os' )
5+ local lfs = require (' lfs' )
6+ local uci = require (' uci' )
77local utils = require (' openwisp.utils' )
88local arg = {... }
99
1010-- parse arguments
11- MERGE = true
12- TEST = false
13- for key , value in pairs (arg ) do
11+ local MERGE = true
12+ local TEST = false
13+ for _ , value in pairs (arg ) do
1414 -- test argument
1515 if value == ' --merge=0' then MERGE = false ; end
1616 if value == ' --test=1' then TEST = true ; end
1717end
1818
19- working_dir = lfs .currentdir ()
20- tmp_dir = not TEST and ' /tmp/openwisp' or working_dir
21- check_dir = tmp_dir .. ' /check'
22- check_config_dir = check_dir .. ' /etc/config'
23- downloaded_conf = tmp_dir .. ' /configuration.tar.gz'
24- openwisp_dir = not TEST and ' /etc/openwisp' or working_dir .. ' /openwisp'
25- standard_config_dir = not TEST and ' /etc/config' or working_dir .. ' /update-test/etc/config'
26- test_root_dir = working_dir .. ' /update-test'
27- remote_dir = openwisp_dir .. ' /remote'
28- remote_config_dir = remote_dir .. ' /etc/config'
29- stored_dir = openwisp_dir .. ' /stored'
30- added_file = openwisp_dir .. ' /added.list'
31- modified_file = openwisp_dir .. ' /modified.list'
32- get_standard = function () return uci .cursor (standard_config_dir ) end
33- get_remote = function () return uci .cursor (remote_config_dir , ' /tmp/openwisp/.uci' ) end
34- get_check = function () return uci .cursor (check_config_dir , ' /tmp/openwisp/.uci' ) end
19+ local working_dir = lfs .currentdir ()
20+ local tmp_dir = not TEST and ' /tmp/openwisp' or working_dir
21+ local check_dir = tmp_dir .. ' /check'
22+ local check_config_dir = check_dir .. ' /etc/config'
23+ local downloaded_conf = tmp_dir .. ' /configuration.tar.gz'
24+ local openwisp_dir = not TEST and ' /etc/openwisp' or working_dir .. ' /openwisp'
25+ local standard_config_dir = not TEST and ' /etc/config' or working_dir .. ' /update-test/etc/config'
26+ local test_root_dir = working_dir .. ' /update-test'
27+ local remote_dir = openwisp_dir .. ' /remote'
28+ local remote_config_dir = remote_dir .. ' /etc/config'
29+ local stored_dir = openwisp_dir .. ' /stored'
30+ local added_file = openwisp_dir .. ' /added.list'
31+ local modified_file = openwisp_dir .. ' /modified.list'
32+ local get_standard = function () return uci .cursor (standard_config_dir ) end
33+ local get_remote = function () return uci .cursor (remote_config_dir , ' /tmp/openwisp/.uci' ) end
34+ local get_check = function () return uci .cursor (check_config_dir , ' /tmp/openwisp/.uci' ) end
3535
3636-- uci cursors
37- standard = get_standard ()
38- remote = get_remote ()
37+ local standard = get_standard ()
38+ local remote = get_remote ()
3939
4040-- check downloaded UCI files before proceeding
4141os.execute (' mkdir -p ' .. check_dir )
4242os.execute (' tar -zxf ' .. downloaded_conf .. ' -C ' .. check_dir )
43- check = get_check ()
43+ local check = get_check ()
4444
4545if lfs .attributes (check_config_dir , ' mode' ) == ' directory' then
4646 for file in lfs .dir (check_config_dir ) do
47- path = check_config_dir .. ' /' .. file
47+ local path = check_config_dir .. ' /' .. file
4848 if lfs .attributes (path , ' mode' ) == ' file' then
4949 if check :get_all (file ) == nil then
5050 print (' ERROR: invalid UCI configuration file: ' .. file )
@@ -67,7 +67,7 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
6767 for file in lfs .dir (remote_config_dir ) do
6868 local standard_path = standard_config_dir .. ' /' .. file
6969 if lfs .attributes (standard_path , ' mode' ) == ' file' then
70- for key , section in pairs (remote :get_all (file )) do
70+ for _ , section in pairs (remote :get_all (file )) do
7171 -- search section in the downloaded configuration
7272 local section_check = check :get (file , section [' .name' ])
7373 -- remove section from current configuration if not in downloaded configuration
@@ -92,8 +92,8 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
9292 end
9393 standard :commit (file )
9494 -- remove uci file if empty
95- local uci = standard :get_all (file )
96- if uci and utils .is_table_empty (uci ) then
95+ local uciFile = standard :get_all (file )
96+ if uciFile and utils .is_table_empty (uciFile ) then
9797 os.remove (standard_path )
9898 end
9999 end
@@ -120,7 +120,7 @@ if lfs.attributes(remote_config_dir, 'mode') == 'directory' then
120120 if MERGE then
121121 -- avoid "file does not exist" error
122122 os.execute (' touch ' .. standard_path )
123- for key , section in pairs (remote :get_all (file )) do
123+ for _ , section in pairs (remote :get_all (file )) do
124124 utils .write_uci_section (standard , file , section )
125125 end
126126 standard :commit (file )
@@ -136,14 +136,14 @@ end
136136os.execute (' touch ' .. added_file )
137137os.execute (' touch ' .. modified_file )
138138-- convert lists to lua sets
139- added = utils .file_to_set (added_file )
140- modified = utils .file_to_set (modified_file )
141- added_changed = false
142- modified_changed = false
139+ local added = utils .file_to_set (added_file )
140+ local modified = utils .file_to_set (modified_file )
141+ local added_changed = false
142+ local modified_changed = false
143143
144144-- loop each file except directories and standard UCI config files
145- ignored_path = remote_config_dir .. ' /'
146- function is_ignored (path )
145+ local ignored_path = remote_config_dir .. ' /'
146+ local function is_ignored (path )
147147 return utils .starts_with (path , ignored_path )
148148end
149149
0 commit comments