Skip to content

Commit

Permalink
Adding usage functions to bluechictl commands
Browse files Browse the repository at this point in the history
Relates to: eclipse-bluechi#691

Based on the previous refactoring, usage functions have also been
added to the other bluechictl commands and integrated into main.

Signed-off-by: Michael Engel <[email protected]>
  • Loading branch information
engelmi committed Sep 13, 2024
1 parent 65ee2fb commit 1fc7741
Show file tree
Hide file tree
Showing 21 changed files with 135 additions and 32 deletions.
46 changes: 25 additions & 21 deletions src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
#include "libbluechi/common/opt.h"

#include "client.h"
#include "method-daemon-reload.h"
#include "method-enable-disable.h"
#include "method-freeze-thaw.h"
#include "method-help.h"
#include "method-kill.h"
#include "method-list-unit-files.h"
#include "method-list-units.h"
#include "method-loglevel.h"
#include "method-metrics.h"
#include "method-monitor.h"
#include "method-reset-failed.h"
#include "method-status.h"
#include "method-unit-actions.h"
#include "method-unit-lifecycle.h"

#define OPT_NONE 0u
#define OPT_HELP 1u << 0u
Expand All @@ -37,26 +41,26 @@ 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_bluechi },
{ "list-units", 0, 1, OPT_FILTER, method_list_units, usage_bluechi },
{ "start", 2, 2, OPT_NONE, method_start, usage_bluechi },
{ "stop", 2, 2, OPT_NONE, method_stop, usage_bluechi },
{ "freeze", 2, 2, OPT_NONE, method_freeze, usage_bluechi },
{ "thaw", 2, 2, OPT_NONE, method_thaw, usage_bluechi },
{ "restart", 2, 2, OPT_NONE, method_restart, usage_bluechi },
{ "reload", 2, 2, OPT_NONE, method_reload, usage_bluechi },
{ "reset-failed", 0, ARG_ANY, OPT_NONE, method_reset_failed, usage_bluechi },
{ "kill", 2, 2, OPT_KILL_WHOM | OPT_SIGNAL, method_kill, usage_method_kill },
{ "monitor", 0, 2, OPT_NONE, method_monitor, usage_bluechi },
{ "metrics", 1, 1, OPT_NONE, method_metrics, usage_bluechi },
{ "enable", 2, ARG_ANY, OPT_FORCE | OPT_RUNTIME | OPT_NO_RELOAD, method_enable, usage_bluechi },
{ "disable", 2, ARG_ANY, OPT_NO_RELOAD, method_disable, usage_bluechi },
{ "daemon-reload", 1, 1, OPT_NONE, method_daemon_reload, usage_bluechi },
{ "status", 0, ARG_ANY, OPT_WATCH, method_status, usage_bluechi },
{ "set-loglevel", 1, 2, OPT_NONE, method_set_loglevel, usage_bluechi },
{ "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 },
{ "version", 0, 0, OPT_NONE, method_version, usage_bluechi },
{ NULL, 0, 0, 0, NULL, NULL }
};

const OptionType option_types[] = {
Expand Down
6 changes: 5 additions & 1 deletion src/client/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ client_src = [
'method-list-units.c',
'method-metrics.c',
'method-monitor.c',
'method-unit-actions.c',
'method-unit-lifecycle.c',
'method-status.c',
'method-kill.c',
'method-freeze-thaw.c',
'method-enable-disable.c',
'method-reset-failed.c',
'method-daemon-reload.c',
'usage.c',
]

Expand Down
1 change: 1 addition & 0 deletions src/client/method-daemon-reload.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#include "method-daemon-reload.h"
#include "client.h"
#include "usage.h"

#include "libbluechi/common/opt.h"

Expand Down
5 changes: 3 additions & 2 deletions src/client/method-enable-disable.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "method-enable-disable.h"
#include "client.h"
#include "method-daemon-reload.h"
#include "usage.h"

#include "libbluechi/common/opt.h"

Expand Down Expand Up @@ -174,7 +175,7 @@ int method_enable(Command *command, void *userdata) {
}

if (!command_flag_exists(command, ARG_NO_RELOAD_SHORT)) {
r = method_daemon_reload_on(userdata, command->opargv[0]);
r = method_daemon_reload(command, userdata);
}

return r;
Expand All @@ -195,7 +196,7 @@ int method_disable(Command *command, void *userdata) {
strerror(-r));
}
if (!command_flag_exists(command, ARG_NO_RELOAD_SHORT)) {
r = method_daemon_reload_on(userdata, command->opargv[0]);
r = method_daemon_reload(command, userdata);
}

