1 parent
4a92d09e63
commit
45eb2cab42
12 files changed
+991
-162
No files matched your search
+168
-9
@@ -7,12 +7,16 @@
|
||||
// TeamSpeak client, no Weston/RDP, no plugin directory.
|
||||
|
||||
#include "echoion/host/CoreLoader.hpp"
|
||||
#include "echoion/protocol/Identity.hpp"
|
||||
#include "echoion/standalone/StandaloneClientInterface.hpp"
|
||||
#include "echoion/standalone/ReleaseManifest.hpp"
|
||||
|
||||
#include <CoreInterface.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Stacktrace/Stacktrace.hpp>
|
||||
#include <Void/System/FileWatcher.hpp>
|
||||
#include <Void/System/Process.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/TeamSpeak/Identity.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
@@ -30,6 +34,7 @@
|
||||
namespace
|
||||
{
|
||||
std::atomic<bool> g_stop{false};
|
||||
std::atomic<bool> g_updateRequested{false};
|
||||
std::condition_variable g_cv;
|
||||
std::mutex g_mu;
|
||||
|
||||
@@ -39,6 +44,23 @@ namespace
|
||||
g_cv.notify_all();
|
||||
}
|
||||
|
||||
void requestUpdate()
|
||||
{
|
||||
g_updateRequested = true;
|
||||
g_cv.notify_all();
|
||||
}
|
||||
|
||||
std::optional<std::string_view> argumentValue(
|
||||
int argc, char** argv, std::string_view name)
|
||||
{
|
||||
for (int index = 1; index + 1 < argc; ++index) {
|
||||
if (std::string_view{argv[index]} == name) {
|
||||
return argv[index + 1];
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::filesystem::path configDir()
|
||||
{
|
||||
if (const char* x = std::getenv("XDG_CONFIG_HOME")) {
|
||||
@@ -50,6 +72,22 @@ namespace
|
||||
return std::filesystem::path(".") / "echoion";
|
||||
}
|
||||
|
||||
void recordInterruptedRelease(const std::filesystem::path& directory)
|
||||
{
|
||||
const auto transaction = directory / "release-transaction";
|
||||
if (!std::filesystem::exists(transaction) ||
|
||||
std::getenv("ECHOION_REEXEC_VERSION")) return;
|
||||
auto version = Void::FileSystem::read_file(transaction);
|
||||
if (version && !version->empty()) {
|
||||
std::ignore = Void::FileSystem::atomic_write(
|
||||
directory / "failed-release", *version);
|
||||
Void::Log::error("main",
|
||||
"Release {} failed its startup health check; stable service restored",
|
||||
*version);
|
||||
}
|
||||
std::ignore = Void::FileSystem::remove_path(transaction);
|
||||
}
|
||||
|
||||
std::string envOr(const char* key, const std::string& fallback)
|
||||
{
|
||||
const char* v = std::getenv(key);
|
||||
@@ -126,7 +164,7 @@ namespace
|
||||
return 2;
|
||||
}
|
||||
const auto identity =
|
||||
Echoion::Protocol::Identity::importTeamSpeakIni(source->string());
|
||||
Void::TeamSpeak::Identity::importTeamSpeakIni(source->string());
|
||||
if (const auto parent = destination.parent_path(); !parent.empty()) {
|
||||
std::filesystem::create_directories(parent);
|
||||
}
|
||||
@@ -148,8 +186,22 @@ int main(int argc, char** argv)
|
||||
std::cerr << Void::Stacktrace::format_stacktrace(frames) << '\n';
|
||||
std::abort();
|
||||
});
|
||||
if (const auto validationPath = argumentValue(argc, argv,
|
||||
"--validate-release")) {
|
||||
auto manifest = Echoion::Standalone::ReleaseManifest::load(*validationPath);
|
||||
if (!manifest) {
|
||||
std::cerr << manifest.error().message << '\n';
|
||||
return 2;
|
||||
}
|
||||
if (auto result = manifest->validate(); !result) {
|
||||
std::cerr << result.error().message << '\n';
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::filesystem::path cfgDir = configDir();
|
||||
recordInterruptedRelease(cfgDir);
|
||||
std::filesystem::path cfgPath = cfgDir / "config.json";
|
||||
const auto identityPath = std::filesystem::path(envOr(
|
||||
"ECHOION_IDENTITY_PATH", (cfgDir / "identity.txt").string()));
|
||||
@@ -187,6 +239,13 @@ int main(int argc, char** argv)
|
||||
Void::Log::error("main", "failed to load Core: {}", r.error().message);
|
||||
return 1;
|
||||
}
|
||||
if (const auto restorePath = argumentValue(argc, argv, "--restore-state")) {
|
||||
if (auto result = loader.restoreCheckpoint(*restorePath); !result) {
|
||||
Void::Log::error("main", "failed to restore restart checkpoint: {}",
|
||||
result.error().message);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
auto core = loader.getCore();
|
||||
if (!core) {
|
||||
@@ -198,11 +257,13 @@ int main(int argc, char** argv)
|
||||
interface->registerEventCallback([core](const Echoion::Api::PluginEvent& ev) {
|
||||
std::ignore = core->onPluginEvent(ev);
|
||||
});
|
||||
loader.setHotReloadPrepareCallback([interface] {
|
||||
interface->quiesceEventCallbacks();
|
||||
});
|
||||
// Re-register the Core callback after each hot reload (new Core instance).
|
||||
loader.setHotReloadCallback([&loader, interface] {
|
||||
if (auto c = loader.getCore()) {
|
||||
c->setPluginInterface(interface);
|
||||
interface->unregisterEventCallback({});
|
||||
interface->registerEventCallback(
|
||||
[c](const Echoion::Api::PluginEvent& ev) {
|
||||
std::ignore = c->onPluginEvent(ev);
|
||||
@@ -212,21 +273,119 @@ int main(int argc, char** argv)
|
||||
});
|
||||
|
||||
core->triggerAutoConnect();
|
||||
loader.enableAutoReload();
|
||||
std::unique_ptr<Void::FileSystem::FileWatcher> releaseWatcher;
|
||||
std::optional<std::filesystem::path> releaseManifestPath;
|
||||
if (const char* manifest = std::getenv("ECHOION_RELEASE_MANIFEST")) {
|
||||
releaseManifestPath = manifest;
|
||||
releaseWatcher = std::make_unique<Void::FileSystem::FileWatcher>();
|
||||
releaseWatcher->watch(*releaseManifestPath,
|
||||
[](const Void::FileSystem::FileWatcher::Event& event) {
|
||||
if (event.exists) {
|
||||
requestUpdate();
|
||||
}
|
||||
});
|
||||
Void::Log::info("main", "Watching release manifest {}",
|
||||
releaseManifestPath->string());
|
||||
} else {
|
||||
// Compatibility mode for development. Production release bundles use
|
||||
// transactional daemon re-exec instead of replacing Core in isolation.
|
||||
loader.enableAutoReload();
|
||||
}
|
||||
|
||||
std::signal(SIGINT, onSignal);
|
||||
std::signal(SIGTERM, onSignal);
|
||||
Void::Log::info("main", "Echoion standalone running. Ctrl-C to stop.");
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(g_mu);
|
||||
g_cv.wait(lock, [] { return g_stop.load(); });
|
||||
if (const char* version = std::getenv("ECHOION_REEXEC_VERSION")) {
|
||||
std::ignore = Void::FileSystem::atomic_write(
|
||||
cfgDir / "release-health", version);
|
||||
std::ignore = Void::FileSystem::remove_path(
|
||||
cfgDir / "release-transaction");
|
||||
std::ignore = Void::FileSystem::remove_path(cfgDir / "failed-release");
|
||||
::unsetenv("ECHOION_REEXEC_VERSION");
|
||||
Void::Log::info("main", "Release {} passed startup health check", version);
|
||||
}
|
||||
|
||||
Void::Log::info("main", "Shutting down...");
|
||||
std::optional<Echoion::Standalone::ReleaseManifest> pendingRelease;
|
||||
std::filesystem::path checkpointPath;
|
||||
while (!g_stop.load() && !pendingRelease) {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(g_mu);
|
||||
g_cv.wait(lock, [] {
|
||||
return g_stop.load() || g_updateRequested.load();
|
||||
});
|
||||
}
|
||||
if (g_stop.load() || !releaseManifestPath) {
|
||||
break;
|
||||
}
|
||||
g_updateRequested = false;
|
||||
auto manifest = Echoion::Standalone::ReleaseManifest::load(
|
||||
*releaseManifestPath);
|
||||
if (!manifest) {
|
||||
Void::Log::error("main", "release rejected: {}",
|
||||
manifest.error().message);
|
||||
continue;
|
||||
}
|
||||
if (auto failed = Void::FileSystem::read_file(cfgDir / "failed-release");
|
||||
failed && *failed == manifest->version) {
|
||||
Void::Log::error("main",
|
||||
"Release {} was previously rolled back; publish a new version to retry",
|
||||
manifest->version);
|
||||
continue;
|
||||
}
|
||||
auto executable = std::filesystem::canonical("/proc/self/exe");
|
||||
auto validation = Void::Process::run(executable,
|
||||
{"--validate-release", releaseManifestPath->string()}, {},
|
||||
std::chrono::seconds{30});
|
||||
if (!validation || validation->exit_code != 0) {
|
||||
Void::Log::error("main", "release validation failed: {}",
|
||||
validation ? validation->err : validation.error().message());
|
||||
continue;
|
||||
}
|
||||
checkpointPath = configDir() / "restart-state.bin";
|
||||
if (auto checkpoint = loader.checkpoint(checkpointPath); !checkpoint) {
|
||||
Void::Log::error("main", "release checkpoint failed: {}",
|
||||
checkpoint.error().message);
|
||||
continue;
|
||||
}
|
||||
pendingRelease = std::move(*manifest);
|
||||
}
|
||||
|
||||
releaseWatcher.reset();
|
||||
Void::Log::info("main", "{}", pendingRelease
|
||||
? "Applying validated release..." : "Shutting down...");
|
||||
loader.disableAutoReload();
|
||||
(void)interface->disconnectFromServer();
|
||||
interface->quiesceEventCallbacks();
|
||||
(void)loader.unload();
|
||||
(void)interface->shutdown();
|
||||
if (pendingRelease) {
|
||||
if (auto transaction = Void::FileSystem::atomic_write(
|
||||
cfgDir / "release-transaction", pendingRelease->version);
|
||||
!transaction) {
|
||||
Void::Log::error("main", "cannot record release transaction: {}",
|
||||
transaction.error().message());
|
||||
return 1;
|
||||
}
|
||||
::setenv("ECHOION_CORE_PATH", pendingRelease->core.c_str(), 1);
|
||||
::setenv("ECHOION_REEXEC_VERSION", pendingRelease->version.c_str(), 1);
|
||||
std::vector<std::string> arguments;
|
||||
arguments.reserve(static_cast<std::size_t>(argc) + 2);
|
||||
for (int index = 1; index < argc; ++index) {
|
||||
const std::string_view argument{argv[index]};
|
||||
if (argument == "--restore-state") {
|
||||
++index;
|
||||
continue;
|
||||
}
|
||||
arguments.emplace_back(argument);
|
||||
}
|
||||
arguments.emplace_back("--restore-state");
|
||||
arguments.push_back(checkpointPath.string());
|
||||
if (auto result = Void::Process::reexec(pendingRelease->executable,
|
||||
arguments); !result) {
|
||||
Void::Log::error("main", "release re-exec failed: {}",
|
||||
result.error().message());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user