]>
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 DM |
8 | use PVE::HA::Groups; |
9 | use PVE::HA::Resources; | |
139a9b90 | 10 | use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file); |
cc32b737 DM |
11 | |
12 | PVE::HA::Groups->register(); | |
13 | ||
14 | PVE::HA::Groups->init(); | |
15 | ||
16 | PVE::HA::Resources::PVEVM->register(); | |
85d36c36 | 17 | #PVE::HA::Resources::IPAddr->register(); |
cc32b737 DM |
18 | |
19 | PVE::HA::Resources->init(); | |
20 | ||
139a9b90 DM |
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 | ||
b96c37ec | 42 | return defined($data) ? decode_json($data) : {}; |
139a9b90 DM |
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 | ||
cc32b737 DM |
71 | sub parse_groups_config { |
72 | my ($filename, $raw) = @_; | |
73 | ||
139a9b90 | 74 | return PVE::HA::Groups->parse_config($filename, $raw); |
cc32b737 DM |
75 | } |
76 | ||
ce216792 | 77 | sub parse_resources_config { |
cc32b737 DM |
78 | my ($filename, $raw) = @_; |
79 | ||
80 | return PVE::HA::Resources->parse_config($filename, $raw); | |
81 | } | |
82 | ||
b83b4ae8 DM |
83 | sub resources_config_exists { |
84 | ||
85 | return (-f "/etc/pve/$ha_resources_config") ? 1 : 0; | |
86 | } | |
87 | ||
139a9b90 DM |
88 | sub read_resources_config { |
89 | ||
90 | return cfs_read_file($ha_resources_config); | |
91 | } | |
92 | ||
93 | sub read_group_config { | |
139a9b90 DM |
94 | |
95 | return cfs_read_file($ha_groups_config); | |
96 | } | |
97 | ||
9eb0f126 DM |
98 | sub write_group_config { |
99 | my ($cfg) = @_; | |
100 | ||
101 | cfs_write_file($ha_groups_config, $cfg); | |
102 | } | |
103 | ||
139a9b90 DM |
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 | ||
eda9314d | 164 | my $sid = "vm:$vmid"; |
139a9b90 DM |
165 | |
166 | return defined($conf->{ids}->{$sid}); | |
167 | } | |
168 | ||
cc32b737 | 169 | 1; |