2 # Copyright (c) 2008,2009 Zmanda, Inc. All Rights Reserved.
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.
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
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
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94085, 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 );
38 Amanda::Script_App - perl utility functions for Scripts.
42 This module should not be used directly. Instead, use C<Amanda::Application> or
49 my ($execute_where, $type, $config_name) = @_;
52 bless ($self, $class);
54 # extract the last component of the class name
57 $self->{'name'} = $name;
59 if(!defined $execute_where) {
60 $execute_where = "client";
62 Amanda::Util::setup_application($name, $execute_where, $CONTEXT_DAEMON);
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");
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");
84 Amanda::Util::finish_setup($RUNNING_AS_ANY);
86 $self->{error_status} = $Amanda::Script_App::GOOD;
87 $self->{type} = $type;
88 $self->{known_commands} = {};
90 debug("$type: $name\n");
97 #$_[1] status: GOOD or ERROR
100 my($msg, $status) = @_;
102 $self->{error_status} = $status;
104 if ($self->{action} eq "check") {
105 if ($status == $Amanda::Script_App::GOOD) {
106 print STDOUT "OK $msg\n";
108 print STDOUT "ERROR $msg\n";
110 } elsif ($self->{action} eq "estimate") {
111 if ($status == $Amanda::Script_App::GOOD) {
114 print STDERR "ERROR $msg\n";
116 } elsif ($self->{action} eq "backup") {
117 if ($status == $Amanda::Script_App::GOOD) {
118 print {$self->{mesgout}} "| $msg\n";
120 print {$self->{mesgout}} "? $msg\n";
122 } elsif ($self->{action} eq "restore") {
123 print STDERR "$msg\n";
124 } elsif ($self->{action} eq "validate") {
125 print STDERR "$msg\n";
127 print STDERR "$msg\n";
132 #$_[1] status: GOOD or ERROR
133 sub print_to_server_and_die {
136 $self->print_to_server( @_ );
137 if (!defined $self->{die} && $self->can("check_for_backup_failure")) {
139 $self->check_for_backup_failure();
149 if (!defined $command) {
150 $self->print_to_server_and_die("check", "no command",
151 $Amanda::Script_App::ERROR);
154 $command =~ tr/A-Z-/a-z_/;
155 debug("command: $command");
157 # first make sure this is a valid command.
158 if (!exists($self->{known_commands}->{$command})) {
159 print STDERR "Unknown command `$command'.\n";
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_//;
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';
183 if ($action eq 'backup') {
184 $self->_set_mesgout();
187 # now convert it to a function name and see if it's
189 my $function_name = "command_$command";
190 my $default_name = "default_$command";
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";
198 $self->$default_name();
202 # it exists -- call it
203 $self->$function_name();