autoupdater-wifi-fallback 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/lua
  2. local fs = require('nixio.fs')
  3. local uci = require('luci.model.uci').cursor()
  4. local site = require 'gluon.site_config'
  5. local util = require 'luci.util'
  6. local ut = require('autoupdater-wifi-fallback.util')
  7. local gluon = require 'gluon.util'
  8. local configname = 'autoupdater-wifi-fallback'
  9. local force = false
  10. local min_uptime_secs = 3600
  11. local function parse_args()
  12. local i = 1
  13. while arg[i] do
  14. if arg[i] == '-f' then
  15. force = true
  16. elseif arg[i] == '-b' then
  17. i=i+1
  18. if not arg[i] then
  19. io.stderr:write("Error parsing command line: expected branch name\n")
  20. os.exit(1)
  21. end
  22. branch_name = arg[i]
  23. else
  24. io.stderr:write("Error parsing command line: unexpected argument '" .. arg[i] .. "'\n")
  25. os.exit(1)
  26. end
  27. i = i+1
  28. end
  29. end
  30. local function preflight_check()
  31. if not uci:get_bool(configname,'settings','enabled') then
  32. return false
  33. end
  34. if not uci:get_bool('autoupdater','settings','enabled') then
  35. return false
  36. end
  37. if tonumber(fs.readfile('/proc/uptime'):match('^([^ ]+) ')) < min_uptime_secs then
  38. return false
  39. end
  40. return true
  41. end
  42. local function connectivity_check()
  43. for line in io.lines("/sys/kernel/debug/batman_adv/bat0/gateways") do
  44. local gateway_mac = line:match("^=?>? +([0-9a-f:]+)")
  45. if gateway_mac then
  46. if os.execute("batctl ping -t5 -c1 " .. gateway_mac .. " > /dev/null 2>&1") == 0 then
  47. return true
  48. end
  49. end
  50. end
  51. -- connectivity check against updateserver
  52. for _, host in ipairs(get_update_hosts()) do
  53. if os.execute("ping -w2 -c1 " .. host .. " > /dev/null 2>&1") == 0 then
  54. return true
  55. end
  56. end
  57. return false
  58. end
  59. local function run_autoupdater()
  60. os.execute("/usr/sbin/autoupdater -f")
  61. end
  62. local function switch_to_fallback_mode(wifiNetwork, radio)
  63. print("connect to: " .. wifiNetwork .. " on " .. radio)
  64. uci:delete_all('wireless', 'wifi-iface')
  65. uci:section('wireless', 'wifi-iface', 'fallback',
  66. {
  67. device = radio,
  68. network = 'fallback',
  69. mode = 'managed',
  70. disabled = 0,
  71. macaddr = gluon.generate_mac(3, 10),
  72. ifname = 'fallback',
  73. encryption = 'none',
  74. ssid = wifiNetwork,
  75. }
  76. )
  77. uci:set('wireless', radio, 'disabled', 0)
  78. uci:save('wireless')
  79. io.popen("wifi")
  80. os.execute("sleep 5")
  81. os.execute("echo \"2\" > /proc/sys/net/ipv6/conf/fallback/accept_ra")
  82. uci:revert('wireless')
  83. os.execute("sleep 20")
  84. end
  85. local function revert_to_standard_mode()
  86. print("Going back to standard config")
  87. os.execute("/etc/init.d/network restart")
  88. os.execute("sleep 30")
  89. end
  90. parse_args()
  91. if (force or preflight_check()) and not connectivity_check() then
  92. local offset = 2 * 3600
  93. local unreachable_since = os.time()
  94. if not uci:get('autoupdater-wifi-fallback', 'settings', 'unreachable_since') then
  95. uci:set(configname, 'settings', 'unreachable_since', unreachable_since)
  96. else
  97. uci:set(configname, 'settings', 'last_run', unreachable_since)
  98. unreachable_since = uci:get(configname, 'settings', 'unreachable_since')
  99. end
  100. uci:save(configname)
  101. if force or tonumber(unreachable_since) + offset < os.time() then
  102. local radio, wifiNetwork = get_available_wifi_networks()
  103. if wifiNetwork then
  104. switch_to_fallback_mode(wifiNetwork, radio)
  105. run_autoupdater()
  106. -- this is only reached if no updated happened
  107. revert_to_standard_mode()
  108. end
  109. end
  110. else
  111. uci:delete(configname, 'settings','unreachable_since')
  112. uci:delete(configname, 'settings','last_run')
  113. uci:save(configname)
  114. end