Merge pull request #515 from MikaylaFischler/514-retry-file-downloads-on-failure

514 retry file downloads on failure
This commit is contained in:
Mikayla 2024-07-05 13:39:20 -04:00 committed by GitHub
commit 1f8ea56095
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -18,7 +18,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
local function println(message) print(tostring(message)) end local function println(message) print(tostring(message)) end
local function print(message) term.write(tostring(message)) end local function print(message) term.write(tostring(message)) end
local CCMSI_VERSION = "v1.15" local CCMSI_VERSION = "v1.16"
local install_dir = "/.install-cache" local install_dir = "/.install-cache"
local manifest_path = "https://mikaylafischler.github.io/cc-mek-scada/manifests/" local manifest_path = "https://mikaylafischler.github.io/cc-mek-scada/manifests/"
@ -120,6 +120,27 @@ local function write_install_manifest(manifest, dependencies)
imfile.close() imfile.close()
end end
-- try at most 3 times to download a file from the repository and write into w_path base directory
local function http_get_file(file, w_path)
local dl, err
for i = 1, 3 do
dl, err = http.get(repo_path..file)
if dl then
if i > 1 then green();println("success!");lgray() end
local f = fs.open(w_path..file, "w")
f.write(dl.readAll())
f.close()
break
else
red();println("HTTP Error: "..err)
if i < 3 then lgray();print("> retrying...") end
---@diagnostic disable-next-line: undefined-field
os.sleep(i/3.0)
end
end
return dl ~= nil
end
-- recursively build a tree out of the file manifest -- recursively build a tree out of the file manifest
local function gen_tree(manifest, log) local function gen_tree(manifest, log)
local function _tree_add(tree, split) local function _tree_add(tree, split)
@ -172,6 +193,7 @@ local function clean(manifest)
local log = nil local log = nil
if fs.exists(app..".settings") and settings.load(app..".settings") then if fs.exists(app..".settings") and settings.load(app..".settings") then
log = settings.get("LogPath") log = settings.get("LogPath")
if log:sub(1, 1) == "/" then log = log:sub(2) end
end end
local tree = gen_tree(manifest, log) local tree = gen_tree(manifest, log)
@ -322,7 +344,7 @@ elseif mode == "install" or mode == "update" then
local dl, err = http.get(repo_path.."ccmsi.lua") local dl, err = http.get(repo_path.."ccmsi.lua")
if dl == nil then if dl == nil then
red();println("HTTP Error "..err) red();println("HTTP Error: "..err)
println("Installer download failed.");white() println("Installer download failed.");white()
else else
local handle = fs.open(debug.getinfo(1, "S").source:sub(2), "w") -- this file, regardless of name or location local handle = fs.open(debug.getinfo(1, "S").source:sub(2), "w") -- this file, regardless of name or location
@ -359,9 +381,6 @@ elseif mode == "install" or mode == "update" then
ver.graphics.changed = show_pkg_change("graphics", ver.graphics) ver.graphics.changed = show_pkg_change("graphics", ver.graphics)
ver.lockbox.changed = show_pkg_change("lockbox", ver.lockbox) ver.lockbox.changed = show_pkg_change("lockbox", ver.lockbox)
-- ask for confirmation
if not ask_y_n("Continue", false) then return end
-------------------------- --------------------------
-- START INSTALL/UPDATE -- -- START INSTALL/UPDATE --
-------------------------- --------------------------
@ -376,11 +395,32 @@ elseif mode == "install" or mode == "update" then
table.insert(dependencies, app) table.insert(dependencies, app)
-- helper function to check if a dependency is unchanged
local function unchanged(dependency)
if dependency == "system" then return not ver.boot.changed
elseif dependency == "graphics" then return not ver.graphics.changed
elseif dependency == "lockbox" then return not ver.lockbox.changed
elseif dependency == "common" then return not (ver.common.changed or ver.comms.changed)
elseif dependency == app then return not ver.app.changed
else return true end
end
local any_change = false
for _, dependency in pairs(dependencies) do for _, dependency in pairs(dependencies) do
local size = size_list[dependency] local size = size_list[dependency]
space_required = space_required + size space_required = space_required + size
any_change = any_change or not unchanged(dependency)
end end
if mode == "update" and not any_change then
yellow();println("Nothing to do, everything is already up-to-date!");white()
return
end
-- ask for confirmation
if not ask_y_n("Continue", false) then return end
-- check space constraints -- check space constraints
if space_available < space_required then if space_available < space_required then
single_file_mode = true single_file_mode = true
@ -396,16 +436,6 @@ elseif mode == "install" or mode == "update" then
local success = true local success = true
-- helper function to check if a dependency is unchanged
local function unchanged(dependency)
if dependency == "system" then return not ver.boot.changed
elseif dependency == "graphics" then return not ver.graphics.changed
elseif dependency == "lockbox" then return not ver.lockbox.changed
elseif dependency == "common" then return not (ver.common.changed or ver.comms.changed)
elseif dependency == app then return not ver.app.changed
else return true end
end
if not single_file_mode then if not single_file_mode then
if fs.exists(install_dir) then fs.delete(install_dir);fs.makeDir(install_dir) end if fs.exists(install_dir) then fs.delete(install_dir);fs.makeDir(install_dir) end
@ -420,19 +450,14 @@ elseif mode == "install" or mode == "update" then
local files = file_list[dependency] local files = file_list[dependency]
for _, file in pairs(files) do for _, file in pairs(files) do
println("GET "..file) println("GET "..file)
local dl, err = http.get(repo_path..file) if not http_get_file(file, install_dir.."/") then
red();println("failed to download "..file)
if dl == nil then
red();println("HTTP Error "..err)
success = false success = false
break break
else
local handle = fs.open(install_dir.."/"..file, "w")
handle.write(dl.readAll())
handle.close()
end end
end end
end end
if not success then break end
end end
-- copy in downloaded files (installation) -- copy in downloaded files (installation)
@ -482,19 +507,14 @@ elseif mode == "install" or mode == "update" then
local files = file_list[dependency] local files = file_list[dependency]
for _, file in pairs(files) do for _, file in pairs(files) do
println("GET "..file) println("GET "..file)
local dl, err = http.get(repo_path..file) if not http_get_file(file, "/") then
red();println("failed to download "..file)
if dl == nil then
red();println("HTTP Error "..err)
success = false success = false
break break
else
local handle = fs.open("/"..file, "w")
handle.write(dl.readAll())
handle.close()
end end
end end
end end
if not success then break end
end end
if success then if success then