Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
870a803bd2 | ||
|
|
a2c6dd4c2c |
No files matched your search
+1
-1
Submodule EchoionApi updated: caac928923...3d61e6952f.
+1
-1
Submodule EchoionCore updated: dfbbdaaf01...a277f2102b.
@@ -5,10 +5,27 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
namespace Echoion::Standalone
|
namespace Echoion::Standalone
|
||||||
{
|
{
|
||||||
|
struct ManagedCoreStartupSelection {
|
||||||
|
std::filesystem::path corePath;
|
||||||
|
std::optional<std::filesystem::path> archivedStore;
|
||||||
|
bool runtimeBaselineChanged = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Installed runtime releases own their bundled Core baseline. A runtime
|
||||||
|
// version change archives the previous managed store; Core-only updates
|
||||||
|
// remain active across ordinary restarts of the same runtime.
|
||||||
|
[[nodiscard]] Api::Result<ManagedCoreStartupSelection>
|
||||||
|
selectManagedCoreForStartup(
|
||||||
|
const std::filesystem::path& stateDirectory,
|
||||||
|
const std::filesystem::path& bundledCore,
|
||||||
|
std::optional<std::string_view> installedRuntimeVersion);
|
||||||
|
|
||||||
// Roll back any uncommitted managed-plugin activation before Core loads and
|
// Roll back any uncommitted managed-plugin activation before Core loads and
|
||||||
// discovers plugin `current` links. This must run before PluginHost
|
// discovers plugin `current` links. This must run before PluginHost
|
||||||
// startup.
|
// startup.
|
||||||
|
|||||||
@@ -3162,6 +3162,207 @@ namespace Echoion::Standalone
|
|||||||
return impl_->handle(method, requestJson);
|
return impl_->handle(method, requestJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Api::Result<ManagedCoreStartupSelection>
|
||||||
|
selectManagedCoreForStartup(const std::filesystem::path& stateDirectory,
|
||||||
|
const std::filesystem::path& bundledCore,
|
||||||
|
const std::optional<std::string_view>
|
||||||
|
installedRuntimeVersion)
|
||||||
|
{
|
||||||
|
const auto components = stateDirectory / "updates" / "components";
|
||||||
|
const auto storeRoot = components / "core";
|
||||||
|
const auto markerPath = components / "core-runtime-version";
|
||||||
|
|
||||||
|
const auto selectCurrent =
|
||||||
|
[&]() -> Api::Result<ManagedCoreStartupSelection> {
|
||||||
|
Void::Update::VersionStore store(storeRoot);
|
||||||
|
if (auto initialized = store.initialize(); !initialized) {
|
||||||
|
return Api::fail(Api::ErrorCode::UnknownError,
|
||||||
|
initialized.error().message());
|
||||||
|
}
|
||||||
|
if (auto reconciled = store.reconcile(); !reconciled) {
|
||||||
|
return Api::fail(Api::ErrorCode::UnknownError,
|
||||||
|
reconciled.error().message());
|
||||||
|
}
|
||||||
|
auto current = store.currentVersion();
|
||||||
|
if (!current) {
|
||||||
|
return Api::fail(Api::ErrorCode::UnknownError,
|
||||||
|
current.error().message());
|
||||||
|
}
|
||||||
|
if (!*current) {
|
||||||
|
return ManagedCoreStartupSelection{
|
||||||
|
.corePath = bundledCore,
|
||||||
|
.archivedStore = std::nullopt,
|
||||||
|
.runtimeBaselineChanged = false};
|
||||||
|
}
|
||||||
|
const auto managed =
|
||||||
|
store.versionPath(**current) / bundledCore.filename();
|
||||||
|
std::error_code error;
|
||||||
|
const auto status = std::filesystem::symlink_status(managed, error);
|
||||||
|
if (!error && std::filesystem::is_regular_file(status) &&
|
||||||
|
!std::filesystem::is_symlink(status)) {
|
||||||
|
return ManagedCoreStartupSelection{
|
||||||
|
.corePath = managed,
|
||||||
|
.archivedStore = std::nullopt,
|
||||||
|
.runtimeBaselineChanged = false};
|
||||||
|
}
|
||||||
|
if (error != std::errc::no_such_file_or_directory && error) {
|
||||||
|
return Api::fail(
|
||||||
|
Api::ErrorCode::PermissionDenied,
|
||||||
|
std::format("cannot inspect managed Core '{}': {}",
|
||||||
|
managed.string(), error.message()));
|
||||||
|
}
|
||||||
|
return ManagedCoreStartupSelection{.corePath = bundledCore,
|
||||||
|
.archivedStore = std::nullopt,
|
||||||
|
.runtimeBaselineChanged = false};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Development trees do not have a release manifest and retain the
|
||||||
|
// historical behavior: the current managed Core, if any, wins.
|
||||||
|
if (!installedRuntimeVersion) {
|
||||||
|
return selectCurrent();
|
||||||
|
}
|
||||||
|
if (installedRuntimeVersion->empty() ||
|
||||||
|
!safeIdentifier(*installedRuntimeVersion)) {
|
||||||
|
return Api::fail(Api::ErrorCode::InvalidArgument,
|
||||||
|
"installed runtime version is not a safe "
|
||||||
|
"identifier");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::error_code error;
|
||||||
|
std::filesystem::create_directories(components, error);
|
||||||
|
if (error) {
|
||||||
|
return Api::fail(
|
||||||
|
Api::ErrorCode::PermissionDenied,
|
||||||
|
std::format("cannot prepare managed Core directory '{}': {}",
|
||||||
|
components.string(), error.message()));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::string> recordedRuntime;
|
||||||
|
const auto markerStatus =
|
||||||
|
std::filesystem::symlink_status(markerPath, error);
|
||||||
|
if (!error &&
|
||||||
|
markerStatus.type() != std::filesystem::file_type::not_found) {
|
||||||
|
if (!std::filesystem::is_regular_file(markerStatus) ||
|
||||||
|
std::filesystem::is_symlink(markerStatus)) {
|
||||||
|
return Api::fail(
|
||||||
|
Api::ErrorCode::PermissionDenied,
|
||||||
|
"managed Core runtime marker must be a regular file");
|
||||||
|
}
|
||||||
|
auto marker = Void::FileSystem::read_file(markerPath);
|
||||||
|
if (!marker) {
|
||||||
|
return Api::fail(Api::ErrorCode::PermissionDenied,
|
||||||
|
marker.error().message());
|
||||||
|
}
|
||||||
|
while (!marker->empty() &&
|
||||||
|
(marker->back() == '\n' || marker->back() == '\r')) {
|
||||||
|
marker->pop_back();
|
||||||
|
}
|
||||||
|
if (!marker->empty() && !safeIdentifier(*marker)) {
|
||||||
|
return Api::fail(Api::ErrorCode::ParseError,
|
||||||
|
"managed Core runtime marker is invalid");
|
||||||
|
}
|
||||||
|
recordedRuntime = std::move(*marker);
|
||||||
|
} else if (error != std::errc::no_such_file_or_directory && error) {
|
||||||
|
return Api::fail(
|
||||||
|
Api::ErrorCode::PermissionDenied,
|
||||||
|
std::format("cannot inspect managed Core runtime marker: {}",
|
||||||
|
error.message()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recordedRuntime && *recordedRuntime == *installedRuntimeVersion) {
|
||||||
|
return selectCurrent();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<std::filesystem::path> archivedStore;
|
||||||
|
error.clear();
|
||||||
|
const auto storeStatus =
|
||||||
|
std::filesystem::symlink_status(storeRoot, error);
|
||||||
|
if (!error &&
|
||||||
|
storeStatus.type() != std::filesystem::file_type::not_found) {
|
||||||
|
if (!std::filesystem::is_directory(storeStatus) ||
|
||||||
|
std::filesystem::is_symlink(storeStatus)) {
|
||||||
|
return Api::fail(Api::ErrorCode::PermissionDenied,
|
||||||
|
"managed Core store must be a real directory");
|
||||||
|
}
|
||||||
|
const auto archiveRoot = components / "core-archives";
|
||||||
|
std::filesystem::create_directories(archiveRoot, error);
|
||||||
|
if (error) {
|
||||||
|
return Api::fail(
|
||||||
|
Api::ErrorCode::PermissionDenied,
|
||||||
|
std::format("cannot prepare managed Core archive: {}",
|
||||||
|
error.message()));
|
||||||
|
}
|
||||||
|
const auto archiveStatus =
|
||||||
|
std::filesystem::symlink_status(archiveRoot, error);
|
||||||
|
if (error || !std::filesystem::is_directory(archiveStatus) ||
|
||||||
|
std::filesystem::is_symlink(archiveStatus)) {
|
||||||
|
return Api::fail(
|
||||||
|
Api::ErrorCode::PermissionDenied,
|
||||||
|
"managed Core archive root must be a real directory");
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto label =
|
||||||
|
recordedRuntime && safeIdentifier(*recordedRuntime)
|
||||||
|
? *recordedRuntime
|
||||||
|
: std::string{"legacy"};
|
||||||
|
for (std::uint32_t suffix = 0; suffix < 10000; ++suffix) {
|
||||||
|
auto candidate =
|
||||||
|
archiveRoot /
|
||||||
|
(suffix == 0 ? label : std::format("{}.{}", label, suffix));
|
||||||
|
error.clear();
|
||||||
|
const auto candidateStatus =
|
||||||
|
std::filesystem::symlink_status(candidate, error);
|
||||||
|
if (error == std::errc::no_such_file_or_directory ||
|
||||||
|
candidateStatus.type() ==
|
||||||
|
std::filesystem::file_type::not_found) {
|
||||||
|
error.clear();
|
||||||
|
std::filesystem::rename(storeRoot, candidate, error);
|
||||||
|
if (error) {
|
||||||
|
return Api::fail(
|
||||||
|
Api::ErrorCode::PermissionDenied,
|
||||||
|
std::format(
|
||||||
|
"cannot archive the previous managed Core "
|
||||||
|
"store: {}",
|
||||||
|
error.message()));
|
||||||
|
}
|
||||||
|
archivedStore = std::move(candidate);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (error) {
|
||||||
|
return Api::fail(
|
||||||
|
Api::ErrorCode::PermissionDenied,
|
||||||
|
std::format("cannot inspect managed Core archive: {}",
|
||||||
|
error.message()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!archivedStore) {
|
||||||
|
return Api::fail(Api::ErrorCode::AlreadyExists,
|
||||||
|
"managed Core archive namespace is exhausted");
|
||||||
|
}
|
||||||
|
} else if (error != std::errc::no_such_file_or_directory && error) {
|
||||||
|
return Api::fail(
|
||||||
|
Api::ErrorCode::PermissionDenied,
|
||||||
|
std::format("cannot inspect managed Core store: {}",
|
||||||
|
error.message()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Update::VersionStore freshStore(storeRoot);
|
||||||
|
if (auto initialized = freshStore.initialize(); !initialized) {
|
||||||
|
return Api::fail(Api::ErrorCode::UnknownError,
|
||||||
|
initialized.error().message());
|
||||||
|
}
|
||||||
|
if (auto written = Void::FileSystem::atomic_write(
|
||||||
|
markerPath, std::string{*installedRuntimeVersion});
|
||||||
|
!written) {
|
||||||
|
return Api::fail(Api::ErrorCode::PermissionDenied,
|
||||||
|
written.error().message());
|
||||||
|
}
|
||||||
|
return ManagedCoreStartupSelection{.corePath = bundledCore,
|
||||||
|
.archivedStore =
|
||||||
|
std::move(archivedStore),
|
||||||
|
.runtimeBaselineChanged = true};
|
||||||
|
}
|
||||||
|
|
||||||
Api::Result<void> reconcileManagedPluginUpdates(
|
Api::Result<void> reconcileManagedPluginUpdates(
|
||||||
const std::filesystem::path& stateDirectory)
|
const std::filesystem::path& stateDirectory)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
#include <Void/System/Process.hpp>
|
#include <Void/System/Process.hpp>
|
||||||
#include <Void/System/SignalWatcher.hpp>
|
#include <Void/System/SignalWatcher.hpp>
|
||||||
#include <Void/TeamSpeak/Identity.hpp>
|
#include <Void/TeamSpeak/Identity.hpp>
|
||||||
#include <Void/Update/Update.hpp>
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
@@ -245,30 +244,6 @@ namespace
|
|||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] std::filesystem::path
|
|
||||||
reconcileManagedCore(const std::filesystem::path& stateDirectory,
|
|
||||||
const std::filesystem::path& fallback)
|
|
||||||
{
|
|
||||||
Void::Update::VersionStore store(stateDirectory / "updates" /
|
|
||||||
"components" / "core");
|
|
||||||
if (auto initialized = store.initialize(); !initialized) {
|
|
||||||
Void::Log::warn("main", "Cannot initialize managed Core store: {}",
|
|
||||||
initialized.error().message());
|
|
||||||
return fallback;
|
|
||||||
}
|
|
||||||
if (auto reconciled = store.reconcile(); !reconciled) {
|
|
||||||
Void::Log::warn("main", "Cannot reconcile managed Core store: {}",
|
|
||||||
reconciled.error().message());
|
|
||||||
return fallback;
|
|
||||||
}
|
|
||||||
auto current = store.currentVersion();
|
|
||||||
if (!current || !*current) {
|
|
||||||
return fallback;
|
|
||||||
}
|
|
||||||
const auto managed = store.versionPath(**current) / fallback.filename();
|
|
||||||
return std::filesystem::is_regular_file(managed) ? managed : fallback;
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] Echoion::Api::Result<std::string>
|
[[nodiscard]] Echoion::Api::Result<std::string>
|
||||||
invokeRuntimeHelper(const std::filesystem::path& helper,
|
invokeRuntimeHelper(const std::filesystem::path& helper,
|
||||||
const std::filesystem::path& root,
|
const std::filesystem::path& root,
|
||||||
@@ -661,10 +636,38 @@ int main(int argc, char** argv)
|
|||||||
configuredCoreOverride &&
|
configuredCoreOverride &&
|
||||||
absolutePath(*configuredCoreOverride) !=
|
absolutePath(*configuredCoreOverride) !=
|
||||||
absolutePath(installRoot / "lib" / "Echoion.ts");
|
absolutePath(installRoot / "lib" / "Echoion.ts");
|
||||||
const auto corePathResolved =
|
auto corePathResolved = configuredCorePath;
|
||||||
customCoreOverride
|
if (!customCoreOverride) {
|
||||||
? configuredCorePath
|
const auto installedRuntimeVersion =
|
||||||
: reconcileManagedCore(stateDirectory, configuredCorePath);
|
runningManifest ? std::optional<std::string_view>{runtimeVersion}
|
||||||
|
: std::nullopt;
|
||||||
|
auto selection = Echoion::Standalone::selectManagedCoreForStartup(
|
||||||
|
stateDirectory, configuredCorePath, installedRuntimeVersion);
|
||||||
|
if (!selection) {
|
||||||
|
Void::Log::warn(
|
||||||
|
"main",
|
||||||
|
"Cannot reconcile managed Core startup state; using the "
|
||||||
|
"bundled Core: {}",
|
||||||
|
selection.error().message);
|
||||||
|
} else {
|
||||||
|
corePathResolved = selection->corePath;
|
||||||
|
if (selection->runtimeBaselineChanged) {
|
||||||
|
if (selection->archivedStore) {
|
||||||
|
Void::Log::info(
|
||||||
|
"main",
|
||||||
|
"Runtime {} selected its bundled Core and archived "
|
||||||
|
"the previous managed Core store at {}",
|
||||||
|
runtimeVersion,
|
||||||
|
selection->archivedStore->string());
|
||||||
|
} else {
|
||||||
|
Void::Log::info(
|
||||||
|
"main",
|
||||||
|
"Runtime {} selected its bundled Core baseline",
|
||||||
|
runtimeVersion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (const auto workingDirectory =
|
if (const auto workingDirectory =
|
||||||
Echoion::Standalone::runtimeWorkingDirectory(installRoot,
|
Echoion::Standalone::runtimeWorkingDirectory(installRoot,
|
||||||
executableDirectory)) {
|
executableDirectory)) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "echoion/host/CoreLoader.hpp"
|
#include "echoion/host/CoreLoader.hpp"
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <ctime>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -34,11 +35,17 @@ int main()
|
|||||||
::setenv("HOME", root.c_str(), 1);
|
::setenv("HOME", root.c_str(), 1);
|
||||||
const auto stateDirectory = root / "runtime-state";
|
const auto stateDirectory = root / "runtime-state";
|
||||||
::setenv("ECHOION_STATE_DIR", stateDirectory.c_str(), 1);
|
::setenv("ECHOION_STATE_DIR", stateDirectory.c_str(), 1);
|
||||||
const auto ytDlp = root / ".local/share/echoion/yt-dlp/yt-dlp";
|
const auto dataDirectory = stateDirectory / "data";
|
||||||
|
::setenv("XDG_DATA_HOME", dataDirectory.c_str(), 1);
|
||||||
|
const auto ytDlp = dataDirectory / "echoion" / "yt-dlp" / "yt-dlp";
|
||||||
std::filesystem::create_directories(ytDlp.parent_path());
|
std::filesystem::create_directories(ytDlp.parent_path());
|
||||||
{
|
{
|
||||||
std::ofstream output{ytDlp};
|
std::ofstream output{ytDlp};
|
||||||
output << "#!/bin/sh\necho 2026.01.01\n";
|
output << "#!/bin/sh\necho 9999.01.01\n";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::ofstream output{ytDlp.parent_path() / ".last_update"};
|
||||||
|
output << std::time(nullptr);
|
||||||
}
|
}
|
||||||
std::filesystem::permissions(ytDlp,
|
std::filesystem::permissions(ytDlp,
|
||||||
std::filesystem::perms::owner_read |
|
std::filesystem::perms::owner_read |
|
||||||
|
|||||||
@@ -250,6 +250,57 @@ int main()
|
|||||||
};
|
};
|
||||||
|
|
||||||
TemporaryTree tree;
|
TemporaryTree tree;
|
||||||
|
const auto startupState = tree.path() / "startup-state";
|
||||||
|
const auto bundledStartupCore =
|
||||||
|
tree.path() / "startup-runtime" / "lib" / "Echoion.ts";
|
||||||
|
check(writeFile(bundledStartupCore, "runtime-core"),
|
||||||
|
"failed to write startup Core fixture");
|
||||||
|
const auto startupStore = startupState / "updates" / "components" / "core";
|
||||||
|
check(publishHistoricalVersion(startupStore, "1.1.0", "Echoion.ts",
|
||||||
|
"stale-managed-core"),
|
||||||
|
"failed to publish stale managed Core fixture");
|
||||||
|
check(activatePublishedVersion(startupStore, "1.1.0"),
|
||||||
|
"failed to activate stale managed Core fixture");
|
||||||
|
|
||||||
|
auto firstRuntimeSelection =
|
||||||
|
Echoion::Standalone::selectManagedCoreForStartup(
|
||||||
|
startupState, bundledStartupCore, std::string_view{"2.0.0"});
|
||||||
|
check(firstRuntimeSelection.has_value(),
|
||||||
|
"runtime Core baseline selection failed");
|
||||||
|
if (firstRuntimeSelection) {
|
||||||
|
check(firstRuntimeSelection->corePath == bundledStartupCore,
|
||||||
|
"new runtime did not prefer its bundled Core");
|
||||||
|
check(firstRuntimeSelection->runtimeBaselineChanged,
|
||||||
|
"new runtime did not report a Core baseline change");
|
||||||
|
check(firstRuntimeSelection->archivedStore.has_value() &&
|
||||||
|
std::filesystem::is_regular_file(
|
||||||
|
*firstRuntimeSelection->archivedStore / "versions" /
|
||||||
|
"1.1.0" / "Echoion.ts"),
|
||||||
|
"stale managed Core store was not archived");
|
||||||
|
}
|
||||||
|
|
||||||
|
check(publishHistoricalVersion(startupStore, "1.1.0", "Echoion.ts",
|
||||||
|
"runtime-core"),
|
||||||
|
"failed to seed the new runtime Core store");
|
||||||
|
check(activatePublishedVersion(startupStore, "1.1.0"),
|
||||||
|
"failed to activate the new runtime Core baseline");
|
||||||
|
auto sameRuntimeSelection =
|
||||||
|
Echoion::Standalone::selectManagedCoreForStartup(
|
||||||
|
startupState, bundledStartupCore, std::string_view{"2.0.0"});
|
||||||
|
check(sameRuntimeSelection.has_value() &&
|
||||||
|
sameRuntimeSelection->corePath ==
|
||||||
|
startupStore / "versions" / "1.1.0" / "Echoion.ts" &&
|
||||||
|
!sameRuntimeSelection->runtimeBaselineChanged,
|
||||||
|
"same runtime did not retain its managed Core selection");
|
||||||
|
|
||||||
|
auto nextRuntimeSelection =
|
||||||
|
Echoion::Standalone::selectManagedCoreForStartup(
|
||||||
|
startupState, bundledStartupCore, std::string_view{"2.1.0"});
|
||||||
|
check(nextRuntimeSelection.has_value() &&
|
||||||
|
nextRuntimeSelection->corePath == bundledStartupCore &&
|
||||||
|
nextRuntimeSelection->runtimeBaselineChanged,
|
||||||
|
"next runtime did not invalidate the prior managed Core baseline");
|
||||||
|
|
||||||
const auto state = tree.path() / "state";
|
const auto state = tree.path() / "state";
|
||||||
const auto incoming = tree.path() / "incoming";
|
const auto incoming = tree.path() / "incoming";
|
||||||
const auto currentCore = tree.path() / "installed" / "Echoion.ts";
|
const auto currentCore = tree.path() / "installed" / "Echoion.ts";
|
||||||
|
|||||||
@@ -52,6 +52,11 @@ namespace Echoion::Plugins::ShortUrl
|
|||||||
std::size_t maximumConcurrency{4};
|
std::size_t maximumConcurrency{4};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Missing endpoint means this optional provider is intentionally dormant.
|
||||||
|
// Malformed or partially-present configuration still counts as configured
|
||||||
|
// so plugin activation rejects it instead of silently ignoring it.
|
||||||
|
[[nodiscard]] bool hasExternalConfiguration(std::string_view payload);
|
||||||
|
|
||||||
[[nodiscard]] std::expected<ExternalConfig, std::string>
|
[[nodiscard]] std::expected<ExternalConfig, std::string>
|
||||||
parseExternalConfig(std::string_view payload);
|
parseExternalConfig(std::string_view payload);
|
||||||
|
|
||||||
|
|||||||
@@ -615,6 +615,18 @@ namespace Echoion::Plugins::ShortUrl
|
|||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
bool hasExternalConfiguration(const std::string_view payload)
|
||||||
|
{
|
||||||
|
if (payload.empty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
auto root = Void::Json::parse(payload);
|
||||||
|
if (!root || !root->is_object()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return settingsObject(*root).find("endpoint") != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
std::expected<ExternalConfig, std::string>
|
std::expected<ExternalConfig, std::string>
|
||||||
parseExternalConfig(const std::string_view payload)
|
parseExternalConfig(const std::string_view payload)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -41,6 +41,11 @@ namespace Echoion::Plugins::ShortUrl
|
|||||||
{
|
{
|
||||||
shortener_.reset();
|
shortener_.reset();
|
||||||
activationError_.clear();
|
activationError_.clear();
|
||||||
|
dormant_ = !hasExternalConfiguration(configPayload_);
|
||||||
|
if (dormant_) {
|
||||||
|
activationError_ = "plugin is not configured";
|
||||||
|
return;
|
||||||
|
}
|
||||||
auto config = parseExternalConfig(configPayload_);
|
auto config = parseExternalConfig(configPayload_);
|
||||||
if (!config) {
|
if (!config) {
|
||||||
activationError_ = std::move(config).error();
|
activationError_ = std::move(config).error();
|
||||||
@@ -56,6 +61,11 @@ namespace Echoion::Plugins::ShortUrl
|
|||||||
|
|
||||||
void on_unload() override { shortener_.reset(); }
|
void on_unload() override { shortener_.reset(); }
|
||||||
|
|
||||||
|
[[nodiscard]] bool ready() const noexcept
|
||||||
|
{
|
||||||
|
return dormant_ || shortener_ != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] Void::ShortUrl::Health health() const override
|
[[nodiscard]] Void::ShortUrl::Health health() const override
|
||||||
{
|
{
|
||||||
if (shortener_ != nullptr) {
|
if (shortener_ != nullptr) {
|
||||||
@@ -73,10 +83,16 @@ namespace Echoion::Plugins::ShortUrl
|
|||||||
shorten(const Void::ShortUrl::ShortenRequest& request) override
|
shorten(const Void::ShortUrl::ShortenRequest& request) override
|
||||||
{
|
{
|
||||||
if (shortener_ == nullptr) {
|
if (shortener_ == nullptr) {
|
||||||
return {.error = {
|
return {
|
||||||
|
.short_url = {},
|
||||||
|
.key = {},
|
||||||
|
.expires_at_unix_seconds = 0,
|
||||||
|
.error =
|
||||||
|
{
|
||||||
.code = Void::ShortUrl::ErrorCode::unavailable,
|
.code = Void::ShortUrl::ErrorCode::unavailable,
|
||||||
.message = "external shortener is unavailable",
|
.message = "external shortener is unavailable",
|
||||||
}};
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return shortener_->shorten(request);
|
return shortener_->shorten(request);
|
||||||
}
|
}
|
||||||
@@ -86,6 +102,7 @@ namespace Echoion::Plugins::ShortUrl
|
|||||||
std::string configPayload_;
|
std::string configPayload_;
|
||||||
std::string activationError_;
|
std::string activationError_;
|
||||||
std::unique_ptr<ExternalShortener> shortener_;
|
std::unique_ptr<ExternalShortener> shortener_;
|
||||||
|
bool dormant_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Echoion::Plugins::ShortUrl
|
} // namespace Echoion::Plugins::ShortUrl
|
||||||
@@ -124,11 +141,11 @@ void void_plugin_configure(void* instance, const char* config)
|
|||||||
|
|
||||||
int void_plugin_ready(void* instance)
|
int void_plugin_ready(void* instance)
|
||||||
{
|
{
|
||||||
const auto health =
|
return static_cast<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin*>(
|
||||||
static_cast<Echoion::Plugins::ShortUrl::ExternalShortenerPlugin*>(
|
instance)
|
||||||
instance)
|
->ready()
|
||||||
->health();
|
? 0
|
||||||
return health.status == Void::ShortUrl::HealthStatus::healthy ? 0 : 1;
|
: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Void::Service::IService* void_plugin_service(void* instance)
|
Void::Service::IService* void_plugin_service(void* instance)
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ namespace
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
using Echoion::Plugins::ShortUrl::ExternalShortener;
|
using Echoion::Plugins::ShortUrl::ExternalShortener;
|
||||||
|
using Echoion::Plugins::ShortUrl::hasExternalConfiguration;
|
||||||
using Echoion::Plugins::ShortUrl::parseExternalConfig;
|
using Echoion::Plugins::ShortUrl::parseExternalConfig;
|
||||||
|
|
||||||
int failures{};
|
int failures{};
|
||||||
@@ -46,6 +47,15 @@ int main()
|
|||||||
};
|
};
|
||||||
|
|
||||||
EnvironmentVariable token{"ECHOION_SHORTENER_TEST_TOKEN", "test secret"};
|
EnvironmentVariable token{"ECHOION_SHORTENER_TEST_TOKEN", "test secret"};
|
||||||
|
check(!hasExternalConfiguration(
|
||||||
|
R"JSON({"settings":{},"_host":{"stateDirectory":"/tmp"}})JSON"),
|
||||||
|
"empty host envelope did not keep the optional provider dormant");
|
||||||
|
check(
|
||||||
|
hasExternalConfiguration(R"JSON({"settings":{"endpoint":false}})JSON"),
|
||||||
|
"partially configured provider was incorrectly treated as dormant");
|
||||||
|
check(hasExternalConfiguration("{"),
|
||||||
|
"malformed provider configuration was incorrectly treated as "
|
||||||
|
"dormant");
|
||||||
const auto jsonConfig = R"JSON({
|
const auto jsonConfig = R"JSON({
|
||||||
"endpoint":"https://short.example/api",
|
"endpoint":"https://short.example/api",
|
||||||
"method":"POST",
|
"method":"POST",
|
||||||
|
|||||||
Reference in new issue
Block a user