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