2 # Copyright (c) 2009 Zmanda, Inc. All Rights Reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 package Installcheck::Application;
22 use Amanda::MainLoop qw( :GIOCondition );
27 use POSIX qw( :errno_h :fcntl_h );
28 use POSIX qw( EAGAIN );
35 Installcheck::Application - driver Application API programs and scripts
39 use Installcheck::Application;
41 my $app = Installcheck::Application->new('myapp');
43 $app->add_property('foo' => 'bar');
44 $app->add_property('baz');
45 $app->add_property('bat', 1, 2, 3);
46 $app->delete_property('foo');
47 $app->get_property('foo');
48 $app->set_property('foo', ['bar', '']);
49 my @props = $app->list_properties();
51 my $feats = $app->support('config' => 'TESTCONF', 'temp-prop' => 1);
52 die "need calcsize support" unless ($feats{'CALCSIZE'});
54 my $backup = $app->backup('device' => 'file:/path', 'level' => 0);
55 die join("\n", @{$backup->{'errors'}}) if $backup->{'errors'};
56 print STDERR join("\n", @{$backup->{'index'}});
57 print $backup->{'data'};
63 =item C< new($app_name) >
65 Create a new C< Installcheck::Application > object that will run C< $app_name >.
71 Properties are automatically passed as arguments to any command.
72 Their names will be lowercase.
76 =item C< add_property($prop_name, 'val1', 'val2') >
78 Add one or more values for property C< $prop_name >.
80 =item C< delete_property($prop_name) >
82 Delete all values for property C< $prop_name >.
84 =item C< get_property($prop_name) >
86 Get all values for property C< $prop_name >.
88 =item C< set_property($prop_name, 'val1', 'val2') >
90 Set the values for property C< $prop_name >, removing any previous values.
92 =item C< list_properties() >
94 Returns all properties that have a value.
104 Runs the C< support > command and returns the output as a hashref,
105 with all keys in uppercase.
107 =item C<< backup('disk' => '/some/path', 'device' => '/some/path, 'level' => 0) >>
109 Runs the C< backup() > command on the given device.
110 If a C< disk > argument is not given, it defaults to the C< device >.
117 The data stream produced by the application
121 An array of index lines produced by the application
125 The size of the backup (C< data >)
129 Any normal/informative messages
139 =item C< exit_status >
141 The exit status of the application
145 =item C<< restore('data' => $data_from_backup, 'objects' => ['a', 'b'], 'level' => 0) >>
147 Runs the C< restore > command to restore the C< objects > to the
148 current working directory, supplying it with C< data >.
149 The optional C< level > argument (defaulting to 0) specifies the level of the backup
156 Any output from the application
158 =item C< exit_status >
160 The exit status of the application
164 =item C<< estimate('device' => '/some/path, 'level' => 0) >>
172 The level of the backup that would result
176 The size of the backup that would result
178 =item C< exit_status >
180 The exit status of the application
184 =item C<< selfcheck('device' => '/some/path, 'disk' => '/some/path') >>
186 Runs the C< selfcheck > command on the given device.
187 If a C< disk > argument is not given, it defaults to the C< device >.)
200 =item C< exit_status >
202 The exit status of the application
211 my ($class, $app_name) = @_;
214 'app_name' => $app_name,
218 bless($self, $class);
231 my $name = _propify(shift @_);
234 $self->{'props'}->{$name} ||= [];
235 push @{$self->{'props'}->{$name}}, @values;
238 sub delete_property {
240 my $name = _propify(shift @_);
242 delete $self->{'props'}->{$name};
247 my $name = _propify(shift @_);
249 defined($self->{'props'}->{$name}) ? @{$self->{'props'}->{$name}} : ();
254 my $name = _propify(shift @_);
257 @{$self->{'props'}->{$name}} = @values;
260 sub list_properties {
263 my @prop_names = keys %{$self->{'props'}};
264 # return only non-empty properties
265 grep { $self->{'props'}->{$_} } @prop_names;
268 # setup and run the application
269 # $cmd - the command to give the application
270 # $extra_args - an arrayref of additional arguments
271 # $fds - a hashref of hashrefs specifying file descriptors
272 # The key specifies the target file descriptor number in the child process.
273 # Each hashref can have:
274 # 'child_mode' - 'r' or 'w', how the fd will be used in the child process
275 # 'cb' - an explicit callback to use when the fd is ready for reading/writing
276 # 'write' - a string to write out. An appropriate callback will be generated
277 # 'save_to' - a scalarref to save output to. An appropriate callback will be generated
278 # For each key in $fds, a pipe will be setup.
279 # Additional keys will be added:
280 # 'child_fd' - the file descriptor used by the child
281 # 'parent_fd' - the file descriptor used by the parent
282 # 'handle' - an anonymous filehandle (IO::Handle) for 'parent_fd'
283 # 'src' - the event source (for Amanda::MainLoop)
284 # 'done' - a callback (coderef) that must be called when you're done with the fd
285 # returns child exit status
287 my ($self, $cmd, $extra_args, $fds) = @_;
288 confess 'must have a command' unless $cmd;
290 my $fdn; # file descriptor number
294 if (defined($exit_status)) {
297 foreach $fdn (keys %$fds) {
298 my $fd = $fds->{$fdn};
299 if (($fd->{'child_mode'} eq 'w') and ref($fd->{'done'})) {
304 Amanda::MainLoop::quit() if $really_done;
308 # start setting up pipes
309 foreach $fdn (keys %$fds) {
310 my $fd = $fds->{$fdn};
311 confess "mode must be either 'r' or 'w'" unless $fd->{'child_mode'} =~ /^r|w$/;
312 my ($fd0, $fd1) = POSIX::pipe();
313 my ($c_fd, $p_fd, $p_mode);
314 if ($fd->{'child_mode'} eq 'r') {
315 $p_fd = $fd->{'parent_fd'} = $fd1;
317 $c_fd = $fd->{'child_fd'} = $fd0;
319 $p_fd = $fd->{'parent_fd'} = $fd0;
321 $c_fd = $fd->{'child_fd'} = $fd1;
324 my $p_handle = $fd->{'handle'} = IO::Handle->new_from_fd($p_fd, $p_mode);
325 confess "unable to fdopen $p_fd with mode $p_mode" unless $p_handle;
327 if ($fd->{'save_to'}) {
328 $fd->{'cb'} = _make_save_cb($fd->{'save_to'}, $fd);
329 } elsif ($fd->{'write'}) {
330 $fd->{'cb'} = _make_write_cb($fd->{'write'}, $fd);
332 $fd->{'done'} = _make_done_cb($fd, $all_done);
334 my $events = ($fd->{'child_mode'} eq 'r') ? $G_IO_OUT : ($G_IO_IN|$G_IO_HUP);
335 $fd->{'src'} = Amanda::MainLoop::fd_source($p_handle, $events);
336 $fd->{'src'}->set_callback($fd->{'cb'}) if $fd->{'cb'};
341 my @args = ($cmd, @$extra_args);
342 foreach my $name (keys %{$self->{'props'}}) {
343 $self->{'props'}->{$name} ||= [];
344 foreach my $val (@{$self->{'props'}->{$name}}) {
345 push @args, "--$name", "$val";
350 if ($pid) { # in parent
351 # parent shouldn't use child_fd
352 foreach $fdn (keys %$fds) {
353 my $fd = $fds->{$fdn};
354 POSIX::close($fd->{'child_fd'});
356 my $wait_src = Amanda::MainLoop::child_watch_source($pid);
357 $wait_src->set_callback(sub {
358 $exit_status = $_[2];
362 Amanda::MainLoop::run();
365 # don't need to remove wait_src, that's done automatically
366 foreach $fdn (keys %$fds) {
367 my $fd = $fds->{$fdn};
368 $fd->{'src'}->remove();
369 POSIX::close($fd->{'parent_fd'});
373 my @child_fds = keys %$fds;
374 foreach $fdn (@child_fds) {
375 my $fd = $fds->{$fdn};
376 confess "failed to call dup2($fd->{'child_fd'}, $fdn)"
377 unless POSIX::dup2($fd->{'child_fd'}, $fdn);
378 POSIX::close($fd->{'child_fd'})
379 unless grep {$_ == $fd->{'child_fd'}} @child_fds;
380 POSIX::close($fd->{'parent_fd'})
381 unless grep {$_ == $fd->{'parent_fd'}} @child_fds;
385 exec "$APPLICATION_DIR/$self->{'app_name'}", @args;
391 # given a fd hashref, make a callback that will make the fd non-blocking
392 sub _make_nonblock_cb {
394 confess "a hash reference (representing a fd) is required" unless $fd;
400 fcntl($fd->{'handle'}, F_GETFL, $flags)
401 or confess "Couldn't get flags: $!\n";
402 $flags |= O_NONBLOCK;
403 fcntl($fd->{'handle'}, F_SETFL, $flags)
404 or confess "Couldn't set flags: $!\n";
411 # given a scalar/string and a fd hashref,
412 # make a callback that will write the string to the fd
414 my ($data, $fd) = @_;
415 confess "a hash reference (representing a fd) is required" unless $fd;
416 my $len = length($data);
418 my $nonblock_cb = _make_nonblock_cb($fd);
420 my $BYTES_TO_WRITE = 4096;
425 # this shouldn't happen since the src is removed once we're done (below)
426 confess "offset greater than length" if $offset >= $len;
428 my $rv = $fd->{'handle'}->syswrite($data, $BYTES_TO_WRITE, $offset);
430 confess "Error writing: $!" unless $! == EAGAIN;
434 $fd->{'done'}->() if ($offset >= $len);
439 # given a scalarref and a fd hashref,
440 # make a callback that will save bytes from fd in scalarref
442 my ($store, $fd) = @_;
443 confess "a scalar reference is required" unless ref($store) eq "SCALAR";
444 confess "a hash reference (representing a fd) is required" unless $fd;
447 my $nonblock_cb = _make_nonblock_cb($fd);
449 my $BYTES_TO_READ = 4096;
454 my $rv = $fd->{'handle'}->sysread($$store, $BYTES_TO_READ, $offset);
456 $fd->{'done'}->() if (0 == $rv);
458 confess "Error reading: $!" unless $! == EAGAIN;
465 my ($fd, $all_done) = @_;
467 $fd->{'src'}->remove();
468 $fd->{'handle'}->close();
474 # parse the size string output by various commands, returning the number of bytes
478 confess "failed to parse size" unless ($sstr =~ /^(\d+)(\D?)$/i);
482 $suf = 'k' unless $suf;
483 my %suf_pows = ('k' => 10, 'm' => 20, 'g' => 30);
484 confess "invalid suffix $suf" unless $suf_pows{$suf};
485 $size *= 1 << $suf_pows{$suf};
494 _exec($self, 'support', undef, {
495 1 => {'child_mode' => 'w', 'save_to' => \$sup_str},
498 my %sup = split(/\s+/, $sup_str);
499 # fold into uppercase
500 foreach my $k (keys %sup) {
513 foreach my $k ( qw(device level) ) {
514 confess "$k required" unless defined($nargs{$k});
516 $nargs{'disk'} ||= $nargs{'device'};
518 my @args = map {my $k = $_; ("--$k", $nargs{$k}) } keys(%nargs);
520 my ($data, $msg_str, $index_str);
521 my $exit_status = _exec($self, 'backup', \@args,
523 1 => {'child_mode' => 'w', 'save_to' => \$data},
524 3 => {'child_mode' => 'w', 'save_to' => \$msg_str},
525 4 => {'child_mode' => 'w', 'save_to' => \$index_str},
529 my @index = split(/\n/, $index_str);
533 my ($size, @infos, @errors, @unknowns);
534 foreach my $l (split(/\n/, $msg_str)) {
535 if ($l =~ /^([|?&]) (.*)$/) {
536 my ($sym, $rem) = ($1, $2);
540 } elsif ($sym eq '?') {
542 } elsif ($sym eq '&') {
543 push(@unknowns, $rem);
545 confess "should not be reached";
547 } elsif ($l =~ /^sendbackup: (.*)$/) {
549 if ($rem =~ /^size (\d+)$/i) {
550 $size = _parse_size($1);
551 } elsif (lc($rem) eq 'end') {
554 confess "failed to parse: $l";
557 confess "failed to parse: $l";
561 {'data' => $data, 'index' => \@index, 'size' => $size,
562 'infos' => \@infos, 'errors' => \@errors, 'unknowns' => \@unknowns,
563 'exit_status' => $exit_status};
570 foreach my $k ( qw(objects data) ) {
571 confess "'$k' required" unless defined($args{$k});
573 $args{'level'} ||= 0;
576 my $exit_status = _exec($self, 'restore', ['--level', $args{'level'}, @{$args{'objects'}}], {
577 0 => {'child_mode' => 'r', 'write' => $args{'data'}},
578 1 => {'child_mode' => 'w', 'save_to' => \$msgs},
581 {'msgs' => $msgs, 'exit_status' => $exit_status};
590 foreach my $k ( qw(device level) ) {
591 confess "$k required" unless defined($nargs{$k});
593 $nargs{'disk'} ||= $nargs{'device'};
594 my $calcsize = $nargs{'calcsize'};
595 delete $nargs{'calcsize'};
597 my @args = map {my $k = $_; ("--$k", $nargs{$k}) } keys(%nargs);
598 push @args, '--calcsize' if $calcsize;
601 my $exit_status = _exec($self, 'estimate', \@args,
603 1 => {'child_mode' => 'w', 'save_to' => \$est},
606 $est =~ /^(\d+) (\d+) 1\n$/;
607 my ($level, $size) = ($1, $2);
609 $size = ($size eq '-1')? -1 : _parse_size($size);
611 {'level' => $level, 'size' => $size, 'exit_status' => $exit_status};
618 foreach my $k ( qw(device) ) {
619 confess "$k required" unless defined($nargs{$k});
621 $nargs{'disk'} ||= $nargs{'device'};
623 my @args = map {my $k = $_; ("--$k", $nargs{$k}) } keys(%nargs);
626 my $exit_status = _exec($self, 'selfcheck', \@args,
628 1 => {'child_mode' => 'w', 'save_to' => \$msg_str},
633 foreach my $l (split(/\n/, $msg_str)) {
634 confess "invalid line: $l" unless $l =~ /^(OK|ERROR) (.*)$/;
635 my ($type, $rem) = ($1, $2);
638 } elsif ($type eq 'ERROR') {
641 confess "should not be reached";
645 {'oks' => \@oks, 'errors' => \@errors, 'exit_status' => $exit_status};