autoupdater-wifi-fallback 3.7 KB

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