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