From: Arvin Schnell Date: Thu, 22 Dec 2022 08:05:10 +0000 (+0100) Subject: - call generic plugins before and after the action X-Git-Tag: v0.10.5~73^2 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=74f4210dc92aa54080aebff74deb63a4e5d7a602;p=thirdparty%2Fsnapper.git - call generic plugins before and after the action --- diff --git a/LIBVERSION b/LIBVERSION index dfda3e0b..6abaeb2f 100644 --- a/LIBVERSION +++ b/LIBVERSION @@ -1 +1 @@ -6.1.0 +6.2.0 diff --git a/client/cmd-rollback.cc b/client/cmd-rollback.cc index 2bf41c34..f1703569 100644 --- a/client/cmd-rollback.cc +++ b/client/cmd-rollback.cc @@ -247,7 +247,8 @@ namespace snapper Hooks::rollback(filesystem->snapshotDir(snapshot1->getNum()), filesystem->snapshotDir(snapshot2->getNum())); - Hooks::rollback(subvolume, filesystem, snapshot1->getNum(), snapshot2->getNum()); + Hooks::rollback(Hooks::Stage::POST_ACTION, subvolume, filesystem, snapshot1->getNum(), + snapshot2->getNum()); if (print_number) cout << snapshot2->getNum() << endl; @@ -292,7 +293,8 @@ namespace snapper Hooks::rollback(filesystem->snapshotDir(previous_default->getNum()), filesystem->snapshotDir(snapshot->getNum())); - Hooks::rollback(subvolume, filesystem, previous_default->getNum(), snapshot->getNum()); + Hooks::rollback(Hooks::Stage::POST_ACTION, subvolume, filesystem, previous_default->getNum(), + snapshot->getNum()); } break; diff --git a/client/installation-helper.cc b/client/installation-helper.cc index 874c6c99..248cf350 100644 --- a/client/installation-helper.cc +++ b/client/installation-helper.cc @@ -214,7 +214,7 @@ step4() cout << "running external programs" << endl; - Hooks::create_config("/", &btrfs); + Hooks::create_config(Hooks::Stage::POST_ACTION, "/", &btrfs); cout << "done" << endl; } diff --git a/doc/snapper.xml.in b/doc/snapper.xml.in index 5d79eccb..fce06589 100644 --- a/doc/snapper.xml.in +++ b/doc/snapper.xml.in @@ -2,13 +2,13 @@ - 2022-06-02 + 2022-12-22 snapper 8 - 2022-06-02 + 2022-12-22 @VERSION@ Filesystem Snapshot Management @@ -848,45 +848,81 @@ following actions are defined: - + - Executed when a new config gets created + Executed before a new config is created - + - Executed when a config gets deleted + Executed after a new config was created - + - Executed when a new snapshot gets created + Executed before a config is deleted - + - Executed when a snapshot is removed + Executed after a config was deleted - + - Executed when a snapshot gets modified + Executed before a new snapshot is created - + - Executed when the default snapshot gets changed + Executed after a new snapshot was created - + - Executed when a rollback is done + Executed before a snapshot is modified + + + + + + Executed after a snapshot was modified + + + + + + Executed before a snapshot is removed + + + + + + Executed after a snapshot was removed + + + + + + Executed before the default snapshot is changed + + + + + + Executed after the default snapshot was changed + + + + + + Executed after a rollback was done diff --git a/package/snapper.changes b/package/snapper.changes index 3dde191a..8f0f10dc 100644 --- a/package/snapper.changes +++ b/package/snapper.changes @@ -1,3 +1,8 @@ +------------------------------------------------------------------- +Thu Dec 22 09:03:33 CET 2022 - aschnell@suse.com + +- call generic plugins before and after the action + ------------------------------------------------------------------- Mon Dec 05 08:13:07 CET 2022 - aschnell@suse.com diff --git a/snapper/Btrfs.cc b/snapper/Btrfs.cc index 0f85b2df..5e68fceb 100644 --- a/snapper/Btrfs.cc +++ b/snapper/Btrfs.cc @@ -1518,6 +1518,8 @@ namespace snapper { try { + Hooks::set_default_snapshot(Hooks::Stage::PRE_ACTION, subvolume, this, num); + SDir general_dir = openGeneralDir(); if (num == 0) @@ -1533,7 +1535,7 @@ namespace snapper set_default_id(general_dir.fd(), id); } - Hooks::set_default_snapshot(subvolume, this, num); + Hooks::set_default_snapshot(Hooks::Stage::POST_ACTION, subvolume, this, num); } catch (const runtime_error& e) { diff --git a/snapper/Hooks.cc b/snapper/Hooks.cc index 3acf160a..d5a51eea 100644 --- a/snapper/Hooks.cc +++ b/snapper/Hooks.cc @@ -66,50 +66,113 @@ namespace snapper } + // Actions without -pre/-post are legacy and deprecated (2022-12-22). + + void - Hooks::create_config(const string& subvolume, const Filesystem* filesystem) + Hooks::create_config(Stage stage, const string& subvolume, const Filesystem* filesystem) { - grub(subvolume, filesystem, "--enable"); - run_scripts({ "create-config", subvolume, filesystem->fstype() }); + switch (stage) + { + case Stage::PRE_ACTION: + run_scripts({ "create-config-pre", subvolume, filesystem->fstype() }); + break; + + case Stage::POST_ACTION: + grub(subvolume, filesystem, "--enable"); + run_scripts({ "create-config", subvolume, filesystem->fstype() }); + run_scripts({ "create-config-post", subvolume, filesystem->fstype() }); + break; + } } void - Hooks::delete_config(const string& subvolume, const Filesystem* filesystem) + Hooks::delete_config(Stage stage, const string& subvolume, const Filesystem* filesystem) { - grub(subvolume, filesystem, "--disable"); - run_scripts({ "delete-config", subvolume, filesystem->fstype() }); + switch (stage) + { + case Stage::PRE_ACTION: + grub(subvolume, filesystem, "--disable"); + run_scripts({ "delete-config-pre", subvolume, filesystem->fstype() }); + run_scripts({ "delete-config", subvolume, filesystem->fstype() }); + break; + + case Stage::POST_ACTION: + run_scripts({ "delete-config-post", subvolume, filesystem->fstype() }); + break; + } } void - Hooks::create_snapshot(const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot) + Hooks::create_snapshot(Stage stage, const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot) { - grub(subvolume, filesystem, "--refresh"); - run_scripts({ "create-snapshot", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); + switch (stage) + { + case Stage::PRE_ACTION: + run_scripts({ "create-snapshot-pre", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); + break; + + case Stage::POST_ACTION: + grub(subvolume, filesystem, "--refresh"); + run_scripts({ "create-snapshot", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); + run_scripts({ "create-snapshot-post", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); + break; + } } void - Hooks::modify_snapshot(const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot) + Hooks::modify_snapshot(Stage stage, const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot) { - grub(subvolume, filesystem, "--refresh"); - run_scripts({ "modify-snapshot", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); + switch (stage) + { + case Stage::PRE_ACTION: + run_scripts({ "modify-snapshot-pre", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); + break; + + case Stage::POST_ACTION: + grub(subvolume, filesystem, "--refresh"); + run_scripts({ "modify-snapshot", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); + run_scripts({ "modify-snapshot-post", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); + break; + } } void - Hooks::delete_snapshot(const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot) + Hooks::delete_snapshot(Stage stage, const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot) { - grub(subvolume, filesystem, "--refresh"); - run_scripts({ "delete-snapshot", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); + switch (stage) + { + case Stage::PRE_ACTION: + run_scripts({ "delete-snapshot-pre", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); + break; + + case Stage::POST_ACTION: + grub(subvolume, filesystem, "--refresh"); + run_scripts({ "delete-snapshot", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); + run_scripts({ "delete-snapshot-post", subvolume, filesystem->fstype(), std::to_string(snapshot.getNum()) }); + break; + } } void - Hooks::set_default_snapshot(const string& subvolume, const Filesystem* filesystem, unsigned int num) + Hooks::set_default_snapshot(Stage stage, const string& subvolume, const Filesystem* filesystem, unsigned int num) { - run_scripts({ "set-default-snapshot", subvolume, filesystem->fstype(), std::to_string(num) }); + switch (stage) + { + case Stage::PRE_ACTION: + run_scripts({ "set-default-snapshot-pre", subvolume, filesystem->fstype(), std::to_string(num) }); + break; + + case Stage::POST_ACTION: + run_scripts({ "set-default-snapshot", subvolume, filesystem->fstype(), std::to_string(num) }); + run_scripts({ "set-default-snapshot-post", subvolume, filesystem->fstype(), std::to_string(num) }); + break; + } } @@ -145,9 +208,31 @@ namespace snapper void - Hooks::rollback(const string& subvolume, const Filesystem* filesystem, unsigned int old_num, unsigned int new_num) + Hooks::rollback(const string& subvolume, const Filesystem* filesystem, unsigned int old_num, + unsigned int new_num) + { + rollback(Stage::POST_ACTION, subvolume, filesystem, old_num, new_num); + } + + + void + Hooks::rollback(Stage stage, const string& subvolume, const Filesystem* filesystem, unsigned int old_num, + unsigned int new_num) { - run_scripts({ "rollback", subvolume, filesystem->fstype(), std::to_string(old_num), std::to_string(new_num) }); + switch (stage) + { + case Stage::PRE_ACTION: + run_scripts({ "rollback-pre", subvolume, filesystem->fstype(), std::to_string(old_num), + std::to_string(new_num) }); + break; + + case Stage::POST_ACTION: + run_scripts({ "rollback", subvolume, filesystem->fstype(), std::to_string(old_num), + std::to_string(new_num) }); + run_scripts({ "rollback-post", subvolume, filesystem->fstype(), std::to_string(old_num), + std::to_string(new_num) }); + break; + } } } diff --git a/snapper/Hooks.h b/snapper/Hooks.h index 97a96ef1..ef0a7c35 100644 --- a/snapper/Hooks.h +++ b/snapper/Hooks.h @@ -38,17 +38,27 @@ namespace snapper { public: - static void create_config(const string& subvolume, const Filesystem* filesystem); - static void delete_config(const string& subvolume, const Filesystem* filesystem); + enum class Stage { PRE_ACTION, POST_ACTION }; - static void create_snapshot(const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot); - static void modify_snapshot(const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot); - static void delete_snapshot(const string& subvolume, const Filesystem* filesystem, const Snapshot& snapshot); + static void create_config(Stage stage, const string& subvolume, const Filesystem* filesystem); + static void delete_config(Stage stage, const string& subvolume, const Filesystem* filesystem); - static void set_default_snapshot(const string& subvolume, const Filesystem* filesystem, unsigned int num); + static void create_snapshot(Stage stage, const string& subvolume, const Filesystem* filesystem, + const Snapshot& snapshot); + static void modify_snapshot(Stage stage, const string& subvolume, const Filesystem* filesystem, + const Snapshot& snapshot); + static void delete_snapshot(Stage stage, const string& subvolume, const Filesystem* filesystem, + const Snapshot& snapshot); + + static void set_default_snapshot(Stage stage, const string& subvolume, const Filesystem* filesystem, + unsigned int num); static void rollback(const string& old_root, const string& new_root); - static void rollback(const string& subvolume, const Filesystem* filesystem, unsigned int old_num, unsigned int new_num); + + static void rollback(const string& subvolume, const Filesystem* filesystem, unsigned int old_num, + unsigned int new_num) __attribute__((deprecated)); + static void rollback(Stage stage, const string& subvolume, const Filesystem* filesystem, unsigned int old_num, + unsigned int new_num); private: diff --git a/snapper/Snapper.cc b/snapper/Snapper.cc index 87303500..3d8b4371 100644 --- a/snapper/Snapper.cc +++ b/snapper/Snapper.cc @@ -390,6 +390,8 @@ namespace snapper SN_THROW(CreateConfigFailedException(e.what())); } + Hooks::create_config(Hooks::Stage::PRE_ACTION, subvolume, filesystem.get()); + try { SysconfigFile sysconfig(SYSCONFIG_FILE); @@ -452,7 +454,7 @@ namespace snapper SN_RETHROW(e); } - Hooks::create_config(subvolume, filesystem.get()); + Hooks::create_config(Hooks::Stage::POST_ACTION, subvolume, filesystem.get()); } @@ -464,7 +466,7 @@ namespace snapper unique_ptr snapper(new Snapper(config_name, root_prefix)); - Hooks::delete_config(snapper->subvolumeDir(), snapper->getFilesystem()); + Hooks::delete_config(Hooks::Stage::PRE_ACTION, snapper->subvolumeDir(), snapper->getFilesystem()); Snapshots& snapshots = snapper->getSnapshots(); @@ -524,6 +526,8 @@ namespace snapper SN_THROW(DeleteConfigFailedException("modifying sysconfig-file failed")); } + + Hooks::delete_config(Hooks::Stage::POST_ACTION, snapper->subvolumeDir(), snapper->getFilesystem()); } diff --git a/snapper/Snapshot.cc b/snapper/Snapshot.cc index abf17aa8..1974bbac 100644 --- a/snapper/Snapshot.cc +++ b/snapper/Snapshot.cc @@ -701,6 +701,9 @@ namespace snapper // parent == end indicates the btrfs default subvolume. Unclean, but // adding a special snapshot like current needs too many API changes. + Hooks::create_snapshot(Hooks::Stage::PRE_ACTION, snapper->subvolumeDir(), snapper->getFilesystem(), + snapshot); + try { if (parent != end()) @@ -733,7 +736,8 @@ namespace snapper SN_RETHROW(e); } - Hooks::create_snapshot(snapper->subvolumeDir(), snapper->getFilesystem(), snapshot); + Hooks::create_snapshot(Hooks::Stage::POST_ACTION, snapper->subvolumeDir(), snapper->getFilesystem(), + snapshot); return entries.insert(entries.end(), snapshot); } @@ -747,13 +751,17 @@ namespace snapper checkUserdata(smd.userdata); + Hooks::modify_snapshot(Hooks::Stage::PRE_ACTION, snapper->subvolumeDir(), snapper->getFilesystem(), + *snapshot); + snapshot->description = smd.description; snapshot->cleanup = smd.cleanup; snapshot->userdata = smd.userdata; snapshot->writeInfo(); - Hooks::modify_snapshot(snapper->subvolumeDir(), snapper->getFilesystem(), *snapshot); + Hooks::modify_snapshot(Hooks::Stage::POST_ACTION, snapper->subvolumeDir(), snapper->getFilesystem(), + *snapshot); } @@ -764,6 +772,9 @@ namespace snapper snapshot->isActive()) SN_THROW(IllegalSnapshotException()); + Hooks::delete_snapshot(Hooks::Stage::PRE_ACTION, snapper->subvolumeDir(), snapper->getFilesystem(), + *snapshot); + snapshot->deleteFilesystemSnapshot(); SDir info_dir = snapshot->openInfoDir(); @@ -792,7 +803,8 @@ namespace snapper SDir infos_dir = snapper->openInfosDir(); infos_dir.unlink(decString(snapshot->getNum()), AT_REMOVEDIR); - Hooks::delete_snapshot(snapper->subvolumeDir(), snapper->getFilesystem(), *snapshot); + Hooks::delete_snapshot(Hooks::Stage::POST_ACTION, snapper->subvolumeDir(), snapper->getFilesystem(), + *snapshot); entries.erase(snapshot); }