Imported Upstream version 3.3.2
[debian/amanda] / perl / Amanda / Script_App.pm
1 # vim:ft=perl
2 # Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
3 #
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.
7 #
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
11 # for more details.
12 #
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
16 #
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19
20 package Amanda::Script_App;
21
22 no warnings;
23 no strict;
24 $GOOD  = 0;
25 $ERROR = 1;
26 $FAILURE = 2;
27
28 use strict;
29 use warnings;
30 use Amanda::Constants;
31 use Amanda::Config qw( :init :getconf  config_dir_relative );
32 use Amanda::Debug qw( :logging );
33 use Amanda::Paths;
34 use Amanda::Util qw( :constants );
35 use Carp;
36
37 =head1 NAME
38
39 Amanda::Script_App - perl utility functions for Scripts.
40
41 =head1 SYNOPSIS
42
43 This module should not be used directly. Instead, use C<Amanda::Application> or
44 C<Amanda::Script>.
45
46 =cut
47
48 sub new {
49     my $class = shift;
50     my ($execute_where, $type, $config_name) = @_;
51
52     my $self = {};
53     bless ($self, $class);
54
55     # extract the last component of the class name
56     my $name = $class;
57     $name =~ s/^.*:://;
58     $self->{'name'} = $name;
59
60     if(!defined $execute_where) {
61         $execute_where = "client";
62     }
63     Amanda::Util::setup_application($name, $execute_where, $CONTEXT_DAEMON);
64     debug("Arguments: " . join(' ', @ARGV));
65
66     #initialize config client to get values from amanda-client.conf
67     config_init($CONFIG_INIT_CLIENT, undef);
68     my ($cfgerr_level, @cfgerr_errors) = config_errors();
69     if ($cfgerr_level >= $CFGERR_WARNINGS) {
70         config_print_errors();
71         if ($cfgerr_level >= $CFGERR_ERRORS) {
72             confess("errors processing config file");
73         }
74     }
75     if ($config_name) {
76         config_init($CONFIG_INIT_CLIENT | $CONFIG_INIT_EXPLICIT_NAME | $CONFIG_INIT_OVERLAY, $config_name);
77         ($cfgerr_level, @cfgerr_errors) = config_errors();
78         if ($cfgerr_level >= $CFGERR_WARNINGS) {
79             config_print_errors();
80             if ($cfgerr_level >= $CFGERR_ERRORS) {
81                 confess("errors processing config file for $config_name");
82             }
83         }
84     }
85
86     Amanda::Util::finish_setup($RUNNING_AS_ANY);
87
88     $self->{error_status} = $Amanda::Script_App::GOOD;
89     $self->{type} = $type;
90     $self->{known_commands} = {};
91
92     debug("$type: $name\n");
93
94     return $self;
95 }
96
97
98 #$_[0] message
99 #$_[1] status: GOOD or ERROR
100 sub print_to_server {
101     my $self = shift;
102     my($msg, $status) = @_;
103     if ($status != 0) {
104         $self->{error_status} = $status;
105     }
106     if ($self->{action} eq "check") {
107         if ($status == $Amanda::Script_App::GOOD) {
108             print STDOUT "OK $msg\n";
109         } else {
110             print STDOUT "ERROR $msg\n";
111         }
112     } elsif ($self->{action} eq "estimate") {
113         if ($status == $Amanda::Script_App::GOOD) {
114             #do nothing
115         } else {
116             print STDERR "ERROR $msg\n";
117         }
118     } elsif ($self->{action} eq "backup") {
119         if ($status == $Amanda::Script_App::GOOD) {
120             print {$self->{mesgout}} "| $msg\n";
121         } elsif ($status == $Amanda::Script_App::ERROR) {
122             print {$self->{mesgout}} "? $msg\n";
123         } else {
124             print {$self->{mesgout}} "sendbackup: error $msg\n";
125         }
126     } elsif ($self->{action} eq "restore") {
127         print STDERR "$msg\n";
128     } elsif ($self->{action} eq "validate") {
129         print STDERR "$msg\n";
130     } else {
131         print STDERR "$msg\n";
132     }
133 }
134
135 #$_[0] message
136 #$_[1] status: GOOD or ERROR
137 sub print_to_server_and_die {
138     my $self = shift;
139
140     $self->print_to_server( @_ );
141     if (!defined $self->{die} && $self->can("check_for_backup_failure")) {
142         $self->{die} = 1;
143         $self->check_for_backup_failure();
144     }
145     exit 1;
146 }
147
148
149 sub do {
150     my $self = shift;
151     my $command  = shift;
152
153     if (!defined $command) {
154         $self->print_to_server_and_die("check", "no command",
155                                        $Amanda::Script_App::ERROR);
156         return;
157     }
158     $command =~ tr/A-Z-/a-z_/;
159     debug("command: $command");
160
161     # first make sure this is a valid command.
162     if (!exists($self->{known_commands}->{$command})) {
163         print STDERR "Unknown command `$command'.\n";
164         exit 1;
165     }
166
167     my $action = $command;
168     $action =~ s/^pre_//;
169     $action =~ s/^post_//;
170     $action =~ s/^inter_//;
171     $action =~ s/^dle_//;
172     $action =~ s/^host_//;
173     $action =~ s/^level_//;
174
175     if ($action eq 'amcheck' || $action eq 'selfcheck') {
176         $self->{action} = 'check';
177     } elsif ($action eq 'estimate') {
178         $self->{action} = 'estimate';
179     } elsif ($action eq 'backup') {
180         $self->{action} = 'backup';
181     } elsif ($action eq 'recover' || $action eq 'restore') {
182         $self->{action} = 'restore';
183     } elsif ($action eq 'validate') {
184         $self->{action} = 'validate';
185     }
186
187     if ($action eq 'backup') {
188         $self->_set_mesgout();
189     }
190
191     # now convert it to a function name and see if it's
192     # defined
193     my $function_name = "command_$command";
194     my $default_name = "default_$command";
195
196     if (!$self->can($function_name)) {
197         if (!$self->can($default_name)) {
198             print STDERR "command `$command' is not supported by the '" .
199                          $self->{name} . "' " . $self->{type} . ".\n";
200             exit 1;
201         }
202         $self->$default_name();
203         return;
204     }
205
206     # it exists -- call it
207     $self->$function_name();
208 }
209
210 1;