$hostname =~ s/\..*$//;
} elsif (my $search = $conf->{searchdomain}) {
$fqdn = "$hostname.$search";
+ } else {
+ $fqdn = $hostname;
}
return ($hostname, $fqdn);
}
$content .= "hostname: $hostname\n";
$content .= "manage_etc_hosts: true\n";
- $content .= "fqdn: $fqdn\n" if defined($fqdn);
+ $content .= "fqdn: $fqdn\n";
my $username = $conf->{ciuser};
my $password = $conf->{cipassword};
$content .= " - default\n";
}
- $content .= "package_upgrade: true\n";
+ $content .= "package_upgrade: true\n" if !defined($conf->{ciupgrade}) || $conf->{ciupgrade};
return $content;
}
sub read_cloudinit_snippets_file {
my ($storage_conf, $volid) = @_;
- my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid);
- die "$volid is not in the snippets directory\n" if $type ne 'snippets';
+ my ($vtype, undef) = PVE::Storage::parse_volname($storage_conf, $volid);
+
+ die "$volid is not in the snippets directory\n" if $vtype ne 'snippets';
+
+ my $full_path = PVE::Storage::abs_filesystem_path($storage_conf, $volid, 1);
return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024);
}
opennebula => \&generate_opennebula,
};
-sub generate_cloudinitconfig {
+sub has_changes {
+ my ($conf) = @_;
+
+ return !!$conf->{cloudinit}->%*;
+}
+
+sub generate_cloudinit_config {
my ($conf, $vmid) = @_;
my $format = get_cloudinit_format($conf);
+ my $has_changes = has_changes($conf);
+
PVE::QemuConfig->foreach_volume($conf, sub {
my ($ds, $drive) = @_;
$generator->($conf, $vmid, $drive, $volname, $storeid);
});
- my $cloudinitconf = delete $conf->{cloudinit};
- $cloudinitconf = {};
-
- my @cloudinit_opts = keys %{PVE::QemuServer::cloudinit_config_properties()};
- push @cloudinit_opts, 'name';
-
- for my $opt (@cloudinit_opts) {
-
- if ($opt =~ m/^ipconfig(\d+)/) {
- my $netid = "net$1";
- next if !defined($conf->{$netid});
- $conf->{cloudinit}->{$netid} = $conf->{$netid};
- }
+ return $has_changes;
+}
- $conf->{cloudinit}->{$opt} = $conf->{$opt} if $conf->{$opt};
- }
+sub apply_cloudinit_config {
+ my ($conf, $vmid) = @_;
- $conf->{cloudinit}->{name} = "VM$vmid" if !$conf->{cloudinit}->{name};
+ my $has_changes = generate_cloudinit_config($conf, $vmid);
- for my $opt (keys %{$conf}) {
- if (PVE::QemuServer::is_valid_drivename($opt)) {
- my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
- if (PVE::QemuServer::drive_is_cloudinit($drive)) {
- $conf->{cloudinit}->{$opt} = $conf->{$opt};
- }
- }
+ if ($has_changes) {
+ delete $conf->{cloudinit};
+ PVE::QemuConfig->write_config($vmid, $conf);
+ return 1;
}
- PVE::QemuConfig->write_config($vmid, $conf);
-
+ return $has_changes;
}
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);
- });
- for 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;