From 1fc774161570db0d115543b96c549f7dd9deb20d Mon Sep 17 00:00:00 2001 From: Michael Engel Date: Thu, 12 Sep 2024 18:46:43 +0200 Subject: [PATCH] Adding usage functions to bluechictl commands Relates to: https://github.com/eclipse-bluechi/bluechi/issues/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 --- src/client/main.c | 46 ++++++++++++++++------------- src/client/meson.build | 6 +++- src/client/method-daemon-reload.c | 1 + src/client/method-enable-disable.c | 5 ++-- src/client/method-freeze-thaw.c | 1 + src/client/method-freeze-thaw.h | 11 ++----- src/client/method-help.c | 2 ++ src/client/method-list-unit-files.c | 12 ++++++++ src/client/method-list-unit-files.h | 3 ++ src/client/method-list-units.c | 12 ++++++++ src/client/method-list-units.h | 3 ++ src/client/method-loglevel.c | 13 ++++++++ src/client/method-loglevel.h | 1 + src/client/method-metrics.c | 11 +++++++ src/client/method-metrics.h | 1 + src/client/method-monitor.c | 15 ++++++++++ src/client/method-monitor.h | 3 ++ src/client/method-reset-failed.c | 1 + src/client/method-status.c | 16 ++++++++++ src/client/method-status.h | 3 ++ src/client/method-unit-lifecycle.c | 1 + 21 files changed, 135 insertions(+), 32 deletions(-) diff --git a/src/client/main.c b/src/client/main.c index 7e2b7c8e2f..d02ba90643 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -11,6 +11,9 @@ #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" @@ -18,8 +21,9 @@ #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 @@ -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[] = { diff --git a/src/client/meson.build b/src/client/meson.build index 8faefb6f9f..f456ac8493 100644 --- a/src/client/meson.build +++ b/src/client/meson.build @@ -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', ] diff --git a/src/client/method-daemon-reload.c b/src/client/method-daemon-reload.c index ed75afc612..a6da006f87 100644 --- a/src/client/method-daemon-reload.c +++ b/src/client/method-daemon-reload.c @@ -5,6 +5,7 @@ */ #include "method-daemon-reload.h" #include "client.h" +#include "usage.h" #include "libbluechi/common/opt.h" diff --git a/src/client/method-enable-disable.c b/src/client/method-enable-disable.c index 3778fb5b98..0681c9f101 100644 --- a/src/client/method-enable-disable.c +++ b/src/client/method-enable-disable.c @@ -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" @@ -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; @@ -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; diff --git a/src/client/method-freeze-thaw.c b/src/client/method-freeze-thaw.c index d9e1eab4f2..bfb6c33b27 100644 --- a/src/client/method-freeze-thaw.c +++ b/src/client/method-freeze-thaw.c @@ -5,6 +5,7 @@ */ #include "method-freeze-thaw.h" #include "client.h" +#include "usage.h" #include "libbluechi/common/opt.h" diff --git a/src/client/method-freeze-thaw.h b/src/client/method-freeze-thaw.h index 3b41e536ce..cce281eec5 100644 --- a/src/client/method-freeze-thaw.h +++ b/src/client/method-freeze-thaw.h @@ -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(); diff --git a/src/client/method-help.c b/src/client/method-help.c index b59473e5f4..825e18b291 100644 --- a/src/client/method-help.c +++ b/src/client/method-help.c @@ -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) { diff --git a/src/client/method-list-unit-files.c b/src/client/method-list-unit-files.c index f6f1b0ce3f..35bdac37f0 100644 --- a/src/client/method-list-unit-files.c +++ b/src/client/method-list-unit-files.c @@ -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" @@ -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); +} diff --git a/src/client/method-list-unit-files.h b/src/client/method-list-unit-files.h index 1bed0c0ac2..0d0e80638f 100644 --- a/src/client/method-list-unit-files.h +++ b/src/client/method-list-unit-files.h @@ -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(); diff --git a/src/client/method-list-units.c b/src/client/method-list-units.c index c9499cd761..6dfd4edc01 100644 --- a/src/client/method-list-units.c +++ b/src/client/method-list-units.c @@ -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" @@ -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); +} diff --git a/src/client/method-list-units.h b/src/client/method-list-units.h index b17d56f78c..8f5a72d716 100644 --- a/src/client/method-list-units.h +++ b/src/client/method-list-units.h @@ -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(); diff --git a/src/client/method-loglevel.c b/src/client/method-loglevel.c index ef4005ae3c..ccd39a6842 100644 --- a/src/client/method-loglevel.c +++ b/src/client/method-loglevel.c @@ -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; @@ -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"); +} diff --git a/src/client/method-loglevel.h b/src/client/method-loglevel.h index a9a8517a83..36b99b2128 100644 --- a/src/client/method-loglevel.h +++ b/src/client/method-loglevel.h @@ -8,3 +8,4 @@ #include "libbluechi/cli/command.h" int method_set_loglevel(Command *command, void *userdata); +void usage_method_set_loglevel(); diff --git a/src/client/method-metrics.c b/src/client/method-metrics.c index 03e1cf34ca..2735dadf9f 100644 --- a/src/client/method-metrics.c +++ b/src/client/method-metrics.c @@ -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" @@ -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"); +} diff --git a/src/client/method-metrics.h b/src/client/method-metrics.h index ad0cad8086..714cf3a608 100644 --- a/src/client/method-metrics.h +++ b/src/client/method-metrics.h @@ -8,3 +8,4 @@ #include "libbluechi/cli/command.h" int method_metrics(Command *command, void *userdata); +void usage_method_metrics(); diff --git a/src/client/method-monitor.c b/src/client/method-monitor.c index c9367c7b28..10ee27c7ef 100644 --- a/src/client/method-monitor.c +++ b/src/client/method-monitor.c @@ -7,6 +7,7 @@ #include "client.h" #include "method-monitor.h" +#include "usage.h" #include "libbluechi/common/common.h" #include "libbluechi/common/list.h" @@ -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"); +} diff --git a/src/client/method-monitor.h b/src/client/method-monitor.h index 76dc39aef7..612f087be3 100644 --- a/src/client/method-monitor.h +++ b/src/client/method-monitor.h @@ -3,8 +3,11 @@ * * SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + #include #include "libbluechi/cli/command.h" int method_monitor(Command *command, void *userdata); +void usage_method_monitor(); diff --git a/src/client/method-reset-failed.c b/src/client/method-reset-failed.c index 4a1167c4da..bd0d240134 100644 --- a/src/client/method-reset-failed.c +++ b/src/client/method-reset-failed.c @@ -5,6 +5,7 @@ */ #include "method-reset-failed.h" #include "client.h" +#include "usage.h" #include "libbluechi/common/opt.h" diff --git a/src/client/method-status.c b/src/client/method-status.c index dfb594978a..58a065e2fc 100644 --- a/src/client/method-status.c +++ b/src/client/method-status.c @@ -5,6 +5,7 @@ */ #include "method-status.h" #include "client.h" +#include "usage.h" #include "libbluechi/bus/utils.h" #include "libbluechi/common/common.h" @@ -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); +} diff --git a/src/client/method-status.h b/src/client/method-status.h index 0ddaf82052..9ecf8f8bbe 100644 --- a/src/client/method-status.h +++ b/src/client/method-status.h @@ -3,8 +3,11 @@ * * SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + #include #include "libbluechi/cli/command.h" int method_status(Command *command, void *userdata); +void usage_method_status(); diff --git a/src/client/method-unit-lifecycle.c b/src/client/method-unit-lifecycle.c index 60764dcc6d..6080b05468 100644 --- a/src/client/method-unit-lifecycle.c +++ b/src/client/method-unit-lifecycle.c @@ -5,6 +5,7 @@ */ #include "method-unit-lifecycle.h" #include "client.h" +#include "usage.h" #include "libbluechi/common/opt.h"