Browse Source

ffho-autoupdater-wifi-fallback: update + code improvements

Karsten Böddeker 6 years ago
parent
commit
6f94d6bc72

+ 23 - 28
ffho/ffho-autoupdater-wifi-fallback/luasrc/lib/gluon/upgrade/510-autoupdater-wifi-fallback

@@ -1,40 +1,35 @@
 #!/usr/bin/lua
 
-local uci = require 'luci.model.uci'.cursor()
-local util = require 'gluon.util'
-local fs = require('nixio.fs')
+local uci = require('simple-uci').cursor()
+local fs = require 'nixio.fs'
 
-local enabled = uci:get('autoupdater','settings','enabled')
+local enabled
 if uci:get('autoupdater-wifi-fallback', 'settings') then
-  enabled = uci:get_bool('autoupdater-wifi-fallback', 'settings', 'enabled') and 1 or 0
+  enabled = uci:get_bool('autoupdater-wifi-fallback', 'settings', 'enabled')
   uci:delete('autoupdater-wifi-fallback', 'settings')
+else
+  enabled = uci:get_bool('autoupdater', 'settings', 'enabled')
 end
 
-uci:section('autoupdater-wifi-fallback','autoupdater-wifi-fallback','settings',
-  {
-    enabled = enabled,
-  }
-)
+uci:section('autoupdater-wifi-fallback', 'autoupdater-wifi-fallback', 'settings',{
+  enabled = enabled,
+})
 
 uci:delete('wireless', 'fallback')
-uci:delete('network','fallback')
-uci:delete('network','fallback6')
-
-uci:section('network', 'interface', 'fallback',
-  {
-    proto = 'dhcp',
-    peerdns = 1,
-    sourcefilter = 0,
-  }
-)
-uci:section('network', 'interface', 'fallback6',
-  {
-    ifname = '@fallback',
-    proto = 'dhcpv6',
-    peerdns = 1,
-    sourcefilter = 0,
-  }
-)
+uci:delete('network', 'fallback')
+uci:delete('network', 'fallback6')
+
+uci:section('network', 'interface', 'fallback',{
+  proto = 'dhcp',
+  peerdns = true,
+  sourcefilter = false,
+})
+uci:section('network', 'interface', 'fallback6',{
+  ifname = '@fallback',
+  proto = 'dhcpv6',
+  peerdns = true,
+  sourcefilter = false,
+})
 
 uci:save('autoupdater-wifi-fallback')
 uci:save('network')

+ 10 - 11
ffho/ffho-autoupdater-wifi-fallback/luasrc/usr/lib/lua/autoupdater-wifi-fallback/util.lua

@@ -1,6 +1,6 @@
 #!/usr/bin/lua
-local uci = require('luci.model.uci').cursor()
-local iwinfo = require "iwinfo"
+local uci = require('simple-uci').cursor()
+local iwinfo = require 'iwinfo'
 
 function get_available_wifi_networks()
   local radios = {}
@@ -14,14 +14,13 @@ function get_available_wifi_networks()
   for radio, _ in pairs(radios) do
     local wifitype = iwinfo.type(radio)
     local iw = iwinfo[wifitype]
-    if iw then
-      local tmplist = iw.scanlist(radio)
-      for _, net in ipairs(tmplist) do
-        if net.ssid and net.bssid then
-          if net.ssid:match('.*[Ff][Rr][Ee][Ii][Ff][Uu][Nn][Kk].*') then
-            table.insert (radios[radio], net)
-          end
-        end
+    if not iw then
+      return null
+	end
+    local tmplist = iw.scanlist(radio)
+    for _, net in ipairs(tmplist) do
+      if net.ssid and net.bssid and net.ssid:match('.*[Ff][Rr][Ee][Ii][Ff][Uu][Nn][Kk].*') then
+        table.insert (radios[radio], net)
       end
     end
   end
@@ -34,7 +33,7 @@ function get_update_hosts(branch)
   local mirrors = uci:get_list('autoupdater', branch, 'mirror')
 
   for _, mirror in ipairs(mirrors) do
-    local host = mirror:match("://%[?([a-zA-Z0-9\:\.]+)%]?/")
+    local host = mirror:match('://%[?([a-zA-Z0-9\:\.]+)%]?/')
     table.insert(hosts, 1, host)
   end
   return hosts

+ 31 - 34
ffho/ffho-autoupdater-wifi-fallback/luasrc/usr/sbin/autoupdater-wifi-fallback

@@ -1,15 +1,14 @@
 #!/usr/bin/lua
-local fs = require('nixio.fs')
-local uci = require('luci.model.uci').cursor()
+local fs = require 'nixio.fs'
+local uci = require('simple-uci').cursor()
 local site = require 'gluon.site_config'
-local util = require 'luci.util'
-local ut = require('autoupdater-wifi-fallback.util')
-local gluon = require 'gluon.util'
+local autil = require 'autoupdater-wifi-fallback.util'
+local util = require 'gluon.util'
 
 local configname = 'autoupdater-wifi-fallback'
 local force = false
 local min_uptime_secs = 3600
