]> git.proxmox.com Git - pve-ha-manager.git/commitdiff
Replacing hardcoded qemu commands with plugin calls
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 2 Sep 2015 15:52:32 +0000 (17:52 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 3 Sep 2015 06:52:39 +0000 (08:52 +0200)
Now a service specific plugin gets loaded and the calls to commands
like 'migrate' or 'stop' will be handled by the plugin.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/PVE/HA/Env/PVE2.pm

index 4193f495103e76a7b1b1b614db60d7d4350d932c..2df80ab16d5b336b405957ea57635fe09b9717b8 100644 (file)
@@ -16,8 +16,6 @@ use PVE::HA::Tools;
 use PVE::HA::Env;
 use PVE::HA::Config;
 
-use PVE::QemuServer;
-use PVE::API2::Qemu;
 
 my $lockdir = "/etc/pve/priv/lock";
 
@@ -99,7 +97,7 @@ sub read_service_config {
        my $d = $res->{ids}->{$sid};
        my (undef, undef, $name) = PVE::HA::Tools::parse_sid($sid);
        $d->{state} = 'enabled' if !defined($d->{state});
-       if ($d->{type} eq 'vm') {
+       if (PVE::HA::Resources->lookup($d->{type})) {
            if (my $vmd = $vmlist->{ids}->{$name}) {
                if (!$vmd) {
                    warn "no such VM '$name'\n";
@@ -125,9 +123,9 @@ sub change_service_location {
 
     my (undef, $type, $name) = PVE::HA::Tools::parse_sid($sid);
 
-    if ($type eq 'vm') {
-       my $old = PVE::QemuServer::config_file($name, $current_node);
-       my $new = PVE::QemuServer::config_file($name, $new_node);
+    if(my $plugin = PVE::HA::Resources->lookup($type)) {
+       my $old = $plugin->config_file($name, $current_node);
+       my $new = $plugin->config_file($name, $new_node);
        rename($old, $new) ||
            die "rename '$old' to '$new' failed - $!\n";
     } else {
@@ -382,16 +380,17 @@ sub exec_resource_agent {
 
     my (undef, $service_type, $service_name) = PVE::HA::Tools::parse_sid($sid);
 
-    die "service type '$service_type'not implemented" if $service_type ne 'vm';
+    my $plugin = PVE::HA::Resources->lookup($service_type);
+    die "service type '$service_type' not implemented" if !$plugin;
+
+    # fixme: return valid_exit code
+    die "service '$sid' not on this node" if $service_config->{node} ne $nodename;
 
     my $vmid = $service_name;
 
-    my $running = PVE::QemuServer::check_running($vmid, 1);
-    if ($cmd eq 'started') {
+    my $running = $plugin->check_running($vmid);
 
-       # fixme: return valid_exit code
-       die "service '$sid' not on this node" if $service_config->{node} ne $nodename;
+    if ($cmd eq 'started') {
 
        # fixme: count failures
        
@@ -399,10 +398,14 @@ sub exec_resource_agent {
 
        $self->log("info", "starting service $sid");
 
-       my $upid = PVE::API2::Qemu->vm_start({node => $nodename, vmid => $vmid});
-       $self->upid_wait($upid);
+       my $params = {
+           node => $nodename,
+           vmid => $vmid
+       };
+
+       $plugin->start($self, $params);
 
-       $running = PVE::QemuServer::check_running($vmid, 1);
+       $running = $plugin->check_running($vmid);
 
        if ($running) {
            $self->log("info", "service status $sid started");
@@ -414,26 +417,22 @@ sub exec_resource_agent {
 
     } elsif ($cmd eq 'request_stop' || $cmd eq 'stopped') {
 
-       # fixme: return valid_exit code
-       die "service '$sid' not on this node" if $service_config->{node} ne $nodename;
-
        return 0 if !$running;
 
        $self->log("info", "stopping service $sid");
 
        my $timeout = 60; # fixme: make this configurable
-       
-       my $param = {
-           node => $nodename, 
-           vmid => $vmid, 
+
+       my $params = {
+           node => $nodename,
+           vmid => $vmid,
            timeout => $timeout,
            forceStop => 1,
        };
 
-       my $upid = PVE::API2::Qemu->vm_shutdown($param);
-       $self->upid_wait($upid);
+       $plugin->shutdown($self, $params);
 
-       $running = PVE::QemuServer::check_running($vmid, 1);
+       $running = $plugin->check_running($vmid);
 
        if (!$running) {
            $self->log("info", "service status $sid stopped");
@@ -447,42 +446,31 @@ sub exec_resource_agent {
        my $target = $params[0];
        die "$cmd '$sid' failed - missing target\n" if !defined($target);
 
-       # fixme: return valid_exit code
-       die "service '$sid' not on this node" if $service_config->{node} ne $nodename;
-       
        if ($service_config->{node} eq $target) {
            # already there
            return 0;
-       } 
-       
-       if (!$running) {
-           $self->change_service_location($sid, $nodename, $target);
-           $self->log("info", "service $sid moved to node '$target'");
-           return 0;
-       } else {
-           # we alwas do live migration if VM is online
+       }
 
-           my $params = {
-               node => $nodename, 
-               vmid => $vmid,
-               target => $target,
-               online => 1,
-           };
+       # we always do (live) migration
+       my $params = {
+           node => $nodename,
+           vmid => $vmid,
+           target => $target,
+           online => 1,
+       };
 
-           my $oldconfig = PVE::QemuServer::config_file($vmid, $nodename);
+       my $oldconfig = $plugin->config_file($vmid, $nodename);
 
-           my $upid = PVE::API2::Qemu->migrate_vm($params);
-           $self->upid_wait($upid);
+       $plugin->migrate($self, $params);
 
-           # something went wrong if old config file is still there
-           if (-f $oldconfig) {
-               $self->log("err", "service $sid not moved (migration error)");
-               return 1;
-           }
-           
-           return 0;
+       # something went wrong if old config file is still there
+       if (-f $oldconfig) {
+           $self->log("err", "service $sid not moved (migration error)");
+           return 1;
        }
-       
+
+       return 0;
+
     }
 
     die "implement me (cmd '$cmd')";