Skip to content

Commit

Permalink
Set Log Level For Node And Agent
Browse files Browse the repository at this point in the history
The APIs of the node and the agent are connected.
The API of Agent is internal. So if the user wants
to change the log level for the agent he needs to
use the public API of the node

Signed-off-by: Artiom Divak <[email protected]>
  • Loading branch information
ArtiomDivak committed Jun 14, 2023
1 parent 4251eaa commit 9258008
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
4 changes: 3 additions & 1 deletion data/org.containers.hirte.Node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
<method name="ListUnits">
<arg name="units" type="a(ssssssouso)" direction="out" />
</method>
<method name="Reload" />
<method name="SelNodeLogLevel">
<arg name="level" type="s" direction="in" />
</method>

<property name="Name" type="s" access="read" />
<property name="Status" type="s" access="read" />
Expand Down
4 changes: 3 additions & 1 deletion data/org.containers.hirte.internal.Agent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
<method name="StopDep">
<arg name="unit" type="s" direction="in" />
</method>
<method name="Reload" />
<method name="SetLogLevel">
<arg name="level" type="s" direction="in" />
</method>

<signal name="JobDone">
<arg name="id" type="u" />
Expand Down
22 changes: 22 additions & 0 deletions src/agent/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,27 @@ static int agent_method_disable_metrics(sd_bus_message *m, void *userdata, UNUSE
return sd_bus_reply_method_return(m, "");
}

/*************************************************************************
************** org.containers.hirte.Agent.SetNodeLogLevel ***************
************************************************************************/

static int agent_method_set_log_level(
UNUSED sd_bus_message *m, UNUSED void *userdata, UNUSED sd_bus_error *ret_error) {
const char *level = NULL;
int r = sd_bus_message_read(m, "s", &level);
if (r < 0) {
return sd_bus_reply_method_errorf(
m, SD_BUS_ERROR_FAILED, "Failed to read the parameter: %s", strerror(-r));
}
LogLevel loglevel = string_to_log_level(level);
if (loglevel == LOG_LEVEL_INVALID) {
return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_FAILED, "Invalid input for log level");
}
hirte_log_set_level(loglevel);
hirte_log_infof("Log level changed to %s", level);
return r;
}


static const sd_bus_vtable internal_agent_vtable[] = {
SD_BUS_VTABLE_START(0),
Expand All @@ -1348,6 +1369,7 @@ static const sd_bus_vtable internal_agent_vtable[] = {
SD_BUS_METHOD("Unsubscribe", "s", "", agent_method_unsubscribe, 0),
SD_BUS_METHOD("EnableMetrics", "", "", agent_method_enable_metrics, 0),
SD_BUS_METHOD("DisableMetrics", "", "", agent_method_disable_metrics, 0),
SD_BUS_METHOD("SetLogLevel", "s", "", agent_method_set_log_level, 0),
SD_BUS_SIGNAL_WITH_NAMES("JobDone", "us", SD_BUS_PARAM(id) SD_BUS_PARAM(result), 0),
SD_BUS_SIGNAL_WITH_NAMES("JobStateChanged", "us", SD_BUS_PARAM(id) SD_BUS_PARAM(state), 0),
SD_BUS_SIGNAL_WITH_NAMES(
Expand Down
2 changes: 1 addition & 1 deletion src/libhirte/common/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,4 @@ const char *cfg_dump(struct config *config) {
}
}
return cfg_info;
}
}
39 changes: 38 additions & 1 deletion src/manager/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static int node_method_stop_unit(sd_bus_message *m, void *userdata, UNUSED sd_bu
static int node_method_restart_unit(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error);
static int node_method_reload_unit(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error);
static int node_method_passthrough_to_agent(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error);
static int node_method_set_log_level(sd_bus_message *m, void *userdata, UNUSED sd_bus_error *ret_error);
static int node_property_get_nodename(
sd_bus *bus,
const char *path,
Expand Down Expand Up @@ -61,7 +62,7 @@ static const sd_bus_vtable node_vtable[] = {
SD_BUS_METHOD("SetUnitProperties", "sba(sv)", "", node_method_set_unit_properties, 0),
SD_BUS_METHOD("EnableUnitFiles", "asbb", "ba(sss)", node_method_passthrough_to_agent, 0),
SD_BUS_METHOD("DisableUnitFiles", "asb", "a(sss)", node_method_passthrough_to_agent, 0),
SD_BUS_METHOD("Reload", "", "", node_method_passthrough_to_agent, 0),
SD_BUS_METHOD("SetNodeLogLevel", "s", "", node_method_set_log_level, 0),
SD_BUS_PROPERTY("Name", "s", node_property_get_nodename, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Status", "s", node_property_get_status, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_VTABLE_END
Expand Down Expand Up @@ -1511,6 +1512,42 @@ static int node_method_reload_unit(sd_bus_message *m, void *userdata, UNUSED sd_
return node_run_unit_lifecycle_method(m, (Node *) userdata, "reload", "ReloadUnit");
}

/*************************************************************************
********** org.containers.hirte.Node.SetLogLevel *******************
************************************************************************/

static int node_method_set_log_level(sd_bus_message *m, UNUSED void *userdata, UNUSED sd_bus_error *ret_error) {
const char *level = NULL;
Node *node = (Node *) userdata;
sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_sd_bus_message_ sd_bus_message *sub_m = NULL;

int r = sd_bus_message_read(m, "s", &level);
if (r < 0) {
return r;
}
LogLevel loglevel = string_to_log_level(level);
if (loglevel == LOG_LEVEL_INVALID) {
return r;
}
r = sd_bus_call_method(
node->agent_bus,
HIRTE_AGENT_DBUS_NAME,
INTERNAL_AGENT_OBJECT_PATH,
INTERNAL_AGENT_INTERFACE,
"SetLogLevel",
&error,
&sub_m,
"s",
level);
if (r < 0) {
hirte_log_errorf("Failed to set log level call: %s", error.message);
sd_bus_error_free(&error);
return false;
}
return 1;
}

static int send_agent_simple_message(Node *node, const char *method, const char *arg) {
_cleanup_sd_bus_message_ sd_bus_message *m = NULL;
int r = sd_bus_message_new_method_call(
Expand Down

0 comments on commit 9258008

Please sign in to comment.