[pve-devel] [PATCH pve-ha-manager 3/3] convert pve-ha-crm into a PVE::Service class

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Sep 14 17:21:56 CEST 2015


Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/Makefile                  | 12 ++++---
 src/PVE/Service/Makefile      |  2 +-
 src/PVE/Service/pve_ha_crm.pm | 74 ++++++++++++++++++++++++++++++++++++++++
 src/pve-ha-crm                | 79 +++++--------------------------------------
 4 files changed, 91 insertions(+), 76 deletions(-)
 create mode 100644 src/PVE/Service/pve_ha_crm.pm

diff --git a/src/Makefile b/src/Makefile
index 358ca12..1928d4c 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -18,8 +18,11 @@ all: watchdog-mux
 	rm -f $@
 	cat $<|pod2man -n $* -s 1 -r ${VERSION} -c "Proxmox Documentation"|gzip -c9 >$@
 
-pve-ha-crm.1.pod: pve-ha-crm
-	perl -I. ./pve-ha-crm printmanpod >$@
+pve-ha-crm.1.pod:
+	perl -I. -T -e "use PVE::Service::pve_ha_crm; PVE::Service::pve_ha_crm->generate_pod_manpage();" >$@
+
+pve-ha-crm.bash-completion:
+	perl -I. -T -e "use PVE::Service::pve_ha_crm; PVE::Service::pve_ha_crm->generate_bash_completions();" >$@
 
 pve-ha-lrm.1.pod:
 	perl -I. -T -e "use PVE::Service::pve_ha_lrm; PVE::Service::pve_ha_lrm->generate_pod_manpage();" >$@
@@ -39,8 +42,8 @@ watchdog-mux: watchdog-mux.c
 	gcc watchdog-mux.c -o watchdog-mux -Wall -Wl,-z,relro $$(pkg-config --libs --cflags libsystemd-daemon)
 
 .PHONY: install
-install: watchdog-mux pve-ha-crm pve-ha-lrm ha-manager.1.pod ha-manager.1.gz pve-ha-crm.1.pod pve-ha-crm.1.gz pve-ha-lrm.1.pod pve-ha-lrm.1.gz ha-manager.bash-completion pve-ha-lrm.bash-completion
-	perl -I. ./pve-ha-crm verifyapi
+install: watchdog-mux pve-ha-crm pve-ha-lrm ha-manager.1.pod ha-manager.1.gz pve-ha-crm.1.pod pve-ha-crm.1.gz pve-ha-lrm.1.pod pve-ha-lrm.1.gz ha-manager.bash-completion pve-ha-lrm.bash-completion pve-ha-crm.bash-completion
+	perl -I. -T -e "use PVE::Service::pve_ha_crm; PVE::Service::pve_ha_crm->verify_api();"
 	perl -I. -T -e "use PVE::Service::pve_ha_lrm; PVE::Service::pve_ha_lrm->verify_api();"
 	perl -I. -T -e "use PVE::CLI::ha_manager; PVE::CLI::ha_manager->verify_api();"
 	install -d ${DESTDIR}${SBINDIR}
@@ -48,6 +51,7 @@ install: watchdog-mux pve-ha-crm pve-ha-lrm ha-manager.1.pod ha-manager.1.gz pve
 	install -m 0755 pve-ha-lrm ${DESTDIR}${SBINDIR}
 	install -m 0755 ha-manager ${DESTDIR}${SBINDIR}
 	install -m 0755 watchdog-mux ${DESTDIR}${SBINDIR}
+	install -m 0644 -D pve-ha-crm.bash-completion ${DESTDIR}${BASHCOMPLDIR}/pve-ha-crm
 	install -m 0644 -D pve-ha-lrm.bash-completion ${DESTDIR}${BASHCOMPLDIR}/pve-ha-lrm
 	install -m 0644 -D ha-manager.bash-completion ${DESTDIR}${BASHCOMPLDIR}/ha-manager
 	make -C PVE install
diff --git a/src/PVE/Service/Makefile b/src/PVE/Service/Makefile
index 8502c8d..5105885 100644
--- a/src/PVE/Service/Makefile
+++ b/src/PVE/Service/Makefile
@@ -1,4 +1,4 @@
-SOURCES=pve_ha_lrm.pm
+SOURCES=pve_ha_lrm.pm pve_ha_crm.pm
 
 .PHONY: install
 install: ${SOURCES}
