]>
Commit | Line | Data |
---|---|---|
f5a14b93 TL |
1 | package PVE::HA::Sim::Resources; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use base qw(PVE::HA::Resources); | |
7 | ||
8 | # provides some base methods for virtual resources (used in simulation/testing). | |
9 | # reduces code reuse and it's targeted for the main PVE service types, namely | |
10 | # virtual machines (VM) and container (CT) | |
11 | ||
12 | sub verify_name { | |
13 | my ($class, $id) = @_; | |
14 | ||
15 | die "invalid VMID '$id'\n" if $id !~ m/^[1-9][0-9]+$/; | |
16 | } | |
17 | ||
18 | sub options { | |
19 | return { | |
20 | state => { optional => 1 }, | |
21 | group => { optional => 1 }, | |
22 | comment => { optional => 1 }, | |
23 | max_restart => { optional => 1 }, | |
24 | max_relocate => { optional => 1 }, | |
25 | }; | |
26 | } | |
27 | ||
28 | sub config_file { | |
29 | my ($class, $id, $nodename) = @_; | |
30 | ||
31 | my $service_type = $class->type(); | |
32 | ||
33 | # virtual path | |
34 | return "$nodename/$service_type:$id"; | |
35 | } | |
36 | ||
37 | sub is_on_node { | |
38 | my ($class, $haenv, $node, $id) = @_; | |
39 | ||
40 | my $service_type = $class->type(); | |
41 | my $hardware = $haenv->hardware(); | |
42 | my $ss = $hardware->read_service_status($node); | |
43 | ||
44 | return defined($ss->{"$service_type:$id"}) ? 1 : 0; | |
45 | } | |
46 | ||
47 | sub start { | |
48 | my ($class, $haenv, $id) = @_; | |
49 | ||
50 | my $service_type = $class->type(); | |
51 | my $nodename = $haenv->nodename(); | |
52 | my $hardware = $haenv->hardware(); | |
53 | my $ss = $hardware->read_service_status($nodename); | |
54 | ||
55 | $haenv->sleep(2); | |
56 | ||
57 | $ss->{"$service_type:$id"} = 1; | |
58 | ||
59 | $hardware->write_service_status($nodename, $ss); | |
60 | ||
61 | } | |
62 | ||
63 | sub shutdown { | |
64 | my ($class, $haenv, $id) = @_; | |
65 | ||
66 | my $service_type = $class->type(); | |
67 | my $nodename = $haenv->nodename(); | |
68 | my $hardware = $haenv->hardware(); | |
69 | my $ss = $hardware->read_service_status($nodename); | |
70 | ||
71 | $haenv->sleep(2); | |
72 | ||
73 | $ss->{"$service_type:$id"} = 0; | |
74 | ||
75 | $hardware->write_service_status($nodename, $ss); | |
76 | } | |
77 | ||
78 | sub check_running { | |
79 | my ($class, $haenv, $id) = @_; | |
80 | ||
81 | my $service_type = $class->type(); | |
82 | my $nodename = $haenv->nodename(); | |
83 | my $hardware = $haenv->hardware(); | |
84 | ||
85 | my $ss = $hardware->read_service_status($nodename); | |
86 | ||
87 | return ($ss->{"$service_type:$id"}) ? 1 : 0; | |
88 | } | |
89 | ||
90 | ||
91 | sub migrate { | |
92 | my ($class, $haenv, $id, $target, $online) = @_; | |
93 | ||
94 | my $sid = $class->type() . ":$id"; | |
95 | my $nodename = $haenv->nodename(); | |
96 | my $hardware = $haenv->hardware(); | |
97 | my $ss = $hardware->read_service_status($nodename); | |
98 | ||
99 | my $cmd = $online ? "migrate" : "relocate"; | |
100 | $haenv->log("info", "service $sid - start $cmd to node '$target'"); | |
101 | ||
102 | # explicitly shutdown if $online isn't true (relocate) | |
103 | if (!$online && $class->check_running($haenv, $id)) { | |
104 | $haenv->log("info", "stopping service $sid (relocate)"); | |
105 | $class->shutdown($haenv, $id); | |
106 | $haenv->log("info", "service status $sid stopped"); | |
107 | } else { | |
108 | $haenv->sleep(2); # (live) migration time | |
109 | } | |
110 | ||
111 | $haenv->change_service_location($sid, $nodename, $target); | |
112 | $haenv->log("info", "service $sid - end $cmd to node '$target'"); | |
113 | # ensure that the old node doesn't has the service anymore | |
114 | delete $ss->{$sid}; | |
115 | $hardware->write_service_status($nodename, $ss); | |
116 | } | |
117 | ||
118 | ||
119 | 1; |