diff options
author | Julian Andres Klode <julian.klode@canonical.com> | 2021-04-22 12:53:54 +0200 |
---|---|---|
committer | Julian Andres Klode <julian.klode@canonical.com> | 2021-04-23 12:26:46 +0200 |
commit | de86a4b076a37db2f70ee5fbfbb295cc71c344df (patch) | |
tree | fad919bc724a8f842ba0110f7f109e82ad61c253 | |
parent | 4b5215e8e2e31637cb0998ecb80d3c3146760579 (diff) |
upgrade: Add JSON hook support (AptCli::Hooks::Upgrade)
-rw-r--r-- | apt-private/private-upgrade.cc | 16 | ||||
-rwxr-xr-x | test/integration/test-apt-cli-json-hooks | 45 |
2 files changed, 54 insertions, 7 deletions
diff --git a/apt-private/private-upgrade.cc b/apt-private/private-upgrade.cc index aeaf5066b..a87ffbc46 100644 --- a/apt-private/private-upgrade.cc +++ b/apt-private/private-upgrade.cc @@ -1,12 +1,14 @@ // Includes /*{{{*/ #include <config.h> +#include <apt-pkg/cmndline.h> #include <apt-pkg/configuration.h> #include <apt-pkg/error.h> #include <apt-pkg/upgrade.h> #include <apt-private/private-cachefile.h> #include <apt-private/private-install.h> +#include <apt-private/private-json-hooks.h> #include <apt-private/private-output.h> #include <apt-private/private-upgrade.h> @@ -24,10 +26,18 @@ static bool UpgradeHelper(CommandLine &CmdL, int UpgradeFlags) if (Cache.OpenForInstall() == false || Cache.CheckDeps() == false) return false; - if(!DoCacheManipulationFromCommandLine(CmdL, VolatileCmdL, Cache, UpgradeFlags)) + std::map<unsigned short, APT::VersionSet> verset; + std::set<std::string> UnknownPackages; + if (!DoCacheManipulationFromCommandLine(CmdL, VolatileCmdL, Cache, verset, UpgradeFlags, UnknownPackages)) + { + RunJsonHook("AptCli::Hooks::Upgrade", "org.debian.apt.hooks.install.fail", CmdL.FileList, Cache, UnknownPackages); return false; - - return InstallPackages(Cache,true); + } + RunJsonHook("AptCli::Hooks::Upgrade", "org.debian.apt.hooks.install.pre-prompt", CmdL.FileList, Cache); + if (InstallPackages(Cache, true)) + return RunJsonHook("AptCli::Hooks::Upgrade", "org.debian.apt.hooks.install.post", CmdL.FileList, Cache); + else + return RunJsonHook("AptCli::Hooks::Upgrade", "org.debian.apt.hooks.install.fail", CmdL.FileList, Cache); } // DoDistUpgrade - Automatic smart upgrader /*{{{*/ diff --git a/test/integration/test-apt-cli-json-hooks b/test/integration/test-apt-cli-json-hooks index 7169a04cb..07801a547 100755 --- a/test/integration/test-apt-cli-json-hooks +++ b/test/integration/test-apt-cli-json-hooks @@ -18,6 +18,8 @@ insertpackage 'unstable' 'foo' 'all' '1.0' '' '' "$DESCR . and paragraphs and everything." insertpackage 'testing' 'bar' 'i386' '2.0' '' '' "$DESCR2" +insertpackage 'testing' 'upgrade' 'i386' '2.0' +insertinstalledpackage 'upgrade' 'i386' '1.0' setupaptarchive @@ -52,6 +54,7 @@ HOOK="$(readlink -f ./json-hook.sh)" # Setup all hooks cat >> rootdir/etc/apt/apt.conf.d/99-json-hooks << EOF AptCli::Hooks::Install:: "$HOOK"; + AptCli::Hooks::Upgrade:: "$HOOK"; AptCli::Hooks::Search:: "$HOOK"; EOF @@ -111,21 +114,55 @@ Building dependency tree... HOOK: HELLO HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} HOOK: empty -HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.pre-prompt","params":{"command":"install","search-terms":["foo"],"unknown-packages":[],"packages":[{"id":1,"name":"foo","architecture":"i386","mode":"install","automatic":false,"versions":{"candidate":{"id":1,"version":"1.0","architecture":"all","pin":500,"origins":[{"archive":"unstable","codename":"sid","origin":"Oranges","label":"Lemons","site":""}]},"install":{"id":1,"version":"1.0","architecture":"all","pin":500,"origins":[{"archive":"unstable","codename":"sid","origin":"Oranges","label":"Lemons","site":""}]}}}]}} +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.pre-prompt","params":{"command":"install","search-terms":["foo"],"unknown-packages":[],"packages":[{"id":2,"name":"foo","architecture":"i386","mode":"install","automatic":false,"versions":{"candidate":{"id":2,"version":"1.0","architecture":"all","pin":500,"origins":[{"archive":"unstable","codename":"sid","origin":"Oranges","label":"Lemons","site":""}]},"install":{"id":2,"version":"1.0","architecture":"all","pin":500,"origins":[{"archive":"unstable","codename":"sid","origin":"Oranges","label":"Lemons","site":""}]}}}]}} HOOK: empty HOOK: BYE The following NEW packages will be installed: foo -0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. +0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded. Inst foo (1.0 Lemons:unstable [all]) Conf foo (1.0 Lemons:unstable [all]) HOOK: HELLO HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} HOOK: empty -HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.post","params":{"command":"install","search-terms":["foo"],"unknown-packages":[],"packages":[{"id":1,"name":"foo","architecture":"i386","mode":"install","automatic":false,"versions":{"candidate":{"id":1,"version":"1.0","architecture":"all","pin":500,"origins":[{"archive":"unstable","codename":"sid","origin":"Oranges","label":"Lemons","site":""}]},"install":{"id":1,"version":"1.0","architecture":"all","pin":500,"origins":[{"archive":"unstable","codename":"sid","origin":"Oranges","label":"Lemons","site":""}]}}}]}} +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.post","params":{"command":"install","search-terms":["foo"],"unknown-packages":[],"packages":[{"id":2,"name":"foo","architecture":"i386","mode":"install","automatic":false,"versions":{"candidate":{"id":2,"version":"1.0","architecture":"all","pin":500,"origins":[{"archive":"unstable","codename":"sid","origin":"Oranges","label":"Lemons","site":""}]},"install":{"id":2,"version":"1.0","architecture":"all","pin":500,"origins":[{"archive":"unstable","codename":"sid","origin":"Oranges","label":"Lemons","site":""}]}}}]}} HOOK: empty HOOK: BYE' apt install foo -s +############################# Failed dist-upgrade ####################### + +testfailureequal 'Reading package lists... +Building dependency tree... +HOOK: HELLO +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} +HOOK: empty +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.fail","params":{"command":"dist-upgrade","search-terms":["foxxx"],"unknown-packages":["foxxx"],"packages":[]}} +HOOK: empty +HOOK: BYE +E: Unable to locate package foxxx' apt dist-upgrade foxxx + +############################# Success install ####################### + +testsuccessequal 'Reading package lists... +Building dependency tree... +Calculating upgrade... +HOOK: HELLO +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} +HOOK: empty +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.pre-prompt","params":{"command":"dist-upgrade","search-terms":[],"unknown-packages":[],"packages":[{"id":1,"name":"upgrade","architecture":"i386","mode":"install","automatic":false,"versions":{"candidate":{"id":1,"version":"2.0","architecture":"i386","pin":500,"origins":[{"archive":"testing","codename":"testing","origin":"Oranges","label":"Lemons","site":""}]},"install":{"id":1,"version":"2.0","architecture":"i386","pin":500,"origins":[{"archive":"testing","codename":"testing","origin":"Oranges","label":"Lemons","site":""}]},"current":{"id":4,"version":"1.0","architecture":"i386","pin":100,"origins":[]}}}]}} +HOOK: empty +HOOK: BYE +The following packages will be upgraded: + upgrade +1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. +Inst upgrade [1.0] (2.0 Lemons:testing [i386]) +Conf upgrade (2.0 Lemons:testing [i386]) +HOOK: HELLO +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} +HOOK: empty +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.post","params":{"command":"dist-upgrade","search-terms":[],"unknown-packages":[],"packages":[{"id":1,"name":"upgrade","architecture":"i386","mode":"install","automatic":false,"versions":{"candidate":{"id":1,"version":"2.0","architecture":"i386","pin":500,"origins":[{"archive":"testing","codename":"testing","origin":"Oranges","label":"Lemons","site":""}]},"install":{"id":1,"version":"2.0","architecture":"i386","pin":500,"origins":[{"archive":"testing","codename":"testing","origin":"Oranges","label":"Lemons","site":""}]},"current":{"id":4,"version":"1.0","architecture":"i386","pin":100,"origins":[]}}}]}} +HOOK: empty +HOOK: BYE' apt dist-upgrade -s ################## Error in hello response ######################### cat > json-hook.sh << EOF @@ -206,7 +243,7 @@ testsuccessequal 'Reading package lists... Building dependency tree... The following NEW packages will be installed: foo -0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. +0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded. Inst foo (1.0 Lemons:unstable [all]) Conf foo (1.0 Lemons:unstable [all])' apt install foo -s |