]> git.proxmox.com Git - qemu-server.git/commitdiff
add function to dump cloudinit config
authorMira Limbeck <m.limbeck@proxmox.com>
Wed, 5 Jun 2019 09:09:41 +0000 (11:09 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 6 Jun 2019 12:34:11 +0000 (14:34 +0200)
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 <format>_gen_metadata functions are
each used twice now.

Signed-off-by: Mira Limbeck <m.limbeck@proxmox.com>
PVE/QemuServer/Cloudinit.pm

index 45176eaaa5a90fc9b02a0c278f175810a2d0cc00..609e8394d779ec5e455234c8c499a600d04492cc 100644 (file)
@@ -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;