]>
Commit | Line | Data |
---|---|---|
58a3c91c FG |
1 | package PVE::AbstractMigrate; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use POSIX qw(strftime); | |
6 | use PVE::Tools; | |
9591475a | 7 | use PVE::Cluster; |
58a3c91c FG |
8 | |
9 | my $msg2text = sub { | |
10 | my ($level, $msg) = @_; | |
11 | ||
12 | chomp $msg; | |
13 | ||
14 | return '' if !$msg; | |
15 | ||
16 | my $res = ''; | |
17 | ||
9ed0fe88 | 18 | my $tstr = strftime("%F %H:%M:%S", localtime); |
58a3c91c FG |
19 | |
20 | foreach my $line (split (/\n/, $msg)) { | |
21 | if ($level eq 'err') { | |
22 | $res .= "$tstr ERROR: $line\n"; | |
23 | } else { | |
24 | $res .= "$tstr $line\n"; | |
25 | } | |
26 | } | |
27 | ||
28 | return $res; | |
29 | }; | |
30 | ||
31 | sub log { | |
32 | my ($self, $level, $msg) = @_; | |
33 | ||
34 | chomp $msg; | |
35 | ||
36 | return if !$msg; | |
37 | ||
38 | print &$msg2text($level, $msg); | |
39 | } | |
40 | ||
41 | sub cmd { | |
42 | my ($self, $cmd, %param) = @_; | |
43 | ||
44 | my $logfunc = sub { | |
45 | my $line = shift; | |
46 | $self->log('info', $line); | |
47 | }; | |
48 | ||
49 | $self->log('info', "# " . PVE::Tools::cmd2string($cmd)); | |
50 | ||
51 | PVE::Tools::run_command($cmd, %param, outfunc => $logfunc, errfunc => $logfunc); | |
52 | } | |
53 | ||
54 | my $run_command_quiet_full = sub { | |
55 | my ($self, $cmd, $logerr, %param) = @_; | |
56 | ||
57 | my $log = ''; | |
58 | my $logfunc = sub { | |
59 | my $line = shift; | |
60 | $log .= &$msg2text('info', $line);; | |
61 | }; | |
62 | ||
63 | eval { PVE::Tools::run_command($cmd, %param, outfunc => $logfunc, errfunc => $logfunc); }; | |
64 | if (my $err = $@) { | |
65 | $self->log('info', "# " . PVE::Tools::cmd2string($cmd)); | |
66 | print $log; | |
67 | if ($logerr) { | |
68 | $self->{errors} = 1; | |
69 | $self->log('err', $err); | |
70 | } else { | |
71 | die $err; | |
72 | } | |
73 | } | |
74 | }; | |
75 | ||
76 | sub cmd_quiet { | |
77 | my ($self, $cmd, %param) = @_; | |
78 | return &$run_command_quiet_full($self, $cmd, 0, %param); | |
79 | } | |
80 | ||
81 | sub cmd_logerr { | |
82 | my ($self, $cmd, %param) = @_; | |
83 | return &$run_command_quiet_full($self, $cmd, 1, %param); | |
84 | } | |
85 | ||
58a3c91c FG |
86 | my $eval_int = sub { |
87 | my ($self, $func, @param) = @_; | |
88 | ||
89 | eval { | |
90 | local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { | |
91 | $self->{delayed_interrupt} = 0; | |
92 | die "interrupted by signal\n"; | |
93 | }; | |
94 | local $SIG{PIPE} = sub { | |
95 | $self->{delayed_interrupt} = 0; | |
96 | die "interrupted by signal\n"; | |
97 | }; | |
98 | ||
99 | my $di = $self->{delayed_interrupt}; | |
100 | $self->{delayed_interrupt} = 0; | |
101 | ||
102 | die "interrupted by signal\n" if $di; | |
103 | ||
104 | &$func($self, @param); | |
105 | }; | |
106 | }; | |
107 | ||
108 | my @ssh_opts = ('-o', 'BatchMode=yes'); | |
109 | my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts); | |
58a3c91c | 110 | |
9591475a | 111 | # FIXME: nodeip is now unused |
58a3c91c FG |
112 | sub migrate { |
113 | my ($class, $node, $nodeip, $vmid, $opts) = @_; | |
114 | ||
115 | $class = ref($class) || $class; | |
116 | ||
40318e97 WB |
117 | my $dc_conf = PVE::Cluster::cfs_read_file('datacenter.cfg'); |
118 | ||
9591475a WB |
119 | my $migration_network = $opts->{migration_network}; |
120 | if (!defined($migration_network)) { | |
9591475a WB |
121 | $migration_network = $dc_conf->{migration}->{network}; |
122 | } | |
123 | my $ssh_info = PVE::Cluster::get_ssh_info($node, $migration_network); | |
124 | $nodeip = $ssh_info->{ip}; | |
125 | ||
40318e97 WB |
126 | my $migration_type = 'secure'; |
127 | if (defined($opts->{migration_type})) { | |
128 | $migration_type = $opts->{migration_type}; | |
129 | } elsif (defined($dc_conf->{migration}->{type})) { | |
130 | $migration_type = $dc_conf->{migration}->{type}; | |
131 | } | |
132 | $opts->{migration_type} = $migration_type; | |
133 | ||
58a3c91c FG |
134 | my $self = { |
135 | delayed_interrupt => 0, | |
136 | opts => $opts, | |
137 | vmid => $vmid, | |
138 | node => $node, | |
9591475a | 139 | ssh_info => $ssh_info, |
58a3c91c | 140 | nodeip => $nodeip, |
9591475a | 141 | rem_ssh => PVE::Cluster::ssh_info_to_command($ssh_info) |
58a3c91c FG |
142 | }; |
143 | ||
144 | $self = bless $self, $class; | |
145 | ||
146 | my $starttime = time(); | |
147 | ||
58a3c91c FG |
148 | local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub { |
149 | $self->log('err', "received interrupt - delayed"); | |
150 | $self->{delayed_interrupt} = 1; | |
151 | }; | |
152 | ||
58a3c91c FG |
153 | # lock container during migration |
154 | eval { $self->lock_vm($self->{vmid}, sub { | |
155 | ||
156 | $self->{running} = 0; | |
157 | &$eval_int($self, sub { $self->{running} = $self->prepare($self->{vmid}); }); | |
158 | die $@ if $@; | |
159 | ||
9591475a | 160 | if (defined($migration_network)) { |
58a3c91c FG |
161 | $self->log('info', "use dedicated network address for sending " . |
162 | "migration traffic ($self->{nodeip})"); | |
163 | ||
164 | # test if we can connect to new IP | |
165 | my $cmd = [ @{$self->{rem_ssh}}, '/bin/true' ]; | |
166 | eval { $self->cmd_quiet($cmd); }; | |
167 | die "Can't connect to destination address ($self->{nodeip}) using " . | |
168 | "public key authentication\n" if $@; | |
169 | } | |
170 | ||
171 | &$eval_int($self, sub { $self->phase1($self->{vmid}); }); | |
172 | my $err = $@; | |
173 | if ($err) { | |
174 | $self->log('err', $err); | |
175 | eval { $self->phase1_cleanup($self->{vmid}, $err); }; | |
176 | if (my $tmperr = $@) { | |
177 | $self->log('err', $tmperr); | |
178 | } | |
179 | eval { $self->final_cleanup($self->{vmid}); }; | |
180 | if (my $tmperr = $@) { | |
181 | $self->log('err', $tmperr); | |
182 | } | |
183 | die $err; | |
184 | } | |
185 | ||
186 | # vm is now owned by other node | |
187 | # Note: there is no VM config file on the local node anymore | |
188 | ||
189 | if ($self->{running}) { | |
190 | ||
191 | &$eval_int($self, sub { $self->phase2($self->{vmid}); }); | |
192 | my $phase2err = $@; | |
193 | if ($phase2err) { | |
194 | $self->{errors} = 1; | |
195 | $self->log('err', "online migrate failure - $phase2err"); | |
196 | } | |
197 | eval { $self->phase2_cleanup($self->{vmid}, $phase2err); }; | |
198 | if (my $err = $@) { | |
199 | $self->log('err', $err); | |
200 | $self->{errors} = 1; | |
201 | } | |
202 | } | |
203 | ||
204 | # phase3 (finalize) | |
205 | &$eval_int($self, sub { $self->phase3($self->{vmid}); }); | |
206 | my $phase3err = $@; | |
207 | if ($phase3err) { | |
208 | $self->log('err', $phase3err); | |
209 | $self->{errors} = 1; | |
210 | } | |
211 | eval { $self->phase3_cleanup($self->{vmid}, $phase3err); }; | |
212 | if (my $err = $@) { | |
213 | $self->log('err', $err); | |
214 | $self->{errors} = 1; | |
215 | } | |
216 | eval { $self->final_cleanup($self->{vmid}); }; | |
217 | if (my $err = $@) { | |
218 | $self->log('err', $err); | |
219 | $self->{errors} = 1; | |
220 | } | |
221 | })}; | |
222 | ||
223 | my $err = $@; | |
224 | ||
225 | my $delay = time() - $starttime; | |
226 | my $mins = int($delay/60); | |
227 | my $secs = $delay - $mins*60; | |
228 | my $hours = int($mins/60); | |
229 | $mins = $mins - $hours*60; | |
230 | ||
231 | my $duration = sprintf "%02d:%02d:%02d", $hours, $mins, $secs; | |
232 | ||
233 | if ($err) { | |
234 | $self->log('err', "migration aborted (duration $duration): $err"); | |
235 | die "migration aborted\n"; | |
236 | } | |
237 | ||
238 | if ($self->{errors}) { | |
239 | $self->log('err', "migration finished with problems (duration $duration)"); | |
240 | die "migration problems\n" | |
241 | } | |
242 | ||
243 | $self->log('info', "migration finished successfully (duration $duration)"); | |
244 | } | |
245 | ||
246 | sub lock_vm { | |
247 | my ($self, $vmid, $code, @param) = @_; | |
248 | ||
249 | die "abstract method - implement me"; | |
250 | } | |
251 | ||
252 | sub prepare { | |
253 | my ($self, $vmid) = @_; | |
254 | ||
255 | die "abstract method - implement me"; | |
256 | ||
257 | # return $running; | |
258 | } | |
259 | ||
260 | # transfer all data and move VM config files | |
261 | sub phase1 { | |
262 | my ($self, $vmid) = @_; | |
263 | die "abstract method - implement me"; | |
264 | } | |
265 | ||
266 | # only called if there are errors in phase1 | |
267 | sub phase1_cleanup { | |
268 | my ($self, $vmid, $err) = @_; | |
269 | die "abstract method - implement me"; | |
270 | } | |
271 | ||
272 | # only called when VM is running and phase1 was successful | |
273 | sub phase2 { | |
274 | my ($self, $vmid) = @_; | |
275 | die "abstract method - implement me"; | |
276 | } | |
277 | ||
278 | # only called when VM is running and phase1 was successful | |
279 | sub phase2_cleanup { | |
280 | my ($self, $vmid, $err) = @_; | |
281 | }; | |
282 | ||
283 | # only called when phase1 was successful | |
284 | sub phase3 { | |
285 | my ($self, $vmid) = @_; | |
286 | } | |
287 | ||
288 | # only called when phase1 was successful | |
289 | sub phase3_cleanup { | |
290 | my ($self, $vmid, $err) = @_; | |
291 | } | |
292 | ||
293 | # final cleanup - always called | |
294 | sub final_cleanup { | |
295 | my ($self, $vmid) = @_; | |
296 | die "abstract method - implement me"; | |
297 | } | |
298 | ||
299 | 1; |