1# vim:ft=perl
2# Copyright (c) 2008-2013 Zmanda, Inc.  All Rights Reserved.
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; either version 2
7# of the License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12# for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc.,
16# 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17#
18# Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
19# Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20
21package Amanda::Extract;
22
23use strict;
24use warnings;
25use IPC::Open3;
26
27use Amanda::Debug qw( :logging );
28use Amanda::Paths;
29
30=head1 NAME
31
32Amanda::Extract - perl utilities to run scripts and applications
33
34=head1 SYNOPSIS
35
36  use Amanda::Extract;
37
38  my (@bsu, @err)= Amanda::Extract::BSU(application => $application,
39					config      => $config,
40					host        => $host,
41					disk        => $disk,
42					device      => $device);
43  my (@bsu, @err)= Amanda::Extract::Run($application_name, \@g_options,
44					$hdr, @properties);
45
46=cut
47
48sub BSU {
49    my (%params) = @_;
50
51    my %bsu;
52    my @err;
53    my @command;
54
55    push @command, $Amanda::Paths::APPLICATION_DIR . '/' . $params{'application'};
56    push @command, "support";
57    push @command, "--config", $params{'config'} if $params{'config'};
58    push @command, "--host"  , $params{'host'}   if $params{'host'};
59    push @command, "--disk"  , $params{'disk'}   if $params{'disk'};
60    push @command, "--device", $params{'device'} if $params{'device'};
61    debug("Running: " . join(' ', @command));
62
63    my $in;
64    my $out;
65    my $err = Symbol::gensym;
66    my $pid = open3($in, $out, $err, @command);
67
68    close($in);
69    while (my $line = <$out>) {
70	chomp $line;
71	debug("support: $line");
72	my ($name, $value) = split ' ', $line;
73
74	$name = lc($name);
75
76	if ($name eq 'config' ||
77	    $name eq 'host' ||
78	    $name eq 'disk' ||
79	    $name eq 'index-line' ||
80	    $name eq 'index-xml' ||
81	    $name eq 'message-line' ||
82	    $name eq 'message-xml' ||
83	    $name eq 'record' ||
84	    $name eq 'include-file' ||
85	    $name eq 'include-list' ||
86	    $name eq 'include-list-glob' ||
87	    $name eq 'include-optional' ||
88	    $name eq 'exclude-file' ||
89	    $name eq 'exclude-list' ||
90	    $name eq 'exclude-list-glob' ||
91	    $name eq 'exclude-optional' ||
92	    $name eq 'collection' ||
93	    $name eq 'caclsize' ||
94	    $name eq 'client-estimate' ||
95	    $name eq 'multi-estimate' ||
96	    $name eq 'amfeatures' ||
97	    $name eq 'recover-dump-state-file') {
98	    $bsu{$name} = ($value eq "YES");
99	} elsif ($name eq 'max-level') {
100	    $bsu{$name} = $value;
101	} elsif ($name eq 'recover-mode') {
102	    $bsu{'smb-recover-mode'} = $value eq 'SMB';
103	} elsif ($name eq 'recover-path') {
104	    $bsu{'recover-path-cwd'} = $value eq 'CWD';
105	    $bsu{'recover-path-remote'} = $value eq 'REMOTE';
106	} elsif ($name eq 'data-path') {
107	    if ($value eq 'AMANDA') {
108		$bsu{'data-path-amanda'} = 1;
109	    } elsif ($value eq 'DIRECTTCP') {
110		$bsu{'data-path-directtcp'} = 1;
111	    }
112	}
113    }
114    close($out);
115
116    while (my $line = <$err>) {
117	chomp($line);
118	next if $line == '';
119	push @err, $line;
120    }
121    close($err);
122
123    waitpid($pid, 0);
124    my $child_exit_status = $? >> 8;
125
126    if ($child_exit_status != 0) {
127	push @err, "exited with status $child_exit_status";
128    }
129    return (\%bsu, \@err);
130}
131
1321;
133