bool mesos_state_t::parse_groups(const std::string& json) { Json::Value root; Json::Reader reader; if(reader.parse(json, root, false)) { return handle_groups(root, add_group(root, 0)); } else { throw sinsp_exception("Invalid JSON (Marathon groups parsing failed)."); } }
bool mesos_state_t::handle_groups(const Json::Value& root, marathon_group::ptr_t to_group, const std::string& framework_id) { Json::Value groups = root["groups"]; if(!groups.isNull() && groups.isArray()) { for(const auto& group : groups) { to_group = add_group(group, to_group, framework_id); ASSERT(to_group); handle_groups(group, to_group, framework_id); } } else { g_logger.log("No groups found.", sinsp_logger::SEV_WARNING); return false; } return true; }
marathon_group::ptr_t mesos_state_t::add_group(const Json::Value& group, marathon_group::ptr_t to_group, const std::string& framework_id) { const Json::Value& group_id = group["id"]; if(!group_id.isNull()) { std::string id = group_id.asString(); std::ostringstream os; os << "Adding Marathon group [" << id << ']'; if(to_group) { os << " to group [" << to_group->get_id() << ']'; } g_logger.log(os.str(), sinsp_logger::SEV_DEBUG); marathon_group::ptr_t pg(new marathon_group(id, framework_id)); add_or_replace_group(pg, to_group); const Json::Value& apps = group["apps"]; if(!apps.isNull()) { for(const auto& app : apps) { const Json::Value& app_id = app["id"]; if(!app_id.isNull()) { const Json::Value& instances = app["instances"]; if(!instances.isNull() && instances.isInt() && instances.asInt() > 0) { marathon_app::ptr_t p_app = get_app(app_id.asString()); if(!p_app) { p_app = add_app(app, framework_id); } if(p_app) { pg->add_or_replace_app(p_app); if(!framework_id.empty()) { for(const auto& task : get_tasks(framework_id)) { if(task.second->get_marathon_app_id() == app_id.asString()) { add_task_to_app(p_app, task.first); } } } } else { g_logger.log("An error occured adding app [" + app_id.asString() + "] to group [" + id + ']', sinsp_logger::SEV_ERROR); } } } } } Json::Value groups = group["groups"]; if(!groups.isNull() && groups.isArray()) { handle_groups(group, pg, framework_id); } return pg; } return 0; }