Skip to content

Commit

Permalink
tukit: move getBindDir as protected
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Planas <[email protected]>
  • Loading branch information
aplanas committed Jun 25, 2024
1 parent c374ee9 commit 88d51b3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
18 changes: 9 additions & 9 deletions doc/tukit-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ user should not depend on the calling order.
## Stages

Some actions will trigger a plugin call before and after the action
itself, depending if makes sense in the context of this action. The
next table summarize the action, the stage and different parameters
sent to the plugin.
itself, depending if it makes sense in the context of this action.
The next table summarizes the action, the stage and different
parameters sent to the plugin.

| Action | Stage | Parameters | Notes |
|----------|-------|-----------------------------------|-------|
Expand Down Expand Up @@ -85,15 +85,15 @@ transaction is still alive and active.
#!/bin/bash

call_pre() {
local path="$1"; shift
local snapshot_id="$1"; shift
local cmd="$@"
local path="$1"; shift
local snapshot_id="$1"; shift
local cmd="$@"

# The live snapshot is in "$path", and the future closed snapshot in
# "/.snapshots/${snapshot_id}/snapshot
# "/.snapshots/${snapshot_id}/snapshot

mkdir -p /var/lib/report
echo "${snapshot_id}: $cmd" >> /var/lib/report/all_commands
mkdir -p /var/lib/report
echo "${snapshot_id}: $cmd" >> /var/lib/report/all_commands
}

