33c5f8cc74ec55a7ef55ed3e4126c920866d86c8
[debian/amanda] / perl / Amanda / Script_App.pm
1 # vim:ft=perl
2 # Copyright (c) 2005-2008 Zmanda, Inc.  All Rights Reserved.
3 #
4 # This library is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU Lesser General Public License version 2.1 as
6 # published by the Free Software Foundation.
7 #
8 # This library 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 Lesser General Public
11 # License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this library; if not, write to the Free Software Foundation,
15 # Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
16 #
17 # Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
18 # Sunnyvale, CA 94086, 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
35 =head1 NAME
36
37 Amanda::Script - perl utility functions for Scripts.
38
39 =head1 SYNOPSIS
40
41 =cut
42
43 sub new {
44     my $class = shift;
45     my $execute_where = shift;
46     my $type = shift;
47
48     my $self = {};
49     bless ($self, $class);
50
51     # extract the last component of the class name
52     my $name = $class;
53     $name =~ s/^.*:://;
54     $self->{'name'} = $name;
55
56     if(!defined $execute_where) {
57         $execute_where = "client";
58     }
59     Amanda::Util::setup_application($name, $execute_where, $CONTEXT_DAEMON);
60
61     #initialize config client to get values from amanda-client.conf
62     config_init($CONFIG_INIT_CLIENT, undef);
63     my ($cfgerr_level, @cfgerr_errors) = config_errors();
64     if ($cfgerr_level >= $CFGERR_WARNINGS) {
65         config_print_errors();
66         if ($cfgerr_level >= $CFGERR_ERRORS) {
67             die("errors processing config file");
68         }
69     }
70
71     Amanda::Util::finish_setup($RUNNING_AS_ANY);
72
73     $self->{'suf'} = '';
74     if ( $Amanda::Constants::USE_VERSION_SUFFIXES =~ /^yes$/i ) {
75         $self->{'suf'} = "-$Amanda::Constants::VERSION";
76     }
77
78     $self->{error_status} = $Amanda::Script_App::GOOD;
79     $self->{type} = $type;
80     $self->{known_commands} = {};
81
82     debug("$type: $name\n");
83
84     return $self;
85 }
86
87
88 #$_[0] action
89 #$_[1] message
90 #$_[2] status: GOOD or ERROR
91 sub print_to_server {
92     my $self = shift;
93     my($action,$msg, $status) = @_;
94     if ($status != 0) {
95         $self->{error_status} = $status;
96     }
97     if ($action eq "check") {
98         if ($status == $Amanda::Script_App::GOOD) {
99             print STDOUT "OK $msg\n";
100         } else {
101             print STDOUT "ERROR $msg\n";
102         }
103     } elsif ($action eq "estimate") {
104         if ($status == $Amanda::Script_App::GOOD) {
105             #do nothing
106         } else {
107             print STDERR "ERROR $msg\n";
108         }
109     } elsif ($action eq "backup") {
110         if ($status == $Amanda::Script_App::GOOD) {
111             print {$self->{mesgout}} "| $msg\n";
112         } else {
113             print {$self->{mesgout}} "? $msg\n";
114         }
115     } elsif ($action eq "restore") {
116         print STDOUT "$msg\n";
117     } elsif ($action eq "validate") {
118         print STDERR "$msg\n";
119     } else {
120         print STDERR "$msg\n";
121     }
122 }
123
124 #$_[0] action
125 #$_[1] message
126 #$_[2] status: GOOD or ERROR
127 sub print_to_server_and_die {
128     my $self = shift;
129
130     my $action = $_[0];
131     $self->print_to_server( @_ );
132     if (!defined $self->{die} && $self->can("check_for_backup_failure")) {
133         $self->{die} = 1;
134         $self->check_for_backup_failure($action);
135     }
136     exit 1;
137 }
138
139
140 sub do {
141     my $self = shift;
142     my $command  = shift;
143
144     if (!defined $command) {
145         $self->print_to_server_and_die("check", "no command",
146                                        $Amanda::Script_App::ERROR);
147         return;
148     }
149     $command =~ tr/A-Z-/a-z_/;
150     debug("command: $command");
151
152     # first make sure this is a valid command.
153     if (!exists($self->{known_commands}->{$command})) {
154         print STDERR "Unknown command `$command'.\n";
155         exit 1;
156     }
157
158     # now convert it to a function name and see if it's
159     # defined
160     my $function_name = "command_$command";
161
162     if (!$self->can($function_name)) {
163         print STDERR "command `$command' is not supported by the '" .
164                      $self->{name} . "' " . $self->{type} . ".\n";
165         exit 1;
166     }
167
168     # it exists -- call it
169     $self->$function_name();
170 }
171
172 1;