]> git.proxmox.com Git - pve-ha-manager.git/blame - src/PVE/HA/Config.pm
crm: simply wait if there is no resource config
[pve-ha-manager.git] / src / PVE / HA / Config.pm
CommitLineData
cc32b737
DM
1package PVE::HA::Config;
2
3use strict;
4use warnings;
139a9b90 5use JSON;
cc32b737 6
139a9b90 7use PVE::HA::Tools;
cc32b737
DM
8use PVE::HA::Groups;
9use PVE::HA::Resources;
139a9b90 10use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
cc32b737
DM
11
12PVE::HA::Groups->register();
13
14PVE::HA::Groups->init();
15
16PVE::HA::Resources::PVEVM->register();
85d36c36 17#PVE::HA::Resources::IPAddr->register();
cc32b737
DM
18
19PVE::HA::Resources->init();
20
139a9b90
DM
21my $manager_status_filename = "ha/manager_status";
22my $ha_groups_config = "ha/groups.cfg";
23my $ha_resources_config = "ha/resources.cfg";
24my $crm_commands_filename = "ha/crm_commands";
25
26cfs_register_file($crm_commands_filename,
27 sub { my ($fn, $raw) = @_; return defined($raw) ? $raw : ''; },
28 sub { my ($fn, $raw) = @_; return $raw; });
29cfs_register_file($ha_groups_config,
30 sub { PVE::HA::Groups->parse_config(@_); },
31 sub { PVE::HA::Groups->write_config(@_); });
32cfs_register_file($ha_resources_config,
33 sub { PVE::HA::Resources->parse_config(@_); },
34 sub { PVE::HA::Resources->write_config(@_); });
35cfs_register_file($manager_status_filename,
36 \&json_reader,
37 \&json_writer);
38
39sub json_reader {
40 my ($filename, $data) = @_;
41
b96c37ec 42 return defined($data) ? decode_json($data) : {};
139a9b90
DM
43}
44
45sub json_writer {
46 my ($filename, $data) = @_;
47
48 return encode_json($data);
49}
50
51sub 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
61sub 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
71sub parse_groups_config {
72 my ($filename, $raw) = @_;
73
139a9b90 74 return PVE::HA::Groups->parse_config($filename, $raw);
cc32b737
DM
75}
76
ce216792 77sub parse_resources_config {
cc32b737
DM
78 my ($filename, $raw) = @_;
79
80 return PVE::HA::Resources->parse_config($filename, $raw);
81}
82
b83b4ae8
DM
83sub resources_config_exists {
84
85 return (-f "/etc/pve/$ha_resources_config") ? 1 : 0;
86}
87
139a9b90
DM
88sub read_resources_config {
89
90 return cfs_read_file($ha_resources_config);
91}
92
93sub read_group_config {
139a9b90
DM
94
95 return cfs_read_file($ha_groups_config);
96}
97
9eb0f126
DM
98sub write_group_config {
99 my ($cfg) = @_;
100
101 cfs_write_file($ha_groups_config, $cfg);
102}
103
139a9b90
DM
104sub write_resources_config {
105 my ($cfg) = @_;
106
107 cfs_write_file($ha_resources_config, $cfg);
108}
109
110sub read_manager_status {
111 my () = @_;
112
113 return cfs_read_file($manager_status_filename);
114}
115
116sub write_manager_status {
117 my ($status_obj) = @_;
118
119 cfs_write_file($manager_status_filename, $status_obj);
120}
121
122sub 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
134sub 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
148sub 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
159sub 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 1691;