blob: 93514df2cc64c029b426095f81eb921756dd60fb [file] [log] [blame]
#include <string>
#include <utility>
#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 "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/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::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;
}
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) {
RETURN_IF_ERROR(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::StatusOr<SafePowerAgentConfig> safepower_config =
ReadLocalConfig(local_config_path);
absl::Status load_status;
if (safepower_config.ok()) {
DaemonContext::Get().set_config(*std::move(safepower_config));
load_status = Load(state_monitor, action_context_manager);
} else {
load_status = std::move(safepower_config).status();
}
if (!load_status.ok()) {
LOG(ERROR) << "Unable to load configs: " << load_status
<< " Using baked in config";
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;
}
} 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