[pve-devel] [RFC ha-manager] Fix #1160: add template resource

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Nov 2 16:50:44 CET 2016


This is a very limited resource representing both LXC and QEMU
templates.
Methods like start, stop and check_running do nothing or tell the
LRM want he wants to hear.

While templates are a questionable HA resource they are already used
as such (see bug 1160) and prohibiting that usage is a draw back for
those users and has no real value for us.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/PVE/HA/Config.pm                |   2 +-
 src/PVE/HA/Env/PVE2.pm              |   2 +
 src/PVE/HA/Resources/Makefile       |   2 +-
 src/PVE/HA/Resources/PVETemplate.pm | 131 ++++++++++++++++++++++++++++++++++++
 4 files changed, 135 insertions(+), 2 deletions(-)
 create mode 100644 src/PVE/HA/Resources/PVETemplate.pm

diff --git a/src/PVE/HA/Config.pm b/src/PVE/HA/Config.pm
index 1802a7d..bb6d7e1 100644
--- a/src/PVE/HA/Config.pm
+++ b/src/PVE/HA/Config.pm
@@ -215,7 +215,7 @@ sub vm_is_ha_managed {
     my $conf = cfs_read_file($ha_resources_config);
 
     my $types = PVE::HA::Resources->lookup_types();
-    foreach my $type ('vm', 'ct') {
+    foreach my $type ('vm', 'ct', 'template') {
 	return 1 if &$service_check_ha_state($conf, "$type:$vmid", $has_state);
     }
 
diff --git a/src/PVE/HA/Env/PVE2.pm b/src/PVE/HA/Env/PVE2.pm
index 6b8802e..cd9b4e1 100644
--- a/src/PVE/HA/Env/PVE2.pm
+++ b/src/PVE/HA/Env/PVE2.pm
@@ -19,9 +19,11 @@ use PVE::HA::FenceConfig;
 use PVE::HA::Resources;
 use PVE::HA::Resources::PVEVM;
 use PVE::HA::Resources::PVECT;
+use PVE::HA::Resources::PVETemplate;
 
 PVE::HA::Resources::PVEVM->register();
 PVE::HA::Resources::PVECT->register();
+PVE::HA::Resources::PVETemplate->register();
 
 PVE::HA::Resources->init();
 
diff --git a/src/PVE/HA/Resources/Makefile b/src/PVE/HA/Resources/Makefile
index ea840a7..9e53ca7 100644
--- a/src/PVE/HA/Resources/Makefile
+++ b/src/PVE/HA/Resources/Makefile
@@ -1,4 +1,4 @@
-SOURCES=PVEVM.pm PVECT.pm
+SOURCES=PVEVM.pm PVECT.pm PVETemplate.pm
 
 .PHONY: install
 install:
diff --git a/src/PVE/HA/Resources/PVETemplate.pm b/src/PVE/HA/Resources/PVETemplate.pm
new file mode 100644
index 0000000..ecb31b0
--- /dev/null
+++ b/src/PVE/HA/Resources/PVETemplate.pm
@@ -0,0 +1,131 @@
+package PVE::HA::Resources::PVETemplate;
+
+use strict;
+use warnings;
+
+use PVE::HA::Tools;
+
+use PVE::Cluster;
+
+use PVE::AbstractConfig;
+use PVE::QemuConfig;
+use PVE::LXC::Config;
+
+use base qw(PVE::HA::Resources);
+
+sub type {
+    return 'template';
+}
+
+sub verify_name {
+    my ($class, $name) = @_;
+
+    die "invalid VMID\n" if $name !~ m/^[1-9][0-9]+$/;
+}
+
+sub options {
+    return {
+	state => { optional => 1 },
+	group => { optional => 1 },
+	comment => { optional => 1 },
+	max_restart => { optional => 1 },
+	max_relocate => { optional => 1 },
+    };
+}
+
+my $get_template_type = sub {
+    my ($vmid, $nodename) = @_;
+
+    my $vmlist = PVE::Cluster::get_vmlist();
+
+    return $vmlist->{ids}->{$vmid}->{type};
+};
+
+sub config_file {
+    my ($class, $vmid, $nodename) = @_;
+
+    my $type = &$get_template_type($vmid, $nodename);
+
+    if ($type eq 'qemu') {
+	return PVE::QemuConfig->config_file($vmid, $nodename);
+    } elsif ($type eq 'lxc') {
+	return PVE::LXC::Config->config_file($vmid, $nodename);
+    } else {
+	die "unknown template type '$type'!";
+    }
+}
+
+sub exists {
+    my ($class, $vmid, $noerr) = @_;
+
+    my $vmlist = PVE::Cluster::get_vmlist();
+
+    if(!defined($vmlist->{ids}->{$vmid})) {
+	die "resource 'template:$vmid' does not exists in cluster\n" if !$noerr;
+	return undef;
+    } else {
+	return 1;
+    }
+}
+
+sub start {
+    my ($class, $haenv, $id) = @_;
+    # do nothing, templates cannot start
+}
+
+sub shutdown {
+    my ($class, $haenv, $id) = @_;
+    # do nothing, templates cannot start
+}
+
+sub migrate {
+    my ($class, $haenv, $id, $target, $online) = @_;
+
+    my $nodename = $haenv->nodename();
+
+    my $params = {
+	node => $nodename,
+	vmid => $id,
+	target => $target,
+	online => 0, # templates are never online.
+    };
+
+    my $oldconfig = $class->config_file($id, $nodename);
+
+    my $upid;
+
+    my $type = &$get_template_type($id, $nodename);
+    if ($type eq 'qemu') {
+	$upid = PVE::API2::Qemu->migrate_vm($params);
+    } elsif ($type eq 'lxc') {
+	$upid = PVE::API2::LXC->migrate_vm($params);
+    } else {
+	die "unknown template type '$type'!";
+    }
+
+    PVE::HA::Tools::upid_wait($upid, $haenv);
+
+    # check if vm really moved
+    return !(-f $oldconfig);
+}
+
+sub check_running {
+    my ($class, $haenv, $id) = @_;
+
+    my $conf = $haenv->read_service_config();
+
+    # always tell the lrm what he wants to hear as template cannot be started
+    if ($conf->{"template:$id"}->{state} eq 'enabled') {
+	return 1;
+    } else {
+	return 0;
+    }
+}
+
+sub remove_locks {
+    my ($self, $haenv, $id, $locks, $service_node) = @_;
+
+    return undef;
+}
+
+1;
-- 
2.1.4





More information about the pve-devel mailing list