2 # Copyright (c) 2005-2008 Zmanda, Inc. All Rights Reserved.
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.
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.
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.
17 # Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 package Amanda::Script_App;
29 use Amanda::Constants;
30 use Amanda::Config qw( :init :getconf config_dir_relative );
31 use Amanda::Debug qw( :logging );
33 use Amanda::Util qw( :constants );
37 Amanda::Script - perl utility functions for Scripts.
45 my $execute_where = shift;
49 bless ($self, $class);
51 # extract the last component of the class name
54 $self->{'name'} = $name;
56 if(!defined $execute_where) {
57 $execute_where = "client";
59 Amanda::Util::setup_application($name, $execute_where, $CONTEXT_DAEMON);
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");
71 Amanda::Util::finish_setup($RUNNING_AS_ANY);
74 if ( $Amanda::Constants::USE_VERSION_SUFFIXES =~ /^yes$/i ) {
75 $self->{'suf'} = "-$Amanda::Constants::VERSION";
78 $self->{error_status} = $Amanda::Script_App::GOOD;
79 $self->{type} = $type;
80 $self->{known_commands} = {};
82 debug("$type: $name\n");
90 #$_[2] status: GOOD or ERROR
93 my($action,$msg, $status) = @_;
95 $self->{error_status} = $status;
97 if ($action eq "check") {
98 if ($status == $Amanda::Script_App::GOOD) {
99 print STDOUT "OK $msg\n";
101 print STDOUT "ERROR $msg\n";
103 } elsif ($action eq "estimate") {
104 if ($status == $Amanda::Script_App::GOOD) {
107 print STDERR "ERROR $msg\n";
109 } elsif ($action eq "backup") {
110 if ($status == $Amanda::Script_App::GOOD) {
111 print {$self->{mesgout}} "| $msg\n";
113 print {$self->{mesgout}} "? $msg\n";
115 } elsif ($action eq "restore") {
116 print STDOUT "$msg\n";
117 } elsif ($action eq "validate") {
118 print STDERR "$msg\n";
120 print STDERR "$msg\n";
126 #$_[2] status: GOOD or ERROR
127 sub print_to_server_and_die {
131 $self->print_to_server( @_ );
132 if (!defined $self->{die} && $self->can("check_for_backup_failure")) {
134 $self->check_for_backup_failure($action);
144 if (!defined $command) {
145 $self->print_to_server_and_die("check", "no command",
146 $Amanda::Script_App::ERROR);
149 $command =~ tr/A-Z-/a-z_/;
150 debug("command: $command");
152 # first make sure this is a valid command.
153 if (!exists($self->{known_commands}->{$command})) {
154 print STDERR "Unknown command `$command'.\n";
158 # now convert it to a function name and see if it's
160 my $function_name = "command_$command";
162 if (!$self->can($function_name)) {
163 print STDERR "command `$command' is not supported by the '" .
164 $self->{name} . "' " . $self->{type} . ".\n";
168 # it exists -- call it
169 $self->$function_name();