blob: fceab5008e23e3861e53d5e18d0b2b005244198e [file]
#include <string>
#include "action_context.h"
#include "bmc/machine_configs/embedded_data.h"
#include "bmc/proto_reader.h"
#include "bmc/register_actions_bmc.h"
#include "bmc/state_monitor_bmc.h"
#include "parse_text_proto.h"
#include "safepower_agent_config.pb.h"
#include "absl/flags/flag.h"
#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "bmc/status_macros.h"
ABSL_FLAG(std::string, config_file, "/etc/gpowerd/gpowerd_config.proto",
"proto config file for gpowerd");
namespace setup_configs {
using safepower_agent::RegisterActionFromConfig;
using safepower_agent_config::SafePowerAgentConfig;
using safepower_agent::StateMonitorBMC;
using safepower_agent::ActionContextManager;
static SafePowerAgentConfig GetBakedinConfig(){
LOG(ERROR) <<
"Loading bakedin Config, gPowerD action set "
"is limited without machine specific config";
SafePowerAgentConfig bakedin_config;
bool status = bakedin_config.ParseFromArray
(bmc_machine_configs_binary_proto_bin,
bmc_machine_configs_binary_proto_bin_len);
if (!status){
LOG(ERROR) << "Failed to parse bakedin config";
}
LOG(ERROR) << absl::StrCat("bakedin config: ",
bakedin_config.DebugString());
return bakedin_config;
}
static absl::StatusOr<SafePowerAgentConfig>
ReadLocalConfig(absl::string_view config_file_path){
ASSIGN_OR_RETURN(std::string config_proto_str,
proto_reader::ReadFileToString(config_file_path));
LOG(INFO) << "Config proto " << config_proto_str;
return safepower_agent::ParseTextProto<SafePowerAgentConfig>
(config_proto_str);
}
// Load the configs provided
static absl::Status Load(StateMonitorBMC& state_monitor,
ActionContextManager& action_context_manager,
const SafePowerAgentConfig& safepower_config)
{
RETURN_IF_ERROR(state_monitor.BuildFromConfig(
safepower_config.gpowerd_config()));
LOG(INFO) << "State monitor built";
RETURN_IF_ERROR(RegisterActionFromConfig(
&action_context_manager, safepower_config));
LOG(INFO) << "Actions registered";
return absl::OkStatus();
}
// Try to load the configs from the local config file, if it exists. otherwise
// loads the bakedin config.
absl::Status SetupConfigs(StateMonitorBMC& state_monitor,
ActionContextManager& action_context_manager,
absl::string_view local_config_path){
absl::StatusOr<SafePowerAgentConfig> safepower_config =
ReadLocalConfig(local_config_path);
absl::Status load_status = absl::FailedPreconditionError("No config found");
if (safepower_config.ok())
{
load_status = Load(state_monitor,
action_context_manager,
*safepower_config);
} else {
LOG(ERROR) << "Unable to read file: " << safepower_config.status();
}
if (!load_status.ok() || !safepower_config.ok()){
LOG(ERROR) << "Unable to load configs: " << load_status <<
" Using baked in config";
SafePowerAgentConfig bakedin_safepower_config = GetBakedinConfig();
load_status = Load(state_monitor,
action_context_manager,
bakedin_safepower_config);
if (!load_status.ok()){
LOG(ERROR) << "Unable to load baked in config: " << load_status;
}
} else{
LOG(INFO) << "Using local configs";
}
return load_status;
}
absl::Status SetupConfigs(StateMonitorBMC& state_monitor,
ActionContextManager& action_context_manager){
std::string config_file = absl::GetFlag(FLAGS_config_file);
return SetupConfigs(state_monitor, action_context_manager, config_file);
}
} // namespace setup_configs