cli/device: add modify command

It modifies the applied connection using the Reapply API.
This commit is contained in:
Lubomir Rintel 2016-06-22 18:25:48 +02:00
parent a0bb8cd6cb
commit 8b4494598d
3 changed files with 181 additions and 5 deletions

View file

@ -30,6 +30,7 @@
#include "utils.h"
#include "common.h"
#include "devices.h"
#include "connections.h"
/* define some prompts */
#define PROMPT_INTERFACE _("Interface: ")
@ -358,6 +359,26 @@ usage_device_reapply (void)
"made since it was last applied.\n\n"));
}
static void
usage_device_modify (void)
{
g_printerr (_("Usage: nmcli connection modify { ARGUMENTS | --help }\n"
"\n"
"ARGUMENTS := <ifname> ([+|-]<setting>.<property> <value>)+\n"
"\n"
"Modify one or more properties currently active on the device without modifying\n"
"the connection profile. The changes have immediate effect. For multi-valued\n"
"properties you can use optional '+' or '-' prefix to the property name.\n"
"The '+' sign allows appending items instead of overwriting the whole value.\n"
"The '-' sign allows removing selected items instead of the whole value.\n"
"\n"
"Examples:\n"
"nmcli dev mod em1 ipv4.method manual ipv4.addr \"192.168.1.2/24, 10.10.1.5/8\"\n"
"nmcli dev mod em1 +ipv4.dns 8.8.4.4\n"
"nmcli dev mod em1 -ipv4.dns 1\n"
"nmcli dev mod em1 -ipv6.addr \"abbe::cafe/56\"\n"));
}
static void
usage_device_disconnect (void)
{
@ -1997,6 +2018,108 @@ do_device_reapply (NmCli *nmc, int argc, char **argv)
return nmc->return_value;
}
typedef struct {
NmCli *nmc;
int argc;
char **argv;
} ModifyInfo;
static void
modify_reapply_cb (GObject *object, GAsyncResult *result, gpointer user_data)
{
NMDevice *device = NM_DEVICE (object);
ModifyInfo *info = user_data;
NmCli *nmc = info->nmc;
GError *error = NULL;
if (!nm_device_reapply_finish (device, result, &error)) {
g_string_printf (nmc->return_text, _("Error: Reapplying connection to device '%s' (%s) failed: %s"),
nm_device_get_iface (device),
nm_object_get_path (NM_OBJECT (device)),
error->message);
g_error_free (error);
nmc->return_value = NMC_RESULT_ERROR_DEV_DISCONNECT;
} else {
if (nmc->print_output == NMC_PRINT_PRETTY)
nmc_terminal_erase_line ();
g_print (_("Connection successfully reapplied to device '%s'.\n"),
nm_device_get_iface (device));
}
g_slice_free (ModifyInfo, info);
quit ();
}
static void
modify_get_applied_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
NMDevice *device = NM_DEVICE (object);
ModifyInfo *info = user_data;
NmCli *nmc = info->nmc;
gs_free_error GError *error = NULL;
NMConnection *connection;
guint64 version_id;
connection = nm_device_get_applied_connection_finish (device,
result,
&version_id,
&error);
if (!connection) {
g_string_printf (nmc->return_text, _("Error: Reading applied connection from device '%s' (%s) failed: %s"),
nm_device_get_iface (device),
nm_object_get_path (NM_OBJECT (device)),
error->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_slice_free (ModifyInfo, info);
quit ();
return;
}
if (!nmc_read_connection_properties (info->nmc, connection, &info->argc, &info->argv, &error)) {
g_string_assign (nmc->return_text, error->message);
nmc->return_value = error->code;
g_slice_free (ModifyInfo, info);
quit ();
return;
}
if (nmc->complete)
quit ();
else
nm_device_reapply_async (device, connection, version_id, 0, NULL, modify_reapply_cb, info);
}
static NMCResultCode
do_device_modify (NmCli *nmc, int argc, char **argv)
{
NMDevice *device = NULL;
ModifyInfo *info = NULL;
gs_free_error GError *error = NULL;
device = get_device (nmc, &argc, &argv, &error);
if (!device) {
g_string_printf (nmc->return_text, _("Error: %s."), error->message);
return error->code;
}
if (nmc->timeout == -1)
nmc->timeout = 10;
nmc->nowait_flag = (nmc->timeout == 0);
nmc->should_wait++;
info = g_slice_new0 (ModifyInfo);
info->nmc = nmc;
info->argc = argc;
info->argv = argv;
nm_device_get_applied_connection_async (device, 0, NULL, modify_get_applied_cb, info);
return nmc->return_value;
}
static void
disconnect_device_cb (GObject *object, GAsyncResult *result, gpointer user_data)
{
@ -3757,6 +3880,7 @@ static const NMCCommand device_cmds[] = {
{"monitor", do_devices_monitor, usage_device_monitor },
{"wifi", do_device_wifi, usage_device_wifi },
{"lldp", do_device_lldp, usage_device_lldp },
{"modify", do_device_modify, usage_device_modify },
{NULL, do_devices_status, usage },
};

View file

@ -1184,7 +1184,7 @@ _nmcli()
;;
d|de|dev|devi|devic|device)
if [[ ${#words[@]} -eq 2 ]]; then
_nmcli_compl_COMMAND "$command" status show connect reapply disconnect delete monitor wifi set lldp
_nmcli_compl_COMMAND "$command" status show connect reapply modify disconnect delete monitor wifi set lldp
elif [[ ${#words[@]} -gt 2 ]]; then
case "$command" in
s|st|sta|stat|statu|status)
@ -1199,6 +1199,14 @@ _nmcli()
_nmcli_compl_COMMAND_nl "${words[2]}" "$(_nmcli_dev_status DEVICE)"
fi
;;
mod|modi|modif|modify)
if [[ ${#words[@]} -eq 3 ]]; then
_nmcli_compl_COMMAND_nl "${words[2]}" "$(nmcli --complete-args device modify "" 2>/dev/null)"
else
_nmcli_array_delete_at words 0 1
_nmcli_list_nl "$(nmcli --complete-args device modify "${words[@]}" 2>/dev/null)"
fi
;;
d|di|dis|disc|disco|discon|disconn|disconne|disconnec|disconnect| \
de|del|dele|delet|delete| \
m|mo|mon|moni|monit|monito|monitor)

View file

@ -1163,6 +1163,7 @@
<arg choice='plain'><command>set</command></arg>
<arg choice='plain'><command>connect</command></arg>
<arg choice='plain'><command>reapply</command></arg>
<arg choice='plain'><command>modify</command></arg>
<arg choice='plain'><command>disconnect</command></arg>
<arg choice='plain'><command>delete</command></arg>
<arg choice='plain'><command>monitor</command></arg>
@ -1254,6 +1255,33 @@
</listitem>
</varlistentry>
<varlistentry>
<term>
<command>modify</command>
<arg rep='repeat' choice='plain'>
<group>
<arg choice='plain'><replaceable>option</replaceable> <replaceable>value</replaceable></arg>
<arg choice='plain'>[+|-]<replaceable>setting</replaceable>.<replaceable>property</replaceable> <replaceable>value</replaceable></arg>
</group>
</arg>
</term>
<listitem>
<para>Modify the settings currently active on the device.</para>
<para>This command lets you do temporary changes to a configuration active on
a particular device. The changes are not preserved in the connection profile.</para>
<para>See <citerefentry><refentrytitle>nm-settings</refentrytitle><manvolnum>5</manvolnum>
</citerefentry> for the list of available properties. Please note that some
properties can't be changed on an already connected device.</para>
<para>You can also use the aliases described in
<link linkend='property_aliases' endterm='property_aliases.title' /> section. The syntax is
the same as of the <command>nmcli connection modify</command> command.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<command>disconnect</command>
@ -1599,10 +1627,10 @@
<refsect1 id='property_aliases'><title id='property_aliases.title'>Property Aliases</title>
<para>Apart from the property-value pairs, <command>connection
add</command> and <command>connection modify</command> also accept short forms
of some properties. They exist for convenience and compatiblity with older
versions of <command>nmcli</command> that could not accept the raw
<para>Apart from the property-value pairs, <command>connection add</command>,
<command>connection modify</command> and <command>device modify</command> also
accept short forms of some properties. They exist for convenience and compatiblity
with older versions of <command>nmcli</command> that could not accept the raw
properties.</para>
<para>The overview of the aliases is below. An actual connection type is used to
@ -2228,6 +2256,22 @@ It's equivalent of using <literal>+ipv6.addresses</literal> syntax.</entry>
</listitem>
</varlistentry>
<varlistentry>
<term><userinput>nmcli dev modify em1 ipv4.method shared</userinput></term>
<listitem>
<para>starts IPv4 connection sharing using em1 device. The sharing will be active
until the device is disconnected.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><userinput>nmcli dev modify em1 ipv6.address 2001:db8::a:bad:c0de</userinput></term>
<listitem>
<para>temporarily adds an IP address to a device. The address will be removed
when the same connection is activated again.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><userinput>nmcli connection add type ethernet autoconnect no ifname eth0</userinput></term>
<listitem>