[pve-devel] [PATCH ha-manager 05/12] Tools: make PVE::Cluster and HA-Env optional

Fabian Grünbichler f.gruenbichler at proxmox.com
Fri Sep 28 12:48:52 CEST 2018


since Tools is used by the simulator as well, which does not need
PVE::Cluster otherwise.

the bash completion methods are only used by ha-manager's CLI tools, and
parse_sid is never called with an argument requiring PVE::Cluster by the
simulation/testing stack.

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
note: is there a reason we don't use this $INC check in other parts where we do optional imports?

 src/PVE/HA/Tools.pm | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/PVE/HA/Tools.pm b/src/PVE/HA/Tools.pm
index 88f775e..fd537b3 100644
--- a/src/PVE/HA/Tools.pm
+++ b/src/PVE/HA/Tools.pm
@@ -3,9 +3,9 @@ package PVE::HA::Tools;
 use strict;
 use warnings;
 use JSON;
+
 use PVE::JSONSchema;
 use PVE::Tools;
-use PVE::Cluster;
 use PVE::ProcFSTools;
 
 # return codes used in the ha environment
@@ -84,6 +84,19 @@ PVE::JSONSchema::register_standard_option('pve-ha-group-id', {
     type => 'string', format => 'pve-configid',
 });
 
+my $have_cluster = 0;
+eval {
+    if (! defined($INC{'PVE::Cluster'})) {
+	require PVE::Cluster;
+	PVE::Cluster->import();
+    }
+    if (! defined($INC{'PVE::HA::Config'})) {
+	require PVE::HA::Config;
+	PVE::HA::Env->import();
+    }
+    $have_cluster = 1;
+};
+
 sub parse_sid {
     my ($sid) = @_;
 
@@ -91,6 +104,9 @@ sub parse_sid {
 
     if ($sid =~ m/^(\d+)$/) {
 	$name = $1;
+
+	die "PVE::Cluster not available!\n" if !$have_cluster;
+
 	my $vmlist = PVE::Cluster::get_vmlist();
 	if (defined($vmlist->{ids}->{$name})) {
 	    my $vm_type = $vmlist->{ids}->{$name}->{type};
@@ -128,6 +144,9 @@ sub read_json_from_file {
 	# workaround for bug #775
 	if ($filename =~ m|^/etc/pve/|) {
 	    $filename =~ s|^/etc/pve/+||;
+
+	    die "PVE::Cluster not available!\n" if !$have_cluster;
+
 	    $raw = PVE::Cluster::get_config($filename);
 	    die "unable to read file '/etc/pve/$filename'\n" 
 		if !defined($raw);
@@ -218,6 +237,7 @@ sub upid_wait {
 sub complete_sid {
     my ($cmd, $pname, $cur) = @_;
 
+    die "PVE::Cluster not available!\n" if !$have_cluster;
     my $cfg = PVE::HA::Config::read_resources_config();
 
     my $res = [];
@@ -255,6 +275,7 @@ sub complete_sid {
 
 sub complete_enabled_sid {
 
+    die "PVE::Cluster not available!\n" if !$have_cluster;
     my $cfg = PVE::HA::Config::read_resources_config();
 
     my $res = [];
@@ -269,6 +290,7 @@ sub complete_enabled_sid {
 
 sub complete_disabled_sid {
 
+    die "PVE::Cluster not available!\n" if !$have_cluster;
     my $cfg = PVE::HA::Config::read_resources_config();
 
     my $res = [];
@@ -284,6 +306,7 @@ sub complete_disabled_sid {
 sub complete_group {
     my ($cmd, $pname, $cur) = @_;
 
+    die "PVE::Cluster not available!\n" if !$have_cluster;
     my $cfg = PVE::HA::Config::read_group_config();
 
     my $res = [];
-- 
2.19.0





More information about the pve-devel mailing list