Skip to content

Commit

Permalink
flux-config: add get subcommand
Browse files Browse the repository at this point in the history
Problem: flux_get_conf() only works in the broker or its loadable
modules; there is no way to access the broker's config from other
locations, for example from rc files.

Add flux-config get subcommand.
Run without arguments, the entire config object is dumped
Run wtih a 'path' argument, a subset of the object, or an individual key
may be dumped.

Output is in compact JSON form.

Fixes flux-framework#4161
  • Loading branch information
garlick committed Feb 25, 2022
1 parent ae70caf commit 289841e
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/cmd/builtin/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
* SPDX-License-Identifier: LGPL-3.0
\************************************************************/

#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <jansson.h>

#include "src/common/libutil/jpath.h"

#include "builtin.h"


static int internal_config_reload (optparse_t *p, int ac, char *av[])
{
flux_t *h;
Expand All @@ -30,6 +38,43 @@ static int internal_config_reload (optparse_t *p, int ac, char *av[])
return (0);
}

static int config_get (optparse_t *p, int ac, char *av[])
{
int optindex = optparse_option_index (p);
flux_t *h;
flux_future_t *f = NULL;
json_t *o;
const char *path = NULL;
char *val;

if (optindex < ac)
path = av[optindex++];
if (ac - optindex > 0) {
optparse_print_usage (p);
exit (1);
}
if (!(h = builtin_get_flux_handle (p)))
log_err_exit ("flux_open");
if (!(f = flux_rpc (h, "config.get", NULL, FLUX_NODEID_ANY, 0))
|| flux_rpc_get_unpack (f, "o", &o) < 0)
log_msg_exit ("Error fetching config object: %s",
future_strerror (f, errno));
if (path) {
if (!(o = jpath_get (o, path))) {
if (errno == ENOENT)
log_msg_exit ("Unknown config key '%s'", path);
log_err_exit ("%s", path);
}
}
if (!(val = json_dumps (o, JSON_COMPACT | JSON_ENCODE_ANY)))
log_msg_exit ("error encoding json object");
printf ("%s\n", val);
free (val);
flux_future_destroy (f);
flux_close (h);
return (0);
}

int cmd_config (optparse_t *p, int ac, char *av[])
{
log_init ("flux-config");
Expand All @@ -42,11 +87,18 @@ int cmd_config (optparse_t *p, int ac, char *av[])
static struct optparse_subcommand config_subcmds[] = {
{ "reload",
"[OPTIONS]",
"Reload configuration from files",
"Reload broker configuration from files",
internal_config_reload,
0,
NULL,
},
{ "get",
"[OPTIONS]",
"Query broker configuration",
config_get,
0,
NULL,
},
OPTPARSE_SUBCMD_END
};

Expand Down

0 comments on commit 289841e

Please sign in to comment.