declare -A commands
Expand Down
18 changes: 13 additions & 5 deletions lib/Plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ namespace TransactionalUpdate {

using namespace std;

Plugins::Plugins() {
Plugins::Plugins(TransactionalUpdate::Transaction* transaction): transaction(transaction) {
set<string> plugins_set{};

const filesystem::path plugins_dir{filesystem::path(CONFDIR)/"tukit"/"plugins"};
const filesystem::path system_plugins_dir{filesystem::path(PREFIX)/"lib"/"tukit"/"plugins"};

for (auto d: {plugins_dir, system_plugins_dir}) {
if (!filesystem::exists(d))
continue;

for (auto const& dir_entry: filesystem::directory_iterator{d}) {
auto path = dir_entry.path();
auto filename = dir_entry.path().filename();
Expand All @@ -40,6 +43,7 @@ Plugins::Plugins() {
if (!(filesystem::is_regular_file(path) && (access(path.c_str(), X_OK) == 0)))
continue;

tulog.info("Found plugin ", path);
plugins.push_back(path);
plugins_set.insert(filename);
}
Expand All @@ -60,16 +64,20 @@ void Plugins::run(string stage, string args) {

try {
output = Util::exec(cmd);
tulog.info("plugin ", p, " output: ", output);
if (!output.empty())
tulog.info("Output of plugin ", p, ": ", output);
} catch (const ExecutionException &e) {
// An error in the plugin should not discard the transaction
tulog.error("ERROR: plugin ", p, " fails with ", e.what());
tulog.error("ERROR: Plugin ", p, " failed with ", e.what());
}
}
}

void Plugins::run(string stage, std::filesystem::path path, std::string id, char* argv[]) {
std::string args = path.string() + " " + id;
void Plugins::run(string stage, char* argv[]) {
std::string args;

if (transaction != nullptr)
args.append(transaction->getBindDir().string() + " " + transaction->getSnapshot());

int i = 0;
while (argv != nullptr && argv[i]) {
Expand Down
6 changes: 4 additions & 2 deletions lib/Plugins.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef T_U_PLUGINS_H
#define T_U_PLUGINS_H

#include "Transaction.hpp"
#include <filesystem>
#include <string>
#include <vector>
Expand All @@ -14,11 +15,12 @@ namespace TransactionalUpdate {

class Plugins {
public:
Plugins();
Plugins(TransactionalUpdate::Transaction* transaction);
virtual ~Plugins();
void run(std::string stage, std::string args);
void run(std::string stage, std::filesystem::path path, std::string id, char* argv[]);
void run(std::string stage, char* argv[]);
protected:
TransactionalUpdate::Transaction* transaction;
std::vector<std::filesystem::path> plugins;
};

Expand Down
8 changes: 2 additions & 6 deletions lib/Transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,8 @@ class Transaction {
*/
std::filesystem::path getRoot();

/**
* @brief Return the bindDir path of the snapshot
* @return bindDir path
*
* The bindDir is the live snapshot, that includes auxiliary mounts as /etc or /run.
*/
friend class Plugins;
protected:
std::filesystem::path getBindDir();
private:
class impl;
Expand Down
31 changes: 19 additions & 12 deletions tukit/tukit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,21 @@ int TUKit::parseOptions(int argc, char *argv[]) {
}

int TUKit::processCommand(char *argv[]) {
TransactionalUpdate::Plugins plugins{};
if (argv[0] == nullptr) {
throw invalid_argument{"Missing command. See --help for usage information."};
}
string arg = argv[0];
if (arg == "execute") {
TransactionalUpdate::Transaction transaction{};
TransactionalUpdate::Plugins plugins{&transaction};
if (discardSnapshot) {
transaction.setDiscardIfUnchanged(true);
}
transaction.init(baseSnapshot);
plugins.run(arg + "-pre", transaction.getBindDir(), transaction.getSnapshot(), &argv[1]);
plugins.run(arg + "-pre", &argv[1]);
int status = transaction.execute(&argv[1]); // All remaining arguments
if (status == 0) {
plugins.run(arg + "-post", transaction.getBindDir(), transaction.getSnapshot(), &argv[1]);
plugins.run(arg + "-post", &argv[1]);
transaction.finalize();
} else {
throw runtime_error{"Application returned with exit status " + to_string(status)};
Expand All @@ -156,11 +156,12 @@ int TUKit::processCommand(char *argv[]) {
}
else if (arg == "open") {
TransactionalUpdate::Transaction transaction{};
TransactionalUpdate::Plugins plugins{&transaction};
if (discardSnapshot) {
transaction.setDiscardIfUnchanged(true);
}
transaction.init(baseSnapshot, description);
plugins.run(arg + "-post", transaction.getBindDir(), transaction.getSnapshot(), nullptr);
plugins.run(arg + "-post", nullptr);
cout << "ID: " << transaction.getSnapshot() << endl;
transaction.keep();
return 0;
Expand All @@ -171,10 +172,11 @@ int TUKit::processCommand(char *argv[]) {
throw invalid_argument{"Missing argument for 'call'"};
}
TransactionalUpdate::Transaction transaction{};
TransactionalUpdate::Plugins plugins{&transaction};
transaction.resume(argv[1]);
plugins.run(arg + "-pre", transaction.getBindDir(), argv[1], &argv[2]);
plugins.run(arg + "-pre", &argv[2]);
int status = transaction.execute(&argv[2]); // All remaining arguments
plugins.run(arg + "-post", transaction.getBindDir(), argv[1], &argv[2]);
plugins.run(arg + "-post", &argv[2]);
transaction.keep();
return status;
}
Expand All @@ -184,10 +186,11 @@ int TUKit::processCommand(char *argv[]) {
throw invalid_argument{"Missing argument for 'callext'"};
}
TransactionalUpdate::Transaction transaction{};
TransactionalUpdate::Plugins plugins{&transaction};
transaction.resume(argv[1]);
plugins.run(arg + "-pre", transaction.getBindDir(), argv[1], &argv[2]);
plugins.run(arg + "-pre", &argv[2]);
int status = transaction.callExt(&argv[2]); // All remaining arguments
plugins.run(arg + "-post", transaction.getBindDir(), argv[1], &argv[1]);
plugins.run(arg + "-post", &argv[2]);
transaction.keep();
return status;
}
Expand All @@ -197,8 +200,9 @@ int TUKit::processCommand(char *argv[]) {
throw invalid_argument{"Missing argument for 'close'"};
}
TransactionalUpdate::Transaction transaction{};
TransactionalUpdate::Plugins plugins{&transaction};
transaction.resume(argv[1]);
plugins.run(arg + "-pre", transaction.getBindDir(), argv[1], nullptr);
plugins.run(arg + "-pre", nullptr);
transaction.finalize();
return 0;
}
Expand All @@ -208,8 +212,9 @@ int TUKit::processCommand(char *argv[]) {
throw invalid_argument{"Missing argument for 'abort'"};
}
TransactionalUpdate::Transaction transaction{};
TransactionalUpdate::Plugins plugins{&transaction};
transaction.resume(argv[1]);
plugins.run(arg + "-pre", transaction.getBindDir(), argv[1], nullptr);
plugins.run(arg + "-pre", nullptr);
return 0;
}
else if (arg == "rollback") {
Expand All @@ -218,9 +223,10 @@ int TUKit::processCommand(char *argv[]) {
throw invalid_argument{"Missing argument for 'rollback'"};
}
unique_ptr<TransactionalUpdate::SnapshotManager> snapshotMgr = TransactionalUpdate::SnapshotFactory::get();
plugins.run(arg + "-pre", argv[1]);
TransactionalUpdate::Plugins plugins{nullptr};
plugins.run(arg + "-pre", &argv[1]);
snapshotMgr->rollbackTo(argv[1]);
plugins.run(arg + "-post", argv[1]);
plugins.run(arg + "-post", &argv[1]);
return 0;
}
else if (arg == "snapshots") {
Expand All @@ -244,6 +250,7 @@ int TUKit::processCommand(char *argv[]) {
} else {
method = "auto";
}
TransactionalUpdate::Plugins plugins{nullptr};
plugins.run(arg + "-pre", method);
TransactionalUpdate::Reboot rebootmgr{method};
rebootmgr.reboot();
Expand Down

0 comments on commit 88d51b3

Please sign in to comment.