Skip to content

Commit

Permalink
bluechictl: status command to show nodes list
Browse files Browse the repository at this point in the history
In this commit we remove the `bluechictl monitor node-connection`
functionality and move it to `bluechictl status`, slightly expanding
it.

`bluechictl status` will print the list of nodes with their status
and when it was last seen.

`bluechictl status node` will print the same for for the node `node`

Also adding `-w/--watch` flag to `bluechictl status` command,
allowing to continuously monitor the node/nodes status (like the
original `bluechictl monitor node-connection`

Signed-off-by: Mark Kemel <mkemel@redhat.com>
  • Loading branch information
mkemel committed Jan 15, 2024
1 parent ed15bd5 commit 5a9f5f2
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 387 deletions.
31 changes: 31 additions & 0 deletions src/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "client.h"

#include "libbluechi/service/shutdown.h"

Client *new_client() {
Client *client = malloc0(sizeof(Client));
if (client == NULL) {
Expand Down Expand Up @@ -135,3 +137,32 @@ int client_create_message_new_method_call(
*new_message = sd_bus_message_ref(outgoing_message);
return 0;
}

int client_start_event_loop(Client *client) {
_cleanup_sd_event_ sd_event *event = NULL;
int r = sd_event_default(&event);
if (r < 0) {
fprintf(stderr, "Failed to create event loop: %s", strerror(-r));
return r;
}

r = sd_bus_attach_event(client->api_bus, event, SD_EVENT_PRIORITY_NORMAL);
if (r < 0) {
fprintf(stderr, "Failed to attach api bus to event: %s", strerror(-r));
return r;
}

r = event_loop_add_shutdown_signals(event, NULL);
if (r < 0) {
fprintf(stderr, "Failed to add signals to agent event loop: %s", strerror(-r));
return r;
}

r = sd_event_loop(event);
if (r < 0) {
fprintf(stderr, "Failed to start event loop: %s", strerror(-r));
return r;
}

return 0;
}
2 changes: 2 additions & 0 deletions src/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ DEFINE_CLEANUP_FUNC(Client, client_unref)

int client_create_message_new_method_call(
Client *client, const char *node_name, const char *member, sd_bus_message **new_message);

int client_start_event_loop(Client *client);
7 changes: 5 additions & 2 deletions src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define OPT_FORCE 1u << 2u
#define OPT_RUNTIME 1u << 3u
#define OPT_NO_RELOAD 1u << 4u
#define OPT_WATCH 1u << 5u

int method_version(UNUSED Command *command, UNUSED void *userdata) {
printf("bluechictl version %s\n", CONFIG_H_BC_VERSION);
Expand All @@ -41,7 +42,7 @@ const Method methods[] = {
{ "enable", 2, ARG_ANY, OPT_FORCE | OPT_RUNTIME | OPT_NO_RELOAD, method_enable, usage_bluechi},
{ "disable", 2, ARG_ANY, OPT_NONE, method_disable, usage_bluechi},
{ "daemon-reload", 1, 1, OPT_NONE, method_daemon_reload, usage_bluechi},
{ "status", 2, ARG_ANY, OPT_NONE, method_status, 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 }
Expand All @@ -52,16 +53,18 @@ const OptionType option_types[] = {
{ ARG_FORCE_SHORT, ARG_FORCE, OPT_FORCE },
{ ARG_RUNTIME_SHORT, ARG_RUNTIME, OPT_RUNTIME },
{ ARG_NO_RELOAD_SHORT, ARG_NO_RELOAD, OPT_NO_RELOAD},
{ ARG_WATCH_SHORT, ARG_WATCH, OPT_WATCH },
{ 0, NULL, 0 }
};

#define GETOPT_OPTSTRING ARG_HELP_SHORT_S ARG_FORCE_SHORT_S
#define GETOPT_OPTSTRING ARG_HELP_SHORT_S ARG_FORCE_SHORT_S ARG_WATCH_SHORT_S
const struct option getopt_options[] = {
{ARG_HELP, no_argument, 0, ARG_HELP_SHORT },
{ ARG_FILTER, required_argument, 0, ARG_FILTER_SHORT },
{ ARG_FORCE, no_argument, 0, ARG_FORCE_SHORT },
{ ARG_RUNTIME, no_argument, 0, ARG_RUNTIME_SHORT },
{ ARG_NO_RELOAD, no_argument, 0, ARG_NO_RELOAD_SHORT},
{ ARG_WATCH, no_argument, 0, ARG_WATCH_SHORT },
{ NULL, 0, 0, '\0' }
};

Expand Down
4 changes: 2 additions & 2 deletions src/client/method-help.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ void usage_bluechi() {
printf(" usage: metrics listen\n");
printf(" - monitor: creates a monitor on the given node to observe changes in the specified units\n");
printf(" usage: monitor [node] [unit1,unit2,...]\n");
printf(" - monitor node-connection: creates a monitor to observe changes in state of all nodes\n");
printf(" usage: monitor node-connection\n");
printf(" - status: shows the status of a node, or statuses of all nodes, or status of a unit on node\n");
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");
}
Expand Down
Loading

0 comments on commit 5a9f5f2

Please sign in to comment.