-- -- logger.lua -- script version 0.93 -- -- meant to run from Scripts::PM::Post -- -- Matthew Miller and Joe Szep -- -- Includes ideas/bits from Panu Matilainen, too. -- -- Writes a message to /var/log/messages for each package successfully -- installed or removed, and for packages which fail to install or remove. -- -- Also, provides a double check which causes apt to error out if a -- transaction didn't go as expected. -- -- Don't log if logging is turned off. if confget("RPM::Log-Info/b", "true") == "false" then return end -- Don't log when in --test mode. configs = { "RPM::Options", "RPM::Install-Options", "RPM::Erase-Options" } for i, conf in ipairs(configs) do rpmopts = confgetlist(conf, "") for j, opt in ipairs(rpmopts) do if string.find(opt, "test") then return end end end -- change the 'kernel#version' hack back to just the base package name function unmungepackagename(packagename) fixedname,i=string.gsub(packagename,"#.*$",'') return fixedname end -- Check using RPM to see what the actual result is function isversioninstalled(checkpackagename,checkversionstr) if string.find(checkversionstr,':') then -- use epoch querypattern='%{epoch}:%{version}-%{release}\n' else querypattern='%{version}-%{release}\n' end verlist = io.popen("rpm --nosignature --nodigest --nomd5 --qf '"..querypattern.."' -q "..checkpackagename) for line in verlist:lines() do if line == checkversionstr then -- print("DEBUG: version "..checkversionstr.." of "..checkpackagename.." found.") return true end end -- print("DEBUG: version "..checkversionstr.." of "..checkpackagename.." NOT found.") return false end -- Helper function to pick the right words for the log message function gettransactiontype(checkpackage) if statnewinstall(checkpackage) then return "install" elseif statinstall(checkpackage) then return "upgrade" elseif statremove(checkpackage) then return "remove" elseif statdowngrade(checkpackage) then return "downgrade" elseif statkeep(checkpackage) then return "keep" else return "do something strange with" end end -- Another helper function to pick the right words for the log message function getpasttransactiontype(checkpackage) if statnewinstall(checkpackage) then return "installed" elseif statinstall(checkpackage) then return "upgraded" elseif statremove(checkpackage) then return "removed" elseif statdowngrade(checkpackage) then return "downgraded" elseif statkeep(checkpackage) then return "kept" else return "did something strange with" end end -- And, now, go through all the to-be installed packages and check and log if pkgs_install then for i, attemptedpkg in pairs(pkgs_install) do attemptedverstr = verstr(pkgverinst(attemptedpkg)) attemptedpkgname = unmungepackagename(pkgname(attemptedpkg)) if isversioninstalled(attemptedpkgname,attemptedverstr) then logstring='successfully '..getpasttransactiontype(attemptedpkg)..' package: '..attemptedpkgname..' ('..attemptedverstr..')' else logstring='FAILED to '..gettransactiontype(attemptedpkg)..' package: '..attemptedpkgname..' ('..attemptedverstr..')' apterror(logstring) end -- print("DEBUG: LOG: "..logstring) os.execute('/usr/bin/logger -t apt-get "'..logstring..'"') end end -- And same for to-be-removed, but with the check reversed. if pkgs_remove then for i, attemptedpkg in pairs(pkgs_remove) do -- use 'current' version -- that's what *was* current. attemptedverstr = verstr(pkgvercur(attemptedpkg)) attemptedpkgname = unmungepackagename(pkgname(attemptedpkg)) if not isversioninstalled(attemptedpkgname,attemptedverstr) then logstring='successfully '..getpasttransactiontype(attemptedpkg)..' package: '..attemptedpkgname..' ('..attemptedverstr..')' else logstring='FAILED to '..gettransactiontype(attemptedpkg)..' package: '..attemptedpkgname..' ('..attemptedverstr..')' apterror(logstring) end -- print("DEBUG: LOG: "..logstring) os.execute('/usr/bin/logger -t apt-get "'..logstring..'"') end end