From e73ca4d0ce1c9456e78ac3fb551dcefd9030bcb0 Mon Sep 17 00:00:00 2001 From: Mira Limbeck Date: Wed, 5 Jun 2019 11:09:41 +0200 Subject: [PATCH] add function to dump cloudinit config This adds a function to dump the generated cloudinit config. Only one can be dumped at a time, either 'user', 'network' or 'meta'. The logic to get user, network and metadata is copied from the other path that also creates the ISO image to keep it simple and not complicate the other code path further. The hash generation for the metadata config is unified between nocloud and configdrive2 formats. We need it a 3rd time with the new dump functions so it makes sense to combine it and the metadata config generation in a single function. The _gen_metadata functions are each used twice now. Signed-off-by: Mira Limbeck --- PVE/QemuServer/Cloudinit.pm | 49 +++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/PVE/QemuServer/Cloudinit.pm b/PVE/QemuServer/Cloudinit.pm index 45176ea..609e839 100644 --- a/PVE/QemuServer/Cloudinit.pm +++ b/PVE/QemuServer/Cloudinit.pm @@ -207,6 +207,13 @@ sub configdrive2_network { return $content; } +sub configdrive2_gen_metadata { + my ($user, $network) = @_; + + my $uuid_str = Digest::SHA::sha1_hex($user.$network); + return configdrive2_metadata($uuid_str); +} + sub configdrive2_metadata { my ($uuid) = @_; return <<"EOF"; @@ -225,10 +232,7 @@ sub generate_configdrive2 { $network_data = configdrive2_network($conf) if !defined($network_data); if (!defined($meta_data)) { - my $digest_data = $user_data . $network_data; - my $uuid_str = Digest::SHA::sha1_hex($digest_data); - - $meta_data = configdrive2_metadata($uuid_str); + $meta_data = configdrive2_gen_metadata($user_data, $network_data); } my $files = { '/openstack/latest/user_data' => $user_data, @@ -389,6 +393,13 @@ sub nocloud_metadata { return "instance-id: $uuid\n"; } +sub nocloud_gen_metadata { + my ($user, $network) = @_; + + my $uuid_str = Digest::SHA::sha1_hex($user.$network); + return nocloud_metadata($uuid_str); +} + sub generate_nocloud { my ($conf, $vmid, $drive, $volname, $storeid) = @_; @@ -397,10 +408,7 @@ sub generate_nocloud { $network_data = nocloud_network($conf) if !defined($network_data); if (!defined($meta_data)) { - my $digest_data = $user_data . $network_data; - my $uuid_str = Digest::SHA::sha1_hex($digest_data); - - $meta_data = nocloud_metadata($uuid_str); + $meta_data = nocloud_gen_metadata($user_data, $network_data); } my $files = { @@ -473,4 +481,29 @@ sub generate_cloudinitconfig { }); } +sub dump_cloudinit_config { + my ($conf, $vmid, $type) = @_; + + my $format = get_cloudinit_format($conf); + + if ($type eq 'user') { + return cloudinit_userdata($conf, $vmid); + } elsif ($type eq 'network') { + if ($format eq 'nocloud') { + return nocloud_network($conf); + } else { + return configdrive2_network($conf); + } + } else { # metadata config + my $user = cloudinit_userdata($conf, $vmid); + if ($format eq 'nocloud') { + my $network = nocloud_network($conf); + return nocloud_gen_metadata($user, $network); + } else { + my $network = configdrive2_network($conf); + return configdrive2_gen_metadata($user, $network); + } + } +} + 1; -- 2.39.2