return r;
Expand Down
1 change: 1 addition & 0 deletions src/client/method-freeze-thaw.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#include "method-freeze-thaw.h"
#include "client.h"
#include "usage.h"

#include "libbluechi/common/opt.h"

Expand Down
11 changes: 3 additions & 8 deletions src/client/method-freeze-thaw.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@

#include "libbluechi/cli/command.h"

int method_start(Command *command, void *userdata);
int method_stop(Command *command, void *userdata);
int method_restart(Command *command, void *userdata);
int method_reset_failed(Command *command, void *userdata);
int method_reload(Command *command, void *userdata);
int method_freeze(Command *command, void *userdata);
void usage_method_freeze();

int method_thaw(Command *command, void *userdata);
int method_enable(Command *command, void *userdata);
int method_disable(Command *command, void *userdata);
int method_daemon_reload(Command *command, void *userdata);
void usage_method_thaw();
2 changes: 2 additions & 0 deletions src/client/method-help.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ void usage_bluechi() {
printf(" usage: status [nodename [unitname]] [-w/--watch]\n");
printf(" - daemon-reload: reload systemd daemon on a specific node\n");
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");
}

int method_help(UNUSED Command *command, UNUSED void *userdata) {
Expand Down
12 changes: 12 additions & 0 deletions src/client/method-list-unit-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "client.h"
#include "method-list-unit-files.h"
#include "usage.h"

#include "libbluechi/common/math-util.h"
#include "libbluechi/common/opt.h"
Expand Down Expand Up @@ -215,3 +216,14 @@ int method_list_unit_files(Command *command, void *userdata) {
return method_list_unit_files_on(
client->api_bus, command->opargv[0], print_unit_file_list_simple, filter_glob);
}

void usage_method_list_unit_files() {
usage_print_header();
usage_print_description("Get a list of installed systemd unit files");
usage_print_usage("bluechictl list-unit-files [nodename] [options]");
printf(" If [nodename] is not given, the systemd unit files of all nodes are queried.\n");
printf("\n");
printf("Available options:\n");
printf(" --%s \t shows this help message\n", ARG_HELP);
printf(" --%s \t filter the queried systemd unit files by name using a glob\n", ARG_FILTER);
}
3 changes: 3 additions & 0 deletions src/client/method-list-unit-files.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#pragma once

#include "libbluechi/cli/command.h"

int method_list_unit_files(Command *command, void *userdata);
void usage_method_list_unit_files();
12 changes: 12 additions & 0 deletions src/client/method-list-units.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "client.h"
#include "method-list-units.h"
#include "usage.h"

#include "libbluechi/common/math-util.h"
#include "libbluechi/common/opt.h"
Expand Down Expand Up @@ -214,3 +215,14 @@ int method_list_units(Command *command, void *userdata) {
}
return method_list_units_on(client->api_bus, command->opargv[0], print_unit_list_simple, filter_glob);
}

void usage_method_list_units() {
usage_print_header();
usage_print_description("Get a list of loaded systemd units");
usage_print_usage("bluechictl list-units [nodename] [options]");
printf(" If [nodename] is not given, the systemd units of all nodes are queried.\n");
printf("\n");
printf("Available options:\n");
printf(" --%s \t shows this help message\n", ARG_HELP);
printf(" --%s \t filter the queried systemd units by name using a glob\n", ARG_FILTER);
}
3 changes: 3 additions & 0 deletions src/client/method-list-units.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#pragma once

#include "libbluechi/cli/command.h"

int method_list_units(Command *command, void *userdata);
void usage_method_list_units();
13 changes: 13 additions & 0 deletions src/client/method-loglevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#include "method-loglevel.h"
#include "client.h"
#include "usage.h"

static int method_set_loglevel_on(Client *client, char *node_name, char *loglevel) {
int r = 0;
Expand Down Expand Up @@ -63,3 +64,15 @@ int method_set_loglevel(Command *command, void *userdata) {
}
return method_set_loglevel_on(userdata, command->opargv[0], command->opargv[1]);
}

