]>
Commit | Line | Data |
---|---|---|
cc32b737 DM |
1 | package PVE::HA::Config; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
139a9b90 | 5 | use JSON; |
cc32b737 | 6 | |
139a9b90 | 7 | use PVE::HA::Tools; |
cc32b737 | 8 | use PVE::HA::Groups; |
139a9b90 | 9 | use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file); |
d6edb3ea | 10 | use PVE::HA::Resources; |
cc32b737 DM |
11 | |
12 | PVE::HA::Groups->register(); | |
13 | ||
14 | PVE::HA::Groups->init(); | |
15 | ||
139a9b90 DM |
16 | my $manager_status_filename = "ha/manager_status"; |
17 | my $ha_groups_config = "ha/groups.cfg"; | |
18 | my $ha_resources_config = "ha/resources.cfg"; | |
19 | my $crm_commands_filename = "ha/crm_commands"; | |
f83e7e6e | 20 | my $ha_fence_config = "ha/fence.cfg"; |
139a9b90 DM |
21 | |
22 | cfs_register_file($crm_commands_filename, | |
23 | sub { my ($fn, $raw) = @_; return defined($raw) ? $raw : ''; }, | |
24 | sub { my ($fn, $raw) = @_; return $raw; }); | |
25 | cfs_register_file($ha_groups_config, | |
26 | sub { PVE::HA::Groups->parse_config(@_); }, | |
27 | sub { PVE::HA::Groups->write_config(@_); }); | |
28 | cfs_register_file($ha_resources_config, | |
29 | sub { PVE::HA::Resources->parse_config(@_); }, | |
30 | sub { PVE::HA::Resources->write_config(@_); }); | |
31 | cfs_register_file($manager_status_filename, | |
32 | \&json_reader, | |
33 | \&json_writer); | |
f83e7e6e TL |
34 | cfs_register_file($ha_fence_config, |
35 | \&PVE::HA::FenceConfig::parse_config, | |
36 | \&PVE::HA::FenceConfig::write_config); | |
139a9b90 DM |
37 | |
38 | sub json_reader { | |
39 | my ($filename, $data) = @_; | |
40 | ||
f1817b68 | 41 | return defined($data) && length($data) > 0 ? decode_json($data) : {}; |
139a9b90 DM |
42 | } |
43 | ||
44 | sub json_writer { | |
45 | my ($filename, $data) = @_; | |
46 | ||
47 | return encode_json($data); | |
48 | } | |
49 | ||
50 | sub read_lrm_status { | |
51 | my ($node) = @_; | |
52 | ||
53 | die "undefined node" if !defined($node); | |
54 | ||
55 | my $filename = "/etc/pve/nodes/$node/lrm_status"; | |
56 | ||
57 | return PVE::HA::Tools::read_json_from_file($filename, {}); | |
58 | } | |
59 | ||
60 | sub write_lrm_status { | |
61 | my ($node, $status_obj) = @_; | |
62 | ||
63 | die "undefined node" if !defined($node); | |
64 | ||
65 | my $filename = "/etc/pve/nodes/$node/lrm_status"; | |
66 | ||
67 | PVE::HA::Tools::write_json_to_file($filename, $status_obj); | |
68 | } | |
69 | ||
cc32b737 DM |
70 | sub parse_groups_config { |
71 | my ($filename, $raw) = @_; | |
72 | ||
139a9b90 | 73 | return PVE::HA::Groups->parse_config($filename, $raw); |
cc32b737 DM |
74 | } |
75 | ||
ce216792 | 76 | sub parse_resources_config { |
cc32b737 DM |
77 | my ($filename, $raw) = @_; |
78 | ||
79 | return PVE::HA::Resources->parse_config($filename, $raw); | |
80 | } | |
81 | ||
139a9b90 DM |
82 | sub read_resources_config { |
83 | ||
84 | return cfs_read_file($ha_resources_config); | |
85 | } | |
86 | ||
87 | sub read_group_config { | |
139a9b90 DM |
88 | |
89 | return cfs_read_file($ha_groups_config); | |
90 | } | |
91 | ||
9eb0f126 DM |
92 | sub write_group_config { |
93 | my ($cfg) = @_; | |
94 | ||
95 | cfs_write_file($ha_groups_config, $cfg); | |
96 | } | |
97 | ||
139a9b90 DM |
98 | sub write_resources_config { |
99 | my ($cfg) = @_; | |
100 | ||
101 | cfs_write_file($ha_resources_config, $cfg); | |
102 | } | |
103 | ||
104 | sub read_manager_status { | |
105 | my () = @_; | |
106 | ||
107 | return cfs_read_file($manager_status_filename); | |
108 | } | |
109 | ||
110 | sub write_manager_status { | |
111 | my ($status_obj) = @_; | |
f83e7e6e | 112 | |
139a9b90 DM |
113 | cfs_write_file($manager_status_filename, $status_obj); |
114 | } | |
115 | ||
f83e7e6e TL |
116 | sub read_fence_config { |
117 | my () = @_; | |
118 | ||
119 | cfs_read_file($ha_fence_config); | |
120 | } | |
121 | ||
b3c07baa TL |
122 | sub write_fence_config { |
123 | my ($cfg) = @_; | |
124 | ||
125 | cfs_write_file($ha_fence_config, $cfg); | |
126 | } | |
127 | ||
66c7e7ef | 128 | sub lock_ha_domain { |
139a9b90 DM |
129 | my ($code, $errmsg) = @_; |
130 | ||
66c7e7ef | 131 | my $res = PVE::Cluster::cfs_lock_domain("ha", undef, $code); |
139a9b90 DM |
132 | my $err = $@; |
133 | if ($err) { | |
134 | $errmsg ? die "$errmsg: $err" : die $err; | |
135 | } | |
136 | return $res; | |
137 | } | |
138 | ||
139 | sub queue_crm_commands { | |
140 | my ($cmd) = @_; | |
141 | ||
142 | chomp $cmd; | |
143 | ||
144 | my $code = sub { | |
145 | my $data = cfs_read_file($crm_commands_filename); | |
146 | $data .= "$cmd\n"; | |
147 | cfs_write_file($crm_commands_filename, $data); | |
148 | }; | |
149 | ||
66c7e7ef | 150 | return lock_ha_domain($code); |
139a9b90 DM |
151 | } |
152 | ||
153 | sub read_crm_commands { | |
154 | ||
155 | my $code = sub { | |
156 | my $data = cfs_read_file($crm_commands_filename); | |
157 | cfs_write_file($crm_commands_filename, ''); | |
158 | return $data; | |
159 | }; | |
160 | ||
66c7e7ef | 161 | return lock_ha_domain($code); |
139a9b90 DM |
162 | } |
163 | ||
0e703a14 | 164 | my $service_check_ha_state = sub { |
e709d3d0 DM |
165 | my ($conf, $sid, $has_state) = @_; |
166 | ||
167 | if (my $d = $conf->{ids}->{$sid}) { | |
168 | return 1 if !defined($has_state); | |
169 | ||
170 | $d->{state} = 'enabled' if !defined($d->{state}); | |
171 | return 1 if $d->{state} eq $has_state; | |
172 | } | |
173 | ||
174 | return undef; | |
175 | }; | |
176 | ||
139a9b90 | 177 | sub vm_is_ha_managed { |
55c82a65 | 178 | my ($vmid, $has_state) = @_; |
139a9b90 DM |
179 | |
180 | my $conf = cfs_read_file($ha_resources_config); | |
181 | ||
8facb786 | 182 | my $types = PVE::HA::Resources->lookup_types(); |
e709d3d0 | 183 | foreach my $type ('vm', 'ct') { |
0e703a14 | 184 | return 1 if &$service_check_ha_state($conf, "$type:$vmid", $has_state); |
e709d3d0 | 185 | } |
8facb786 TL |
186 | |
187 | return undef; | |
139a9b90 DM |
188 | } |
189 | ||
9e03f787 TL |
190 | sub service_is_ha_managed { |
191 | my ($sid, $has_state, $noerr) = @_; | |
192 | ||
193 | my $conf = cfs_read_file($ha_resources_config); | |
194 | ||
0e703a14 | 195 | return 1 if &$service_check_ha_state($conf, $sid, $has_state); |
9e03f787 TL |
196 | |
197 | die "resource '$sid' is not HA managed\n" if !$noerr; | |
198 | ||
199 | return undef; | |
200 | } | |
917ea00c TL |
201 | |
202 | sub get_service_status { | |
203 | my ($sid) = @_; | |
204 | ||
205 | my $status = { managed => 0 }; | |
206 | ||
207 | my $conf = cfs_read_file($ha_resources_config); | |
208 | ||
0e703a14 | 209 | if (&$service_check_ha_state($conf, $sid)) { |
917ea00c TL |
210 | my $manager_status = cfs_read_file($manager_status_filename); |
211 | ||
212 | $status->{managed} = 1; | |
213 | $status->{group} = $conf->{ids}->{$sid}->{group}; | |
214 | $status->{state} = $manager_status->{service_status}->{$sid}->{state}; | |
215 | } | |
216 | ||
217 | return $status; | |
218 | } | |
219 | ||
cc32b737 | 220 | 1; |