[pve-devel] [RFC ha-manager 1/8] Split up resources and move them to own sub folder
Thomas Lamprecht
t.lamprecht at proxmox.com
Fri Jan 22 17:06:35 CET 2016
Implementing every resource in the base 'Resources' class is a bit
messy, especially if more resource agent get added.
This moves each class to a own Perl module in the new 'Resources'
sub folder.
Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
src/PVE/HA/Config.pm | 5 +-
src/PVE/HA/Makefile | 2 +
src/PVE/HA/Resources.pm | 218 ------------------------------------------
src/PVE/HA/Resources/Makefile | 11 +++
src/PVE/HA/Resources/PVECT.pm | 109 +++++++++++++++++++++
src/PVE/HA/Resources/PVEVM.pm | 109 +++++++++++++++++++++
6 files changed, 235 insertions(+), 219 deletions(-)
create mode 100644 src/PVE/HA/Resources/Makefile
create mode 100644 src/PVE/HA/Resources/PVECT.pm
create mode 100644 src/PVE/HA/Resources/PVEVM.pm
diff --git a/src/PVE/HA/Config.pm b/src/PVE/HA/Config.pm
index 0dc486c..1190f6d 100644
--- a/src/PVE/HA/Config.pm
+++ b/src/PVE/HA/Config.pm
@@ -6,9 +6,12 @@ use JSON;
use PVE::HA::Tools;
use PVE::HA::Groups;
-use PVE::HA::Resources;
use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
+# virtual resource classes
+use PVE::HA::Resources::PVEVM;
+use PVE::HA::Resources::PVECT;
+
PVE::HA::Groups->register();
PVE::HA::Groups->init();
diff --git a/src/PVE/HA/Makefile b/src/PVE/HA/Makefile
index 4357015..00731e2 100644
--- a/src/PVE/HA/Makefile
+++ b/src/PVE/HA/Makefile
@@ -4,10 +4,12 @@ SOURCES=CRM.pm Env.pm Groups.pm Resources.pm Config.pm LRM.pm Manager.pm NodeSta
install:
install -d -m 0755 ${DESTDIR}${PERLDIR}/PVE/HA
for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/HA/$$i; done
+ make -C Resources install
make -C Env install
.PHONY: installsim
installsim:
install -d -m 0755 ${DESTDIR}${PERLDIR}/PVE/HA
for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/HA/$$i; done
+ make -C Resources installsim
make -C Sim installsim
diff --git a/src/PVE/HA/Resources.pm b/src/PVE/HA/Resources.pm
index 1129fcb..fce7d54 100644
--- a/src/PVE/HA/Resources.pm
+++ b/src/PVE/HA/Resources.pm
@@ -126,224 +126,6 @@ sub check_running {
# virtual machine resource class
-package PVE::HA::Resources::PVEVM;
-
-use strict;
-use warnings;
-
-use PVE::QemuServer;
-use PVE::API2::Qemu;
-
-use base qw(PVE::HA::Resources);
-
-sub type {
- return 'vm';
-}
-
-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 },
- };
-}
-
-sub config_file {
- my ($class, $vmid, $nodename) = @_;
-
- return PVE::QemuServer::config_file($vmid, $nodename);
-}
-
-sub exists {
- my ($class, $vmid, $noerr) = @_;
-
- my $vmlist = PVE::Cluster::get_vmlist();
-
- if(!defined($vmlist->{ids}->{$vmid})) {
- die "resource 'vm:$vmid' does not exists in cluster\n" if !$noerr;
- return undef;
- } else {
- return 1;
- }
-}
-
-sub start {
- my ($class, $haenv, $id) = @_;
-
- my $nodename = $haenv->nodename();
-
- my $params = {
- node => $nodename,
- vmid => $id
- };
-
- my $upid = PVE::API2::Qemu->vm_start($params);
- $haenv->upid_wait($upid);
-}
-
-sub shutdown {
- my ($class, $haenv, $id) = @_;
-
- my $nodename = $haenv->nodename();
- my $shutdown_timeout = 60; # fixme: make this configurable
-
- my $params = {
- node => $nodename,
- vmid => $id,
- timeout => $shutdown_timeout,
- forceStop => 1,
- };
-
- my $upid = PVE::API2::Qemu->vm_shutdown($params);
- $haenv->upid_wait($upid);
-}
-
-
-sub migrate {
- my ($class, $haenv, $id, $target, $online) = @_;
-
- my $nodename = $haenv->nodename();
-
- my $params = {
- node => $nodename,
- vmid => $id,
- target => $target,
- online => $online,
- };
-
- # explicitly shutdown if $online isn't true (relocate)
- if (!$online && $class->check_running($id)) {
- $class->shutdown($haenv, $id);
- }
-
- my $upid = PVE::API2::Qemu->migrate_vm($params);
- $haenv->upid_wait($upid);
-}
-
-sub check_running {
- my ($class, $vmid) = @_;
-
- return PVE::QemuServer::check_running($vmid, 1);
-}
-
-
-# container resource class
-package PVE::HA::Resources::PVECT;
-
-use strict;
-use warnings;
-
-use PVE::LXC;
-use PVE::API2::LXC;
-use PVE::API2::LXC::Status;
-
-use base qw(PVE::HA::Resources);
-
-sub type {
- return 'ct';
-}
-
-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 },
- };
-}
-
-sub config_file {
- my ($class, $vmid, $nodename) = @_;
-
- return PVE::LXC::config_file($vmid, $nodename);
-}
-
-sub exists {
- my ($class, $vmid, $noerr) = @_;
-
- my $vmlist = PVE::Cluster::get_vmlist();
-
- if(!defined($vmlist->{ids}->{$vmid})) {
- die "resource 'ct:$vmid' does not exists in cluster\n" if !$noerr;
- return undef;
- } else {
- return 1;
- }
-}
-
-sub start {
- my ($class, $haenv, $id) = @_;
-
- my $nodename = $haenv->nodename();
-
- my $params = {
- node => $nodename,
- vmid => $id
- };
-
- my $upid = PVE::API2::LXC::Status->vm_start($params);
- $haenv->upid_wait($upid);
-}
-
-sub shutdown {
- my ($class, $haenv, $id) = @_;
-
- my $nodename = $haenv->nodename();
- my $shutdown_timeout = 60; # fixme: make this configurable
-
- my $params = {
- node => $nodename,
- vmid => $id,
- timeout => $shutdown_timeout,
- forceStop => 1,
- };
-
- my $upid = PVE::API2::LXC::Status->vm_shutdown($params);
- $haenv->upid_wait($upid);
-}
-
-sub migrate {
- my ($class, $haenv, $id, $target, $online) = @_;
-
- my $nodename = $haenv->nodename();
-
- my $params = {
- node => $nodename,
- vmid => $id,
- target => $target,
- online => 0, # we cannot migrate CT (yet) online, only relocate
- };
-
- # always relocate container for now
- if ($class->check_running($id)) {
- $class->shutdown($haenv, $id);
- }
-
- my $upid = PVE::API2::LXC->migrate_vm($params);
- $haenv->upid_wait($upid);
-}
-
-sub check_running {
- my ($class, $vmid) = @_;
-
- return PVE::LXC::check_running($vmid);
-}
-
# package PVE::HA::Resources::IPAddr;
diff --git a/src/PVE/HA/Resources/Makefile b/src/PVE/HA/Resources/Makefile
new file mode 100644
index 0000000..ea840a7
--- /dev/null
+++ b/src/PVE/HA/Resources/Makefile
@@ -0,0 +1,11 @@
+SOURCES=PVEVM.pm PVECT.pm
+
+.PHONY: install
+install:
+ install -d -m 0755 ${DESTDIR}${PERLDIR}/PVE/HA/Resources
+ for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/HA/Resources/$$i; done
+
+.PHONY: installsim
+installsim:
+ install -d -m 0755 ${DESTDIR}${PERLDIR}/PVE/HA/Resources
+ for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/HA/Resources/$$i; done
diff --git a/src/PVE/HA/Resources/PVECT.pm b/src/PVE/HA/Resources/PVECT.pm
new file mode 100644
index 0000000..e70c358
--- /dev/null
+++ b/src/PVE/HA/Resources/PVECT.pm
@@ -0,0 +1,109 @@
+package PVE::HA::Resources::PVECT;
+
+use strict;
+use warnings;
+
+use PVE::LXC;
+use PVE::API2::LXC;
+use PVE::API2::LXC::Status;
+
+use base qw(PVE::HA::Resources);
+
+sub type {
+ return 'ct';
+}
+
+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 },
+ };
+}
+
+sub config_file {
+ my ($class, $vmid, $nodename) = @_;
+
+ return PVE::LXC::config_file($vmid, $nodename);
+}
+
+sub exists {
+ my ($class, $vmid, $noerr) = @_;
+
+ my $vmlist = PVE::Cluster::get_vmlist();
+
+ if(!defined($vmlist->{ids}->{$vmid})) {
+ die "resource 'ct:$vmid' does not exists in cluster\n" if !$noerr;
+ return undef;
+ } else {
+ return 1;
+ }
+}
+
+sub start {
+ my ($class, $haenv, $id) = @_;
+
+ my $nodename = $haenv->nodename();
+
+ my $params = {
+ node => $nodename,
+ vmid => $id
+ };
+
+ my $upid = PVE::API2::LXC::Status->vm_start($params);
+ $haenv->upid_wait($upid);
+}
+
+sub shutdown {
+ my ($class, $haenv, $id) = @_;
+
+ my $nodename = $haenv->nodename();
+ my $shutdown_timeout = 60; # fixme: make this configurable
+
+ my $params = {
+ node => $nodename,
+ vmid => $id,
+ timeout => $shutdown_timeout,
+ forceStop => 1,
+ };
+
+ my $upid = PVE::API2::LXC::Status->vm_shutdown($params);
+ $haenv->upid_wait($upid);
+}
+
+sub migrate {
+ my ($class, $haenv, $id, $target, $online) = @_;
+
+ my $nodename = $haenv->nodename();
+
+ my $params = {
+ node => $nodename,
+ vmid => $id,
+ target => $target,
+ online => 0, # we cannot migrate CT (yet) online, only relocate
+ };
+
+ # always relocate container for now
+ if ($class->check_running($id)) {
+ $class->shutdown($haenv, $id);
+ }
+
+ my $upid = PVE::API2::LXC->migrate_vm($params);
+ $haenv->upid_wait($upid);
+}
+
+sub check_running {
+ my ($class, $vmid) = @_;
+
+ return PVE::LXC::check_running($vmid);
+}
+
+1;
diff --git a/src/PVE/HA/Resources/PVEVM.pm b/src/PVE/HA/Resources/PVEVM.pm
new file mode 100644
index 0000000..03b894b
--- /dev/null
+++ b/src/PVE/HA/Resources/PVEVM.pm
@@ -0,0 +1,109 @@
+package PVE::HA::Resources::PVEVM;
+
+use strict;
+use warnings;
+
+use PVE::QemuServer;
+use PVE::API2::Qemu;
+
+use base qw(PVE::HA::Resources);
+
+sub type {
+ return 'vm';
+}
+
+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 },
+ };
+}
+
+sub config_file {
+ my ($class, $vmid, $nodename) = @_;
+
+ return PVE::QemuServer::config_file($vmid, $nodename);
+}
+
+sub exists {
+ my ($class, $vmid, $noerr) = @_;
+
+ my $vmlist = PVE::Cluster::get_vmlist();
+
+ if(!defined($vmlist->{ids}->{$vmid})) {
+ die "resource 'vm:$vmid' does not exists in cluster\n" if !$noerr;
+ return undef;
+ } else {
+ return 1;
+ }
+}
+
+sub start {
+ my ($class, $haenv, $id) = @_;
+
+ my $nodename = $haenv->nodename();
+
+ my $params = {
+ node => $nodename,
+ vmid => $id
+ };
+
+ my $upid = PVE::API2::Qemu->vm_start($params);
+ $haenv->upid_wait($upid);
+}
+
+sub shutdown {
+ my ($class, $haenv, $id) = @_;
+
+ my $nodename = $haenv->nodename();
+ my $shutdown_timeout = 60; # fixme: make this configurable
+
+ my $params = {
+ node => $nodename,
+ vmid => $id,
+ timeout => $shutdown_timeout,
+ forceStop => 1,
+ };
+
+ my $upid = PVE::API2::Qemu->vm_shutdown($params);
+ $haenv->upid_wait($upid);
+}
+
+
+sub migrate {
+ my ($class, $haenv, $id, $target, $online) = @_;
+
+ my $nodename = $haenv->nodename();
+
+ my $params = {
+ node => $nodename,
+ vmid => $id,
+ target => $target,
+ online => $online,
+ };
+
+ # explicitly shutdown if $online isn't true (relocate)
+ if (!$online && $class->check_running($id)) {
+ $class->shutdown($haenv, $id);
+ }
+
+ my $upid = PVE::API2::Qemu->migrate_vm($params);
+ $haenv->upid_wait($upid);
+}
+
+sub check_running {
+ my ($class, $vmid) = @_;
+
+ return PVE::QemuServer::check_running($vmid, 1);
+}
+
+1;
--
2.1.4
More information about the pve-devel
mailing list