Verified Commit f099f89d authored by Karel Koci's avatar Karel Koci 🤘
Browse files

lib/transaction: add PKG_UPGRADE variable to postinst scripts

parent 33d66c46
Pipeline #59082 passed with stages
in 5 minutes and 6 seconds
......@@ -996,9 +996,10 @@ If the script doesn't exist, true is returned (and no stderr is provided).
- pkg_name: Name of the package.
- script_name: Suffix of the script (eg. 'control')
- is_upgrade: Boolean value to set PKG_UPGRADE environment variable
- More parameters: Parameters to pass to the script.
]]
function script_run(pkg_name, script_name, ...)
function script_run(pkg_name, script_name, is_upgrade, ...)
local fname = pkg_name .. "." .. script_name
local fname_full = syscnf.info_dir:gsub('^../', getcwd() .. "/../"):gsub('^./', getcwd() .. "/") .. "/" .. fname
local ftype, perm = stat(fname_full)
......@@ -1012,6 +1013,7 @@ function script_run(pkg_name, script_name, ...)
local dir = syscnf.root_dir:gsub('^/+$', '')
setenv("PKG_ROOT", dir)
setenv("IPKG_INSTROOT", dir)
setenv("PKG_UPGRADE", is_upgrade and "1" or "0")
chdir(syscnf.root_dir)
end,
fname_full, ...)
......
......@@ -58,8 +58,8 @@ module "transaction"
-- luacheck: globals perform recover perform_queue recover_pretty queue_remove queue_install queue_install_downloaded cleanup_actions
-- Wrap the call to the maintainer script, and store any possible errors for later use
local function script(errors_collected, name, suffix, ...)
local ok, stderr = backend.script_run(name, suffix, ...)
local function script(errors_collected, name, suffix, is_upgrade, ...)
local ok, stderr = backend.script_run(name, suffix, is_upgrade, ...)
if not ok then
errors_collected[name] = errors_collected[name] or {}
errors_collected[name][suffix] = stderr
......@@ -182,9 +182,9 @@ local function pkg_move(status, plan, early_remove, errors_collected)
backend.pkg_merge_control(op.dir .. "/control", op.control.Package, op.control.files)
if utils.multi_index(status, op.control.Package, "Status", 3) == "installed" then
-- There's a previous version. So this is an upgrade.
script(errors_collected, op.control.Package, "preinst", "upgrade", status[op.control.Package].Version)
script(errors_collected, op.control.Package, "preinst", true, "upgrade", status[op.control.Package].Version)
else
script(errors_collected, op.control.Package, "preinst", "install", op.control.Version)
script(errors_collected, op.control.Package, "preinst", false, "install", op.control.Version)
end
if early_remove[op.control.Package] then
backend.pkg_cleanup_files(early_remove[op.control.Package], all_configs)
......@@ -215,7 +215,7 @@ local function pkg_move(status, plan, early_remove, errors_collected)
else
status[op.name] = nil
end
script(errors_collected, op.name, "prerm", "remove")
script(errors_collected, op.name, "prerm", false, "remove")
end
-- Ignore others, at least for now.
end
......@@ -236,9 +236,10 @@ local function pkg_scripts(status, plan, removes, to_install, errors_collected,
INFO("Running post-install and post-rm scripts")
for _, op in ipairs(plan) do
if op.op == "install" then
script(errors_collected, op.control.Package, "postinst", "configure")
local is_upgrade = utils.multi_index(status, op.control.Package, "Status", 3) == "installed"
script(errors_collected, op.control.Package, "postinst", is_upgrade, "configure")
elseif op.op == "remove" and not to_install[op.name] then
script(errors_collected, op.name, "postrm", "remove")
script(errors_collected, op.name, "postrm", false, "remove")
end
end
return status, errors_collected
......
......@@ -878,28 +878,37 @@ function test_script_run()
subprocess_kill_timeout(0) -- Run tests faster
syscnf.info_dir = (os.getenv("S") or ".") .. "/tests/data/scripts"
-- This one doesn't exist. So the call succeeds.
local result, stderr = B.script_run("xyz", "preinst", "install")
local result, stderr = B.script_run("xyz", "preinst", false, "install")
assert(result)
assert_nil(stderr)
-- This one fails and outputs some of the data passed to it on stderr
result, stderr = B.script_run("xyz", "postinst", "install")
result, stderr = B.script_run("xyz", "postinst", false, "install")
assert_false(result)
assert_equal([[
install
PKG_ROOT=
PKG_UPGRADE=0
]], stderr)
-- This one is same as previous one but reports upgrade
result, stderr = B.script_run("xyz", "postinst", true, "upgrade")
assert_false(result)
assert_equal([[
upgrade
PKG_ROOT=
PKG_UPGRADE=1
]], stderr)
-- This one doesn't have executable permission, won't be run
result, stderr = B.script_run("xyz", "prerm", "remove")
result, stderr = B.script_run("xyz", "prerm", false, "remove")
assert(result)
assert_nil(stderr)
-- This one terminates successfully
result, stderr = B.script_run("xyz", "postrm", "remove")
result, stderr = B.script_run("xyz", "postrm", false, "remove")
assert(result)
assert_equal("test\n", stderr)
-- This one hangs and should timeout, we set shorter timeout just to make test faster
local old_cmd_timeout = B.cmd_timeout
B.cmd_timeout = 1000
result, stderr = B.script_run("hang", "postinst", "install")
result, stderr = B.script_run("hang", "postinst", false, "install")
assert_false(result)
assert_equal("", stderr)
B.cmd_timeout = old_cmd_timeout
......
......@@ -4,4 +4,5 @@ set -e
echo "$@" >&2
env | grep "^PKG_ROOT=" >&2
env | grep "^PKG_UPGRADE=" >&2
false
......@@ -272,7 +272,7 @@ function test_perform_ok()
},
{
f = "backend.script_run",
p = {"pkg-name", "preinst", "upgrade", "0"}
p = {"pkg-name", "preinst", true, "upgrade", "0"}
},
{
f = "backend.pkg_merge_files",
......@@ -284,7 +284,7 @@ function test_perform_ok()
},
{
f = "backend.script_run",
p = {"pkg-rem", "prerm", "remove"}
p = {"pkg-rem", "prerm", false, "remove"}
},
{
f = "journal.write",
......@@ -309,11 +309,11 @@ function test_perform_ok()
},
{
f = "backend.script_run",
p = {"pkg-name", "postinst", "configure"}
p = {"pkg-name", "postinst", true, "configure"}
},
{
f = "backend.script_run",
p = {"pkg-rem", "postrm", "remove"}
p = {"pkg-rem", "postrm", false, "remove"}
},
{
f = "journal.write",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment