From 1708c4926c39767afd3197a5c29b8f96b89c93cb Mon Sep 17 00:00:00 2001 From: Tim Wilkinson Date: Fri, 15 Sep 2023 21:16:27 -0700 Subject: [PATCH] Remember and reinstall packages are firmware upgrade (#930) --- files/etc/arednsysupgrade.conf | 1 + files/etc/cron.boot/reinstall-packages | 59 ++++++++++++++++++++++++++ files/usr/local/bin/mgr/periodic.lua | 1 + files/www/cgi-bin/admin | 28 ++++++++++++ 4 files changed, 89 insertions(+) create mode 100755 files/etc/cron.boot/reinstall-packages diff --git a/files/etc/arednsysupgrade.conf b/files/etc/arednsysupgrade.conf index f5a19da1..e22b909c 100644 --- a/files/etc/arednsysupgrade.conf +++ b/files/etc/arednsysupgrade.conf @@ -35,3 +35,4 @@ /etc/local/uci/hsmmmesh /etc/passwd /etc/shadow +/etc/package_store diff --git a/files/etc/cron.boot/reinstall-packages b/files/etc/cron.boot/reinstall-packages new file mode 100755 index 00000000..270bf522 --- /dev/null +++ b/files/etc/cron.boot/reinstall-packages @@ -0,0 +1,59 @@ +#!/usr/bin/lua +--[[ + + Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks + Copyright (C) 2023 Tim Wilkinson + See Contributors file for additional contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation version 3 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + Additional Terms: + + Additional use restrictions exist on the AREDN(TM) trademark and logo. + See AREDNLicense.txt for more info. + + Attributions to the AREDN Project must be retained in the source code. + If importing this code into a new or existing project attribution + to the AREDN project must be added to the source code. + + You must not misrepresent the origin of the material contained within. + + Modified versions must be modified to attribute to the original source + and be marked in reasonable ways as differentiate it from the original + version + +--]] + +require("nixio") +require("luci.jsonc") + +local package_store = "/etc/package_store" +local package_catalog = package_store .. "/catalog.json" + +if nixio.fs.stat(package_catalog) then + os.execute("opkg update") + local catalog = luci.jsonc.parse(io.open(package_catalog):read("*a")) + for ipkg, state in pairs(catalog.installed) + do + if state == "local" then + local file = package_store .. "/" .. ipkg .. ".ipk" + if nixio.fs.stat(file) then + os.execute("opkg install " .. file) + end + elseif state == "global" then + os.execute("opkg install " .. ipkg) + end + end +end + +os.remove("/etc/cron.boot/reinstall-packages") diff --git a/files/usr/local/bin/mgr/periodic.lua b/files/usr/local/bin/mgr/periodic.lua index 2f4e065c..e2bb72ca 100755 --- a/files/usr/local/bin/mgr/periodic.lua +++ b/files/usr/local/bin/mgr/periodic.lua @@ -49,6 +49,7 @@ function periodic() wait_for_ticks(120) -- Initial wait before starting up period tasks local hours = 0 local days = 0 + run_scripts("/etc/cron.boot") while true do run_scripts("/etc/cron.hourly") diff --git a/files/www/cgi-bin/admin b/files/www/cgi-bin/admin index 50a7212d..d6b1226e 100755 --- a/files/www/cgi-bin/admin +++ b/files/www/cgi-bin/admin @@ -591,6 +591,31 @@ function update_advertised_services() end end +local package_store = "/etc/package_store" +local package_catalog = package_store .. "/catalog.json" + +function record_package(op, ipkg, file) + nixio.fs.mkdir(package_store) + local catalog = luci.jsonc.parse(read_all(package_catalog) or '{"installed":{}}') + if op == "upload" then + local package = ipkg:match("^([^_]+)") + if package then + os.execute("/bin/cp -f " .. file .. " " .. package_store .. "/" .. package .. ".ipk") + catalog.installed[package] = "local" + end + elseif op == "download" then + local result = capture("opkg status " .. ipkg .. " 2>&1") + local package = result:match("Package: (%S+)") + if package then + catalog.installed[package] = "global" + end + elseif op == "remove" then + nixio.fs.remove(package_store .. "/" .. ipkg .. ".ipk") + catalog.installed[ipkg] = nil + end + write_all(package_catalog, luci.jsonc.stringify(catalog)) +end + -- upload package if parms.button_ul_pkg and nixio.fs.stat("/tmp/web/upload/file") then os.execute("mv -f /tmp/web/upload/file /tmp/web/upload/newpkg.ipk") @@ -603,6 +628,7 @@ if parms.button_ul_pkg and nixio.fs.stat("/tmp/web/upload/file") then result = capture("opkg -force-overwrite install /tmp/web/upload/newpkg.ipk 2>&1") end pkgout(result) + record_package("upload", firmfile, "/tmp/web/upload/newpkg.ipk") os.execute("rm -rf /tmp/opkg-*") nixio.fs.remove("/tmp/web/upload/newpkg.ipk") if os.execute("/usr/local/bin/uploadctlservices restore > /dev/null 2>&1") ~= 0 then @@ -624,6 +650,7 @@ if parms.button_dl_pkg and parms.dl_pkg ~= "default" then result = capture("opkg -force-overwrite install " .. parms.dl_pkg .. " 2>&1") end pkgout(result) + record_package("download", parms.dl_pkg) if os.execute("/usr/local/bin/uploadctlservices restore > /dev/null 2>&1") ~= 0 then pkgout("Failed to restart all services, please reboot this node.") else @@ -651,6 +678,7 @@ if parms.button_rm_pkg and parms.rm_pkg ~= "default" and not permpkg[parms.rm_pk if not output:match("No packages removed") then pkgout("Package removed successfully") update_advertised_services() + record_package("remove", parms.rm_pkg) end end