]>
git.proxmox.com Git - pve-ha-manager.git/blob - src/PVE/HA/Env/PVE2.pm
1 package PVE
::HA
::Env
::PVE2
;
5 use POSIX
qw(:errno_h :fcntl_h);
11 use PVE
::Cluster
qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
13 use PVE
::RPCEnvironment
;
20 my $lockdir = "/etc/pve/priv/lock";
23 my ($this, $nodename) = @_;
25 die "missing nodename" if !$nodename;
27 my $class = ref($this) || $this;
29 my $self = bless {}, $class;
31 $self->{nodename
} = $nodename;
39 return $self->{nodename
};
42 sub read_manager_status
{
45 return PVE
::HA
::Config
::read_manager_status
();
48 sub write_manager_status
{
49 my ($self, $status_obj) = @_;
51 PVE
::HA
::Config
::write_manager_status
($status_obj);
55 my ($self, $node) = @_;
57 $node = $self->{nodename
} if !defined($node);
59 return PVE
::HA
::Config
::read_lrm_status
($node);
62 sub write_lrm_status
{
63 my ($self, $status_obj) = @_;
65 my $node = $self->{nodename
};
67 PVE
::HA
::Config
::write_lrm_status
($node, $status_obj);
70 sub queue_crm_commands
{
71 my ($self, $cmd) = @_;
73 return PVE
::HA
::Config
::queue_crm_commands
($cmd);
76 sub read_crm_commands
{
79 return PVE
::HA
::Config
::read_crm_commands
();
82 sub service_config_exists
{
85 return PVE
::HA
::Config
::resources_config_exists
();
88 sub read_service_config
{
91 my $res = PVE
::HA
::Config
::read_resources_config
();
93 my $vmlist = PVE
::Cluster
::get_vmlist
();
96 foreach my $sid (keys %{$res->{ids
}}) {
97 my $d = $res->{ids
}->{$sid};
98 my (undef, undef, $name) = PVE
::HA
::Tools
::parse_sid
($sid);
99 $d->{state} = 'enabled' if !defined($d->{state});
100 if (PVE
::HA
::Resources-
>lookup($d->{type
})) {
101 if (my $vmd = $vmlist->{ids
}->{$name}) {
103 warn "no such VM '$name'\n";
105 $d->{node
} = $vmd->{node
};
109 if (defined($d->{node
})) {
112 warn "service '$sid' without node\n";
121 sub change_service_location
{
122 my ($self, $sid, $current_node, $new_node) = @_;
124 my (undef, $type, $name) = PVE
::HA
::Tools
::parse_sid
($sid);
126 if(my $plugin = PVE
::HA
::Resources-
>lookup($type)) {
127 my $old = $plugin->config_file($name, $current_node);
128 my $new = $plugin->config_file($name, $new_node);
129 rename($old, $new) ||
130 die "rename '$old' to '$new' failed - $!\n";
136 sub read_group_config
{
139 return PVE
::HA
::Config
::read_group_config
();
142 # this should return a hash containing info
143 # what nodes are members and online.
147 my ($node_info, $quorate) = ({}, 0);
149 my $nodename = $self->{nodename
};
151 $quorate = PVE
::Cluster
::check_cfs_quorum
(1) || 0;
153 my $members = PVE
::Cluster
::get_members
();
155 foreach my $node (keys %$members) {
156 my $d = $members->{$node};
157 $node_info->{$node}->{online
} = $d->{online
};
160 $node_info->{$nodename}->{online
} = 1; # local node is always up
162 return ($node_info, $quorate);
166 my ($self, $level, $msg) = @_;
170 syslog
($level, $msg);
173 my $last_lock_status = {};
176 my ($self, $lockid) = @_;
180 my $filename = "$lockdir/$lockid";
182 my $last = $last_lock_status->{$lockid} || 0;
187 my $retry_timeout = 100; # fixme: what timeout
193 # pve cluster filesystem not online
194 die "can't create '$lockdir' (pmxcfs not mounted?)\n" if ! -d
$lockdir;
196 if ($last && (($ctime - $last) < $retry_timeout)) {
197 # send cfs lock update request (utime)
198 if (!utime(0, $ctime, $filename)) {
200 die "cfs lock update failed - $!\n";
204 # fixme: wait some time?
205 if (!(mkdir $filename)) {
206 utime 0, 0, $filename; # cfs unlock request
207 die "can't get cfs lock\n";
217 # $self->log('err', $err) if $err; # for debugging
221 $last_lock_status->{$lockid} = $got_lock ?
$ctime : 0;
223 if (!!$got_lock != !!$last) {
225 $self->log('info', "successfully aquired lock '$lockid'");
227 my $msg = "lost lock '$lockid";
228 $msg .= " - $err" if $err;
229 $self->log('err', $msg);
232 # $self->log('err', $err) if $err; # for debugging
238 sub get_ha_manager_lock
{
241 return $self->get_pve_lock("ha_manager_lock");
244 sub get_ha_agent_lock
{
245 my ($self, $node) = @_;
247 $node = $self->nodename() if !defined($node);
249 return $self->get_pve_lock("ha_agent_${node}_lock");
257 $quorate = PVE
::Cluster
::check_cfs_quorum
();
270 my ($self, $delay) = @_;
276 my ($self, $end_time) = @_;
279 my $cur_time = time();
281 last if $cur_time >= $end_time;
287 sub loop_start_hook
{
290 PVE
::Cluster
::cfs_update
();
292 $self->{loop_start
} = $self->get_time();
298 my $delay = $self->get_time() - $self->{loop_start
};
300 warn "loop take too long ($delay seconds)\n" if $delay > 30;
308 die "watchdog already open\n" if defined($watchdog_fh);
310 $watchdog_fh = IO
::Socket
::UNIX-
>new(
311 Type
=> SOCK_STREAM
(),
312 Peer
=> "/run/watchdog-mux.sock") ||
313 die "unable to open watchdog socket - $!\n";
315 $self->log('info', "watchdog active");
318 sub watchdog_update
{
319 my ($self, $wfh) = @_;
321 my $res = $watchdog_fh->syswrite("\0", 1);
322 if (!defined($res)) {
323 $self->log('err', "watchdog update failed - $!\n");
327 $self->log('err', "watchdog update failed - write $res bytes\n");
335 my ($self, $wfh) = @_;
337 $watchdog_fh->syswrite("V", 1); # magic watchdog close
338 if (!$watchdog_fh->close()) {
339 $self->log('err', "watchdog close failed - $!");
341 $watchdog_fh = undef;
342 $self->log('info', "watchdog closed (disabled)");
347 my ($self, $upid) = @_;
349 my $task = PVE
::Tools
::upid_decode
($upid);
352 while (PVE
::ProcFSTools
::check_process_running
($task->{pid
}, $task->{pstart
})) {
353 $self->log('debug', "Task still active, waiting");
364 sub exec_resource_agent
{
365 my ($self, $sid, $service_config, $cmd, @params) = @_;
367 # setup execution environment
369 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
371 PVE
::INotify
::inotify_close
();
373 PVE
::INotify
::inotify_init
();
375 PVE
::Cluster
::cfs_update
();
377 my $nodename = $self->{nodename
};
379 # fixme: return valid_exit code (instead of using die) ?
381 my (undef, $service_type, $service_name) = PVE
::HA
::Tools
::parse_sid
($sid);
383 my $plugin = PVE
::HA
::Resources-
>lookup($service_type);
384 die "service type '$service_type' not implemented" if !$plugin;
386 # fixme: return valid_exit code
387 die "service '$sid' not on this node" if $service_config->{node
} ne $nodename;
389 my $vmid = $service_name;
391 my $running = $plugin->check_running($vmid);
393 if ($cmd eq 'started') {
395 # fixme: count failures
397 return 0 if $running;
399 $self->log("info", "starting service $sid");
406 $plugin->start($self, $params);
408 $running = $plugin->check_running($vmid);
411 $self->log("info", "service status $sid started");
414 $self->log("info", "unable to start service $sid");
418 } elsif ($cmd eq 'request_stop' || $cmd eq 'stopped') {
420 return 0 if !$running;
422 $self->log("info", "stopping service $sid");
424 my $timeout = 60; # fixme: make this configurable
433 $plugin->shutdown($self, $params);
435 $running = $plugin->check_running($vmid);
438 $self->log("info", "service status $sid stopped");
444 } elsif ($cmd eq 'migrate' || $cmd eq 'relocate') {
446 my $target = $params[0];
447 die "$cmd '$sid' failed - missing target\n" if !defined($target);
449 if ($service_config->{node
} eq $target) {
454 # we always do (live) migration
462 my $oldconfig = $plugin->config_file($vmid, $nodename);
464 $plugin->migrate($self, $params);
466 # something went wrong if old config file is still there
468 $self->log("err", "service $sid not moved (migration error)");
474 } elsif ($cmd eq 'error') {
478 $self->log("err", "service $sid is in an error state while running");
480 $self->log("info", "service $sid is not running and in an error state");
486 die "implement me (cmd '$cmd')";