diff --git a/src/main.cpp b/src/main.cpp index 724c0ba..fa71730 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -815,10 +815,45 @@ int main(int argc, char** argv) Void::Log::info("main", "Echoion standalone running. Ctrl-C to stop."); std::optional healthVersion; + std::optional installerRollbackVersion; if (const char* version = std::getenv("ECHOION_REEXEC_VERSION")) { healthVersion = version; Void::Log::info("main", "Release {} entered its 30 second health check", *healthVersion); + } else if (auto pendingInstall = Void::FileSystem::read_file( + stateDirectory / "updates" / "pending-install-generation"); + pendingInstall) { + const auto versionPrefix = std::string{"version="}; + const auto lineEnd = pendingInstall->find('\n'); + const auto firstLine = pendingInstall->substr(0, lineEnd); + if (firstLine.starts_with(versionPrefix) && + firstLine.substr(versionPrefix.size()) == runtimeVersion) { + healthVersion = runtimeVersion; + const auto previousPrefix = std::string{"previous=versions/"}; + if (lineEnd != std::string::npos) { + const auto previousStart = lineEnd + 1; + const auto previousEnd = + pendingInstall->find('\n', previousStart); + const auto previousLine = pendingInstall->substr( + previousStart, previousEnd - previousStart); + if (previousLine.starts_with(previousPrefix) && + previousLine.size() > previousPrefix.size()) { + installerRollbackVersion = + previousLine.substr(previousPrefix.size()); + } + } + Void::Log::info( + "main", + "Pending installed generation {} entered its 30 second " + "local health check", + *healthVersion); + } else { + Void::Log::error( + "main", + "Pending installed generation marker does not match {}", + runtimeVersion); + return 1; + } } auto healthDeadline = std::chrono::steady_clock::now() + std::chrono::seconds{30}; @@ -834,6 +869,29 @@ int main(int argc, char** argv) if (healthVersion && std::chrono::steady_clock::now() >= healthDeadline) { bool committed = true; + auto healthyCore = loader.getCore(); + if (!healthyCore || !healthyCore->isInitialized()) { + committed = false; + Void::Log::error( + "main", + "Local health check failed: Core is not initialized"); + } else if (auto readiness = healthyCore->getConfigJson(); + !readiness) { + committed = false; + Void::Log::error( + "main", + "Local health check failed: RPC/config readiness is " + "unavailable: {}", + readiness.error().message); + } + if (runningManifest && runningManifest->version != runtimeVersion) { + committed = false; + Void::Log::error( + "main", + "Local health check failed: active generation reports " + "{} instead of {}", + runningManifest->version, runtimeVersion); + } const auto handoff = readRuntimeHandoff(activationHandoff, updateRoot); if (handoff && handoff->state == "pending" && @@ -864,10 +922,46 @@ int main(int argc, char** argv) stateDirectory / "release-transaction"); std::ignore = Void::FileSystem::remove_path(stateDirectory / "failed-release"); + std::ignore = Void::FileSystem::remove_path( + stateDirectory / "updates" / "pending-install-generation"); Void::Log::info("main", "Release {} passed its health check", *healthVersion); healthVersion.reset(); + installerRollbackVersion.reset(); ::unsetenv("ECHOION_REEXEC_VERSION"); + } else if (installerRollbackVersion) { + Void::Json::Value operation = Void::Json::object(); + operation.as_object().emplace_back( + "action", Void::Json::Value{"rollback"}); + operation.as_object().emplace_back( + "version", Void::Json::Value{*installerRollbackVersion}); + operation.as_object().emplace_back( + "retainHealthyVersions", + Void::Json::Value{static_cast(2)}); + auto rolledBack = + invokeRuntimeHelper(updaterExecutable, updateRoot, + Void::Json::serialize(operation)); + if (!rolledBack) { + Void::Log::error( + "main", + "Installed generation failed readiness and legacy " + "rollback also failed: {}", + rolledBack.error().message); + healthDeadline = std::chrono::steady_clock::now() + + std::chrono::seconds{5}; + } else { + std::ignore = Void::FileSystem::atomic_write( + stateDirectory / "failed-release", runtimeVersion); + std::ignore = Void::FileSystem::remove_path( + stateDirectory / "updates" / + "pending-install-generation"); + Void::Log::error( + "main", + "Installed generation {} failed local readiness; " + "restored legacy generation {}", + runtimeVersion, *installerRollbackVersion); + return 1; + } } else { healthDeadline = std::chrono::steady_clock::now() + std::chrono::seconds{5};