diff --git a/src/PVE/Service/pve_ha_crm.pm b/src/PVE/Service/pve_ha_crm.pm
new file mode 100644
index 0000000..c8325e2
--- /dev/null
+++ b/src/PVE/Service/pve_ha_crm.pm
@@ -0,0 +1,74 @@
+package PVE::Service::pve_ha_crm;
+
+use strict;
+use warnings;
+
+use PVE::SafeSyslog;
+use PVE::Daemon;
+use Data::Dumper;
+
+use PVE::HA::Env;
+use PVE::HA::Env::PVE2;
+use PVE::HA::CRM;
+
+use base qw(PVE::Daemon);
+
+our $exename = "pve-ha-crm";
+
+my $cmdline = [$0, @ARGV];
+
+my %daemon_options = (stop_wait_time => 60);
+
+my $daemon = __PACKAGE__->new('pve-ha-crm', $cmdline, %daemon_options);
+
+sub run {
+    my ($self) = @_;
+
+    $self->{haenv} = PVE::HA::Env->new('PVE::HA::Env::PVE2', $self->{nodename});
+
+    $self->{crm} = PVE::HA::CRM->new($self->{haenv});
+
+    for (;;) {
+	$self->{haenv}->loop_start_hook();
+
+	my $repeat = $self->{crm}->do_one_iteration();
+
+	$self->{haenv}->loop_end_hook();
+
+	last if !$repeat;
+    }
+}
+
+sub shutdown {
+    my ($self) = @_;
+
+    $self->{crm}->shutdown_request();
+}
+
+$daemon->register_start_command();
+$daemon->register_stop_command();
+$daemon->register_status_command();
+
+our $cmddef = {
+    start => [ __PACKAGE__, 'start', []],
+    stop => [ __PACKAGE__, 'stop', []],
+    status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ],
+};
+
+1;
+
+__END__
+
+=head1 NAME
+
+pve-ha-crm - PVE Cluster Ressource Manager Daemon
+
+=head1 SYNOPSIS
+
+=include synopsis
+
+=head1 DESCRIPTION
+
+This is the Cluster Ressource Manager.
+
+=include pve_copyright
diff --git a/src/pve-ha-crm b/src/pve-ha-crm
index 885d459..362b919 100755
--- a/src/pve-ha-crm
+++ b/src/pve-ha-crm
@@ -2,16 +2,10 @@
 
 use strict;
 use warnings;
-use PVE::SafeSyslog;
-use PVE::Daemon;
-use Data::Dumper;
-use PVE::RPCEnvironment;
 
-use PVE::HA::Env;
-use PVE::HA::Env::PVE2;
-use PVE::HA::CRM;
+use PVE::Service::pve_ha_crm;
 
-use base qw(PVE::Daemon);
+use PVE::RPCEnvironment;
 
 $SIG{'__WARN__'} = sub {
     my $err = $@;
@@ -22,70 +16,13 @@ $SIG{'__WARN__'} = sub {
     $@ = $err;
 };
 
-my $cmdline = [$0, @ARGV];
-
-my %daemon_options = (stop_wait_time => 60);
-
-my $daemon = __PACKAGE__->new('pve-ha-crm', $cmdline, %daemon_options);
-
-my $rpcenv = PVE::RPCEnvironment->init('cli');
-
-$rpcenv->init_request();
-$rpcenv->set_language($ENV{LANG});
-$rpcenv->set_user('root at pam');
-
-sub run {
-    my ($self) = @_;
-
-    $self->{haenv} = PVE::HA::Env->new('PVE::HA::Env::PVE2', $self->{nodename});
-
-    $self->{crm} = PVE::HA::CRM->new($self->{haenv});
-
-    for (;;) {
-	$self->{haenv}->loop_start_hook();
-
-	my $repeat = $self->{crm}->do_one_iteration();
-
-	$self->{haenv}->loop_end_hook();
+my $prepare = sub {
+    my $rpcenv = PVE::RPCEnvironment->init('cli');
 
-	last if !$repeat;
-    }
-}
+    $rpcenv->init_request();
+    $rpcenv->set_language($ENV{LANG});
+    $rpcenv->set_user('root at pam');
 
-sub shutdown {
-    my ($self) = @_;
-
-    $self->{crm}->shutdown_request();
-}
-
-$daemon->register_start_command();
-$daemon->register_stop_command();
-$daemon->register_status_command();
-
-my $cmddef = {
-    start => [ __PACKAGE__, 'start', []],
-    stop => [ __PACKAGE__, 'stop', []],
-    status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ],
 };
 
-my $cmd = shift;
-
-PVE::CLIHandler::handle_cmd($cmddef, $0, $cmd, \@ARGV, undef, $0);
-
-exit (0);
-
-__END__
-
-=head1 NAME
-                                          
-pve-ha-crm - PVE Cluster Ressource Manager Daemon
-
-=head1 SYNOPSIS
-
-=include synopsis
-
-=head1 DESCRIPTION
-
-This is the Cluster Ressource Manager.
-
-=include pve_copyright
+PVE::Service::pve_ha_crm->run_cli(undef, undef, $prepare);
-- 
2.1.4




More information about the pve-devel mailing list