Skip to content
This repository was archived by the owner on Apr 16, 2026. It is now read-only.

Commit 7b25fdb

Browse files
committed
Use Singleton<T> class for the runner
1 parent 6d3565c commit 7b25fdb

10 files changed

Lines changed: 72 additions & 61 deletions

File tree

include/cloysterhpc/functions.h

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717

1818
namespace cloyster {
1919

20+
template <typename B, typename T>
21+
std::unique_ptr<B> makeUniqueDerived()
22+
{
23+
return static_cast<std::unique_ptr<B>>(std::make_unique<T>());
24+
}
25+
26+
//
27+
// Globals
28+
extern bool dryRun;
29+
30+
2031
template <typename T>
2132
class Singleton {
2233
// Private constructor to prevent direct instantiation
@@ -32,12 +43,20 @@ class Singleton {
3243
Singleton& operator=(const Singleton&) = delete;
3344
~Singleton() = delete;
3445

35-
static void init(std::unique_ptr<T> value) {
46+
static void init(std::unique_ptr<T> value)
47+
{
3648
std::call_once(initFlag, [&](){
3749
instance = std::move(value);
3850
});
3951
}
4052

53+
static void init(const auto& factory)
54+
{
55+
std::call_once(initFlag, [&](){
56+
instance = std::move(factory());
57+
});
58+
}
59+
4160
static gsl::not_null<T*> get() {
4261
if (!instance) {
4362
throw std::runtime_error("Singleton read before initialization");
@@ -52,12 +71,24 @@ std::unique_ptr<T> Singleton<T>::instance = nullptr;
5271
template <typename T>
5372
std::once_flag Singleton<T>::initFlag;
5473

55-
// Globals
56-
extern bool dryRun;
74+
using cloyster::services::BaseRunner;
75+
using cloyster::models::OS;
5776

58-
using OS = cloyster::models::OS;
77+
template <typename T>
78+
[[deprecated("WIP")]]
79+
gsl::not_null<T*>
80+
getSingleton(auto func)
81+
{
82+
static std::once_flag once;
83+
static std::unique_ptr<T> instance;
84+
85+
std::call_once(once, [&](){
86+
instance = std::move(func());
87+
});
88+
89+
return gsl::not_null<T*>(instance.get());
90+
}
5991

60-
std::shared_ptr<cloyster::services::BaseRunner> getRunner();
6192
std::shared_ptr<cloyster::services::repos::RepoManager> getRepoManager(
6293
const OS& osinfo);
6394

include/cloysterhpc/mailsystem/postfix.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#ifndef CLOYSTERHPC_POSTFIX_H_
77
#define CLOYSTERHPC_POSTFIX_H_
88

9-
#include <cloysterhpc/services/runner.h>
109
#include <cstdint>
1110
#include <filesystem>
1211
#include <optional>
@@ -21,7 +20,6 @@ class Postfix : public IService {
2120
enum class Profile { Local, Relay, SASL };
2221

2322
private:
24-
cloyster::services::BaseRunner& m_runner;
2523
Profile m_profile;
2624
std::optional<std::string> m_hostname {};
2725
std::optional<std::string> m_domain {};
@@ -75,8 +73,7 @@ class Postfix : public IService {
7573

7674
void setup(const std::filesystem::path& basedir = "/etc/postfix");
7775

78-
explicit Postfix(std::shared_ptr<MessageBus> bus,
79-
cloyster::services::BaseRunner& runner, Profile profile);
76+
explicit Postfix(std::shared_ptr<MessageBus> bus, Profile profile);
8077
};
8178

8279
#endif // CLOYSTERHPC_POSTFIX_H_

include/cloysterhpc/models/cluster.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class Cluster {
188188

189189
std::optional<Postfix>& getMailSystem();
190190
void setMailSystem(
191-
Postfix::Profile profile, std::shared_ptr<services::BaseRunner> runner);
191+
Postfix::Profile profile);
192192

193193
const DiskImage& getDiskImage() const;
194194
void setDiskImage(const std::filesystem::path& diskImagePath);

src/functions.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626

2727
namespace cloyster {
2828

29-
using cloyster::services::BaseRunner;
30-
using cloyster::services::DryRunner;
31-
using cloyster::services::Runner;
32-
33-
3429
namespace {
3530
std::tuple<bool, std::optional<std::string>> retrieveLine(
3631
boost::process::ipstream& pipe_stream,
@@ -42,29 +37,8 @@ namespace {
4237

4338
return make_tuple(pipe_stream.good(), std::nullopt);
4439
}
45-
46-
std::shared_ptr<BaseRunner> makeRunner(const bool dryRun)
47-
{
48-
if (dryRun) {
49-
return std::make_shared<DryRunner>();
50-
}
51-
52-
return std::make_shared<Runner>();
53-
}
54-
5540
} // anonymous namespace
5641

57-
std::shared_ptr<BaseRunner> getRunner()
58-
{
59-
static std::optional<std::shared_ptr<BaseRunner>> runner = std::nullopt;
60-
if (!runner) {
61-
runner = makeRunner(cloyster::dryRun);
62-
}
63-
64-
return runner.value();
65-
}
66-
67-
using cloyster::models::Cluster;
6842
using cloyster::services::repos::RepoManager;
6943

7044
std::shared_ptr<RepoManager> getRepoManager(const OS& osinfo)

src/mailsystem/postfix.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ using cloyster::runCommand;
1717
using cloyster::services::BaseRunner;
1818

1919
Postfix::Postfix(
20-
std::shared_ptr<MessageBus> bus, BaseRunner& runner, Profile profile)
20+
std::shared_ptr<MessageBus> bus, Profile profile)
2121
: IService(bus, "postfix.service")
22-
, m_runner(runner)
2322
, m_profile(profile)
2423
{
2524
}
@@ -118,7 +117,8 @@ void Postfix::setKeyFile(const std::optional<std::filesystem::path>& key_file)
118117
void Postfix::install()
119118
{
120119
LOG_INFO("Installing Postfix");
121-
m_runner.executeCommand("dnf -y install postfix");
120+
cloyster::Singleton<BaseRunner>::get()
121+
->executeCommand("dnf -y install postfix");
122122
}
123123

124124
static void maybeDisableLocalOnMasterFile(std::string& line)
@@ -209,7 +209,7 @@ void Postfix::createFiles(const std::filesystem::path& basedir)
209209

210210
if (!std::filesystem::exists(basedir / "transport.db")) {
211211
auto transport = basedir / "transport";
212-
m_runner.executeCommand(
212+
cloyster::Singleton<BaseRunner>::get()->executeCommand(
213213
fmt::format("postmap hash:{}", transport.string()));
214214
}
215215

@@ -259,7 +259,8 @@ void Postfix::configureSASL(const std::filesystem::path& basedir)
259259
std::filesystem::perm_options::add);
260260

261261
auto passwordFile = basedir / "sasl_password";
262-
m_runner.executeCommand(fmt::format("postmap {}", passwordFile.string()));
262+
cloyster::Singleton<BaseRunner>::get()
263+
->executeCommand(fmt::format("postmap {}", passwordFile.string()));
263264

264265
std::filesystem::permissions(dbFilename,
265266
std::filesystem::perms::owner_write

src/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ int main(int argc, const char** argv)
194194
using cloyster::models::Cluster;
195195
cloyster::Singleton<Cluster>::init(std::move(model));
196196

197+
cloyster::Singleton<cloyster::services::BaseRunner>::init([](){
198+
using cloyster::services::BaseRunner;
199+
using cloyster::services::DryRunner;
200+
using cloyster::services::Runner;
201+
202+
if (cloyster::dryRun) {
203+
return cloyster::makeUniqueDerived<BaseRunner, DryRunner>();
204+
}
205+
206+
return cloyster::makeUniqueDerived<BaseRunner, Runner>();
207+
});
208+
197209
std::unique_ptr<Execution> executionEngine
198210
= std::make_unique<cloyster::services::Shell>();
199211

src/models/cluster.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ std::shared_ptr<DBusClient> Cluster::getDaemonBus() { return m_systemdBus; }
5959
Headnode& Cluster::getHeadnode() { return m_headnode; }
6060

6161
const Headnode& Cluster::getHeadnode() const { return m_headnode; }
62-
// template <typename Runner>
63-
// std::unique_ptr<Runner> Cluster<Runner>::getRunner() const
64-
//{
65-
// return m_runner;
66-
// }
6762

6863
std::string_view Cluster::getName() const { return m_name; }
6964

@@ -241,9 +236,9 @@ void Cluster::setQueueSystem(QueueSystem::Kind kind)
241236
std::optional<Postfix>& Cluster::getMailSystem() { return m_mailSystem; }
242237

243238
void Cluster::setMailSystem(
244-
Postfix::Profile profile, std::shared_ptr<BaseRunner> runner)
239+
Postfix::Profile profile)
245240
{
246-
m_mailSystem.emplace(m_systemdBus, *runner, profile);
241+
m_mailSystem.emplace(m_systemdBus, profile);
247242
}
248243

249244
const DiskImage& Cluster::getDiskImage() const { return m_diskImage; }
@@ -893,7 +888,7 @@ void Cluster::fillData(const std::filesystem::path& answerfilePath)
893888
}
894889

895890
if (answerfil.postfix.enabled) {
896-
setMailSystem(answerfil.postfix.profile, cloyster::getRunner());
891+
setMailSystem(answerfil.postfix.profile);
897892
m_mailSystem->setHostname(this->m_headnode.getHostname());
898893
m_mailSystem->setDomain(getDomainName());
899894
m_mailSystem->setDestination(answerfil.postfix.destination);

src/ofed.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ std::string headnodeDistroName()
5050

5151
void installMellanoxDoca(cloyster::services::repos::RepoManager& repoManager, const OFED& ofed)
5252
{
53-
auto runner = cloyster::getRunner();
53+
auto runner = cloyster::Singleton<cloyster::services::BaseRunner>::get();
5454

5555
if (runner->executeCommand("modprobe mlx5_core") == 0) {
5656
LOG_WARN("mlx5_core module loaded, skiping DOCA setup");

src/presenter/PresenterMailSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PresenterMailSystem::PresenterMailSystem(
2121
magic_enum::enum_names<Postfix::Profile>(),
2222
Messages::Profile::help))
2323
.value();
24-
m_model->setMailSystem(mailSystemProfile, cloyster::getRunner());
24+
m_model->setMailSystem(mailSystemProfile);
2525
auto mailSystem = m_model->getMailSystem().value();
2626

2727
LOG_DEBUG("Enabled Postfix with profile: {}",

src/services/xcat.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
namespace {
2323
using cloyster::models::Cluster;
2424

25-
inline auto cluster()
26-
{
27-
return cloyster::Singleton<Cluster>::get();
28-
}
25+
inline auto cluster() { return cloyster::Singleton<Cluster>::get(); }
2926

3027
}
3128

@@ -79,7 +76,8 @@ void XCAT::patchInstall()
7976

8077
void XCAT::setup()
8178
{
82-
setDHCPInterfaces(cluster()->getHeadnode()
79+
setDHCPInterfaces(cluster()
80+
->getHeadnode()
8381
.getConnection(Network::Profile::Management)
8482
.getInterface()
8583
.value());
@@ -171,7 +169,8 @@ void XCAT::configureTimeService()
171169

172170
m_stateless.postinstall.emplace_back(fmt::format(
173171
"echo \"server {} iburst\" >> $IMG_ROOTIMGDIR/etc/chrony.conf\n\n",
174-
cluster()->getHeadnode()
172+
cluster()
173+
->getHeadnode()
175174
.getConnection(Network::Profile::Management)
176175
.getAddress()
177176
.to_string()));
@@ -207,7 +206,8 @@ void XCAT::configureSLURM()
207206
m_stateless.postinstall.emplace_back(
208207
fmt::format("echo SLURMD_OPTIONS=\\\"--conf-server {}\\\" > "
209208
"$IMG_ROOTIMGDIR/etc/sysconfig/slurmd\n\n",
210-
cluster()->getHeadnode()
209+
cluster()
210+
->getHeadnode()
211211
.getConnection(Network::Profile::Management)
212212
.getAddress()
213213
.to_string()));
@@ -259,7 +259,8 @@ void XCAT::generatePostinstallFile()
259259
"{0}:/home /home nfs nfsvers=3,nodev,nosuid 0 0\n"
260260
"{0}:/opt/ohpc/pub /opt/ohpc/pub nfs nfsvers=3,nodev 0 0\n"
261261
"END\n\n",
262-
cluster()->getHeadnode()
262+
cluster()
263+
->getHeadnode()
263264
.getConnection(Network::Profile::Management)
264265
.getAddress()
265266
.to_string()));
@@ -303,7 +304,7 @@ void XCAT::generateSynclistsFile()
303304

304305
void XCAT::configureOSImageDefinition()
305306
{
306-
auto runner = getRunner();
307+
auto runner = cloyster::Singleton<BaseRunner>::get();
307308
runner->executeCommand(
308309
fmt::format("chdef -t osimage {} --plus otherpkglist="
309310
"/install/custom/netboot/compute.otherpkglist",
@@ -618,7 +619,7 @@ void XCAT::installRepositories()
618619
const std::filesystem::path& repofileDest
619620
= std::filesystem::temp_directory_path();
620621
LOG_INFO("Setting up XCAT repositories");
621-
auto runner = cloyster::getRunner();
622+
auto runner = cloyster::Singleton<BaseRunner>::get();
622623

623624
runner->downloadFile("https://xcat.org/files/xcat/repos/yum/devel/"
624625
"core-snap/xcat-core.repo",

0 commit comments

Comments
 (0)