autoupdater-wifi-fallback 4.1 KB

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