Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-15 19:50:44 +03:00
1 parent 45eb2cab42
commit 8aa968eb9f
3 files changed
+922 -616

No files matched your search

+67 -61
View File
@@ -1,21 +1,16 @@
// Standalone Echoion host.
//
// Replaces the TeamSpeak-client + plugin sandwich: loads the same hot-reloadable
// Core shared library via the (TeamSpeak-independent) CoreLoader, but backs
// IPluginInterface with a native TeamSpeak UDP protocol client instead of the
// TS3 Client Plugin SDK. The bot therefore runs as a plain daemon — no
// TeamSpeak client, no Weston/RDP, no plugin directory.
// Standalone Echoion host. It loads the hot-reloadable Core through CoreLoader
// and adapts Echoion's host contract to VoidTeamspeak's typed client API.
#include "echoion/host/CoreLoader.hpp"
#include "echoion/standalone/StandaloneClientInterface.hpp"
#include "echoion/standalone/ReleaseManifest.hpp"
#include "echoion/standalone/StandaloneClientInterface.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/System/Process.hpp>
#include <Void/TeamSpeak/Identity.hpp>
#include <atomic>
@@ -50,8 +45,8 @@ namespace
g_cv.notify_all();
}
std::optional<std::string_view> argumentValue(
int argc, char** argv, std::string_view name)
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) {
@@ -76,14 +71,17 @@ namespace
{
const auto transaction = directory / "release-transaction";
if (!std::filesystem::exists(transaction) ||
std::getenv("ECHOION_REEXEC_VERSION")) return;
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);
"Release {} failed its startup health check; "
"stable service restored",
*version);
}
std::ignore = Void::FileSystem::remove_path(transaction);
}
@@ -132,10 +130,9 @@ namespace
for (int i = 1; i < argc; ++i) {
const std::string_view argument{argv[i]};
if (argument == "--help") {
std::cout
<< "Usage: echoiond [--config PATH]\n"
" echoiond --import-identity EXPORT.ini "
"[--identity-path PATH] [--force]\n";
std::cout << "Usage: echoiond [--config PATH]\n"
" echoiond --import-identity EXPORT.ini "
"[--identity-path PATH] [--force]\n";
return 0;
}
if (argument == "--import-identity") {
@@ -165,7 +162,8 @@ namespace
}
const auto identity =
Void::TeamSpeak::Identity::importTeamSpeakIni(source->string());
if (const auto parent = destination.parent_path(); !parent.empty()) {
if (const auto parent = destination.parent_path();
!parent.empty()) {
std::filesystem::create_directories(parent);
}
identity.save(destination.string());
@@ -186,9 +184,10 @@ 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 (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;
@@ -203,8 +202,8 @@ int main(int argc, char** argv)
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()));
const auto identityPath = std::filesystem::path(
envOr("ECHOION_IDENTITY_PATH", (cfgDir / "identity.txt").string()));
if (const auto importResult = importIdentity(argc, argv, identityPath);
importResult >= 0) {
return importResult;
@@ -222,8 +221,9 @@ int main(int argc, char** argv)
settings.securityLevel = envInt("ECHOION_SECURITY_LEVEL", 8);
settings.opusBitrate = envInt("ECHOION_OPUS_BITRATE", 96000);
auto interface = std::make_shared<Echoion::Standalone::StandaloneClientInterface>(
settings);
auto interface =
std::make_shared<Echoion::Standalone::StandaloneClientInterface>(
settings);
if (auto r = interface->initialize(); !r) {
Void::Log::error("main", "interface init failed: {}",
r.error().message);
@@ -242,7 +242,7 @@ int main(int argc, char** argv)
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);
result.error().message);
return 1;
}
}
@@ -254,12 +254,12 @@ int main(int argc, char** argv)
}
core->setPluginInterface(interface);
interface->registerEventCallback([core](const Echoion::Api::PluginEvent& ev) {
std::ignore = core->onPluginEvent(ev);
});
loader.setHotReloadPrepareCallback([interface] {
interface->quiesceEventCallbacks();
});
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()) {
@@ -278,14 +278,15 @@ int main(int argc, char** argv)
if (const char* manifest = std::getenv("ECHOION_RELEASE_MANIFEST")) {
releaseManifestPath = manifest;
releaseWatcher = std::make_unique<Void::FileSystem::FileWatcher>();
releaseWatcher->watch(*releaseManifestPath,
releaseWatcher->watch(
*releaseManifestPath,
[](const Void::FileSystem::FileWatcher::Event& event) {
if (event.exists) {
requestUpdate();
}
});
Void::Log::info("main", "Watching release manifest {}",
releaseManifestPath->string());
releaseManifestPath->string());
} else {
// Compatibility mode for development. Production release bundles use
// transactional daemon re-exec instead of replacing Core in isolation.
@@ -296,13 +297,14 @@ int main(int argc, char** argv)
std::signal(SIGTERM, onSignal);
Void::Log::info("main", "Echoion standalone running. Ctrl-C to stop.");
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::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", "Release {} passed startup health check",
version);
}
std::optional<Echoion::Standalone::ReleaseManifest> pendingRelease;
@@ -310,49 +312,52 @@ int main(int argc, char** argv)
while (!g_stop.load() && !pendingRelease) {
{
std::unique_lock<std::mutex> lock(g_mu);
g_cv.wait(lock, [] {
return g_stop.load() || g_updateRequested.load();
});
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);
auto manifest =
Echoion::Standalone::ReleaseManifest::load(*releaseManifestPath);
if (!manifest) {
Void::Log::error("main", "release rejected: {}",
manifest.error().message);
manifest.error().message);
continue;
}
if (auto failed = Void::FileSystem::read_file(cfgDir / "failed-release");
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);
"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});
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());
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);
checkpoint.error().message);
continue;
}
pendingRelease = std::move(*manifest);
}
releaseWatcher.reset();
Void::Log::info("main", "{}", pendingRelease
? "Applying validated release..." : "Shutting down...");
Void::Log::info("main", "{}",
pendingRelease ? "Applying validated release..."
: "Shutting down...");
loader.disableAutoReload();
(void)interface->disconnectFromServer();
interface->quiesceEventCallbacks();
@@ -360,10 +365,10 @@ int main(int argc, char** argv)
(void)interface->shutdown();
if (pendingRelease) {
if (auto transaction = Void::FileSystem::atomic_write(
cfgDir / "release-transaction", pendingRelease->version);
cfgDir / "release-transaction", pendingRelease->version);
!transaction) {
Void::Log::error("main", "cannot record release transaction: {}",
transaction.error().message());
transaction.error().message());
return 1;
}
::setenv("ECHOION_CORE_PATH", pendingRelease->core.c_str(), 1);
@@ -380,10 +385,11 @@ int main(int argc, char** argv)
}
arguments.emplace_back("--restore-state");
arguments.push_back(checkpointPath.string());
if (auto result = Void::Process::reexec(pendingRelease->executable,
arguments); !result) {
if (auto result =
Void::Process::reexec(pendingRelease->executable, arguments);
!result) {
Void::Log::error("main", "release re-exec failed: {}",
result.error().message());
result.error().message());
return 1;
}
}