#117 installer v0.6 saving manifest after operation, checking install state before proceeding

This commit is contained in:
Mikayla Fischler 2023-02-19 19:14:47 -05:00
parent 726d15b48f
commit fa6524d934

View File

@ -22,7 +22,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 VERSION = "v0.5" local VERSION = "v0.6"
local install_dir = "/.install-cache" local install_dir = "/.install-cache"
local repo_path = "http://raw.githubusercontent.com/MikaylaFischler/cc-mek-scada/devel/" local repo_path = "http://raw.githubusercontent.com/MikaylaFischler/cc-mek-scada/devel/"
@ -32,6 +32,29 @@ local opts = { ... }
local mode = nil local mode = nil
local app = nil local app = nil
local function write_install_manifest(manifest, dependencies)
local versions = {}
for key, value in pairs(manifest.versions) do
local is_dependency = false
for _, dependency in pairs(dependencies) do
if key == "bootloader" and dependency == "system" then
is_dependency = true
break
end
end
if key == app or is_dependency then
versions[key] = value
end
end
manifest.versions = versions
local imfile = fs.open("install_manifest.json", "w")
imfile.write(textutils.serializeJSON(manifest))
imfile.close()
end
-- --
-- get and validate command line options -- get and validate command line options
-- --
@ -128,6 +151,11 @@ if mode == "install" or mode == "update" then
local_app_version = local_manifest.versions[app] local_app_version = local_manifest.versions[app]
local_comms_version = local_manifest.versions.comms local_comms_version = local_manifest.versions.comms
local_boot_version = local_manifest.versions.bootloader local_boot_version = local_manifest.versions.bootloader
elseif local_manifest.versions[app] == nil then
term.setTextColor(colors.red)
println("another application is already installed, please purge it before installing a new application")
term.setTextColor(colors.white)
return
end end
local remote_app_version = manifest.versions[app] local remote_app_version = manifest.versions[app]
@ -172,6 +200,7 @@ if mode == "install" or mode == "update" then
term.setTextColor(colors.white) term.setTextColor(colors.white)
end end
else else
print("[" .. app .. "] new install of ")
term.setTextColor(colors.blue) term.setTextColor(colors.blue)
println(remote_app_version) println(remote_app_version)
term.setTextColor(colors.white) term.setTextColor(colors.white)
@ -247,7 +276,7 @@ if mode == "install" or mode == "update" then
end end
for _, dependency in pairs(dependencies) do for _, dependency in pairs(dependencies) do
if (dependency == "system" and local_boot_version == remote_boot_version) or (local_app_version == remote_app_version) then if mode == "update" and ((dependency == "system" and local_boot_version == remote_boot_version) or (local_app_version == remote_app_version)) then
-- skip system package if unchanged, skip app package if not changed -- skip system package if unchanged, skip app package if not changed
-- skip packages that have no version if app version didn't change -- skip packages that have no version if app version didn't change
term.setTextColor(colors.white) term.setTextColor(colors.white)
@ -282,7 +311,7 @@ if mode == "install" or mode == "update" then
if success then if success then
for _, dependency in pairs(dependencies) do for _, dependency in pairs(dependencies) do
if (dependency == "system" and local_boot_version == remote_boot_version) or (local_app_version == remote_app_version) then if mode == "update" and ((dependency == "system" and local_boot_version == remote_boot_version) or (local_app_version == remote_app_version)) then
-- skip system package if unchanged, skip app package if not changed -- skip system package if unchanged, skip app package if not changed
-- skip packages that have no version if app version didn't change -- skip packages that have no version if app version didn't change
term.setTextColor(colors.white) term.setTextColor(colors.white)
@ -311,10 +340,10 @@ if mode == "install" or mode == "update" then
fs.delete(install_dir) fs.delete(install_dir)
if success then if success then
term.setTextColor(colors.green)
-- if we made it here, then none of the file system functions threw exceptions -- if we made it here, then none of the file system functions threw exceptions
-- that means everything is OK -- that means everything is OK
write_install_manifest(manifest, dependencies)
term.setTextColor(colors.green)
if mode == "install" then if mode == "install" then
println("installation completed successfully") println("installation completed successfully")
else else
@ -331,7 +360,7 @@ if mode == "install" or mode == "update" then
end end
else else
for _, dependency in pairs(dependencies) do for _, dependency in pairs(dependencies) do
if (dependency == "system" and local_boot_version == remote_boot_version) or (local_app_version == remote_app_version) then if mode == "update" and ((dependency == "system" and local_boot_version == remote_boot_version) or (local_app_version == remote_app_version)) then
-- skip system package if unchanged, skip app package if not changed -- skip system package if unchanged, skip app package if not changed
-- skip packages that have no version if app version didn't change -- skip packages that have no version if app version didn't change
term.setTextColor(colors.white) term.setTextColor(colors.white)
@ -366,6 +395,7 @@ if mode == "install" or mode == "update" then
if success then if success then
-- if we made it here, then none of the file system functions threw exceptions -- if we made it here, then none of the file system functions threw exceptions
-- that means everything is OK -- that means everything is OK
write_install_manifest(manifest, dependencies)
term.setTextColor(colors.green) term.setTextColor(colors.green)
if mode == "install" then if mode == "install" then
println("installation completed successfully") println("installation completed successfully")
@ -382,15 +412,25 @@ if mode == "install" or mode == "update" then
end end
end end
elseif mode == "remove" or mode == "purge" then elseif mode == "remove" or mode == "purge" then
local imfile = fs.open("install_manifest.json") local imfile = fs.open("install_manifest.json", "r")
local ok, manifest = pcall(function () return textutils.unserializeJSON(imfile.readAll()) end) local ok = false
local manifest = {}
if imfile ~= nil then
ok, manifest = pcall(function () return textutils.unserializeJSON(imfile.readAll()) end)
imfile.close() imfile.close()
end
if not ok then if not ok then
term.setTextColor(colors.red) term.setTextColor(colors.red)
println("error parsing local installation manifest") println("error parsing local installation manifest")
term.setTextColor(colors.white) term.setTextColor(colors.white)
return return
elseif manifest.versions[app] == nil then
term.setTextColor(colors.red)
println(app .. " is not installed")
term.setTextColor(colors.white)
return
end end
term.setTextColor(colors.orange) term.setTextColor(colors.orange)
@ -414,20 +454,29 @@ elseif mode == "remove" or mode == "purge" then
-- delete log file if purging -- delete log file if purging
if mode == "purge" then if mode == "purge" then
local config = require(config_file) local config = require(config_file)
if fs.exists(config.LOG_PATH) then
fs.delete(config.LOG_PATH) fs.delete(config.LOG_PATH)
println("deleted log file " .. config.LOG_PATH) println("deleted log file " .. config.LOG_PATH)
end end
end
-- delete all files except config unless purging -- delete all files except config unless purging
for _, dependency in pairs(dependencies) do for _, dependency in pairs(dependencies) do
local files = file_list[dependency] local files = file_list[dependency]
for _, file in pairs(files) do for _, file in pairs(files) do
if mode == "purge" or file ~= config_file then if mode == "purge" or file ~= config_file then
if fs.exists(file) then
fs.delete(file) fs.delete(file)
println("deleted " .. file) println("deleted " .. file)
end end
end end
end end
end
if mode == "purge" then
fs.delete("install_manifest.json")
println("deleted install_manifest.json")
end
term.setTextColor(colors.green) term.setTextColor(colors.green)
println("done!") println("done!")