Skip to content

Commit

Permalink
Get And Set Default Target
Browse files Browse the repository at this point in the history
This feature will get and set the default target for a specific node.

Solves eclipse-bluechi#944
Signed-off-by: Artiom Divak <[email protected]>
  • Loading branch information
ArtiomDivak committed Oct 7, 2024
1 parent bbc9ae4 commit d4967b4
Show file tree
Hide file tree
Showing 15 changed files with 333 additions and 22 deletions.
24 changes: 24 additions & 0 deletions data/org.eclipse.bluechi.Node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,30 @@
<arg name="name" type="s" direction="in" />
</method>

<!--
GetDefaultTarget:
@defaulttarget the default target.
Get the default value of the system to boot into.
-->
<method name="GetDefaultTarget">
<arg name="defaulttarget" type="s" direction="out" />
</method>

<!--
SetDefaultTarget:
@defaulttarget the default target.
@force bollean value of force.
@out the result of the method.
Set the default value of the system to boot into.
-->
<method name="SetDefaultTarget">
<arg name="defaulttarget" type="s" direction="in" />
<arg name="force" type="b" direction="in" />
<arg name="out" type="a(sss)" direction="out" />
</method>

<!--
Name:
Expand Down
8 changes: 8 additions & 0 deletions data/org.eclipse.bluechi.internal.Agent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@
<method name="ResetFailedUnit">
<arg name="name" type="s" direction="in" />
</method>
<method name="GetDefaultTarget">
<arg name="defaulttarget" type="s" direction="out" />
</method>
<method name="SetDefaultTarget">
<arg name="defaulttarget" type="s" direction="in" />
<arg name="force" type="b" direction="in" />
<arg name="out" type="a(sss)" direction="out" />
</method>

<signal name="JobDone">
<arg name="id" type="u" />
Expand Down
8 changes: 8 additions & 0 deletions doc/man/bluechictl.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,11 @@ Fetches the status of the systemd units for the `bluechi-agent`.
### **bluechictl** *reset-failed* [*agent*] [*unit1*,*...*]

Performs a `reset-failed` on the chosen `bluechi-agent` for the selected units.

### **bluechictl** *get-default* [*agent*]

Fetches the default target on the chosen `bluechi-agent`.

### **bluechictl** *set-default* [*agent*] [TARGET]

Changes the default target to `TARGET` file on the chosen `bluechi-agent`.
3 changes: 2 additions & 1 deletion src/agent/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -1758,7 +1758,6 @@ static int agent_method_job_cancel(sd_bus_message *m, void *userdata, UNUSED sd_
return sd_bus_reply_method_return(m, "");
}


