public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pmg-devel] applied: [PATCH] api: implement live network reload with ifupdown2
@ 2021-07-16 10:43 Thomas Lamprecht
  0 siblings, 0 replies; only message in thread
From: Thomas Lamprecht @ 2021-07-16 10:43 UTC (permalink / raw)
  To: pmg-devel

Like most of the other call here, copied over from PVE, with the SDN
stuff dropped and some task-log feedback if we actually moved a
pending change in. Also adding error handling for the rename, both
should be added to PVE too.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---

As it's basucally what we do in PVE minus SDN and permissions for root-only I
just pushed it out. Still, holler at me if anything is off.

 src/PMG/API2/Network.pm | 59 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/src/PMG/API2/Network.pm b/src/PMG/API2/Network.pm
index a64a394..b6aa87b 100644
--- a/src/PMG/API2/Network.pm
+++ b/src/PMG/API2/Network.pm
@@ -410,6 +410,65 @@ __PACKAGE__->register_method({
 	return undef;
     }});
 
+sub ifupdown2_version {
+    my $v;
+    PVE::Tools::run_command(['ifreload', '-V'], outfunc => sub { $v //= shift });
+    return if !defined($v) || $v !~ /^\s*ifupdown2:(\S+)\s*$/;
+    $v = $1;
+    my ($major, $minor, $extra, $pve) = split(/\.|-/, $v);
+    my $is_pve = defined($pve) && $pve =~ /(pve|pmx|proxmox)/;
+
+    return ($major * 100000 + $minor * 1000 + $extra * 10, $is_pve, $v);
+}
+sub assert_ifupdown2_installed {
+    die "you need ifupdown2 to reload network configuration\n" if ! -e '/usr/share/ifupdown2';
+    my ($v, $pve, $v_str) = ifupdown2_version();
+    die "incompatible 'ifupdown2' package version '$v_str'! Did you installed from Proxmox repositories?\n"
+        if $v < (1*100000 + 2*1000 + 8*10) || !$pve;
+}
+
+__PACKAGE__->register_method({
+    name => 'reload_network_config',
+    path => '',
+    method => 'PUT',
+    description => "Reload network configuration",
+    protected => 1,
+    proxyto => 'node',
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	},
+    },
+    returns => { type => 'string' },
+    code => sub {
+
+	my ($param) = @_;
+
+	my $rpcenv = PMG::RESTEnvironment->get();
+	my $authuser = $rpcenv->get_user();
+
+	assert_ifupdown2_installed();
+
+	my $current_config = "/etc/network/interfaces";
+	my $new_config = "$current_config.new";
+
+	my $worker = sub {
+	    if (-e $new_config) {
+		print "found changes, renaming '$new_config' -> '$current_config'\n";
+		rename($new_config, $current_config) or die "could not rename new config file - $!";
+	    }
+
+	    PVE::Tools::run_command(['ifreload', '-a'], errfunc => sub {
+		my $line = shift;
+		if ($line =~ /(warning|error): (\S+):/) {
+		    print "$2 : $line \n";
+		}
+	    });
+	};
+	return $rpcenv->fork_worker('srvreload', 'networking', $authuser, $worker);
+   }});
+
 __PACKAGE__->register_method({
     name => 'update_network',
     path => '{iface}',
-- 
2.30.2





^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-07-16 10:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-16 10:43 [pmg-devel] applied: [PATCH] api: implement live network reload with ifupdown2 Thomas Lamprecht

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal