| #include <string> |
| #include <utility> |
| |
| #include "action_context.h" |
| #include "bmc/machine_configs/embedded_data.h" |
| #include "bmc/register_actions_bmc.h" |
| #include "bmc/state_monitor_bmc.h" |
| #include "daemon_context.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/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.textproto", |
| "proto config file for gpowerd"); |
| ABSL_FLAG(std::string, legacy_config_file, |
| "/etc/gpowerd/gpowerd_config.proto", |
| "Legacy proto config file for gpowerd, used as a fallback."); |
| |
| namespace setup_configs { |
| |
| using safepower_agent::ActionContextManager; |
| using safepower_agent::DaemonContext; |
| using safepower_agent::RegisterActionFromConfig; |
| using safepower_agent::StateMonitorBMC; |
| using safepower_agent_config::SafePowerAgentConfig; |
| |
| 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; |
| } |
| |
| // Load the configs provided |
| static absl::Status Load(StateMonitorBMC& state_monitor, |
| ActionContextManager& action_context_manager) { |
| state_monitor.BuildFromConfig(DaemonContext::Get().config().gpowerd_config()); |
| |
| LOG(INFO) << "State monitor built"; |
| |
| RETURN_IF_ERROR(RegisterActionFromConfig(&action_context_manager, |
| DaemonContext::Get().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::string_view local_legacy_config_path) { |
| auto safepower_config = |
| safepower_agent::ParseTextProtoFromFile<SafePowerAgentConfig>( |
| std::string(local_config_path)); |
| |
| absl::Status load_status; |
| if (safepower_config.ok()) { |
| DaemonContext::Get().set_config(*safepower_config); |
| load_status = Load(state_monitor, action_context_manager); |
| } else { |
| LOG(ERROR) << "Failed to load config from " << local_config_path << ": " |
| << safepower_config.status(); |
| load_status = safepower_config.status(); |
| } |
| |
| // If the local could not be parsed and loaded, try the legacy |
| if (!load_status.ok()) { |
| safepower_config = |
| safepower_agent::ParseTextProtoFromFile<SafePowerAgentConfig>( |
| std::string(local_legacy_config_path)); |
| if (safepower_config.ok()) { |
| DaemonContext::Get().set_config(*std::move(safepower_config)); |
| LOG(INFO) << "Using legacy configs from: " << local_legacy_config_path; |
| load_status = Load(state_monitor, action_context_manager); |
| } else { |
| LOG(ERROR) << "Failed to load legacy config from " |
| << local_legacy_config_path << ": " |
| << safepower_config.status(); |
| load_status = std::move(safepower_config).status(); |
| } |
| } |
| |
| // If both local and legacy failed, use the baked-in config. |
| if (!load_status.ok()) { |
| LOG(ERROR) << "Unable to load any local configs. " |
| << "Using baked in config. Last error: " << load_status; |
| DaemonContext::Get().set_config(GetBakedinConfig()); |
| load_status = Load(state_monitor, action_context_manager); |
| if (!load_status.ok()) { |
| LOG(ERROR) << "Unable to load baked in config: " << load_status; |
| } |
| } |
| return load_status; |
| } |
| |
| absl::Status SetupConfigs(StateMonitorBMC& state_monitor, |
| ActionContextManager& action_context_manager) { |
| return SetupConfigs(state_monitor, action_context_manager, |
| absl::GetFlag(FLAGS_config_file), |
| absl::GetFlag(FLAGS_legacy_config_file)); |
| } |
| |
| } // namespace setup_configs |