From 4b5215e8e2e31637cb0998ecb80d3c3146760579 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Thu, 22 Apr 2021 10:45:45 +0200 Subject: json: Add origins fields to version Provide access to the origins of a package, such that tools can display information about them; for example, you can write a hook counting security upgrades. --- apt-private/private-json-hooks.cc | 35 ++++++++++++++++++++++++++++++++ doc/json-hooks-protocol.md | 10 +++++++++ test/integration/test-apt-cli-json-hooks | 16 +++++++++------ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/apt-private/private-json-hooks.cc b/apt-private/private-json-hooks.cc index 6bf70b1c6..652a59812 100644 --- a/apt-private/private-json-hooks.cc +++ b/apt-private/private-json-hooks.cc @@ -198,6 +198,33 @@ class APT_HIDDEN JsonWriter } }; +/** + * @brief Write a VerFileIterator to a JsonWriter + */ +static void verFiletoJson(JsonWriter &writer, CacheFile &, pkgCache::VerFileIterator const &vf) +{ + auto pf = vf.File(); // Packages file + auto rf = pf.ReleaseFile(); // release file + + writer.beginObject(); + if (not rf.end()) { + if (rf->Archive != 0) + writer.name("archive").value(rf.Archive()); + if (rf->Codename != 0) + writer.name("codename").value(rf.Codename()); + if (rf->Version != 0) + writer.name("version").value(rf.Version()); + if (rf->Origin != 0) + writer.name("origin").value(rf.Origin()); + if (rf->Label != 0) + writer.name("label").value(rf.Label()); + if (rf->Site != 0) + writer.name("site").value(rf.Site()); + } + + writer.endObject(); +} + /** * @brief Write a VerIterator to a JsonWriter */ @@ -208,6 +235,14 @@ static void verIterToJson(JsonWriter &writer, CacheFile &Cache, pkgCache::VerIte writer.name("version").value(Ver.VerStr()); writer.name("architecture").value(Ver.Arch()); writer.name("pin").value(Cache->GetPolicy().GetPriority(Ver)); + + writer.name("origins"); + writer.beginArray(); + for (auto vf = Ver.FileList(); !vf.end(); vf++) + if ((vf.File()->Flags & pkgCache::Flag::NotSource) == 0) + verFiletoJson(writer, Cache, vf); + writer.endArray(); + writer.endObject(); } diff --git a/doc/json-hooks-protocol.md b/doc/json-hooks-protocol.md index 09633e71d..489b04843 100644 --- a/doc/json-hooks-protocol.md +++ b/doc/json-hooks-protocol.md @@ -106,6 +106,16 @@ install. Each package has the following attributes: - *version*: The version as a string - *architecture*: Architecture of the version - *pin*: The pin priority (optional) + - *origins*: Sources from which the package is retrieved (optional, not in older versions) + + Each origin is represented as an object with the following fields: + + - *archive*: string (optional) + - *codename*: string (optional) + - *version*: string (optional) + - *origin*: string (optional) + - *label*: string (optional) + - *site*: string, empty for local repositories or when using mirror+file:/ method (optional) #### Example diff --git a/test/integration/test-apt-cli-json-hooks b/test/integration/test-apt-cli-json-hooks index 298fae072..7169a04cb 100755 --- a/test/integration/test-apt-cli-json-hooks +++ b/test/integration/test-apt-cli-json-hooks @@ -4,6 +4,10 @@ set -e TESTDIR="$(readlink -f "$(dirname "$0")")" . "$TESTDIR/framework" +getoriginfromsuite() { echo 'Oranges'; } +getlabelfromsuite() { echo 'Lemons'; } +getversionfromsuite() { echo 'Volkamer'; } + setupenvironment configarchitecture "i386" @@ -107,18 +111,18 @@ 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},"install":{"id":1,"version":"1.0","architecture":"all","pin":500}}}]}} +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: empty HOOK: BYE The following NEW packages will be installed: foo 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. -Inst foo (1.0 unstable [all]) -Conf foo (1.0 unstable [all]) +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},"install":{"id":1,"version":"1.0","architecture":"all","pin":500}}}]}} +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: empty HOOK: BYE' apt install foo -s @@ -203,8 +207,8 @@ Building dependency tree... The following NEW packages will be installed: foo 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. -Inst foo (1.0 unstable [all]) -Conf foo (1.0 unstable [all])' apt install foo -s +Inst foo (1.0 Lemons:unstable [all]) +Conf foo (1.0 Lemons:unstable [all])' apt install foo -s -- cgit v1.2.3-70-g09d2 From de86a4b076a37db2f70ee5fbfbb295cc71c344df Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Thu, 22 Apr 2021 12:53:54 +0200 Subject: upgrade: Add JSON hook support (AptCli::Hooks::Upgrade) --- apt-private/private-upgrade.cc | 16 +++++++++--- 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 +#include #include #include #include #include #include +#include #include #include @@ -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 verset; + std::set 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 -- cgit v1.2.3-70-g09d2 From dc11a4422d99cb49817c827b9f827617b5880600 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 23 Apr 2021 10:36:18 +0200 Subject: json: Add `package-list` and `statistics` install hooks This enables hooks to output additional information. --- apt-private/private-install.cc | 11 ++++++++--- apt-private/private-install.h | 6 ++++-- apt-private/private-json-hooks.cc | 13 ++++++++----- apt-private/private-upgrade.cc | 2 +- doc/json-hooks-protocol.md | 8 +++++++- test/integration/test-apt-cli-json-hooks | 24 ++++++++++++++++++++++++ 6 files changed, 52 insertions(+), 12 deletions(-) diff --git a/apt-private/private-install.cc b/apt-private/private-install.cc index 402f8f4b6..0c26c4275 100644 --- a/apt-private/private-install.cc +++ b/apt-private/private-install.cc @@ -100,7 +100,7 @@ static void RemoveDownloadNeedingItemsFromFetcher(pkgAcquire &Fetcher, bool &Tra I = Fetcher.ItemsBegin(); } } -bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask, bool Safety) +bool InstallPackages(CacheFile &Cache, bool ShwKept, bool Ask, bool Safety, std::string const &Hook, CommandLine const &CmdL) { if (not RunScripts("APT::Install::Pre-Invoke")) return false; @@ -168,7 +168,12 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask, bool Safety) if (_config->FindB("APT::Get::Download-Only",false) == false) Essential = !ShowEssential(c1out,Cache); + if (not Hook.empty()) + RunJsonHook(Hook, "org.debian.apt.hooks.install.package-list", CmdL.FileList, Cache); + Stats(c1out,Cache); + if (not Hook.empty()) + RunJsonHook(Hook, "org.debian.apt.hooks.install.statistics", CmdL.FileList, Cache); // Sanity check if (Cache->BrokenCount() != 0) @@ -944,9 +949,9 @@ bool DoInstall(CommandLine &CmdL) // See if we need to prompt // FIXME: check if really the packages in the set are going to be installed if (Cache->InstCount() == verset[MOD_INSTALL].size() && Cache->DelCount() == 0) - result = InstallPackages(Cache, false, false); + result = InstallPackages(Cache, false, false, true, "AptCli::Hooks::Install", CmdL); else - result = InstallPackages(Cache, false); + result = InstallPackages(Cache, false, true, true, "AptCli::Hooks::Install", CmdL); if (result) result = RunJsonHook("AptCli::Hooks::Install", "org.debian.apt.hooks.install.post", CmdL.FileList, Cache); diff --git a/apt-private/private-install.h b/apt-private/private-install.h index 39a040e7d..52f055963 100644 --- a/apt-private/private-install.h +++ b/apt-private/private-install.h @@ -36,8 +36,10 @@ bool DoCacheManipulationFromCommandLine(CommandLine &CmdL, std::vector &VolatileCmdL, CacheFile &Cache, int UpgradeMode); bool DoCacheManipulationFromCommandLine(CommandLine &CmdL, CacheFile &Cache, int UpgradeMode); -APT_PUBLIC bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true, - bool Safety = true); +APT_PUBLIC bool InstallPackages(CacheFile &Cache, bool ShwKept, bool Ask = true, + bool Safety = true, + std::string const &Hook = "", + CommandLine const &CmdL = {}); bool CheckNothingBroken(CacheFile &Cache); bool DoAutomaticRemove(CacheFile &Cache); diff --git a/apt-private/private-json-hooks.cc b/apt-private/private-json-hooks.cc index 652a59812..a40597e0a 100644 --- a/apt-private/private-json-hooks.cc +++ b/apt-private/private-json-hooks.cc @@ -277,11 +277,14 @@ static void NotifyHook(std::ostream &os, std::string const &method, const char * /* Build params */ jsonWriter.name("params").beginObject(); - jsonWriter.name("command").value(FileList[0]); - jsonWriter.name("search-terms").beginArray(); - for (int i = 1; FileList[i] != NULL; i++) - jsonWriter.value(FileList[i]); - jsonWriter.endArray(); + if (FileList != nullptr) + { + jsonWriter.name("command").value(FileList[0]); + jsonWriter.name("search-terms").beginArray(); + for (int i = 1; FileList[i] != NULL; i++) + jsonWriter.value(FileList[i]); + jsonWriter.endArray(); + } jsonWriter.name("unknown-packages").beginArray(); for (auto const &PkgName : UnknownPackages) jsonWriter.value(PkgName); diff --git a/apt-private/private-upgrade.cc b/apt-private/private-upgrade.cc index a87ffbc46..c41e4d2f3 100644 --- a/apt-private/private-upgrade.cc +++ b/apt-private/private-upgrade.cc @@ -34,7 +34,7 @@ static bool UpgradeHelper(CommandLine &CmdL, int UpgradeFlags) return false; } RunJsonHook("AptCli::Hooks::Upgrade", "org.debian.apt.hooks.install.pre-prompt", CmdL.FileList, Cache); - if (InstallPackages(Cache, true)) + if (InstallPackages(Cache, true, true, true, "AptCli::Hooks::Upgrade", CmdL)) 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); diff --git a/doc/json-hooks-protocol.md b/doc/json-hooks-protocol.md index 489b04843..3c7f6e4f2 100644 --- a/doc/json-hooks-protocol.md +++ b/doc/json-hooks-protocol.md @@ -57,7 +57,9 @@ method `org.debian.apt.hooks.bye`. The following methods are supported: -1. `org.debian.apt.hooks.install.pre-prompt` - Run before the y/n prompt +1. `org.debian.apt.hooks.install.pre-prompt` - Run before the package list and y/n prompt +1. `org.debian.apt.hooks.install.package-list` - (optional in 0.1) Run after the package list. You could display additional lists of packages here +1. `org.debian.apt.hooks.install.statistics` - (optional in 0.1) Run after the package list. You could display additional lists of packages here 1. `org.debian.apt.hooks.install.post` - Run after success 1. `org.debian.apt.hooks.install.fail` - Run after failed install 1. `org.debian.apt.hooks.search.pre` - Run before search @@ -93,6 +95,10 @@ install. Each package has the following attributes: - *mode*: One of `install`, `deinstall`, `purge`, or `keep`. `keep` is not exposed in 0.1. To determine an upgrade, check that a current version is installed. +- One of the following optional fields may be set to true to indicate a change relative to an installed version: + - *downgrade*: true if downgrading + - *upgrade*: true if upgrading + - *reinstall*: true if reinstall flag is set - *automatic*: Whether the package is/will be automatically installed - *versions*: An array with up to 3 fields: diff --git a/test/integration/test-apt-cli-json-hooks b/test/integration/test-apt-cli-json-hooks index 07801a547..0e6745d00 100755 --- a/test/integration/test-apt-cli-json-hooks +++ b/test/integration/test-apt-cli-json-hooks @@ -119,7 +119,19 @@ HOOK: empty HOOK: BYE The following NEW packages will be installed: foo +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.package-list","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 0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded. +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.statistics","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 Inst foo (1.0 Lemons:unstable [all]) Conf foo (1.0 Lemons:unstable [all]) HOOK: HELLO @@ -154,7 +166,19 @@ HOOK: empty HOOK: BYE The following packages will be upgraded: 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.package-list","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 1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. +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.statistics","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 Inst upgrade [1.0] (2.0 Lemons:testing [i386]) Conf upgrade (2.0 Lemons:testing [i386]) HOOK: HELLO -- cgit v1.2.3-70-g09d2 From 80dd1447595c536d31912c486ac5e96f983ccc7a Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 23 Apr 2021 11:37:28 +0200 Subject: json: Hook protocol 0.2 (added upgrade,downgrade,reinstall modes) Hook protocol 0.2 makes the new fields we added mandatory, and replaces `install` mode with `upgrade`, `downgrade`, `reinstall` where appropriate. Hook negotiation is hacky, but it's the best we can do for now. Users are advised to upgrade to 0.2 --- apt-private/private-json-hooks.cc | 49 ++++++++++++++---- doc/json-hooks-protocol.md | 48 +++++++++++------ test/integration/test-apt-cli-json-hooks | 88 ++++++++++++++++++++++++++------ 3 files changed, 144 insertions(+), 41 deletions(-) diff --git a/apt-private/private-json-hooks.cc b/apt-private/private-json-hooks.cc index a40597e0a..957140525 100644 --- a/apt-private/private-json-hooks.cc +++ b/apt-private/private-json-hooks.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -265,7 +266,7 @@ static void DpkgChrootDirectory() /** * @brief Send a notification to the hook's stream */ -static void NotifyHook(std::ostream &os, std::string const &method, const char **FileList, CacheFile &Cache, std::set const &UnknownPackages) +static void NotifyHook(std::ostream &os, std::string const &method, const char **FileList, CacheFile &Cache, std::set const &UnknownPackages, int hookVersion) { SortedPackageUniverse Universe(Cache); JsonWriter jsonWriter{os}; @@ -311,7 +312,14 @@ static void NotifyHook(std::ostream &os, std::string const &method, const char * switch (Cache[Pkg].Mode) { case pkgDepCache::ModeInstall: - jsonWriter.name("mode").value("install"); + if (Pkg->CurrentVer != 0 && Cache[Pkg].Upgrade() && hookVersion >= 0x020) + jsonWriter.name("mode").value("upgrade"); + else if (Pkg->CurrentVer != 0 && Cache[Pkg].Downgrade() && hookVersion >= 0x020) + jsonWriter.name("mode").value("downgrade"); + else if (Pkg->CurrentVer != 0 && Cache[Pkg].ReInstall() && hookVersion >= 0x020) + jsonWriter.name("mode").value("reinstall"); + else + jsonWriter.name("mode").value("install"); break; case pkgDepCache::ModeDelete: jsonWriter.name("mode").value(Cache[Pkg].Purge() ? "purge" : "deinstall"); @@ -344,7 +352,7 @@ static void NotifyHook(std::ostream &os, std::string const &method, const char * static std::string BuildHelloMessage() { std::stringstream Hello; - JsonWriter(Hello).beginObject().name("jsonrpc").value("2.0").name("method").value("org.debian.apt.hooks.hello").name("id").value(0).name("params").beginObject().name("versions").beginArray().value("0.1").endArray().endObject().endObject(); + JsonWriter(Hello).beginObject().name("jsonrpc").value("2.0").name("method").value("org.debian.apt.hooks.hello").name("id").value(0).name("params").beginObject().name("versions").beginArray().value("0.1").value("0.2").endArray().endObject().endObject(); return Hello.str(); } @@ -361,11 +369,10 @@ static std::string BuildByeMessage() /// @brief Run the Json hook processes in the given option. bool RunJsonHook(std::string const &option, std::string const &method, const char **FileList, CacheFile &Cache, std::set const &UnknownPackages) { - std::stringstream ss; - NotifyHook(ss, method, FileList, Cache, UnknownPackages); - std::string TheData = ss.str(); + std::unordered_map notifications; std::string HelloData = BuildHelloMessage(); std::string ByeData = BuildByeMessage(); + int hookVersion; bool result = true; @@ -465,6 +472,20 @@ bool RunJsonHook(std::string const &option, std::string const &method, const cha goto out; } + if (strstr(line, "\"0.1\"")) + { + hookVersion = 0x010; + } + else if (strstr(line, "\"0.2\"")) + { + hookVersion = 0x020; + } + else + { + _error->Error("Unknown hook version in handshake from hook %s: %s", Opts->Value.c_str(), line); + goto out; + } + size = getline(&line, &linesize, F); if (size < 0) { @@ -476,9 +497,18 @@ bool RunJsonHook(std::string const &option, std::string const &method, const cha _error->Error("Expected empty line after handshake from %s, received %s", Opts->Value.c_str(), line); goto out; } - - fwrite(TheData.data(), TheData.size(), 1, F); - fwrite("\n\n", 2, 1, F); + { + std::string &data = notifications[hookVersion]; + if (data.empty()) + { + std::stringstream ss; + NotifyHook(ss, method, FileList, Cache, UnknownPackages, hookVersion); + ; + data = ss.str(); + } + fwrite(data.data(), data.size(), 1, F); + fwrite("\n\n", 2, 1, F); + } fwrite(ByeData.data(), ByeData.size(), 1, F); fwrite("\n\n", 2, 1, F); @@ -490,6 +520,7 @@ bool RunJsonHook(std::string const &option, std::string const &method, const cha result = _error->Error("Failure running hook %s", Opts->Value.c_str()); break; } + } signal(SIGINT, old_sigint); signal(SIGPIPE, old_sigpipe); diff --git a/doc/json-hooks-protocol.md b/doc/json-hooks-protocol.md index 3c7f6e4f2..4c0429d06 100644 --- a/doc/json-hooks-protocol.md +++ b/doc/json-hooks-protocol.md @@ -1,3 +1,5 @@ +Version: 0.2 + ## JSON Hooks APT 1.6 introduces support for hooks that talk JSON-RPC 2.0. Hooks act @@ -59,7 +61,7 @@ The following methods are supported: 1. `org.debian.apt.hooks.install.pre-prompt` - Run before the package list and y/n prompt 1. `org.debian.apt.hooks.install.package-list` - (optional in 0.1) Run after the package list. You could display additional lists of packages here -1. `org.debian.apt.hooks.install.statistics` - (optional in 0.1) Run after the package list. You could display additional lists of packages here +1. `org.debian.apt.hooks.install.statistics` - (optional in 0.1) Run after the count of packages to upgrade/install. You could display additional information here, such as `5 security upgrades` 1. `org.debian.apt.hooks.install.post` - Run after success 1. `org.debian.apt.hooks.install.fail` - Run after failed install 1. `org.debian.apt.hooks.search.pre` - Run before search @@ -90,29 +92,30 @@ install. Each package has the following attributes: - *id*: An unsigned integer describing the package - *name*: The name of the package - *architecture*: The architecture of the package. For `"all"` packages, this will be the native architecture; - use per-version architecture fields to see `"all"`. + use per-version architecture fields to see `"all"`. -- *mode*: One of `install`, `deinstall`, `purge`, or `keep`. `keep` - is not exposed in 0.1. To determine an upgrade, check - that a current version is installed. +- *mode*: One of `install`, `upgrade`, `downgrade`, `reinstall`, `deinstall`, `purge`, `keep`. + Version 0.1 does not implement `upgrade`, `downgrade`, and `reinstall` - all of them are represented + as `install`, and you have to compare the `current` version to the `install` version to figure out if + is one of those. - One of the following optional fields may be set to true to indicate a change relative to an installed version: - - *downgrade*: true if downgrading - - *upgrade*: true if upgrading - - *reinstall*: true if reinstall flag is set +- *downgrade*: true if downgrading +- *upgrade*: true if upgrading +- *reinstall*: true if reinstall flag is set - *automatic*: Whether the package is/will be automatically installed - *versions*: An array with up to 3 fields: - - *candidate*: The candidate version - - *install*: The version to be installed - - *current*: The version currently installed +- *candidate*: The candidate version +- *install*: The version to be installed +- *current*: The version currently installed - Each version is represented as an object with the following fields: +Each version is represented as an object with the following fields: - - *id*: An unsigned integer - - *version*: The version as a string - - *architecture*: Architecture of the version - - *pin*: The pin priority (optional) - - *origins*: Sources from which the package is retrieved (optional, not in older versions) +- *id*: An unsigned integer +- *version*: The version as a string +- *architecture*: Architecture of the version +- *pin*: The pin priority (optional) +- *origins*: Sources from which the package is retrieved (since 0.2) Each origin is represented as an object with the following fields: @@ -173,3 +176,14 @@ protocol version only (for example, 1.7 may only support 0.2). Additional fields may be added to objects without bumping the protocol version. + +# Changes: + +## Version 0.2 + +The 0.2 protocol makes one incompatible change, and adds several new compatible changes, all of which are optional in 0.1, +but mandatory in 0.2. + +* (incompatible change) The `mode` flag of arguments gained `upgrade`, `downgrade`, `reinstall` modes. All of these are `install` +* (compatible change) The hooks `org.debian.apt.hooks.install.package-list` and `org.debian.apt.hooks.install.statistics` have been added +* (compatible change) Version objects gained a new `origins` array diff --git a/test/integration/test-apt-cli-json-hooks b/test/integration/test-apt-cli-json-hooks index 0e6745d00..59cf6e5ef 100755 --- a/test/integration/test-apt-cli-json-hooks +++ b/test/integration/test-apt-cli-json-hooks @@ -24,6 +24,7 @@ insertinstalledpackage 'upgrade' 'i386' '1.0' setupaptarchive APTARCHIVE="$(readlink -f ./aptarchive)" +export TEST_HOOK_VERSION=0.2 cat >> json-hook.sh << EOF #!/bin/bash @@ -34,7 +35,7 @@ while true; do if echo "\$request" | grep -q ".hello"; then echo "HOOK: HELLO" - printf '{"jsonrpc": "2.0", "result": {"version": "0.1"}, "id": 0}\n\n' >&\$APT_HOOK_SOCKET + printf '{"jsonrpc": "2.0", "result": {"version": "'\$TEST_HOOK_VERSION'"}, "id": 0}\n\n' >&\$APT_HOOK_SOCKET fi if echo "\$request" | grep -q ".bye"; then @@ -61,7 +62,7 @@ EOF ############################# Success search ####################### testsuccessequal 'HOOK: HELLO -HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} HOOK: empty HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.search.pre","params":{"command":"search","search-terms":["foo"],"unknown-packages":[],"packages":[]}} HOOK: empty @@ -72,7 +73,7 @@ foo/unstable 1.0 all Some description that has a unusual word xxyyzz and aabbcc and a UPPERCASE HOOK: HELLO -HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} HOOK: empty HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.search.post","params":{"command":"search","search-terms":["foo"],"unknown-packages":[],"packages":[]}} HOOK: empty @@ -80,7 +81,7 @@ HOOK: BYE' apt search foo ############################# Failed search ####################### testsuccessequal 'HOOK: HELLO -HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} HOOK: empty HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.search.pre","params":{"command":"search","search-terms":["foox"],"unknown-packages":[],"packages":[]}} HOOK: empty @@ -88,7 +89,7 @@ HOOK: BYE Sorting... Full Text Search... HOOK: HELLO -HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} HOOK: empty HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.search.fail","params":{"command":"search","search-terms":["foox"],"unknown-packages":[],"packages":[]}} HOOK: empty @@ -100,7 +101,7 @@ HOOK: BYE' apt search foox 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: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} HOOK: empty HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.fail","params":{"command":"install","search-terms":["foxxx"],"unknown-packages":["foxxx"],"packages":[]}} HOOK: empty @@ -112,7 +113,7 @@ E: Unable to locate package foxxx' apt install foxxx testsuccessequal '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: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} 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":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 @@ -120,14 +121,14 @@ HOOK: BYE The following NEW packages will be installed: foo HOOK: HELLO -HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} HOOK: empty HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.package-list","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 0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded. HOOK: HELLO -HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} HOOK: empty HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.statistics","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 @@ -135,7 +136,7 @@ HOOK: BYE 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: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} HOOK: empty 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 @@ -146,7 +147,7 @@ HOOK: BYE' apt install foo -s 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: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} 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 @@ -159,7 +160,44 @@ 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: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} +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":"upgrade","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 +HOOK: HELLO +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} +HOOK: empty +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.package-list","params":{"command":"dist-upgrade","search-terms":[],"unknown-packages":[],"packages":[{"id":1,"name":"upgrade","architecture":"i386","mode":"upgrade","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 +1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. +HOOK: HELLO +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} +HOOK: empty +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.statistics","params":{"command":"dist-upgrade","search-terms":[],"unknown-packages":[],"packages":[{"id":1,"name":"upgrade","architecture":"i386","mode":"upgrade","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 +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","0.2"]}} +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":"upgrade","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 + + +################## version 0.1 ######################### +TEST_HOOK_VERSION=0.1 + +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","0.2"]}} 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 @@ -167,14 +205,14 @@ HOOK: BYE The following packages will be upgraded: upgrade HOOK: HELLO -HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} HOOK: empty HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.package-list","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 1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. HOOK: HELLO -HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}} +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} HOOK: empty HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.statistics","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 @@ -182,11 +220,31 @@ HOOK: BYE 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: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} 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 + + +################## Wrong version ######################### +TEST_HOOK_VERSION=42 + +testfailureequal '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","0.2"]}} +HOOK: empty +HOOK: HELLO +HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1","0.2"]}} +HOOK: empty +E: Unknown hook version in handshake from hook '$HOOK': {"jsonrpc": "2.0", "result": {"version": "42"}, "id": 0} +E: Sub-process '$HOOK' returned an error code (1) +E: Failure running hook '$HOOK' +E: Unknown hook version in handshake from hook '$HOOK': {"jsonrpc": "2.0", "result": {"version": "42"}, "id": 0} +E: Sub-process '$HOOK' returned an error code (1) +E: Failure running hook '$HOOK'' apt dist-upgrade -s ################## Error in hello response ######################### cat > json-hook.sh << EOF -- cgit v1.2.3-70-g09d2 From 64127478630b676838735b509fec5cdfa36874c8 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 23 Apr 2021 11:39:15 +0200 Subject: 2.3-only: Warn that the 0.1 protocol is deprecated --- apt-private/private-json-hooks.cc | 1 + test/integration/test-apt-cli-json-hooks | 18 +++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/apt-private/private-json-hooks.cc b/apt-private/private-json-hooks.cc index 957140525..acb9a3bf0 100644 --- a/apt-private/private-json-hooks.cc +++ b/apt-private/private-json-hooks.cc @@ -474,6 +474,7 @@ bool RunJsonHook(std::string const &option, std::string const &method, const cha if (strstr(line, "\"0.1\"")) { + _error->Warning("Hook %s uses deprecated 0.1 protocol", Opts->Value.c_str()); hookVersion = 0x010; } else if (strstr(line, "\"0.2\"")) diff --git a/test/integration/test-apt-cli-json-hooks b/test/integration/test-apt-cli-json-hooks index 59cf6e5ef..3ec470284 100755 --- a/test/integration/test-apt-cli-json-hooks +++ b/test/integration/test-apt-cli-json-hooks @@ -193,7 +193,7 @@ HOOK: BYE' apt dist-upgrade -s ################## version 0.1 ######################### TEST_HOOK_VERSION=0.1 -testsuccessequal 'Reading package lists... +testequal 'Reading package lists... Building dependency tree... Calculating upgrade... HOOK: HELLO @@ -224,7 +224,11 @@ HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"par 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 +HOOK: BYE +W: Hook '$HOOK' uses deprecated 0.1 protocol +W: Hook '$HOOK' uses deprecated 0.1 protocol +W: Hook '$HOOK' uses deprecated 0.1 protocol +W: Hook '$HOOK' uses deprecated 0.1 protocol' apt dist-upgrade -s ################## Wrong version ######################### @@ -256,7 +260,7 @@ while true; do read empty <&\$APT_HOOK_SOCKET if echo "\$request" | grep -q ".hello"; then - printf '{"jsonrpc": "2.0", "error": {"version": "0.1"}, "id": 0}\n\n' >&\$APT_HOOK_SOCKET + printf '{"jsonrpc": "2.0", "error": {"version": "0.2"}, "id": 0}\n\n' >&\$APT_HOOK_SOCKET break fi done @@ -266,8 +270,8 @@ EOF testfailureequal 'Reading package lists... Building dependency tree... -E: Hook '$HOOK' reported an error during hello: {"jsonrpc": "2.0", "error": {"version": "0.1"}, "id": 0} -E: Hook '$HOOK' reported an error during hello: {"jsonrpc": "2.0", "error": {"version": "0.1"}, "id": 0}' apt install foo -s +E: Hook '$HOOK' reported an error during hello: {"jsonrpc": "2.0", "error": {"version": "0.2"}, "id": 0} +E: Hook '$HOOK' reported an error during hello: {"jsonrpc": "2.0", "error": {"version": "0.2"}, "id": 0}' apt install foo -s ################## Missing separator line ######################### cat > json-hook.sh << EOF @@ -279,7 +283,7 @@ while true; do read empty <&\$APT_HOOK_SOCKET if echo "\$request" | grep -q ".hello"; then - printf '{"jsonrpc": "2.0", "result": {"version": "0.1"}, "id": 0}\n' >&\$APT_HOOK_SOCKET + printf '{"jsonrpc": "2.0", "result": {"version": "0.2"}, "id": 0}\n' >&\$APT_HOOK_SOCKET break fi done @@ -302,7 +306,7 @@ while true; do read empty <&\$APT_HOOK_SOCKET if echo "\$request" | grep -q ".hello"; then - printf '{"jsonrpc": "2.0", "result": {"version": "0.1"}, "id": 0}\nXX' >&\$APT_HOOK_SOCKET + printf '{"jsonrpc": "2.0", "result": {"version": "0.2"}, "id": 0}\nXX' >&\$APT_HOOK_SOCKET break fi done -- cgit v1.2.3-70-g09d2