Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Default Target #951

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions data/org.eclipse.bluechi.Node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,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 @@ -93,6 +93,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
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 @@ -1786,6 +1785,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_properties(self, name: str, interface: str) -> Structure:
"""
GetUnitProperties:
Expand Down Expand Up @@ -1011,6 +1020,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
44 changes: 24 additions & 20 deletions src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "method-daemon-reload.h"
#include "method-enable-disable.h"
#include "method-freeze-thaw.h"
#include "method-get-default-target.h"
#include "method-help.h"
#include "method-kill.h"
#include "method-list-unit-files.h"
Expand All @@ -22,6 +23,7 @@
#include "method-metrics.h"
#include "method-monitor.h"
#include "method-reset-failed.h"
#include "method-set-default-target.h"
#include "method-status.h"
#include "method-unit-lifecycle.h"

Expand All @@ -41,26 +43,28 @@ 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 },
{ "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 },
{ "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", 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 }
};

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

Expand Down
56 changes: 56 additions & 0 deletions src/client/method-get-default-target.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Contributors to the Eclipse BlueChi project
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "method-get-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;
}

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


int method_get_default_target(Command *command, void *userdata) {
return method_get_default_target_on(userdata, command->opargv[0]);
}
11 changes: 11 additions & 0 deletions src/client/method-get-default-target.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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();
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] [force]\n");
}

int method_help(UNUSED Command *command, UNUSED void *userdata) {
Expand Down
85 changes: 85 additions & 0 deletions src/client/method-set-default-target.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright Contributors to the Eclipse BlueChi project
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "method-set-default-target.h"
#include "client.h"
#include "usage.h"

#include "libbluechi/common/opt.h"

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_set_default_target() {
usage_print_header();
usage_print_usage("bluechictl set-default [nodename]");
}


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]);
}
11 changes: 11 additions & 0 deletions src/client/method-set-default-target.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Contributors to the Eclipse BlueChi project
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#pragma once

#include "libbluechi/cli/command.h"

int method_set_default_target(Command *command, void *userdata);
void usage_method_set_default_target();
2 changes: 2 additions & 0 deletions src/controller/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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
30 changes: 30 additions & 0 deletions tests/bluechi_test/bluechictl.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,36 @@ 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}".strip()
return self._run(
f"GetDefaultTarget of node {node_name}",
cmd,
check_result,
expected_result,
)

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

def kill_unit(
self,
node_name: str,
Expand Down
Loading