[pve-devel] [v3 ha-manager 2/5] Tools: remove dependency on PVE::Cluster

Fabian Grünbichler f.gruenbichler at proxmox.com
Wed Oct 10 13:55:04 CEST 2018


by moving parse_sid to PVE::HA::Env, with the default implementation in
PVE::HA::Config.

the bash completion methods use PVE::HA::Config (and PVE::Cluster), but
the corresponding use statements are only in PVE::CLI::ha_manager, where the
bash completion is actually used.

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
note: new patch in v3
note: the bash completion handling might be further improved

 src/PVE/API2/HA/Resources.pm | 12 ++++++------
 src/PVE/CLI/ha_manager.pm    |  2 +-
 src/PVE/HA/Config.pm         | 35 +++++++++++++++++++++++++++++++++-
 src/PVE/HA/Env.pm            |  6 ++++++
 src/PVE/HA/Env/PVE2.pm       |  6 ++++++
 src/PVE/HA/LRM.pm            |  2 +-
 src/PVE/HA/Manager.pm        |  2 +-
 src/PVE/HA/Sim/Env.pm        | 12 ++++++++++++
 src/PVE/HA/Tools.pm          | 37 +-----------------------------------
 9 files changed, 68 insertions(+), 46 deletions(-)

diff --git a/src/PVE/API2/HA/Resources.pm b/src/PVE/API2/HA/Resources.pm
index 6b51515..10acedf 100644
--- a/src/PVE/API2/HA/Resources.pm
+++ b/src/PVE/API2/HA/Resources.pm
@@ -149,7 +149,7 @@ __PACKAGE__->register_method ({
 
 	my $cfg = PVE::HA::Config::read_resources_config();
 
-	my $sid = PVE::HA::Tools::parse_sid($param->{sid});
+	my $sid = PVE::HA::Config::parse_sid($param->{sid});
 
 	return &$api_copy_config($cfg, $sid);
     }});
