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

cli: rpc commands for reboot/poweroff and set datetime #81

Merged
merged 2 commits into from
Aug 6, 2023
Merged
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
68 changes: 68 additions & 0 deletions src/klinfix/src/klinfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <sysrepo.h>
#include <sysrepo_types.h>
#include <sysrepo/values.h>

#include <klish/kplugin.h>
#include <klish/kcontext.h>
Expand Down Expand Up @@ -140,6 +141,71 @@ int klix_commit(kcontext_t *ctx)
return -1;
}

int klix_rpc(kcontext_t *ctx)
{
kpargv_pargs_node_t *iter;
size_t icnt = 0, ocnt = 0;
sr_val_t *input = NULL;
sr_session_ctx_t *sess;
sr_conn_ctx_t *conn;
const char *xpath;
sr_val_t *output;
kparg_t *parg;
int err;

xpath = kcontext_script(ctx);
if (!xpath) {
fprintf(stderr, "Error: cannot find rpc xpath\n");
goto err;
}

iter = kpargv_pargs_iter(kcontext_pargv(ctx));
while ((parg = kpargv_pargs_each(&iter))) {
const char *key = kentry_name(kparg_entry(parg));
const char *val = kparg_value(parg);

/* skip leading part of command line: 'set datetime' */
if (!strcmp(key, val))
continue;

sr_realloc_values(icnt, icnt + 1, &input);
/* e.g. /ietf-system:set-current-datetime/current-datetime */
sr_val_build_xpath(&input[icnt], "%s/%s", xpath, key);
sr_val_set_str_data(&input[icnt++], SR_STRING_T, val);
}

if (sr_connect(SR_CONN_DEFAULT, &conn)) {
fprintf(stderr, "Error: Connection to datastore failed\n");
goto err;
}

if (sr_session_start(conn, SR_DS_OPERATIONAL, &sess)) {
fprintf(stderr, "Error: Unable to open transaction to running-config\n");
goto err_disconnect;
}

if ((err = sr_rpc_send(sess, xpath, input, icnt, 0, &output, &ocnt))) {
fprintf(stderr, "Failed sending RPC %s: %s", xpath, sr_strerror(err));
goto err_disconnect;
}

for (size_t i = 0; i < ocnt; i++) {
sr_print_val(&output[i]);
puts("");
}

sr_free_values(input, icnt);
sr_free_values(output, ocnt);
sr_disconnect(conn);
return 0;

err_disconnect:
sr_disconnect(conn);
err:
sr_free_values(input, icnt);
return -1;
}

int kplugin_klinfix_fini(kcontext_t *ctx)
{
return 0;
Expand All @@ -151,5 +217,7 @@ int kplugin_klinfix_init(kcontext_t *ctx)

kplugin_add_syms(plugin, ksym_new("klix_copy", klix_copy));
kplugin_add_syms(plugin, ksym_new("klix_commit", klix_commit));
kplugin_add_syms(plugin, ksym_new("klix_rpc", klix_rpc));

return 0;
}
18 changes: 16 additions & 2 deletions src/klinfix/xml/infix.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<ACTION sym="STRING"/>
</PTYPE>

<PTYPE name="GETSTR">
<ACTION sym="STRING"/>
</PTYPE>

<PTYPE name="PLINE_SET">
<COMPL>
<ACTION sym="srp_compl@sysrepo"/>
Expand Down Expand Up @@ -93,11 +97,11 @@
</PROMPT>

<COMMAND name="poweroff" help="Poweroff system (system policy may yield reboot)">
<ACTION sym="script">poweroff</ACTION>
<ACTION sym="klix_rpc@klinfix" ptype="STRING">/ietf-system:system-shutdown</ACTION>
</COMMAND>

<COMMAND name="reboot" help="Reboot system">
<ACTION sym="script">reboot</ACTION>
<ACTION sym="klix_rpc@klinfix" ptype="STRING">/ietf-system:system-restart</ACTION>
</COMMAND>

<COMMAND name="shell" help="Enter system shell">
Expand All @@ -121,7 +125,17 @@
<ACTION sym="klix_copy@klinfix"/>
</COMMAND>

<COMMAND name="set" help="Set" mode="switch">
<COMMAND name="datetime" help="Set current date and time">
<PARAM name="current-datetime" ptype="/GETSTR" help="yyyy-mm-ddThh:mm:ss(Z|+/-hh:mm)"/>
<ACTION sym="klix_rpc@klinfix">/ietf-system:set-current-datetime</ACTION>
</COMMAND>
</COMMAND>

<COMMAND name="show" help="Show" mode="switch">
<COMMAND name="datetime" help="Show current date and time">
<ACTION sym="script">date -Isec</ACTION>
</COMMAND>
<COMMAND name="running-config" help="Show running-config">
<ACTION sym="srp_show_running@sysrepo"/>
</COMMAND>
Expand Down