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 May 27, 2024
1 parent c374ee9 commit e4d8011
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
10 changes: 7 additions & 3 deletions lib/Plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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"};
Expand All @@ -40,6 +40,7 @@ Plugins::Plugins() {
if (!(filesystem::is_regular_file(path) && (access(path.c_str(), X_OK) == 0)))
continue;

tulog.info("Regiter plugin ", path);
plugins.push_back(path);
plugins_set.insert(filename);
}
Expand Down Expand Up @@ -68,8 +69,11 @@ void Plugins::run(string stage, string args) {
}
}

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 e4d8011

Please sign in to comment.