Skip to content

Commit

Permalink
Set Default Target
Browse files Browse the repository at this point in the history
Added Api to set the default target

Signed-off-by: Artiom Divak <[email protected]>
  • Loading branch information
ArtiomDivak committed Oct 3, 2024
1 parent 5eec39c commit 7e28ac4
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 73 deletions.
14 changes: 14 additions & 0 deletions data/org.eclipse.bluechi.Node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@
<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
5 changes: 5 additions & 0 deletions data/org.eclipse.bluechi.internal.Agent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
<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
4 changes: 4 additions & 0 deletions doc/man/bluechictl.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,7 @@ 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*] [file.target]

Changes the default target to `file.target` on the chosen `bluechi-agent`.
1 change: 1 addition & 0 deletions src/agent/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,7 @@ static const sd_bus_vtable internal_agent_vtable[] = {
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
33 changes: 25 additions & 8 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 All @@ -891,14 +900,6 @@ def get_unit_file_state(self, file: str) -> str:
return self.get_proxy().GetUnitFileState(
file,
)
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_properties(self, name: str, interface: str) -> Structure:
"""
Expand Down Expand Up @@ -1031,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
3 changes: 2 additions & 1 deletion src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

#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-get-default-target.h"
#include "method-help.h"
#include "method-is-enabled.h"
#include "method-kill.h"
Expand Down Expand Up @@ -62,6 +62,7 @@ const Method methods[] = {
{ "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", 3, 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 }
};
Expand Down
2 changes: 1 addition & 1 deletion src/client/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ client_src = [
'method-enable-disable.c',
'method-reset-failed.c',
'method-daemon-reload.c',
'method-get-default-target.c',
'method-default-target.c',
'usage.c',
]

Expand Down
131 changes: 131 additions & 0 deletions src/client/method-default-target.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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("The default target of this node is: %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 = 0;
if (!strcmp(force, "true")) {
force_b = 1;
} else if (!strcmp(force, "false")) {
force_b = 0;
} else {
fprintf(stderr, "Please provide a true/false value for force parameter\n");
return -1;
}


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");

printf("The default target of node %s is now: %s\n", node_name, target);

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]");
}


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) {
return method_set_default_target_on(
userdata, command->opargv[0], command->opargv[1], command->opargv[2]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@

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();
57 changes: 0 additions & 57 deletions src/client/method-get-default-target.c

This file was deleted.

2 changes: 2 additions & 0 deletions src/client/method-help.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ void usage_bluechi() {
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] [force]\n");
}

int method_help(UNUSED Command *command, UNUSED void *userdata) {
Expand Down
1 change: 1 addition & 0 deletions src/controller/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ static const sd_bus_vtable node_vtable[] = {
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
16 changes: 16 additions & 0 deletions tests/bluechi_test/bluechictl.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ def get_default_target(
expected_result,
)

def set_default_target(
self,
node_name: str = "",
target: str = "",
force: str = "true",
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} {force}"
return self._run(
f"SetDefaultTarget of node {node_name} ",
cmd,
check_result,
expected_result,
)

def kill_unit(
self,
node_name: str,
Expand Down
2 changes: 0 additions & 2 deletions tests/tests/tier0/bluechi-get-default-target/main.fmf

This file was deleted.

2 changes: 2 additions & 0 deletions tests/tests/tier0/bluechictl-set-get-default-target/main.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
summary: Test the set and get default target function
id: 6cfa53b6-7429-43fd-a9b5-ff59e98b7408
Loading

0 comments on commit 7e28ac4

Please sign in to comment.