Skip to content

Commit

Permalink
appengine: Check app install along with app run check
Browse files Browse the repository at this point in the history
Check whether an app is installed while verifying whether an app is
running or not in `appengine.isRunning()` call. It guarantees that the
running app is exactly what is expected to be running.

Signed-off-by: Mike Sul <[email protected]>
  • Loading branch information
mike-sul committed Dec 11, 2024
1 parent da3bfab commit dc4ef3d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
41 changes: 38 additions & 3 deletions src/composeapp/appengine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace composeapp {
enum class ExitCode { ExitCodeInsufficientSpace = 100 };

static bool checkAppStatus(const AppEngine::App& app, const Json::Value& status);
static bool checkAppInstallationStatus(const AppEngine::App& app, const Json::Value& status);
static bool isNullOrEmptyIfMember(const Json::Value& val, const std::string& field);

AppEngine::Result AppEngine::fetch(const App& app) {
Result res{false};
Expand Down Expand Up @@ -66,10 +68,16 @@ bool AppEngine::isRunning(const App& app) const {
bool res{false};
try {
std::future<std::string> output;
exec(boost::format{"%s --store %s ps %s --format json"} % composectl_cmd_ % storeRoot() % app.uri, "",
boost::process::std_out > output);
exec(boost::format{"%s --store %s --compose %s ps %s --format json --install"} % composectl_cmd_ % storeRoot() %
installRoot() % app.uri,
"", boost::process::std_out > output);
const auto app_status{Utils::parseJSON(output.get())};
res = checkAppStatus(app, app_status);
// Make sure app images and bundle are properly installed
res = checkAppInstallationStatus(app, app_status);
if (res) {
// Make sure app is running
res = checkAppStatus(app, app_status);
}
} catch (const std::exception& exc) {
LOG_ERROR << "failed to verify whether app is running; app: " << app.name << ", err: " << exc.what();
}
Expand Down Expand Up @@ -227,4 +235,31 @@ static bool checkAppStatus(const AppEngine::App& app, const Json::Value& status)
return is_running;
}

static bool checkAppInstallationStatus(const AppEngine::App& app, const Json::Value& status) {
const auto& app_status{status.get(app.uri, Json::Value())};
if (!app_status.isObject()) {
LOG_ERROR << "could not get app status; uri: " << app.uri;
return false;
}
if (!isNullOrEmptyIfMember(app_status, "missing_images")) {
LOG_INFO << app.name << " is not fully installed; missing images:\n" << app_status["missing_images"];
return false;
}
if (!isNullOrEmptyIfMember(app_status, "bundle_errors")) {
LOG_INFO << app.name << " is not fully installed; invalid bundle installation:\n" << app_status["bundle_errors"];
return false;
}
return true;
}

static bool isNullOrEmptyIfMember(const Json::Value& val, const std::string& field) {
bool res{false};
if (val.isMember(field)) {
res = val[field].isNull() || val[field].empty();
} else {
res = true;
}
return res;
}

} // namespace composeapp
4 changes: 2 additions & 2 deletions src/composeappmanager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ ComposeAppManager::AppsContainer ComposeAppManager::getAppsToUpdate(const Uptane

LOG_DEBUG << app_name << " performing full status check";
if (!app_engine_->isRunning({app_name, app_pair.second})) {
// an App that is supposed to running is not running
// an App that is supposed to be running is not running or is not fully installed
apps_to_update.insert(app_pair);
apps_and_reasons[app_pair.first] = "not running";
LOG_INFO << app_name << " is not installed or not running; will be installed and started";
continue;
}
if (!app_engine_->isFetched({app_name, app_pair.second})) {
// an App that is supposed to be installed is not fully installed
// an App that is supposed to be installed is not fully fetched
apps_to_update.insert(app_pair);
apps_and_reasons[app_pair.first] = "not fetched";
LOG_INFO << app_name << " is not fully fetched; missing blobs will be fetched";
Expand Down

0 comments on commit dc4ef3d

Please sign in to comment.