public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] applied: [PATCH] api: implement live network reload with ifupdown2
Date: Fri, 16 Jul 2021 12:43:38 +0200	[thread overview]
Message-ID: <20210716104338.568537-1-t.lamprecht@proxmox.com> (raw)

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





                 reply	other threads:[~2021-07-16 10:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210716104338.568537-1-t.lamprecht@proxmox.com \
    --to=t.lamprecht@proxmox.com \
    --cc=pmg-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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