autoupdater-wifi-fallback 4.1 KB

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