From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 727B576986 for ; Fri, 16 Jul 2021 12:44:25 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 5B940F7FE for ; Fri, 16 Jul 2021 12:43:55 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id C55E8F7F4 for ; Fri, 16 Jul 2021 12:43:54 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 986B441F6D for ; Fri, 16 Jul 2021 12:43:54 +0200 (CEST) From: Thomas Lamprecht To: pmg-devel@lists.proxmox.com Date: Fri, 16 Jul 2021 12:43:38 +0200 Message-Id: <20210716104338.568537-1-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.186 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_NUMSUBJECT 0.5 Subject ends in numbers excluding current years SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pmg-devel] applied: [PATCH] api: implement live network reload with ifupdown2 X-BeenThere: pmg-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Mail Gateway development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Jul 2021 10:44:25 -0000 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 --- 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