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 D19E5A778 for ; Wed, 27 Apr 2022 16:06:20 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 6219227360 for ; Wed, 27 Apr 2022 16:05:50 +0200 (CEST) Received: from bastionodiso.odiso.net (bastionodiso.odiso.net [IPv6:2a0a:1580:2000::2d]) (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 3410B272C8 for ; Wed, 27 Apr 2022 16:05:47 +0200 (CEST) Received: from kvmformation3.odiso.net (formationkvm3.odiso.net [10.3.94.12]) by bastionodiso.odiso.net (Postfix) with ESMTP id 92F2AA8BE; Wed, 27 Apr 2022 16:05:40 +0200 (CEST) Received: by kvmformation3.odiso.net (Postfix, from userid 0) id 830FE114F1F; Wed, 27 Apr 2022 16:05:40 +0200 (CEST) From: Alexandre Derumier To: pve-devel@lists.proxmox.com Date: Wed, 27 Apr 2022 16:05:34 +0200 Message-Id: <20220427140536.1606918-5-aderumier@odiso.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220427140536.1606918-1-aderumier@odiso.com> References: <20220427140536.1606918-1-aderumier@odiso.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.004 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% HEADER_FROM_DIFFERENT_DOMAINS 0.248 From and EnvelopeFrom 2nd level mail domains are different KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods NO_DNS_FOR_FROM 0.001 Envelope sender has no MX or A DNS records SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record Subject: [pve-devel] [PATCH v4 qemu-server 4/6] api2: add cloudinit config api X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 27 Apr 2022 14:06:20 -0000 --- PVE/API2/Qemu.pm | 68 ++++++++++++++++++++++++++++++++ PVE/CLI/qm.pm | 1 + PVE/QemuServer/Cloudinit.pm | 78 +++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm index 3af2132..5608ebb 100644 --- a/PVE/API2/Qemu.pm +++ b/PVE/API2/Qemu.pm @@ -21,6 +21,7 @@ use PVE::ReplicationConfig; use PVE::GuestHelpers; use PVE::QemuConfig; use PVE::QemuServer; +use PVE::QemuServer::Cloudinit; use PVE::QemuServer::CPUConfig; use PVE::QemuServer::Drive; use PVE::QemuServer::ImportDisk; @@ -1287,6 +1288,73 @@ __PACKAGE__->register_method({ return PVE::GuestHelpers::config_with_pending_array($conf, $pending_delete_hash); }}); +__PACKAGE__->register_method({ + name => 'cloudinit_pending', + path => '{vmid}/cloudinit', + method => 'GET', + proxyto => 'node', + description => "Get the cloudinit configuration with both current and pending values.", + permissions => { + check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]], + }, + parameters => { + additionalProperties => 0, + properties => { + node => get_standard_option('pve-node'), + vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), + }, + }, + returns => { + type => "array", + items => { + type => "object", + properties => { + key => { + description => "Configuration option name.", + type => 'string', + }, + value => { + description => "Current value.", + type => 'string', + optional => 1, + }, + pending => { + description => "Pending value.", + type => 'string', + optional => 1, + }, + delete => { + description => "Indicates a pending delete request if present and not 0. " . + "The value 2 indicates a force-delete request.", + type => 'integer', + minimum => 0, + maximum => 2, + optional => 1, + }, + }, + }, + }, + code => sub { + my ($param) = @_; + + my $vmid = $param->{vmid}; + my $conf = PVE::QemuConfig->load_config($vmid); + + if (defined($conf->{cipassword}) && + defined($conf->{cloudinit}->{cipassword}) && + $conf->{cipassword} ne $conf->{cloudinit}->{cipassword}) { + $conf->{cipassword} = '********** '; + } elsif (defined($conf->{cipassword})) { + $conf->{cipassword} = '**********'; + } + + $conf->{cloudinit}->{cipassword} = '**********' if defined($conf->{cloudinit}->{cipassword}); + + my $res = PVE::QemuServer::Cloudinit::get_pending_config($conf, $vmid); + + return $res; + }}); + # POST/PUT {vmid}/config implementation # # The original API used PUT (idempotent) an we assumed that all operations diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm index cf0d6f3..ba20dd5 100755 --- a/PVE/CLI/qm.pm +++ b/PVE/CLI/qm.pm @@ -986,6 +986,7 @@ our $cmddef = { my $data = shift; print "$data\n"; }], + pending => [ "PVE::API2::Qemu", 'cloudinit_pending', ['vmid'], { node => $nodename }, \&PVE::GuestHelpers::format_pending ] }, }; diff --git a/PVE/QemuServer/Cloudinit.pm b/PVE/QemuServer/Cloudinit.pm index cdaf4e5..2355953 100644 --- a/PVE/QemuServer/Cloudinit.pm +++ b/PVE/QemuServer/Cloudinit.pm @@ -7,6 +7,7 @@ use File::Path; use Digest::SHA; use URI::Escape; use MIME::Base64 qw(encode_base64); +use Storable qw(dclone); use PVE::Tools qw(run_command file_set_contents); use PVE::Storage; @@ -632,4 +633,81 @@ sub dump_cloudinit_config { } } +sub get_pending_config { + my ($conf, $vmid) = @_; + + my $newconf = dclone($conf); + + my $cloudinit_current = $newconf->{cloudinit}; + my @cloudinit_opts = keys %{PVE::QemuServer::cloudinit_config_properties()}; + push @cloudinit_opts, 'name'; + + #add cloud-init drive + my $drives = {}; + PVE::QemuConfig->foreach_volume($newconf, sub { + my ($ds, $drive) = @_; + $drives->{$ds} = 1 if PVE::QemuServer::drive_is_cloudinit($drive); + }); + + PVE::QemuConfig->foreach_volume($cloudinit_current, sub { + my ($ds, $drive) = @_; + $drives->{$ds} = 1 if PVE::QemuServer::drive_is_cloudinit($drive); + }); + foreach my $ds (keys %{$drives}) { + push @cloudinit_opts, $ds; + } + + $newconf->{name} = "VM$vmid" if !$newconf->{name}; + $cloudinit_current->{name} = "VM$vmid" if !$cloudinit_current->{name}; + + #only mac-address is used in cloud-init config. + #We don't want to display other pending net changes. + my $print_cloudinit_net = sub { + my ($conf, $opt) = @_; + + if (defined($conf->{$opt})) { + my $net = PVE::QemuServer::parse_net($conf->{$opt}); + $conf->{$opt} = "macaddr=".$net->{macaddr} if $net->{macaddr}; + } + }; + + my $cloudinit_options = {}; + for my $opt (@cloudinit_opts) { + if ($opt =~ m/^ipconfig(\d+)/) { + my $netid = "net$1"; + + next if !defined($newconf->{$netid}) && !defined($cloudinit_current->{$netid}) && + !defined($newconf->{$opt}) && !defined($cloudinit_current->{$opt}); + + &$print_cloudinit_net($newconf, $netid); + &$print_cloudinit_net($cloudinit_current, $netid); + $cloudinit_options->{$netid} = 1; + } + $cloudinit_options->{$opt} = 1; + } + + my $res = []; + + for my $opt (keys %{$cloudinit_options}) { + + my $item = { + key => $opt, + }; + if ($cloudinit_current->{$opt}) { + $item->{value} = $cloudinit_current->{$opt}; + if (defined($newconf->{$opt})) { + $item->{pending} = $newconf->{$opt} if $newconf->{$opt} ne $cloudinit_current->{$opt}; + } else { + $item->{delete} = 1; + } + } else { + $item->{pending} = $newconf->{$opt} if $newconf->{$opt} + } + + push @$res, $item; + } + + return $res; +} + 1; -- 2.30.2