static const sd_bus_vtable internal_agent_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD("ListUnits", "", UNIT_INFO_STRUCT_ARRAY_TYPESTRING, agent_method_list_units, 0),
Expand Down Expand Up @@ -1787,6 +1786,8 @@ static const sd_bus_vtable internal_agent_vtable[] = {
SD_BUS_METHOD("KillUnit", "ssi", "", agent_method_passthrough_to_systemd, 0),
SD_BUS_METHOD("StartDep", "s", "", agent_method_start_dep, 0),
SD_BUS_METHOD("StopDep", "s", "", agent_method_stop_dep, 0),
SD_BUS_METHOD("GetDefaultTarget", "", "s", agent_method_passthrough_to_systemd, 0),
SD_BUS_METHOD("SetDefaultTarget", "sb", "a(sss)", agent_method_passthrough_to_systemd, 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
25 changes: 25 additions & 0 deletions src/bindings/python/bluechi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,15 @@ def freeze_unit(self, name: str) -> None:
name,
)

def get_default_target(self) -> str:
"""
GetDefaultTarget:
@defaulttarget the default target.
Get the default value of the system to boot into.
"""
return self.get_proxy().GetDefaultTarget()

def get_unit_file_state(self, file: str) -> str:
"""
GetUnitFileState:
Expand Down Expand Up @@ -1023,6 +1032,22 @@ def restart_unit(self, name: str, mode: str) -> ObjPath:
mode,
)

def set_default_target(
self, defaulttarget: str, force: bool
) -> List[Tuple[str, str, str]]:
"""
SetDefaultTarget:
@defaulttarget the default target.
@force bollean value of force.
@out the result of the method.
Set the default value of the system to boot into.
"""
return self.get_proxy().SetDefaultTarget(
defaulttarget,
force,
)

def set_log_level(self, level: str) -> None:
"""
SetLogLevel:
Expand Down
45 changes: 24 additions & 21 deletions src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "client.h"
#include "method-daemon-reload.h"
#include "method-default-target.h"
#include "method-enable-disable.h"
#include "method-freeze-thaw.h"
#include "method-help.h"
Expand Down Expand Up @@ -42,27 +43,29 @@ int method_version(UNUSED Command *command, UNUSED void *userdata) {
}

const Method methods[] = {
{ "help", 0, 0, OPT_NONE, method_help, usage_bluechi },
{ "list-unit-files", 0, 1, OPT_FILTER, method_list_unit_files, usage_method_list_unit_files },
{ "is-enabled", 2, 2, OPT_NONE, method_is_enabled, usage_method_is_enabled },
{ "list-units", 0, 1, OPT_FILTER, method_list_units, usage_method_list_units },
{ "start", 2, 2, OPT_NONE, method_start, usage_method_lifecycle },
{ "stop", 2, 2, OPT_NONE, method_stop, usage_method_lifecycle },
{ "freeze", 2, 2, OPT_NONE, method_freeze, usage_method_freeze },
{ "thaw", 2, 2, OPT_NONE, method_thaw, usage_method_thaw },
{ "restart", 2, 2, OPT_NONE, method_restart, usage_method_lifecycle },
{ "reload", 2, 2, OPT_NONE, method_reload, usage_method_lifecycle },
{ "reset-failed", 0, ARG_ANY, OPT_NONE, method_reset_failed, usage_method_reset_failed },
{ "kill", 2, 2, OPT_KILL_WHOM | OPT_SIGNAL, method_kill, usage_method_kill },
{ "monitor", 0, 2, OPT_NONE, method_monitor, usage_method_monitor },
{ "metrics", 1, 1, OPT_NONE, method_metrics, usage_method_metrics },
{ "enable", 2, ARG_ANY, OPT_FORCE | OPT_RUNTIME | OPT_NO_RELOAD, method_enable, usage_method_enable },
{ "disable", 2, ARG_ANY, OPT_NO_RELOAD, method_disable, usage_method_disable },
{ "daemon-reload", 1, 1, OPT_NONE, method_daemon_reload, usage_method_daemon_reload },
{ "status", 0, ARG_ANY, OPT_WATCH, method_status, usage_method_status },
{ "set-loglevel", 1, 2, OPT_NONE, method_set_loglevel, usage_method_set_loglevel },
{ "version", 0, 0, OPT_NONE, method_version, usage_bluechi },
{ NULL, 0, 0, 0, NULL, NULL }
{ "help", 0, 0, OPT_NONE, method_help, usage_bluechi },
{ "list-unit-files", 0, 1, OPT_FILTER, method_list_unit_files, usage_method_list_unit_files },
{ "is-enabled", 2, 2, OPT_NONE, method_is_enabled, usage_method_is_enabled },
{ "list-units", 0, 1, OPT_FILTER, method_list_units, usage_method_list_units },
{ "start", 2, 2, OPT_NONE, method_start, usage_method_lifecycle },
{ "stop", 2, 2, OPT_NONE, method_stop, usage_method_lifecycle },
{ "freeze", 2, 2, OPT_NONE, method_freeze, usage_method_freeze },
{ "thaw", 2, 2, OPT_NONE, method_thaw, usage_method_thaw },
{ "restart", 2, 2, OPT_NONE, method_restart, usage_method_lifecycle },
{ "reload", 2, 2, OPT_NONE, method_reload, usage_method_lifecycle },
{ "reset-failed", 0, ARG_ANY, OPT_NONE, method_reset_failed, usage_method_reset_failed },
{ "kill", 2, 2, OPT_KILL_WHOM | OPT_SIGNAL, method_kill, usage_method_kill },
{ "monitor", 0, 2, OPT_NONE, method_monitor, usage_method_monitor },
{ "metrics", 1, 1, OPT_NONE, method_metrics, usage_method_metrics },
{ "enable", 2, ARG_ANY, OPT_FORCE | OPT_RUNTIME | OPT_NO_RELOAD, method_enable, usage_method_enable },
{ "disable", 2, ARG_ANY, OPT_NO_RELOAD, method_disable, usage_method_disable },
{ "daemon-reload", 1, 1, OPT_NONE, method_daemon_reload, usage_method_daemon_reload },
{ "status", 0, ARG_ANY, OPT_WATCH, method_status, usage_method_status },
{ "set-loglevel", 1, 2, OPT_NONE, method_set_loglevel, usage_method_set_loglevel },
{ "get-default", 1, 1, OPT_NONE, method_get_default_target, usage_method_get_default_target },
{ "set-default", 2, 3, OPT_NONE, method_set_default_target, usage_method_set_default_target },
{ "version", 0, 0, OPT_NONE, method_version, usage_bluechi },
{ NULL, 0, 0, 0, NULL, NULL }
};

const OptionType option_types[] = {
Expand Down
1 change: 1 addition & 0 deletions src/client/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ client_src = [
'method-enable-disable.c',
'method-reset-failed.c',
'method-daemon-reload.c',
'method-default-target.c',
'usage.c',
]

Expand Down
127 changes: 127 additions & 0 deletions src/client/method-default-target.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright Contributors to the Eclipse BlueChi project
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "method-default-target.h"
#include "client.h"
#include "usage.h"

#include "libbluechi/common/opt.h"

static int method_get_default_target_on(Client *client, char *node_name) {
_cleanup_sd_bus_error_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_sd_bus_message_ sd_bus_message *message = NULL;
char *result = NULL;
int r = 0;

r = assemble_object_path_string(NODE_OBJECT_PATH_PREFIX, node_name, &client->object_path);
if (r < 0) {
return r;
}

r = sd_bus_call_method(
client->api_bus,
BC_INTERFACE_BASE_NAME,
client->object_path,
NODE_INTERFACE,
"GetDefaultTarget",
&error,
&message,
"");
if (r < 0) {
fprintf(stderr, "Failed to issue method call: %s\n", error.message);
return r;
}

r = sd_bus_message_read(message, "s", &result);
if (r < 0) {
fprintf(stderr, "Failed to parse response message: %s\n", strerror(-r));
return r;
}

printf("%s\n", result);

return r;
}


static int method_set_default_target_on(Client *client, char *node_name, char *target, char *force) {
_cleanup_sd_bus_error_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_sd_bus_message_ sd_bus_message *message = NULL;
int r = 0;
bool force_b = 1;
if (streq(force, "false")) {
force_b = 0;
}


r = assemble_object_path_string(NODE_OBJECT_PATH_PREFIX, node_name, &client->object_path);
if (r < 0) {
return r;
}

r = sd_bus_call_method(
client->api_bus,
BC_INTERFACE_BASE_NAME,
client->object_path,
NODE_INTERFACE,
"SetDefaultTarget",
&error,
&message,
"sb",
target,
force_b);
if (r < 0) {
fprintf(stderr, "Failed to issue method call: %s\n", error.message);
return r;
}

r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "(sss)");
if (r < 0) {
fprintf(stderr, "Failed to open the strings array container: %s\n", strerror(-r));
return r;
}
printf("[");
for (;;) {
const char *result1 = NULL;
const char *result2 = NULL;
const char *result3 = NULL;

r = sd_bus_message_read(message, "(sss)", &result1, &result2, &result3);
if (r < 0) {
fprintf(stderr, "Failed to read the SetDefaultTarget response: %s\n", strerror(-r));
return r;
}
if (r == 0) {
break;
}
printf("(%s %s %s),", result1, result2, result3);
}
printf("]\n");

return r;
}

void usage_method_get_default_target() {
usage_print_header();
usage_print_usage("bluechictl get-default [nodename]");
}

void usage_method_set_default_target() {
usage_print_header();
usage_print_usage("bluechictl set-default [nodename] [target]");
}


int method_get_default_target(Command *command, void *userdata) {
return method_get_default_target_on(userdata, command->opargv[0]);
}

int method_set_default_target(Command *command, void *userdata) {
char *force = "true";
if (command->opargc == 3) {
force = command->opargv[2];
}
return method_set_default_target_on(userdata, command->opargv[0], command->opargv[1], force);
}
13 changes: 13 additions & 0 deletions src/client/method-default-target.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Contributors to the Eclipse BlueChi project
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#pragma once

#include "libbluechi/cli/command.h"

int method_get_default_target(Command *command, void *userdata);
void usage_method_get_default_target();
int method_set_default_target(Command *command, void *userdata);
void usage_method_set_default_target();
4 changes: 4 additions & 0 deletions src/client/method-help.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ void usage_bluechi() {
printf(" usage: daemon-reload nodename\n");
printf(" - set-loglevel: change the log level of the bluechi-controller or a connected node\n");
printf(" usage: set-loglevel [nodename] [loglevel]\n");
printf(" - get-default: get a default target of connected node\n");
printf(" usage: get-default [nodename]\n");
printf(" - set-default: set a default target of connected node\n");
printf(" usage: set-default [nodename] [target]\n");
}

int method_help(UNUSED Command *command, UNUSED void *userdata) {
Expand Down
2 changes: 2 additions & 0 deletions src/controller/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ static const sd_bus_vtable node_vtable[] = {
SD_BUS_METHOD("Reload", "", "", node_method_passthrough_to_agent, 0),
SD_BUS_METHOD("KillUnit", "ssi", "", node_method_passthrough_to_agent, 0),
SD_BUS_METHOD("SetLogLevel", "s", "", node_method_set_log_level, 0),
SD_BUS_METHOD("GetDefaultTarget", "", "s", node_method_passthrough_to_agent, 0),
SD_BUS_METHOD("SetDefaultTarget", "sb", "a(sss)", node_method_passthrough_to_agent, 0),
SD_BUS_PROPERTY("Name", "s", NULL, offsetof(Node, name), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Status", "s", node_property_get_status, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("PeerIp", "s", node_property_get_peer_ip, 0, SD_BUS_VTABLE_PROPERTY_EXPLICIT),
Expand Down
29 changes: 29 additions & 0 deletions tests/bluechi_test/bluechictl.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,35 @@ def reset_failed(
expected_result,
)

def get_default_target(
self,
node_name: str = "",
check_result: bool = True,
expected_result: int = 0,
) -> Tuple[Optional[int], Union[Iterator[bytes], Any, Tuple[bytes, bytes]]]:
cmd = f"get-default {node_name}"
return self._run(
f"GetDefaultTarget of node {node_name}",
cmd,
check_result,
expected_result,
)

def set_default_target(
self,
node_name: str = "",
target: str = "",
check_result: bool = True,
expected_result: int = 0,
) -> Tuple[Optional[int], Union[Iterator[bytes], Any, Tuple[bytes, bytes]]]:
cmd = f"set-default {node_name} {target} true"
return self._run(
f"SetDefaultTarget of node {node_name} ",
cmd,
check_result,
expected_result,
)

def kill_unit(
self,
node_name: str,
Expand Down
12 changes: 12 additions & 0 deletions tests/bluechi_test/systemctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ def restart_unit(
unit_name, "restart", check_result, expected_result
)

def set_default_target(
self, target: str, check_result: bool = True, expected_result: int = 0
) -> Tuple[Optional[int], Union[Iterator[bytes], Any, Tuple[bytes, bytes]]]:
command = f"set-default {target}"
return self._do_operation(command, check_result, expected_result)

def get_default_target(
self, check_result: bool = True, expected_result: int = 0
) -> Tuple[Optional[int], Union[Iterator[bytes], Any, Tuple[bytes, bytes]]]:
command = f"get-default"
return self._do_operation(command, check_result, expected_result)

def reset_failed_for_unit(
self, unit_name: str, check_result: bool = True, expected_result: int = 0
) -> Tuple[Optional[int], Union[Iterator[bytes], Any, Tuple[bytes, bytes]]]:
Expand Down
Loading

0 comments on commit d4967b4

Please sign in to comment.