]> git.proxmox.com Git - pve-ha-manager.git/blob - src/PVE/HA/Config.pm
crm: simply wait if there is no resource config
[pve-ha-manager.git] / src / PVE / HA / Config.pm
1 package PVE::HA::Config;
2
3 use strict;
4 use warnings;
5 use JSON;
6
7 use PVE::HA::Tools;
8 use PVE::HA::Groups;
9 use PVE::HA::Resources;
10 use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
11
12 PVE::HA::Groups->register();
13
14 PVE::HA::Groups->init();
15
16 PVE::HA::Resources::PVEVM->register();
17 #PVE::HA::Resources::IPAddr->register();
18
19 PVE::HA::Resources->init();
20
21 my $manager_status_filename = "ha/manager_status";
22 my $ha_groups_config = "ha/groups.cfg";
23 my $ha_resources_config = "ha/resources.cfg";
24 my $crm_commands_filename = "ha/crm_commands";
25
26 cfs_register_file($crm_commands_filename,
27 sub { my ($fn, $raw) = @_; return defined($raw) ? $raw : ''; },
28 sub { my ($fn, $raw) = @_; return $raw; });
29 cfs_register_file($ha_groups_config,
30 sub { PVE::HA::Groups->parse_config(@_); },
31 sub { PVE::HA::Groups->write_config(@_); });
32 cfs_register_file($ha_resources_config,
33 sub { PVE::HA::Resources->parse_config(@_); },
34 sub { PVE::HA::Resources->write_config(@_); });
35 cfs_register_file($manager_status_filename,
36 \&json_reader,
37 \&json_writer);
38
39 sub json_reader {
40 my ($filename, $data) = @_;
41
42 return defined($data) ? decode_json($data) : {};
43 }
44
45 sub json_writer {
46 my ($filename, $data) = @_;
47
48 return encode_json($data);
49 }
50
51 sub read_lrm_status {
52 my ($node) = @_;
53
54 die "undefined node" if !defined($node);
55
56 my $filename = "/etc/pve/nodes/$node/lrm_status";
57
58 return PVE::HA::Tools::read_json_from_file($filename, {});
59 }
60
61 sub write_lrm_status {
62 my ($node, $status_obj) = @_;
63
64 die "undefined node" if !defined($node);
65
66 my $filename = "/etc/pve/nodes/$node/lrm_status";
67
68 PVE::HA::Tools::write_json_to_file($filename, $status_obj);
69 }
70
71 sub parse_groups_config {
72 my ($filename, $raw) = @_;
73
74 return PVE::HA::Groups->parse_config($filename, $raw);
75 }
76
77 sub parse_resources_config {
78 my ($filename, $raw) = @_;
79
80 return PVE::HA::Resources->parse_config($filename, $raw);
81 }
82
83 sub resources_config_exists {
84
85 return (-f "/etc/pve/$ha_resources_config") ? 1 : 0;
86 }
87
88 sub read_resources_config {
89
90 return cfs_read_file($ha_resources_config);
91 }
92
93 sub read_group_config {
94
95 return cfs_read_file($ha_groups_config);
96 }
97
98 sub write_group_config {
99 my ($cfg) = @_;
100
101 cfs_write_file($ha_groups_config, $cfg);
102 }
103
104 sub write_resources_config {
105 my ($cfg) = @_;
106
107 cfs_write_file($ha_resources_config, $cfg);
108 }
109
110 sub read_manager_status {
111 my () = @_;
112
113 return cfs_read_file($manager_status_filename);
114 }
115
116 sub write_manager_status {
117 my ($status_obj) = @_;
118
119 cfs_write_file($manager_status_filename, $status_obj);
120 }
121
122 sub lock_ha_config {
123 my ($code, $errmsg) = @_;
124
125 # fixme: do not use cfs_lock_storage (replace with cfs_lock_ha)
126 my $res = PVE::Cluster::cfs_lock_storage("_ha_crm_commands", undef, $code);
127 my $err = $@;
128 if ($err) {
129 $errmsg ? die "$errmsg: $err" : die $err;
130 }
131 return $res;
132 }
133
134 sub queue_crm_commands {
135 my ($cmd) = @_;
136
137 chomp $cmd;
138
139 my $code = sub {
140 my $data = cfs_read_file($crm_commands_filename);
141 $data .= "$cmd\n";
142 cfs_write_file($crm_commands_filename, $data);
143 };
144
145 return lock_ha_config($code);
146 }
147
148 sub read_crm_commands {
149
150 my $code = sub {
151 my $data = cfs_read_file($crm_commands_filename);
152 cfs_write_file($crm_commands_filename, '');
153 return $data;
154 };
155
156 return lock_ha_config($code);
157 }
158
159 sub vm_is_ha_managed {
160 my ($vmid) = @_;
161
162 my $conf = cfs_read_file($ha_resources_config);
163
164 my $sid = "vm:$vmid";
165
166 return defined($conf->{ids}->{$sid});
167 }
168
169 1;