@@ -172,7 +172,7 @@ __PACKAGE__->register_method ({
 	PVE::Cluster::check_cfs_quorum();
 	mkdir("/etc/pve/ha");
 	
-	my ($sid, $type, $name) = PVE::HA::Tools::parse_sid(extract_param($param, 'sid'));
+	my ($sid, $type, $name) = PVE::HA::Config::parse_sid(extract_param($param, 'sid'));
 
 	if (my $param_type = extract_param($param, 'type')) {
 	    # useless, but do it anyway
@@ -221,7 +221,7 @@ __PACKAGE__->register_method ({
 	my $digest = extract_param($param, 'digest');
 	my $delete = extract_param($param, 'delete');
 
-	my ($sid, $type, $name) = PVE::HA::Tools::parse_sid(extract_param($param, 'sid'));
+	my ($sid, $type, $name) = PVE::HA::Config::parse_sid(extract_param($param, 'sid'));
 
 	if (my $param_type = extract_param($param, 'type')) {
 	    # useless, but do it anyway
@@ -294,7 +294,7 @@ __PACKAGE__->register_method ({
     code => sub {
 	my ($param) = @_;
 
-	my ($sid, $type, $name) = PVE::HA::Tools::parse_sid(extract_param($param, 'sid'));
+	my ($sid, $type, $name) = PVE::HA::Config::parse_sid(extract_param($param, 'sid'));
 
 	PVE::HA::Config::service_is_ha_managed($sid);
 
@@ -334,7 +334,7 @@ __PACKAGE__->register_method ({
     code => sub {
 	my ($param) = @_;
 
-	my ($sid, $type, $name) = PVE::HA::Tools::parse_sid(extract_param($param, 'sid'));
+	my ($sid, $type, $name) = PVE::HA::Config::parse_sid(extract_param($param, 'sid'));
 
 	PVE::HA::Config::service_is_ha_managed($sid);
 
@@ -367,7 +367,7 @@ __PACKAGE__->register_method ({
     code => sub {
 	my ($param) = @_;
 
-	my ($sid, $type, $name) = PVE::HA::Tools::parse_sid(extract_param($param, 'sid'));
+	my ($sid, $type, $name) = PVE::HA::Config::parse_sid(extract_param($param, 'sid'));
 
 	PVE::HA::Config::service_is_ha_managed($sid);
 
diff --git a/src/PVE/CLI/ha_manager.pm b/src/PVE/CLI/ha_manager.pm
index 48a4f4b..5ce4c30 100644
--- a/src/PVE/CLI/ha_manager.pm
+++ b/src/PVE/CLI/ha_manager.pm
@@ -11,9 +11,9 @@ use PVE::CLIHandler;
 use PVE::Cluster;
 use PVE::RPCEnvironment;
 
+use PVE::HA::Config; # needed for bash completion in PVE::HA::Tools!
 use PVE::HA::Env::PVE2;
 use PVE::HA::Tools;
-use PVE::HA::Config;
 use PVE::API2::HA::Resources;
 use PVE::API2::HA::Groups;
 use PVE::API2::HA::Status;
diff --git a/src/PVE/HA/Config.pm b/src/PVE/HA/Config.pm
index 5660d19..611ac49 100644
--- a/src/PVE/HA/Config.pm
+++ b/src/PVE/HA/Config.pm
@@ -94,7 +94,7 @@ sub read_and_check_resources_config {
 
     foreach my $sid (keys %{$res->{ids}}) {
 	my $d = $res->{ids}->{$sid};
-	my (undef, undef, $name) = PVE::HA::Tools::parse_sid($sid);
+	my (undef, undef, $name) = parse_sid($sid);
 	$d->{state} = 'started' if !defined($d->{state});
 	$d->{state} = 'started' if $d->{state} eq 'enabled'; # backward compatibility
 	$d->{max_restart} = 1 if !defined($d->{max_restart});
@@ -120,6 +120,39 @@ sub read_and_check_resources_config {
     return $conf;
 }
 
+sub parse_sid {
+    my ($sid) = @_;
+
+    my ($type, $name);
+
+    if ($sid =~ m/^(\d+)$/) {
+	$name = $1;
+
+	my $vmlist = PVE::Cluster::get_vmlist();
+	if (defined($vmlist->{ids}->{$name})) {
+	    my $vm_type = $vmlist->{ids}->{$name}->{type};
+	    if ($vm_type eq 'lxc') {
+		$type = 'ct';
+	    } elsif ($vm_type eq 'qemu') {
+		$type = 'vm';
+	    } else {
+		die "internal error";
+	    }
+	    $sid = "$type:$name";
+	}
+	else {
+	    die "unable do add resource - VM/CT $1 does not exist\n";
+	}
+    } elsif ($sid =~m/^(\S+):(\S+)$/) {
+	$name = $2;
+	$type = $1;
+    } else {
+	die "unable to parse service id '$sid'\n";
+    }
+
+    return wantarray ? ($sid, $type, $name) : $sid;
+}
+
 sub read_group_config {
 
     return cfs_read_file($ha_groups_config);
diff --git a/src/PVE/HA/Env.pm b/src/PVE/HA/Env.pm
index 50441ea..6393532 100644
--- a/src/PVE/HA/Env.pm
+++ b/src/PVE/HA/Env.pm
@@ -87,6 +87,12 @@ sub read_service_config {
     return $self->{plug}->read_service_config();
 }
 
+sub parse_sid {
+    my ($self, $sid) = @_;
+
+    return $self->{plug}->parse_sid($sid);
+}
+
 sub read_fence_config {
     my ($self) = @_;
 
diff --git a/src/PVE/HA/Env/PVE2.pm b/src/PVE/HA/Env/PVE2.pm
index 9d198b9..00ce438 100644
--- a/src/PVE/HA/Env/PVE2.pm
+++ b/src/PVE/HA/Env/PVE2.pm
@@ -120,6 +120,12 @@ sub read_service_config {
     return PVE::HA::Config::read_and_check_resources_config();
 }
 
+sub parse_sid {
+    my ($self, $sid) = @_;
+
+    return PVE::HA::Config::parse_sid($sid);
+}
+
 sub read_fence_config {
     my ($self) = @_;
 
diff --git a/src/PVE/HA/LRM.pm b/src/PVE/HA/LRM.pm
index af7ad08..dda82eb 100644
--- a/src/PVE/HA/LRM.pm
+++ b/src/PVE/HA/LRM.pm
@@ -686,7 +686,7 @@ sub exec_resource_agent {
 
     my $nodename = $haenv->nodename();
 
-    my (undef, $service_type, $service_name) = PVE::HA::Tools::parse_sid($sid);
+    my (undef, $service_type, $service_name) = $haenv->parse_sid($sid);
 
     my $plugin = PVE::HA::Resources->lookup($service_type);
     if (!$plugin) {
diff --git a/src/PVE/HA/Manager.pm b/src/PVE/HA/Manager.pm
index 003db32..cc13ab1 100644
--- a/src/PVE/HA/Manager.pm
+++ b/src/PVE/HA/Manager.pm
@@ -243,7 +243,7 @@ my $fence_recovery_cleanup = sub {
 
     my $haenv = $self->{haenv};
 
-    my (undef, $type, $id) = PVE::HA::Tools::parse_sid($sid);
+    my (undef, $type, $id) = $haenv->parse_sid($sid);
     my $plugin = PVE::HA::Resources->lookup($type);
 
     # should not happen
diff --git a/src/PVE/HA/Sim/Env.pm b/src/PVE/HA/Sim/Env.pm
index 7344b04..a431e26 100644
--- a/src/PVE/HA/Sim/Env.pm
+++ b/src/PVE/HA/Sim/Env.pm
@@ -203,6 +203,18 @@ sub read_service_config {
     return $self->{hardware}->read_service_config();
 }
 
+sub parse_sid {
+    my ($self, $sid) = @_;
+
+    die "unable to parse service id '$sid'\n"
+	if !($sid =~ m/^(\S+):(\S+)$/);
+
+    my $name = $2;
+    my $type = $1;
+
+    return wantarray ? ($sid, $type, $name) : $sid;
+}
+
 sub read_fence_config {
     my ($self) = @_;
 
diff --git a/src/PVE/HA/Tools.pm b/src/PVE/HA/Tools.pm
index 2bdd6ea..9d037f5 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,38 +84,6 @@ PVE::JSONSchema::register_standard_option('pve-ha-group-id', {
     type => 'string', format => 'pve-configid',
 });
 
-sub parse_sid {
-    my ($sid) = @_;
-
-    my ($type, $name);
-
-    if ($sid =~ m/^(\d+)$/) {
-	$name = $1;
-	my $vmlist = PVE::Cluster::get_vmlist();
-	if (defined($vmlist->{ids}->{$name})) {
-	    my $vm_type = $vmlist->{ids}->{$name}->{type};
-	    if ($vm_type eq 'lxc') {
-		$type = 'ct';
-	    } elsif ($vm_type eq 'qemu') {
-		$type = 'vm';
-	    } else {
-		die "internal error";
-	    }
-	    $sid = "$type:$name";
-	}
-	else {
-	    die "unable do add resource - VM/CT $1 does not exist\n";
-	}
-    } elsif  ($sid =~m/^(\S+):(\S+)$/) {
-	$name = $2;
-	$type = $1;
-    } else {
-	die "unable to parse service id '$sid'\n";
-    }
-
-    return wantarray ? ($sid, $type, $name) : $sid;
-}
-
 sub read_json_from_file {
     my ($filename, $default) = @_;
 
@@ -245,7 +213,6 @@ sub complete_sid {
 }
 
 sub complete_enabled_sid {
-
     my $cfg = PVE::HA::Config::read_resources_config();
 
     my $res = [];
@@ -259,7 +226,6 @@ sub complete_enabled_sid {
 }
 
 sub complete_disabled_sid {
-
     my $cfg = PVE::HA::Config::read_resources_config();
 
     my $res = [];
@@ -289,5 +255,4 @@ sub complete_group {
     return $res;
 }
 
-
 1;
-- 
2.19.1





More information about the pve-devel mailing list