Runtime: reset stale Core baseline on upgrade
This commit is contained in:
1 parent
978930f4d2
commit
2fba7d2dea
5 files changed
+310
-31
No files matched your search
@@ -3162,6 +3162,207 @@ namespace Echoion::Standalone
|
||||
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(
|
||||
const std::filesystem::path& stateDirectory)
|
||||
{
|
||||
|
||||
+32
-29
@@ -17,7 +17,6 @@
|
||||
#include <Void/System/Process.hpp>
|
||||
#include <Void/System/SignalWatcher.hpp>
|
||||
#include <Void/TeamSpeak/Identity.hpp>
|
||||
#include <Void/Update/Update.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
@@ -245,30 +244,6 @@ namespace
|
||||
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>
|
||||
invokeRuntimeHelper(const std::filesystem::path& helper,
|
||||
const std::filesystem::path& root,
|
||||
@@ -661,10 +636,38 @@ int main(int argc, char** argv)
|
||||
configuredCoreOverride &&
|
||||
absolutePath(*configuredCoreOverride) !=
|
||||
absolutePath(installRoot / "lib" / "Echoion.ts");
|
||||
const auto corePathResolved =
|
||||
customCoreOverride
|
||||
? configuredCorePath
|
||||
: reconcileManagedCore(stateDirectory, configuredCorePath);
|
||||
auto corePathResolved = configuredCorePath;
|
||||
if (!customCoreOverride) {
|
||||
const auto installedRuntimeVersion =
|
||||
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 =
|
||||
Echoion::Standalone::runtimeWorkingDirectory(installRoot,
|
||||
executableDirectory)) {
|
||||
|
||||
Reference in new issue
Block a user