]> git.proxmox.com Git - pve-ha-manager.git/blob - src/ha-manager
add warnings if ha group does not exists
[pve-ha-manager.git] / src / ha-manager
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6
7 use PVE::INotify;
8 use JSON;
9
10 use PVE::JSONSchema qw(get_standard_option);
11 use PVE::CLIHandler;
12 use PVE::Cluster;
13
14 use PVE::HA::Tools;
15 use PVE::API2::HA::Resources;
16 use PVE::API2::HA::Groups;
17 use PVE::API2::HA::Status;
18 use PVE::HA::Env::PVE2;
19
20 use base qw(PVE::CLIHandler);
21
22 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
23 $ENV{LC_ALL} = 'C';
24
25 die "please run as root\n" if $> != 0;
26
27 my $nodename = PVE::INotify::nodename();
28
29 __PACKAGE__->register_method ({
30 name => 'enable',
31 path => 'enable',
32 method => 'POST',
33 description => "Enable a HA resource.",
34 parameters => {
35 additionalProperties => 0,
36 properties => {
37 sid => get_standard_option('pve-ha-resource-or-vm-id'),
38 },
39 },
40 returns => { type => 'null' },
41 code => sub {
42 my ($param) = @_;
43
44 my $sid = PVE::HA::Tools::parse_sid($param->{sid});
45
46 # delete state (default is 'enabled')
47 PVE::API2::HA::Resources->update({ sid => $sid, delete => 'state' });
48
49 return undef;
50 }});
51
52 __PACKAGE__->register_method ({
53 name => 'disable',
54 path => 'disable',
55 method => 'POST',
56 description => "Disable a HA resource.",
57 parameters => {
58 additionalProperties => 0,
59 properties => {
60 sid => get_standard_option('pve-ha-resource-or-vm-id'),
61 },
62 },
63 returns => { type => 'null' },
64 code => sub {
65 my ($param) = @_;
66
67 my $sid = PVE::HA::Tools::parse_sid($param->{sid});
68
69 PVE::API2::HA::Resources->update({ sid => $sid, state => 'disabled' });
70
71 return undef;
72 }});
73
74 my $timestamp_to_status = sub {
75 my ($ctime, $timestamp) = @_;
76
77 my $tdiff = $ctime - $timestamp;
78 if ($tdiff > 30) {
79 return "old timestamp - dead?";
80 } elsif ($tdiff < -2) {
81 return "detected time drift!";
82 } else {
83 return "active";
84 }
85 };
86
87 __PACKAGE__->register_method ({
88 name => 'status',
89 path => 'status',
90 method => 'GET',
91 description => "Display HA manger status.",
92 parameters => {
93 additionalProperties => 0,
94 properties => {
95 verbose => {
96 description => "Verbose output. Include complete CRM and LRM status (JSON).",
97 type => 'boolean',
98 default => 0,
99 optional => 1,
100 }
101 },
102 },
103 returns => { type => 'null' },
104 code => sub {
105 my ($param) = @_;
106
107 my $res = PVE::API2::HA::Status->status({});
108 foreach my $e (@$res) {
109 print "$e->{type} $e->{status}\n";
110 }
111
112 if ($param->{verbose}) {
113 print "full cluster state:\n";
114 my $data = PVE::API2::HA::Status->manager_status({});
115 print to_json($data, { pretty => 1, canonical => 1} );
116 }
117
118 return undef;
119 }});
120
121 my $cmddef = {
122 enable => [ __PACKAGE__, 'enable', ['sid']],
123 disable => [ __PACKAGE__, 'disable', ['sid']],
124 status => [ __PACKAGE__, 'status'],
125 config => [ 'PVE::API2::HA::Resources', 'index', [], {}, sub {
126 my $res = shift;
127 foreach my $rec (sort { $a->{sid} cmp $b->{sid} } @$res) {
128 my ($type, $name) = split(':', $rec->{sid}, 2);
129 print "$type:$name\n";
130 foreach my $k (sort keys %$rec) {
131 next if $k eq 'digest' || $k eq 'sid' ||
132 $k eq 'type' || $k eq 'errors';
133 print "\t$k $rec->{$k}\n";
134 }
135 if (my $errors = $rec->{errors}) {
136 foreach my $p (keys %$errors) {
137 warn "error: property '$p' - $errors->{$p}\n";
138 }
139 }
140 print "\n";
141 }}],
142 groups => [ 'PVE::API2::HA::Groups', 'index', [], {}, sub {
143 my $res = shift;
144 foreach my $rec (sort { $a->{group} cmp $b->{group} } @$res) {
145 print "group: $rec->{group}\n";
146 foreach my $k (sort keys %$rec) {
147 next if $k eq 'digest' || $k eq 'group' ||
148 $k eq 'type';
149 print "\t$k $rec->{$k}\n";
150 }
151 print "\n";
152 }}],
153 add => [ "PVE::API2::HA::Resources", 'create', ['sid'] ],
154 remove => [ "PVE::API2::HA::Resources", 'delete', ['sid'] ],
155 set => [ "PVE::API2::HA::Resources", 'update', ['sid'] ],
156
157 migrate => [ "PVE::API2::HA::Resources", 'migrate', ['sid', 'node'] ],
158 relocate => [ "PVE::API2::HA::Resources", 'relocate', ['sid', 'node'] ],
159
160 };
161
162 my $cmd = shift;
163
164 if ($cmd && $cmd ne 'printmanpod' && $cmd ne 'verifyapi') {
165 PVE::Cluster::check_cfs_is_mounted();
166 PVE::Cluster::cfs_update();
167 }
168
169 PVE::CLIHandler::handle_cmd($cmddef, "ha-manager", $cmd, \@ARGV, undef, $0);
170
171 exit 0;
172
173 __END__
174
175 =head1 NAME
176
177 pvecm - Proxmox VE HA Command Line Interface
178
179 =head1 SYNOPSIS
180
181 =include synopsis
182
183 =head1 DESCRIPTION
184
185 ha-manager is a program to manage the HA configuration.
186
187 =include pve_copyright
188
189