-local branch_name = uci:get('autoupdater','settings','branch')
+local branch_name = uci:get('autoupdater', 'settings', 'branch')
 
 local function parse_args()
 local i = 1
@@ -20,7 +19,7 @@ local i = 1
       i=i+1
 
       if not arg[i] then
-        io.stderr:write("Error parsing command line: expected branch name\n")
+        io.stderr:write('Error parsing command line: expected branch name\n')
         os.exit(1)
       end
 
@@ -34,10 +33,10 @@ local i = 1
 end
 
 local function preflight_check()
-  if not uci:get_bool(configname,'settings','enabled') then
+  if not uci:get_bool(configname, 'settings', 'enabled') then
     return false
   end
-  if not uci:get_bool('autoupdater','settings','enabled') then
+  if not uci:get_bool('autoupdater', 'settings', 'enabled') then
     return false
   end
   if tonumber(fs.readfile('/proc/uptime'):match('^([^ ]+) ')) < min_uptime_secs then
@@ -51,9 +50,9 @@ local function connectivity_check()
   local f = io.open('/sys/kernel/debug/batman_adv/bat0/gateways', 'r')
   if f then
     for line in f:lines() do
-      local gateway_mac = line:match("^=?>? +([0-9a-f:]+)")
+      local gateway_mac = line:match('^=?>? +([0-9a-f:]+)')
       if gateway_mac then
-        if os.execute("batctl ping -t5 -c1 " .. gateway_mac .. " > /dev/null 2>&1") == 0 then
+        if os.execute('batctl ping -t5 -c1 ' .. gateway_mac .. ' > /dev/null 2>&1') == 0 then
           return true
         end
       end
@@ -63,7 +62,7 @@ local function connectivity_check()
 
   -- connectivity check against updateserver
   for _, host in ipairs(get_update_hosts(branch_name)) do
-    if os.execute("ping -w2 -c1 " .. host .. " > /dev/null 2>&1") == 0 then
+    if os.execute('ping -w2 -c1 ' .. host .. ' > /dev/null 2>&1') == 0 then
       return true
     end
   end
@@ -74,38 +73,36 @@ end
 
 local function run_autoupdater()
   io.popen('logger -s -t autoupdater-wifi-fallback -p local0.info "execute the autoupdater"')
-  os.execute("/usr/sbin/autoupdater -f -b " .. branch_name)
+  os.execute('/usr/sbin/autoupdater -f -b ' .. branch_name)
 end
 
 local function switch_to_fallback_mode(radio, ssid, bssid)
   io.popen('logger -s -t autoupdater-wifi-fallback -p local0.info "connect to '  .. radio .. ' ' .. ssid .. ' ' .. bssid .. '"')
   uci:delete_all('wireless', 'wifi-iface')
-  uci:section('wireless', 'wifi-iface', 'fallback',
-    {
-      device = radio,
-      network = 'fallback',
-      mode = 'sta',
-      disabled = 0,
-      macaddr = gluon.generate_mac(3, 10),
-      bssid = bssid,
-      ssid = ssid,
-      ifname = 'fallback',
-      encryption = 'none',
-    }
-  )
-  uci:set('wireless', radio, 'disabled', 0)
+  uci:section('wireless', 'wifi-iface', 'fallback', {
+    device = radio,
+    network = 'fallback',
+    mode = 'sta',
+    disabled = false,
+    macaddr = util.generate_mac(3, 10),
+    bssid = bssid,
+    ssid = ssid,
+    ifname = 'fallback',
+    encryption = 'none',
+  })
+  uci:set('wireless', radio, 'disabled', false)
   uci:save('wireless')
 
-  os.execute("wifi")
-  os.execute("sleep 5")
+  os.execute('wifi')
+  os.execute('sleep 5')
   uci:revert('wireless')
-  os.execute("sleep 20")
+  os.execute('sleep 20')
 end
 
 local function revert_to_standard_mode()
   io.popen('logger -s -t autoupdater-wifi-fallback -p local0.info "going back to standard mode"')
-  os.execute("/etc/init.d/network restart")
-  os.execute("sleep 30")
+  os.execute('/etc/init.d/network restart')
+  os.execute('sleep 30')
 end
 
 parse_args()
@@ -140,7 +137,7 @@ if (force or preflight_check()) and not connectivity_check() then
     revert_to_standard_mode()
   end
 else
-  uci:delete(configname, 'settings','unreachable_since')
-  uci:delete(configname, 'settings','last_run')
+  uci:delete(configname, 'settings', 'unreachable_since')
+  uci:delete(configname, 'settings', 'last_run')
   uci:save(configname)
 end

+ 1 - 1
ffho/ffho-autoupdater-wifi-fallback/luasrc/usr/sbin/wpa_supplicant

@@ -1,5 +1,5 @@
 #!/usr/bin/lua
-local uci = require('luci.model.uci').cursor()
+local uci = require('simple-uci').cursor()
 
 local ifnames = {}
 uci:foreach('wireless', 'wifi-iface',