void usage_method_set_loglevel() {
usage_print_header();
usage_print_description("Set the LogLevel of BlueChi");
usage_print_usage("bluechictl set-loglevel [nodename] [loglevel]");
printf(" If [nodename] is not given, the [loglevel] will be set for the bluechi-controller.\n");
printf(" [loglevel] has to be one of [DEBUG, INFO, WARN, ERROR].\n");
printf("\n");
printf("Examples:\n");
printf(" bluechictl set-loglevel INFO\n");
printf(" bluechictl set-loglevel primary DEBUG\n");
}
1 change: 1 addition & 0 deletions src/client/method-loglevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
#include "libbluechi/cli/command.h"

int method_set_loglevel(Command *command, void *userdata);
void usage_method_set_loglevel();
11 changes: 11 additions & 0 deletions src/client/method-metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#include "method-metrics.h"
#include "client.h"
#include "usage.h"

#include "libbluechi/common/string-util.h"
#include "libbluechi/common/time-util.h"
Expand Down Expand Up @@ -155,3 +156,13 @@ int method_metrics(Command *command, void *userdata) {
return -EINVAL;
}
}

void usage_method_metrics() {
usage_print_header();
usage_print_description("View metrics for start/stop systemd units via BlueChi");
usage_print_usage("bluechictl metrics [enable|disable|listen]");
printf("\n");
printf("Examples:\n");
printf(" bluechictl metrics enable\n");
printf(" bluechictl metrics listen\n");
}
1 change: 1 addition & 0 deletions src/client/method-metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
#include "libbluechi/cli/command.h"

int method_metrics(Command *command, void *userdata);
void usage_method_metrics();
15 changes: 15 additions & 0 deletions src/client/method-monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "client.h"
#include "method-monitor.h"
#include "usage.h"

#include "libbluechi/common/common.h"
#include "libbluechi/common/list.h"
Expand Down Expand Up @@ -293,3 +294,17 @@ int method_monitor(Command *command, void *userdata) {
}
return method_monitor_units_on_nodes(userdata, arg0, arg1);
}

void usage_method_monitor() {
usage_print_header();
usage_print_description("Monitor changes in the BlueChi managed system");
usage_print_usage("bluechictl monitor [nodename] [unitname]");
printf(" If [nodename] and [unitname] are not given, changes on all nodes for all units will be monitored.\n");
printf(" If a wildcard '*' is used for [nodename] and/or [unitname], all nodes and/or units are monitored.\n");
printf("\n");
printf("Examples:\n");
printf(" bluechictl monitor\n");
printf(" bluechictl monitor primary \\*\n");
printf(" bluechictl monitor \\* interesting.service\n");
printf(" bluechictl monitor primary interesting.service\n");
}
3 changes: 3 additions & 0 deletions src/client/method-monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#pragma once

#include <systemd/sd-bus.h>

#include "libbluechi/cli/command.h"

int method_monitor(Command *command, void *userdata);
void usage_method_monitor();
1 change: 1 addition & 0 deletions src/client/method-reset-failed.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#include "method-reset-failed.h"
#include "client.h"
#include "usage.h"

#include "libbluechi/common/opt.h"

Expand Down
16 changes: 16 additions & 0 deletions src/client/method-status.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#include "method-status.h"
#include "client.h"
#include "usage.h"

#include "libbluechi/bus/utils.h"
#include "libbluechi/common/common.h"
Expand Down Expand Up @@ -784,3 +785,18 @@ int method_status(Command *command, void *userdata) {
command_flag_exists(command, ARG_WATCH_SHORT));
}
}


void usage_method_status() {
usage_print_header();
usage_print_description("View status for units and nodes of BlueChi");
usage_print_usage("bluechictl status [nodename] [unitname] [options]");
printf(" If [nodename] and [unitname] are not given, the status of all nodes will be displayed.\n");
printf(" If [unitname] is not given, the status of the specific node will be displayed.\n");
printf("\n");
printf("Available options:\n");
printf(" --%s \t shows this help message\n", ARG_HELP);
printf(" --%s, -%c \t continuously watch the status of the node(s) and unit \n",
ARG_WATCH,
ARG_WATCH_SHORT);
}
3 changes: 3 additions & 0 deletions src/client/method-status.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#pragma once

#include <stddef.h>

#include "libbluechi/cli/command.h"

int method_status(Command *command, void *userdata);
void usage_method_status();
1 change: 1 addition & 0 deletions src/client/method-unit-lifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#include "method-unit-lifecycle.h"
#include "client.h"
#include "usage.h"

#include "libbluechi/common/opt.h"

Expand Down

0 comments on commit 1fc7741

Please sign in to comment.