]> git.proxmox.com Git - pve-ha-manager.git/commitdiff
use cfs_read_file and cfs_write_file
authorDietmar Maurer <dietmar@proxmox.com>
Fri, 27 Mar 2015 10:40:21 +0000 (11:40 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 27 Mar 2015 10:40:21 +0000 (11:40 +0100)
src/PVE/HA/Env.pm
src/PVE/HA/Env/PVE2.pm
src/PVE/HA/Sim/Env.pm

index fe1c82a313dedbb76ebae913ba2147150f757c75..b345f682c6fbd0fc2275793abb5e1d33c9a0ca5d 100644 (file)
@@ -67,13 +67,6 @@ sub read_crm_commands {
     return $self->{plug}->read_crm_commands();
 }
 
-# we use this to enable/disbale ha
-sub service_config_exists {
-    my ($self) = @_;
-
-    return $self->{plug}->service_config_exists();
-}
-
 sub read_service_config {
     my ($self) = @_;
 
index b88560c24a94ceb2bf67bfc64a2daf4f287f7534..9f573d6fb691c6d4b3b21432ea578b5d31a440d2 100644 (file)
@@ -5,10 +5,11 @@ use warnings;
 use POSIX qw(:errno_h :fcntl_h);
 use IO::File;
 use IO::Socket::UNIX;
+use JSON;
 
 use PVE::SafeSyslog;
 use PVE::Tools;
-use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_lock_file);
+use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
 use PVE::INotify;
 use PVE::RPCEnvironment;
 
@@ -21,32 +22,45 @@ use PVE::API2::Qemu;
 
 my $lockdir = "/etc/pve/priv/lock";
 
-my $manager_status_filename = "/etc/pve/ha/manager_status";
-my $ha_groups_config = "/etc/pve/ha/groups.cfg";
-my $ha_resources_config = "/etc/pve/ha/resources.cfg";
+my $manager_status_filename = "ha/manager_status";
+my $ha_groups_config = "ha/groups.cfg";
+my $ha_resources_config = "ha/resources.cfg";
+my $crm_commands_filename = "ha/crm_commands";
 
-# fixme:
-#cfs_register_file($ha_groups_config, 
-#                sub { PVE::HA::Groups->parse_config(@_); },
-#                sub { PVE::HA::Groups->write_config(@_); });
-#cfs_register_file($ha_resources_config, 
-#                sub { PVE::HA::Resources->parse_config(@_); },
-#                sub { PVE::HA::Resources->write_config(@_); });
+cfs_register_file($crm_commands_filename, 
+                 sub { my ($fn, $raw) = @_; return defined($raw) ? $raw : ''; },
+                 sub { my ($fn, $raw) = @_; return $raw; });
+cfs_register_file($ha_groups_config, 
+                 sub { PVE::HA::Groups->parse_config(@_); },
+                 sub { PVE::HA::Groups->write_config(@_); });
+cfs_register_file($ha_resources_config, 
+                 sub { PVE::HA::Resources->parse_config(@_); },
+                 sub { PVE::HA::Resources->write_config(@_); });
+cfs_register_file($manager_status_filename, 
+                 \&json_reader, 
+                 \&json_writer);
+
+sub json_reader {
+    my ($filename, $data) = @_;
+
+    return decode_json($data || {});
+}
+
+sub json_writer {
+    my ($filename, $data) = @_;
+
+    return encode_json($data);
+}
 
 sub read_resources_config {
-    my $raw = '';
 
-    $raw = PVE::Tools::file_get_contents($ha_resources_config)
-       if -f $ha_resources_config;
-    
-    return PVE::HA::Config::parse_resources_config($ha_resources_config, $raw);
+    return cfs_read_file($ha_resources_config);
 }
 
 sub write_resources_config {
     my ($cfg) = @_;
 
-    my $raw = PVE::HA::Resources->write_config($ha_resources_config, $cfg);
-    PVE::Tools::file_set_contents($ha_resources_config, $raw);
+    cfs_write_file($ha_resources_config, $cfg);
 }
 
 sub lock_ha_config {
@@ -83,18 +97,14 @@ sub nodename {
 
 sub read_manager_status {
     my ($self) = @_;
-    
-    my $filename = $manager_status_filename;
 
-    return PVE::HA::Tools::read_json_from_file($filename, {});  
+    return cfs_read_file($manager_status_filename);
 }
 
 sub write_manager_status {
     my ($self, $status_obj) = @_;
     
-    my $filename = $manager_status_filename;
-
-    PVE::HA::Tools::write_json_to_file($filename, $status_obj); 
+    cfs_write_file($manager_status_filename, $status_obj);
 }
 
 sub read_lrm_status {
@@ -117,12 +127,6 @@ sub write_lrm_status {
     PVE::HA::Tools::write_json_to_file($filename, $status_obj); 
 }
 
-sub service_config_exists {
-    my ($self) = @_;
-
-    return -f $ha_resources_config ? 1 : 0;
-}
-
 sub read_service_config {
     my ($self) = @_;
 
@@ -174,14 +178,7 @@ sub change_service_location {
 sub read_group_config {
     my ($self) = @_;
 
-    # fixme: use cfs_read_file
-    
-    my $raw = '';
-
-    $raw = PVE::Tools::file_get_contents($ha_groups_config)
-       if -f $ha_groups_config;
-    
-    return PVE::HA::Config::parse_groups_config($ha_groups_config, $raw);
+    return cfs_read_file($ha_groups_config);
 }
 
 sub queue_crm_commands {
@@ -190,13 +187,10 @@ sub queue_crm_commands {
     chomp $cmd;
 
     my $code = sub {
-       my $data = '';
-       my $filename = "/etc/pve/ha/crm_commands";
-       if (-f $filename) {
-           $data = PVE::Tools::file_get_contents($filename);
-       }
+       my $data = cfs_read_file($crm_commands_filename);
        $data .= "$cmd\n";
-       PVE::Tools::file_set_contents($filename, $data);
+       cfs_write_file($crm_commands_filename, $data);
+       print "WRITE:$data\n";
     };
 
     return lock_ha_config($code);
@@ -206,14 +200,8 @@ sub read_crm_commands {
     my ($self) = @_;
 
     my $code = sub {
-       my $data = '';
-
-       my $filename = "/etc/pve/ha/crm_commands";
-       if (-f $filename) {
-           $data = PVE::Tools::file_get_contents($filename);
-           PVE::Tools::file_set_contents($filename, '');
-       }
-
+       my $data = cfs_read_file($crm_commands_filename);
+       cfs_write_file($crm_commands_filename, '');
        return $data;
     };
 
index a024e35d0bab851e2aea7ade2081a9c32e895374..e63c3fc62abed58a50a3e2ffa20b05754b72fecc 100644 (file)
@@ -139,12 +139,6 @@ sub write_lrm_status {
     return $self->{hardware}->write_lrm_status($node, $status_obj);
 }
 
-sub service_config_exists {
-    my ($self) = @_;
-
-    return 1; # assume ha is always enabled here
-}
-
 sub read_service_config {
     my ($self) = @_;